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