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