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