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