4 Copyright (C) 2001 Timo Sirainen
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.
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.
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.
26 #include "net-sendbuffer.h"
28 #include "lib-config/iconfig.h"
30 #include "chat-protocols.h"
32 #include "servers-setup.h"
36 static char *session_file;
37 char *irssi_binary = NULL;
39 static char **session_args;
41 void session_set_binary(const char *path)
43 g_free_and_null(irssi_binary);
45 irssi_binary = g_find_program_in_path(path);
48 void session_upgrade(void)
50 if (session_args == NULL)
53 execv(session_args[0], session_args);
54 fprintf(stderr, "exec failed: %s: %s\n",
55 session_args[0], g_strerror(errno));
58 /* SYNTAX: UPGRADE [<irssi binary path>] */
59 static void cmd_upgrade(const char *data)
62 char *session_file, *str;
68 if ((binary = g_find_program_in_path(data)) == NULL)
69 cmd_return_error(CMDERR_PROGRAM_NOT_FOUND);
71 /* save the session */
72 session_file = g_strdup_printf("%s/session", get_irssi_dir());
73 session = config_open(session_file, 0600);
76 signal_emit("session save", 1, session);
77 config_write(session, NULL, -1);
78 config_close(session);
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());
86 session_args = g_strsplit(str, " ", -1);
89 signal_emit("gui exit", 0);
92 static void session_save_nick(CHANNEL_REC *channel, NICK_REC *nick,
93 CONFIG_REC *config, CONFIG_NODE *node)
95 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
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);
102 config_node_set_str(config, node, "prefixes", nick->prefixes);
104 signal_emit("session save nick", 4, channel, nick, config, node);
107 static void session_save_channel_nicks(CHANNEL_REC *channel, CONFIG_REC *config,
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);
119 static void session_save_channel(CHANNEL_REC *channel, CONFIG_REC *config,
122 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
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);
131 signal_emit("session save channel", 3, channel, config, node);
134 static void session_save_server_channels(SERVER_REC *server,
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);
146 static void session_save_server(SERVER_REC *server, CONFIG_REC *config,
151 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
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);
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);
169 handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
170 config_node_set_int(config, node, "handle", handle);
172 signal_emit("session save server", 3, server, config, node);
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;
179 server->connection_lost = TRUE;
180 server->no_reconnect = TRUE;
181 server_disconnect(server);
184 static void session_restore_channel_nicks(CHANNEL_REC *channel,
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,
200 static void session_restore_channel(SERVER_REC *server, CONFIG_NODE *node)
202 CHANNEL_REC *channel;
203 const char *name, *visible_name;
205 name = config_node_get_str(node, "name", NULL);
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;
217 signal_emit("session restore channel", 2, channel, node);
220 static void session_restore_server_channels(SERVER_REC *server,
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);
234 static void session_restore_server(CONFIG_NODE *node)
236 CHAT_PROTOCOL_REC *proto;
237 SERVER_CONNECT_REC *conn;
239 const char *chat_type, *address, *chatnet, *password, *nick;
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);
250 if (chat_type == NULL || address == NULL || nick == NULL || handle < 0)
253 proto = chat_protocol_find(chat_type);
254 if (proto == NULL || proto->not_initialized) {
255 if (handle < 0) close(handle);
259 conn = server_create_conn(proto->id, address, port,
260 chatnet, password, nick);
262 conn->reconnection = TRUE;
263 conn->connect_handle = g_io_channel_new(handle);
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);
270 proto->server_connect(server);
274 static void sig_session_save(CONFIG_REC *config)
281 node = config_node_traverse(config, "(servers", TRUE);
282 while (servers != NULL)
283 session_save_server(servers->data, config, node);
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);
293 static void sig_session_restore(CONFIG_REC *config)
299 /* restore servers */
300 node = config_node_traverse(config, "(servers", FALSE);
302 tmp = config_node_first(node->value);
303 for (; tmp != NULL; tmp = config_node_next(tmp))
304 session_restore_server(tmp->data);
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));
314 static void sig_init_finished(void)
318 if (session_file == NULL)
321 session = config_open(session_file, -1);
325 config_parse(session);
326 signal_emit("session restore", 1, session);
327 config_close(session);
329 unlink(session_file);
332 void session_register_options(void)
334 static GOptionEntry options[] = {
335 { "session", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &session_file, "Used by /UPGRADE command", "PATH" },
340 args_register(options);
343 void session_init(void)
345 command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);
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);
356 void session_deinit(void)
358 g_free_not_null(irssi_binary);
360 command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);
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);