updates.
[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, get_home_dir());
196         if (irssi_config_file == NULL)
197                 irssi_config_file = g_strdup_printf("%s/"IRSSI_HOME_CONFIG, 
198                                                     irssi_dir);
199
200         session_set_binary(argv[0]);
201 }
202
203 static void sig_irssi_init_finished(void)
204 {
205         irssi_init_finished = TRUE;
206 }
207
208 void core_init(int argc, char *argv[])
209 {
210         dialog_type_queue = NULL;
211         dialog_text_queue = NULL;
212
213         modules_init();
214 #ifndef WIN32
215         pidwait_init();
216 #endif
217
218         net_disconnect_init();
219         net_sendbuffer_init();
220         signals_init();
221
222         signal_add_first("gui dialog", (SIGNAL_FUNC) sig_gui_dialog);
223         signal_add_first("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
224
225         settings_init();
226         commands_init();
227         nickmatch_cache_init();
228         session_init();
229
230         chat_protocols_init();
231         chatnets_init();
232         expandos_init();
233         ignore_init();
234         servers_init();
235         write_buffer_init();
236         log_init();
237         log_away_init();
238         rawlog_init();
239
240         channels_init();
241         queries_init();
242         nicklist_init();
243
244         chat_commands_init();
245
246         settings_add_str("misc", "ignore_signals", "");
247         settings_add_bool("misc", "override_coredump_limit", TRUE);
248
249 #ifdef HAVE_SYS_RESOURCE_H
250         getrlimit(RLIMIT_CORE, &orig_core_rlimit);
251 #endif
252         read_settings();
253         signal_add("setup changed", (SIGNAL_FUNC) read_settings);
254         signal_add("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
255
256         settings_check();
257
258         module_register("core", "core");
259 }
260
261 void core_deinit(void)
262 {
263         module_uniq_destroy("WINDOW ITEM TYPE");
264
265         signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
266         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_irssi_init_finished);
267
268         chat_commands_deinit();
269
270         nicklist_deinit();
271         queries_deinit();
272         channels_deinit();
273
274         rawlog_deinit();
275         log_away_deinit();
276         log_deinit();
277         write_buffer_deinit();
278         servers_deinit();
279         ignore_deinit();
280         expandos_deinit();
281         chatnets_deinit();
282         chat_protocols_deinit();
283
284         session_deinit();
285         nickmatch_cache_deinit();
286         commands_deinit();
287         settings_deinit();
288         signals_deinit();
289         net_sendbuffer_deinit();
290         net_disconnect_deinit();
291
292 #ifndef WIN32
293         pidwait_deinit();
294 #endif
295         modules_deinit();
296
297         g_free(irssi_dir);
298         g_free(irssi_config_file);
299 }