updates.
[runtime.git] / apps / irssi / src / fe-common / core / fe-common-core.c
1 /*
2  fe-common-core.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "module-formats.h"
23 #include "args.h"
24 #include "misc.h"
25 #include "levels.h"
26 #include "settings.h"
27 #include "irssi-version.h"
28
29 #include "servers.h"
30 #include "channels.h"
31 #include "servers-setup.h"
32
33 #include "autorun.h"
34 #include "fe-queries.h"
35 #include "hilight-text.h"
36 #include "command-history.h"
37 #include "completion.h"
38 #include "keyboard.h"
39 #include "printtext.h"
40 #include "formats.h"
41 #include "themes.h"
42 #include "translation.h"
43 #include "fe-channels.h"
44 #include "fe-windows.h"
45 #include "window-items.h"
46 #include "windows-layout.h"
47
48 #include <signal.h>
49
50 static char *autocon_server;
51 static char *autocon_password;
52 static int autocon_port;
53 static int no_autoconnect;
54 static char *cmdline_nick;
55 static char *cmdline_hostname;
56
57 void fe_core_log_init(void);
58 void fe_core_log_deinit(void);
59
60 void fe_exec_init(void);
61 void fe_exec_deinit(void);
62
63 void fe_expandos_init(void);
64 void fe_expandos_deinit(void);
65
66 void fe_help_init(void);
67 void fe_help_deinit(void);
68
69 void fe_ignore_init(void);
70 void fe_ignore_deinit(void);
71
72 void fe_ignore_messages_init(void);
73 void fe_ignore_messages_deinit(void);
74
75 void fe_log_init(void);
76 void fe_log_deinit(void);
77
78 void fe_messages_init(void);
79 void fe_messages_deinit(void);
80
81 void fe_modules_init(void);
82 void fe_modules_deinit(void);
83
84 void fe_server_init(void);
85 void fe_server_deinit(void);
86
87 void fe_settings_init(void);
88 void fe_settings_deinit(void);
89
90 void window_activity_init(void);
91 void window_activity_deinit(void);
92
93 void window_commands_init(void);
94 void window_commands_deinit(void);
95
96 void fe_core_commands_init(void);
97 void fe_core_commands_deinit(void);
98
99 static void print_version(void)
100 {
101         printf(PACKAGE" " IRSSI_VERSION" (%d %04d)\n",
102                IRSSI_VERSION_DATE, IRSSI_VERSION_TIME);
103         exit(0);
104 }
105
106 static void sig_connected(SERVER_REC *server)
107 {
108         MODULE_DATA_SET(server, g_new0(MODULE_SERVER_REC, 1));
109 }
110
111 static void sig_disconnected(SERVER_REC *server)
112 {
113         g_free(MODULE_DATA(server));
114         MODULE_DATA_UNSET(server);
115 }
116
117 static void sig_channel_created(CHANNEL_REC *channel)
118 {
119         MODULE_DATA_SET(channel, g_new0(MODULE_CHANNEL_REC, 1));
120 }
121
122 static void sig_channel_destroyed(CHANNEL_REC *channel)
123 {
124         g_free(MODULE_DATA(channel));
125         MODULE_DATA_UNSET(channel);
126 }
127
128 void fe_common_core_init(void)
129 {
130         static struct poptOption version_options[] = {
131                 { NULL, '\0', POPT_ARG_CALLBACK, (void *)&print_version, '\0', NULL },
132                 { "version", 'v', POPT_ARG_NONE, NULL, 0, "Display irssi version" },
133                 { NULL, '\0', 0, NULL }
134         };
135
136         static struct poptOption options[] = {
137                 { NULL, '\0', POPT_ARG_INCLUDE_TABLE, version_options, 0, NULL, NULL },
138                 POPT_AUTOHELP
139                 { "connect", 'c', POPT_ARG_STRING, &autocon_server, 0, "Automatically connect to server/ircnet", "SERVER" },
140                 { "password", 'w', POPT_ARG_STRING, &autocon_password, 0, "Autoconnect password", "PASSWORD" },
141                 { "port", 'p', POPT_ARG_INT, &autocon_port, 0, "Autoconnect port", "PORT" },
142                 { "noconnect", '!', POPT_ARG_NONE, &no_autoconnect, 0, "Disable autoconnecting", NULL },
143                 { "nick", 'n', POPT_ARG_STRING, &cmdline_nick, 0, "Specify nick to use", NULL },
144                 { "hostname", 'h', POPT_ARG_STRING, &cmdline_hostname, 0, "Specify host name to use", NULL },
145                 { NULL, '\0', 0, NULL }
146         };
147
148         autocon_server = NULL;
149         autocon_password = NULL;
150         autocon_port = 6667;
151         no_autoconnect = FALSE;
152         cmdline_nick = NULL;
153         cmdline_hostname = NULL;
154         args_register(options);
155
156         settings_add_bool("lookandfeel", "timestamps", TRUE);
157         settings_add_str("lookandfeel", "timestamp_level", "ALL");
158         settings_add_int("lookandfeel", "timestamp_timeout", 0);
159
160         settings_add_bool("lookandfeel", "bell_beeps", FALSE);
161         settings_add_str("lookandfeel", "beep_msg_level", "");
162         settings_add_bool("lookandfeel", "beep_when_window_active", TRUE);
163         settings_add_bool("lookandfeel", "beep_when_away", TRUE);
164
165         settings_add_bool("lookandfeel", "hide_text_style", FALSE);
166         settings_add_bool("lookandfeel", "hide_mirc_colors", FALSE);
167         settings_add_bool("lookandfeel", "hide_server_tags", FALSE);
168
169         settings_add_bool("lookandfeel", "use_status_window", TRUE);
170         settings_add_bool("lookandfeel", "use_msgs_window", FALSE);
171
172         themes_init();
173         theme_register(fecommon_core_formats);
174
175         command_history_init();
176         completion_init();
177         keyboard_init();
178         printtext_init();
179         formats_init();
180 #ifndef WIN32
181         fe_exec_init();
182 #endif
183         fe_expandos_init();
184         fe_help_init();
185         fe_ignore_init();
186         fe_log_init();
187         fe_modules_init();
188         fe_server_init();
189         fe_settings_init();
190         translation_init();
191         windows_init();
192         window_activity_init();
193         window_commands_init();
194         window_items_init();
195         windows_layout_init();
196         fe_core_commands_init();
197
198         fe_channels_init();
199         fe_queries_init();
200
201         fe_messages_init();
202         hilight_text_init();
203         fe_ignore_messages_init();
204
205         settings_check();
206
207         signal_add_first("server connected", (SIGNAL_FUNC) sig_connected);
208         signal_add_last("server disconnected", (SIGNAL_FUNC) sig_disconnected);
209         signal_add_first("channel created", (SIGNAL_FUNC) sig_channel_created);
210         signal_add_last("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
211
212         module_register("core", "fe");
213 }
214
215 void fe_common_core_deinit(void)
216 {
217         hilight_text_deinit();
218         command_history_deinit();
219         completion_deinit();
220         keyboard_deinit();
221         printtext_deinit();
222         formats_deinit();
223 #ifndef WIN32
224         fe_exec_deinit();
225 #endif
226         fe_expandos_deinit();
227         fe_help_deinit();
228         fe_ignore_deinit();
229         fe_log_deinit();
230         fe_modules_deinit();
231         fe_server_deinit();
232         fe_settings_deinit();
233         translation_deinit();
234         windows_deinit();
235         window_activity_deinit();
236         window_commands_deinit();
237         window_items_deinit();
238         windows_layout_deinit();
239         fe_core_commands_deinit();
240
241         fe_channels_deinit();
242         fe_queries_deinit();
243
244         fe_messages_deinit();
245         fe_ignore_messages_init();
246
247         theme_unregister();
248         themes_deinit();
249
250         signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
251         signal_remove("server disconnected", (SIGNAL_FUNC) sig_disconnected);
252         signal_remove("channel created", (SIGNAL_FUNC) sig_channel_created);
253         signal_remove("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
254 }
255
256 void glog_func(const char *log_domain, GLogLevelFlags log_level,
257                const char *message)
258 {
259         const char *reason;
260
261         switch (log_level) {
262         case G_LOG_LEVEL_WARNING:
263                 reason = "warning";
264                 break;
265         case G_LOG_LEVEL_CRITICAL:
266                 reason = "critical";
267                 break;
268         default:
269                 reason = "error";
270                 break;
271         }
272
273         if (windows == NULL)
274                 fprintf(stderr, "GLib %s: %s", reason, message);
275         else {
276                 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
277                             TXT_GLIB_ERROR, reason, message);
278         }
279 }
280
281 static void create_windows(void)
282 {
283         WINDOW_REC *window;
284
285         windows_layout_restore();
286         if (windows != NULL)
287                 return;
288
289         if (settings_get_bool("use_status_window")) {
290                 window = window_create(NULL, TRUE);
291                 window_set_name(window, "(status)");
292                 window_set_level(window, MSGLEVEL_ALL ^
293                                  (settings_get_bool("use_msgs_window") ?
294                                   (MSGLEVEL_MSGS|MSGLEVEL_DCCMSGS) : 0));
295                 window_set_immortal(window, TRUE);
296         }
297
298         if (settings_get_bool("use_msgs_window")) {
299                 window = window_create(NULL, TRUE);
300                 window_set_name(window, "(msgs)");
301                 window_set_level(window, MSGLEVEL_MSGS|MSGLEVEL_DCCMSGS);
302                 window_set_immortal(window, TRUE);
303         }
304
305         if (windows == NULL) {
306                 /* we have to have at least one window.. */
307                 window = window_create(NULL, TRUE);
308         }
309 }
310
311 static void autoconnect_servers(void)
312 {
313         GSList *tmp, *chatnets;
314         char *str;
315
316         if (autocon_server != NULL) {
317                 /* connect to specified server */
318                 str = g_strdup_printf(autocon_password == NULL ? "%s %d" : "%s %d %s",
319                                       autocon_server, autocon_port, autocon_password);
320                 signal_emit("command connect", 1, str);
321                 g_free(str);
322                 return;
323         }
324
325         if (no_autoconnect) {
326                 /* don't autoconnect */
327                 return;
328         }
329
330         /* connect to autoconnect servers */
331         chatnets = NULL;
332         for (tmp = setupservers; tmp != NULL; tmp = tmp->next) {
333                 SERVER_SETUP_REC *rec = tmp->data;
334
335                 if (rec->autoconnect &&
336                     (rec->chatnet == NULL ||
337                      gslist_find_icase_string(chatnets, rec->chatnet) == NULL)) {
338                         if (rec->chatnet != NULL)
339                                 chatnets = g_slist_append(chatnets, rec->chatnet);
340
341                         str = g_strdup_printf("%s %d", rec->address, rec->port);
342                         signal_emit("command connect", 1, str);
343                         g_free(str);
344                 }
345         }
346
347         g_slist_free(chatnets);
348 }
349
350 void fe_common_core_finish_init(void)
351 {
352         int setup_changed;
353
354         signal_emit("irssi init read settings", 0);
355
356 #ifdef SIGPIPE
357         signal(SIGPIPE, SIG_IGN);
358 #endif
359
360         setup_changed = FALSE;
361         if (cmdline_nick != NULL) {
362                 /* override nick found from setup */
363                 settings_set_str("nick", cmdline_nick);
364                 setup_changed = TRUE;
365         }
366
367         if (cmdline_hostname != NULL) {
368                 /* override host name found from setup */
369                 settings_set_str("hostname", cmdline_hostname);
370                 setup_changed = TRUE;
371         }
372
373         create_windows();
374
375         /* _after_ windows are created.. */
376         g_log_set_handler(G_LOG_DOMAIN,
377                           (GLogLevelFlags) (G_LOG_LEVEL_CRITICAL |
378                                             G_LOG_LEVEL_WARNING),
379                           (GLogFunc) glog_func, NULL);
380
381         if (setup_changed)
382                 signal_emit("setup changed", 0);
383
384         autorun_startup();
385         autoconnect_servers();
386 }