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