Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / session.c
1 /*
2  session.c : irssi
3
4     Copyright (C) 2001 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 "signals.h"
23 #include "commands.h"
24 #include "args.h"
25 #include "net-sendbuffer.h"
26 #include "pidwait.h"
27 #include "lib-config/iconfig.h"
28
29 #include "chat-protocols.h"
30 #include "servers.h"
31 #include "servers-setup.h"
32 #include "channels.h"
33 #include "nicklist.h"
34
35 static char *session_file;
36 char *irssi_binary = NULL;
37
38 static char **session_args;
39
40 #ifndef HAVE_GLIB2
41 static char *g_find_program_in_path(const char *path)
42 {
43         const char *envpath;
44         char **paths, **tmp;
45         char *str;
46         char *result = NULL;
47
48         if (g_path_is_absolute(path)) {
49                 /* full path - easy */
50                 if(access(path, X_OK) == -1)
51                         return NULL;
52                 else
53                         return g_strdup(path);
54         }
55
56         if (strchr(path, G_DIR_SEPARATOR) != NULL) {
57                 /* relative path */
58                 str = g_get_current_dir();
59                 result = g_strconcat(str, G_DIR_SEPARATOR_S, path, NULL);
60                 g_free(str);
61                 if (access(result, X_OK) == -1) {
62                         g_free(result);
63                         return NULL;
64                 }
65                 else
66                         return result;
67         }
68
69         /* we'll need to find it from path. */
70         envpath = g_getenv("PATH");
71         if (envpath == NULL) return NULL;
72
73         paths = g_strsplit(envpath, ":", -1);
74         for (tmp = paths; *tmp != NULL; tmp++) {
75                 str = g_strconcat(*tmp, G_DIR_SEPARATOR_S, path, NULL);
76                 if (access(str, X_OK) == 0) {
77                         result = str;
78                         break;
79                 }
80                 g_free(str);
81         }
82         g_strfreev(paths);
83
84         return result;
85 }
86 #endif
87
88 void session_set_binary(const char *path)
89 {
90         g_free_and_null(irssi_binary);
91
92         irssi_binary = g_find_program_in_path(path);
93 }
94
95 void session_upgrade(void)
96 {
97         if (session_args == NULL)
98                 return;
99
100         execv(session_args[0], session_args);
101         fprintf(stderr, "exec failed: %s: %s\n",
102                 session_args[0], g_strerror(errno));
103 }
104
105 /* SYNTAX: UPGRADE [<irssi binary path>] */
106 static void cmd_upgrade(const char *data)
107 {
108         CONFIG_REC *session;
109         char *session_file, *str;
110         char *binary;
111
112         if (*data == '\0')
113                 data = irssi_binary;
114
115         if ((binary = g_find_program_in_path(data)) == NULL)
116                 cmd_return_error(CMDERR_PROGRAM_NOT_FOUND);
117
118         /* save the session */
119         session_file = g_strdup_printf("%s/session", get_irssi_dir());
120         session = config_open(session_file, 0600);
121         unlink(session_file);
122
123         signal_emit("session save", 1, session);
124         config_write(session, NULL, -1);
125         config_close(session);
126
127         /* data may contain some other program as well, like
128            /UPGRADE /usr/bin/screen irssi */
129         str = g_strdup_printf("%s --noconnect --session=%s --home=%s --config=%s",
130                               binary, session_file, get_irssi_dir(), get_irssi_config());
131         g_free(binary);
132         g_free(session_file);
133         session_args = g_strsplit(str, " ", -1);
134         g_free(str);
135
136         signal_emit("gui exit", 0);
137 }
138
139 static void session_save_nick(CHANNEL_REC *channel, NICK_REC *nick,
140                               CONFIG_REC *config, CONFIG_NODE *node)
141 {
142         static char other[2];
143         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
144
145         config_node_set_str(config, node, "nick", nick->nick);
146         config_node_set_bool(config, node, "op", nick->op);
147         config_node_set_bool(config, node, "halfop", nick->halfop);
148         config_node_set_bool(config, node, "voice", nick->voice);
149         
150         other[0] = nick->other;
151         other[1] = '\0';
152         config_node_set_str(config, node, "other", other);
153
154         signal_emit("session save nick", 4, channel, nick, config, node);
155 }
156
157 static void session_save_channel_nicks(CHANNEL_REC *channel, CONFIG_REC *config,
158                                        CONFIG_NODE *node)
159 {
160         GSList *tmp, *nicks;
161
162         node = config_node_section(node, "nicks", NODE_TYPE_LIST);
163         nicks = nicklist_getnicks(channel);
164         for (tmp = nicks; tmp != NULL; tmp = tmp->next)
165                 session_save_nick(channel, tmp->data, config, node);
166         g_slist_free(nicks);
167 }
168
169 static void session_save_channel(CHANNEL_REC *channel, CONFIG_REC *config,
170                                  CONFIG_NODE *node)
171 {
172         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
173
174         config_node_set_str(config, node, "name", channel->name);
175         config_node_set_str(config, node, "visible_name", channel->visible_name);
176         config_node_set_str(config, node, "topic", channel->topic);
177         config_node_set_str(config, node, "topic_by", channel->topic_by);
178         config_node_set_int(config, node, "topic_time", channel->topic_time);
179         config_node_set_str(config, node, "key", channel->key);
180
181         signal_emit("session save channel", 3, channel, config, node);
182 }
183
184 static void session_save_server_channels(SERVER_REC *server,
185                                          CONFIG_REC *config,
186                                          CONFIG_NODE *node)
187 {
188         GSList *tmp;
189
190         /* save channels */
191         node = config_node_section(node, "channels", NODE_TYPE_LIST);
192         for (tmp = server->channels; tmp != NULL; tmp = tmp->next)
193                 session_save_channel(tmp->data, config, node);
194 }
195
196 static void session_save_server(SERVER_REC *server, CONFIG_REC *config,
197                                 CONFIG_NODE *node)
198 {
199         int handle;
200
201         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
202
203         config_node_set_str(config, node, "chat_type",
204                             chat_protocol_find_id(server->chat_type)->name);
205         config_node_set_str(config, node, "address", server->connrec->address);
206         config_node_set_int(config, node, "port", server->connrec->port);
207         config_node_set_str(config, node, "chatnet", server->connrec->chatnet);
208         config_node_set_str(config, node, "password", server->connrec->password);
209         config_node_set_str(config, node, "nick", server->nick);
210         config_node_set_str(config, node, "version", server->version);
211
212         config_node_set_bool(config, node, "use_ssl", server->connrec->use_ssl);
213         config_node_set_str(config, node, "ssl_cert", server->connrec->ssl_cert);
214         config_node_set_str(config, node, "ssl_pkey", server->connrec->ssl_pkey);
215         config_node_set_bool(config, node, "ssl_verify", server->connrec->ssl_verify);
216         config_node_set_str(config, node, "ssl_cafile", server->connrec->ssl_cafile);
217         config_node_set_str(config, node, "ssl_capath", server->connrec->ssl_capath);
218
219         handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
220         config_node_set_int(config, node, "handle", handle);
221
222         signal_emit("session save server", 3, server, config, node);
223
224         /* fake the server disconnection */
225         g_io_channel_unref(net_sendbuffer_handle(server->handle));
226         net_sendbuffer_destroy(server->handle, FALSE);
227         server->handle = NULL;
228
229         server->connection_lost = TRUE;
230         server->no_reconnect = TRUE;
231         server_disconnect(server);
232 }
233
234 static void session_restore_channel_nicks(CHANNEL_REC *channel,
235                                           CONFIG_NODE *node)
236 {
237         GSList *tmp;
238
239         /* restore nicks */
240         node = config_node_section(node, "nicks", -1);
241         if (node != NULL && node->type == NODE_TYPE_LIST) {
242                 tmp = config_node_first(node->value);
243                 for (; tmp != NULL; tmp = config_node_next(tmp)) {
244                         signal_emit("session restore nick", 2,
245                                     channel, tmp->data);
246                 }
247         }
248 }
249
250 static void session_restore_channel(SERVER_REC *server, CONFIG_NODE *node)
251 {
252         CHANNEL_REC *channel;
253         const char *name, *visible_name;
254
255         name = config_node_get_str(node, "name", NULL);
256         if (name == NULL)
257                 return;
258
259         visible_name = config_node_get_str(node, "visible_name", NULL);
260         channel = CHAT_PROTOCOL(server)->channel_create(server, name, visible_name, TRUE);
261         channel->topic = g_strdup(config_node_get_str(node, "topic", NULL));
262         channel->topic_by = g_strdup(config_node_get_str(node, "topic_by", NULL));
263         channel->topic_time = config_node_get_int(node, "topic_time", 0);
264         channel->key = g_strdup(config_node_get_str(node, "key", NULL));
265         channel->session_rejoin = TRUE;
266
267         signal_emit("session restore channel", 2, channel, node);
268 }
269
270 static void session_restore_server_channels(SERVER_REC *server,
271                                             CONFIG_NODE *node)
272 {
273         GSList *tmp;
274
275         /* restore channels */
276         node = config_node_section(node, "channels", -1);
277         if (node != NULL && node->type == NODE_TYPE_LIST) {
278                 tmp = config_node_first(node->value);
279                 for (; tmp != NULL; tmp = config_node_next(tmp))
280                         session_restore_channel(server, tmp->data);
281         }
282 }
283
284 static void session_restore_server(CONFIG_NODE *node)
285 {
286         CHAT_PROTOCOL_REC *proto;
287         SERVER_CONNECT_REC *conn;
288         SERVER_REC *server;
289         const char *chat_type, *address, *chatnet, *password, *nick;
290         int port, handle;
291
292         chat_type = config_node_get_str(node, "chat_type", NULL);
293         address = config_node_get_str(node, "address", NULL);
294         port = config_node_get_int(node, "port", 0);
295         chatnet = config_node_get_str(node, "chatnet", NULL);
296         password = config_node_get_str(node, "password", NULL);
297         nick = config_node_get_str(node, "nick", NULL);
298         handle = config_node_get_int(node, "handle", -1);
299
300         if (chat_type == NULL || address == NULL || nick == NULL || handle < 0)
301                 return;
302
303         proto = chat_protocol_find(chat_type);
304         if (proto == NULL || proto->not_initialized) {
305                 if (handle < 0) close(handle);
306                 return;
307         }
308
309         conn = server_create_conn(proto->id, address, port,
310                                   chatnet, password, nick);
311         if (conn != NULL) {
312                 conn->reconnection = TRUE;
313                 conn->connect_handle = g_io_channel_unix_new(handle);
314
315                 server = proto->server_init_connect(conn);
316                 server->version = g_strdup(config_node_get_str(node, "version", NULL));         
317                 server->session_reconnect = TRUE;
318                 signal_emit("session restore server", 2, server, node);
319
320                 proto->server_connect(server);
321         }
322 }
323
324 static void sig_session_save(CONFIG_REC *config)
325 {
326         CONFIG_NODE *node;
327         GSList *tmp;
328         GString *str;
329
330         /* save servers */
331         node = config_node_traverse(config, "(servers", TRUE);
332         while (servers != NULL)
333                 session_save_server(servers->data, config, node);
334
335         /* save pids */
336         str = g_string_new(NULL);
337         for (tmp = pidwait_get_pids(); tmp != NULL; tmp = tmp->next)
338                 g_string_sprintfa(str, "%d ", GPOINTER_TO_INT(tmp->data));
339         config_node_set_str(config, config->mainnode, "pids", str->str);
340         g_string_free(str, TRUE);
341 }
342
343 static void sig_session_restore(CONFIG_REC *config)
344 {
345         CONFIG_NODE *node;
346         GSList *tmp;
347         char **pids, **pid;
348
349         /* restore servers */
350         node = config_node_traverse(config, "(servers", FALSE);
351         if (node != NULL) {
352                 tmp = config_node_first(node->value);
353                 for (; tmp != NULL; tmp = config_node_next(tmp))
354                         session_restore_server(tmp->data);
355         }
356
357         /* restore pids (so we don't leave zombies) */
358         pids = g_strsplit(config_node_get_str(config->mainnode, "pids", ""), " ", -1);
359         for (pid = pids; *pid != NULL; pid++)
360                 pidwait_add(atoi(*pid));
361         g_strfreev(pids);
362 }
363
364 static void sig_init_finished(void)
365 {
366         CONFIG_REC *session;
367
368         if (session_file == NULL)
369                 return;
370
371         session = config_open(session_file, -1);
372         if (session == NULL)
373                 return;
374
375         config_parse(session);
376         signal_emit("session restore", 1, session);
377         config_close(session);
378
379         unlink(session_file);
380         session_file = NULL;
381 }
382
383 void session_init(void)
384 {
385         static struct poptOption options[] = {
386                 { "session", 0, POPT_ARG_STRING, &session_file, 0, "Used by /UPGRADE command", "PATH" },
387                 { NULL, '\0', 0, NULL }
388         };
389
390         session_file = NULL;
391         args_register(options);
392
393         command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);
394
395         signal_add("session save", (SIGNAL_FUNC) sig_session_save);
396         signal_add("session restore", (SIGNAL_FUNC) sig_session_restore);
397         signal_add("session save server", (SIGNAL_FUNC) session_save_server_channels);
398         signal_add("session restore server", (SIGNAL_FUNC) session_restore_server_channels);
399         signal_add("session save channel", (SIGNAL_FUNC) session_save_channel_nicks);
400         signal_add("session restore channel", (SIGNAL_FUNC) session_restore_channel_nicks);
401         signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
402 }
403
404 void session_deinit(void)
405 {
406         g_free_not_null(irssi_binary);
407
408         command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);
409
410         signal_remove("session save", (SIGNAL_FUNC) sig_session_save);
411         signal_remove("session restore", (SIGNAL_FUNC) sig_session_restore);
412         signal_remove("session save server", (SIGNAL_FUNC) session_save_server_channels);
413         signal_remove("session restore server", (SIGNAL_FUNC) session_restore_server_channels);
414         signal_remove("session save channel", (SIGNAL_FUNC) session_save_channel_nicks);
415         signal_remove("session restore channel", (SIGNAL_FUNC) session_restore_channel_nicks);
416         signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
417 }