Merged Irssi 0.8.2 from irssi.org cvs.
[silc.git] / apps / irssi / src / core / core.c
1 /*
2  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 <signal.h>
23
24 #include "args.h"
25 #include "pidwait.h"
26 #include "misc.h"
27
28 #include "net-disconnect.h"
29 #include "net-sendbuffer.h"
30 #include "signals.h"
31 #include "settings.h"
32 #include "session.h"
33
34 #include "chat-protocols.h"
35 #include "servers.h"
36 #include "chatnets.h"
37 #include "commands.h"
38 #include "expandos.h"
39 #include "write-buffer.h"
40 #include "log.h"
41 #include "rawlog.h"
42 #include "ignore.h"
43
44 #include "channels.h"
45 #include "queries.h"
46 #include "nicklist.h"
47 #include "nickmatch-cache.h"
48
49 #ifdef HAVE_SYS_RESOURCE_H
50 #  include <sys/resource.h>
51    struct rlimit orig_core_rlimit;
52 #endif
53
54 void chat_commands_init(void);
55 void chat_commands_deinit(void);
56
57 void log_away_init(void);
58 void log_away_deinit(void);
59
60 int irssi_gui;
61 int irssi_init_finished;
62 int reload_config;
63
64 static char *irssi_dir, *irssi_config_file;
65 static GSList *dialog_type_queue, *dialog_text_queue;
66
67 const char *get_irssi_dir(void)
68 {
69         return irssi_dir;
70 }
71
72 /* return full path for ~/.irssi/config */
73 const char *get_irssi_config(void)
74 {
75         return irssi_config_file;
76 }
77
78 static void sig_reload_config(int signo)
79 {
80         reload_config = TRUE;
81 }
82
83 static void read_settings(void)
84 {
85 #ifndef WIN32
86         static int signals[] = {
87                 SIGINT, SIGQUIT, SIGTERM,
88                 SIGALRM, SIGUSR1, SIGUSR2
89         };
90         static char *signames[] = {
91                 "int", "quit", "term",
92                 "alrm", "usr1", "usr2"
93         };
94
95         const char *ignores;
96         struct sigaction act;
97         int n;
98
99         ignores = settings_get_str("ignore_signals");
100
101         sigemptyset (&act.sa_mask);
102         act.sa_flags = 0;
103
104         /* reload config on SIGHUP */
105         act.sa_handler = sig_reload_config;
106         sigaction(SIGHUP, &act, NULL);
107
108         for (n = 0; n < sizeof(signals)/sizeof(signals[0]); n++) {
109                 act.sa_handler = find_substr(ignores, signames[n]) ?
110                         SIG_IGN : SIG_DFL;
111                 sigaction(signals[n], &act, NULL);
112         }
113
114 #ifdef HAVE_SYS_RESOURCE_H
115         if (!settings_get_bool("override_coredump_limit"))
116                 setrlimit(RLIMIT_CORE, &orig_core_rlimit);
117         else {
118                 struct rlimit rlimit;
119
120                 rlimit.rlim_cur = RLIM_INFINITY;
121                 rlimit.rlim_max = RLIM_INFINITY;
122                 if (setrlimit(RLIMIT_CORE, &rlimit) == -1)
123                         settings_set_bool("override_coredump_limit", FALSE);
124         }
125 #endif
126 #endif
127 }
128
129 static void sig_gui_dialog(const char *type, const char *text)
130 {
131         dialog_type_queue = g_slist_append(dialog_type_queue, g_strdup(type));
132         dialog_text_queue = g_slist_append(dialog_text_queue, g_strdup(text));
133 }
134
135 static void sig_init_finished(void)
136 {
137         GSList *type, *text;
138
139         signal_remove("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
140         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
141
142         /* send the dialog texts that were in queue before irssi
143            was initialized */
144         type = dialog_type_queue;
145         text = dialog_text_queue;
146         for (; text != NULL; text = text->next, type = type->next) {
147                 signal_emit("gui dialog", 2, type->data, text->data);
148                 g_free(type->data);
149                 g_free(text->data);
150         }
151         g_slist_free(dialog_type_queue);
152         g_slist_free(dialog_text_queue);
153 }
154
155 void core_init_paths(int argc, char *argv[])
156 {
157         static struct poptOption options[] = {
158                 { "config", 0, POPT_ARG_STRING, NULL, 0, "Configuration file location (~/.irssi/config)", "PATH" },
159                 { "home", 0, POPT_ARG_STRING, NULL, 0, "Irssi home dir location (~/.irssi)", "PATH" },
160                 { NULL, '\0', 0, NULL }
161         };
162         char *str;
163         int n, len;
164
165         for (n = 1; n < argc; n++) {
166                 if (strncmp(argv[n], "--home=", 7) == 0) {
167                         g_free_not_null(irssi_dir);
168                         irssi_dir = convert_home(argv[n]+7);
169                         len = strlen(irssi_dir);
170                         if (irssi_dir[len-1] == G_DIR_SEPARATOR)
171                                 irssi_dir[len-1] = '\0';
172                 } else if (strncmp(argv[n], "--config=", 9) == 0) {
173                         g_free_not_null(irssi_config_file);
174                         irssi_config_file = convert_home(argv[n]+9);
175                 }
176         }
177
178         if (irssi_dir != NULL && !g_path_is_absolute(irssi_dir)) {
179                 str = irssi_dir;
180                 irssi_dir = g_strdup_printf("%s/%s", g_get_current_dir(), str);
181                 g_free(str);
182         }
183
184         if (irssi_config_file != NULL &&
185             !g_path_is_absolute(irssi_config_file)) {
186                 str = irssi_config_file;
187                 irssi_config_file =
188                         g_strdup_printf("%s/%s", g_get_current_dir(), str);
189                 g_free(str);
190         }
191
192         args_register(options);
193
194         if (irssi_dir == NULL)
195                 irssi_dir = g_strdup_printf(IRSSI_DIR_FULL, g_get_home_dir());
196         if (irssi_config_file == NULL)
197                 irssi_config_file = g_strdup_printf("%s/"IRSSI_HOME_CONFIG, irssi_dir);
198
199         session_set_binary(argv[0]);
200 }
201
202 static void sig_irssi_init_finished(void)
203 {
204         irssi_init_finished = TRUE;
205 }
206
207 void core_init(int argc, char *argv[])
208 {
209         dialog_type_queue = NULL;
210         dialog_text_queue = NULL;
211
212         modules_init();
213 #ifndef WIN32
214         pidwait_init();
215 #endif
216
217         net_disconnect_init();
218         net_sendbuffer_init();
219         signals_init();
220
221         signal_add_first("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
222         signal_add_first("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
223
224         settings_init();
225         commands_init();
226         nickmatch_cache_init();
227         session_init();
228
229         chat_protocols_init();
230         chatnets_init();
231         expandos_init();
232         ignore_init();
233         servers_init();
234         write_buffer_init();
235         log_init();
236         log_away_init();
237         rawlog_init();
238
239         channels_init();
240         queries_init();
241         nicklist_init();
242
243         chat_commands_init();
244
245         settings_add_str("misc", "ignore_signals", "");
246         settings_add_bool("misc", "override_coredump_limit", TRUE);
247
248 #ifdef HAVE_SYS_RESOURCE_H
249         getrlimit(RLIMIT_CORE, &orig_core_rlimit);
250 #endif
251         read_settings();
252         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
253         signal_add("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
254
255         settings_check();
256
257         module_register("core", "core");
258 }
259
260 void core_deinit(void)
261 {
262         module_uniq_destroy("WINDOW ITEM TYPE");
263
264         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
265         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
266
267         chat_commands_deinit();
268
269         nicklist_deinit();
270         queries_deinit();
271         channels_deinit();
272
273         rawlog_deinit();
274         log_away_deinit();
275         log_deinit();
276         write_buffer_deinit();
277         servers_deinit();
278         ignore_deinit();
279         expandos_deinit();
280         chatnets_deinit();
281         chat_protocols_deinit();
282
283         session_deinit();
284         nickmatch_cache_deinit();
285         commands_deinit();
286         settings_deinit();
287         signals_deinit();
288         net_sendbuffer_deinit();
289         net_disconnect_deinit();
290
291 #ifndef WIN32
292         pidwait_deinit();
293 #endif
294         modules_deinit();
295
296         g_free(irssi_dir);
297         g_free(irssi_config_file);
298 }