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