Merged from silc_1_0_branch.
[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 (~/.silc/config)", "PATH" },
159                 { "home", 0, POPT_ARG_STRING, NULL, 0, "Irssi home dir location (~/.silc)", "PATH" },
160                 { NULL, '\0', 0, NULL }
161         };
162         const char *home;
163         char *str;
164         int n, len;
165
166         for (n = 1; n < argc; n++) {
167                 if (strncmp(argv[n], "--home=", 7) == 0) {
168                         g_free_not_null(irssi_dir);
169                         irssi_dir = convert_home(argv[n]+7);
170                         len = strlen(irssi_dir);
171                         if (irssi_dir[len-1] == G_DIR_SEPARATOR)
172                                 irssi_dir[len-1] = '\0';
173                 } else if (strncmp(argv[n], "--config=", 9) == 0) {
174                         g_free_not_null(irssi_config_file);
175                         irssi_config_file = convert_home(argv[n]+9);
176                 }
177         }
178
179         if (irssi_dir != NULL && !g_path_is_absolute(irssi_dir)) {
180                 str = irssi_dir;
181                 irssi_dir = g_strdup_printf("%s/%s", g_get_current_dir(), str);
182                 g_free(str);
183         }
184
185         if (irssi_config_file != NULL &&
186             !g_path_is_absolute(irssi_config_file)) {
187                 str = irssi_config_file;
188                 irssi_config_file =
189                         g_strdup_printf("%s/%s", g_get_current_dir(), str);
190                 g_free(str);
191         }
192
193         args_register(options);
194
195         if (irssi_dir == NULL) {
196                 home = g_get_home_dir();
197                 if (home == NULL)
198                         home = ".";
199
200                 irssi_dir = g_strdup_printf(IRSSI_DIR_FULL, home);
201         }
202         if (irssi_config_file == NULL)
203                 irssi_config_file = g_strdup_printf("%s/"IRSSI_HOME_CONFIG, irssi_dir);
204
205         session_set_binary(argv[0]);
206 }
207
208 static void sig_irssi_init_finished(void)
209 {
210         irssi_init_finished = TRUE;
211 }
212
213 void core_init(int argc, char *argv[])
214 {
215         dialog_type_queue = NULL;
216         dialog_text_queue = NULL;
217
218         modules_init();
219 #ifndef WIN32
220         pidwait_init();
221 #endif
222
223         net_disconnect_init();
224         net_sendbuffer_init();
225         signals_init();
226
227         signal_add_first("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
228         signal_add_first("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
229
230         settings_init();
231         commands_init();
232         nickmatch_cache_init();
233         session_init();
234
235         chat_protocols_init();
236         chatnets_init();
237         expandos_init();
238         ignore_init();
239         servers_init();
240         write_buffer_init();
241         log_init();
242         log_away_init();
243         rawlog_init();
244
245         channels_init();
246         queries_init();
247         nicklist_init();
248
249         chat_commands_init();
250
251         settings_add_str("misc", "ignore_signals", "");
252         settings_add_bool("misc", "override_coredump_limit", TRUE);
253
254 #ifdef HAVE_SYS_RESOURCE_H
255         getrlimit(RLIMIT_CORE, &orig_core_rlimit);
256 #endif
257         read_settings();
258         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
259         signal_add("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
260
261         settings_check();
262
263         module_register("core", "core");
264 }
265
266 void core_deinit(void)
267 {
268         module_uniq_destroy("WINDOW ITEM TYPE");
269
270         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
271         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
272
273         chat_commands_deinit();
274
275         nicklist_deinit();
276         queries_deinit();
277         channels_deinit();
278
279         rawlog_deinit();
280         log_away_deinit();
281         log_deinit();
282         write_buffer_deinit();
283         servers_deinit();
284         ignore_deinit();
285         expandos_deinit();
286         chatnets_deinit();
287         chat_protocols_deinit();
288
289         session_deinit();
290         nickmatch_cache_deinit();
291         commands_deinit();
292         settings_deinit();
293         signals_deinit();
294         net_sendbuffer_deinit();
295         net_disconnect_deinit();
296
297 #ifndef WIN32
298         pidwait_deinit();
299 #endif
300         modules_deinit();
301
302         g_free(irssi_dir);
303         g_free(irssi_config_file);
304 }