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