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
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
25 #include "net-sendbuffer.h"
27 #include "lib-config/iconfig.h"
29 #include "chat-protocols.h"
31 #include "servers-setup.h"
35 static char *session_file;
36 static char *irssi_binary;
38 static char **session_args;
40 void session_set_binary(const char *path)
46 g_free_and_null(irssi_binary);
48 if (g_path_is_absolute(path)) {
49 /* full path - easy */
50 irssi_binary = g_strdup(path);
54 if (strchr(path, G_DIR_SEPARATOR) != NULL) {
56 str = g_get_current_dir();
57 irssi_binary = g_strconcat(str, G_DIR_SEPARATOR_S, path, NULL);
62 /* we'll need to find it from path. */
63 envpath = g_getenv("PATH");
64 if (envpath == NULL) return;
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) {
78 void session_upgrade(void)
80 if (session_args == NULL)
83 execvp(session_args[0], session_args);
84 fprintf(stderr, "exec failed: %s: %s\n",
85 session_args[0], g_strerror(errno));
88 /* SYNTAX: UPGRADE [<irssi binary path>] */
89 static void cmd_upgrade(const char *data)
92 char *session_file, *str;
97 cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
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);
104 signal_emit("session save", 1, session);
105 config_write(session, NULL, -1);
106 config_close(session);
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);
115 signal_emit("gui exit", 0);
118 static void session_save_nick(CHANNEL_REC *channel, NICK_REC *nick,
119 CONFIG_REC *config, CONFIG_NODE *node)
121 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
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);
128 signal_emit("session save nick", 4, channel, nick, config, node);
131 static void session_save_channel_nicks(CHANNEL_REC *channel, CONFIG_REC *config,
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);
143 static void session_save_channel(CHANNEL_REC *channel, CONFIG_REC *config,
146 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
148 config_node_set_str(config, node, "name", channel->name);
149 config_node_set_str(config, node, "topic", channel->topic);
150 config_node_set_str(config, node, "topic_by", channel->topic_by);
151 config_node_set_int(config, node, "topic_time", channel->topic_time);
152 config_node_set_str(config, node, "key", channel->key);
154 signal_emit("session save channel", 3, channel, config, node);
157 static void session_save_server_channels(SERVER_REC *server,
164 node = config_node_section(node, "channels", NODE_TYPE_LIST);
165 for (tmp = server->channels; tmp != NULL; tmp = tmp->next)
166 session_save_channel(tmp->data, config, node);
169 static void session_save_server(SERVER_REC *server, CONFIG_REC *config,
174 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
176 config_node_set_str(config, node, "chat_type",
177 chat_protocol_find_id(server->chat_type)->name);
178 config_node_set_str(config, node, "address", server->connrec->address);
179 config_node_set_int(config, node, "port", server->connrec->port);
180 config_node_set_str(config, node, "chatnet", server->connrec->chatnet);
181 config_node_set_str(config, node, "password", server->connrec->password);
182 config_node_set_str(config, node, "nick", server->nick);
184 handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
185 config_node_set_int(config, node, "handle", handle);
187 signal_emit("session save server", 3, server, config, node);
189 /* fake the server disconnection */
190 g_io_channel_unref(net_sendbuffer_handle(server->handle));
191 net_sendbuffer_destroy(server->handle, FALSE);
192 server->handle = NULL;
194 server->connection_lost = TRUE;
195 server->no_reconnect = TRUE;
196 server_disconnect(server);
199 static void session_restore_channel_nicks(CHANNEL_REC *channel,
205 node = config_node_section(node, "nicks", -1);
206 if (node != NULL && node->type == NODE_TYPE_LIST) {
207 tmp = config_node_first(node->value);
208 for (; tmp != NULL; tmp = config_node_next(tmp)) {
209 signal_emit("session restore nick", 2,
215 static void session_restore_channel(SERVER_REC *server, CONFIG_NODE *node)
217 CHANNEL_REC *channel;
220 name = config_node_get_str(node, "name", NULL);
224 channel = CHAT_PROTOCOL(server)->channel_create(server, name, TRUE);
225 channel->topic = g_strdup(config_node_get_str(node, "topic", NULL));
226 channel->topic_by = g_strdup(config_node_get_str(node, "topic_by", NULL));
227 channel->topic_time = config_node_get_int(node, "topic_time", 0);
228 channel->key = g_strdup(config_node_get_str(node, "key", NULL));
229 channel->session_rejoin = TRUE;
231 signal_emit("session restore channel", 2, channel, node);
234 static void session_restore_server_channels(SERVER_REC *server,
239 /* restore channels */
240 node = config_node_section(node, "channels", -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 session_restore_channel(server, tmp->data);
248 static void session_restore_server(CONFIG_NODE *node)
250 CHAT_PROTOCOL_REC *proto;
251 SERVER_CONNECT_REC *conn;
253 const char *chat_type, *address, *chatnet, *password, *nick;
256 chat_type = config_node_get_str(node, "chat_type", NULL);
257 address = config_node_get_str(node, "address", NULL);
258 port = config_node_get_int(node, "port", 0);
259 chatnet = config_node_get_str(node, "chatnet", NULL);
260 password = config_node_get_str(node, "password", NULL);
261 nick = config_node_get_str(node, "nick", NULL);
262 handle = config_node_get_int(node, "handle", -1);
264 if (chat_type == NULL || address == NULL || nick == NULL || handle < 0)
267 proto = chat_protocol_find(chat_type);
268 if (proto == NULL || proto->not_initialized) {
269 if (handle < 0) close(handle);
273 conn = server_create_conn(proto->id, address, port,
274 chatnet, password, nick);
276 conn->reconnection = TRUE;
278 server = proto->server_connect(conn);
279 server->handle = net_sendbuffer_create(g_io_channel_unix_new(handle), 0);
280 server->session_reconnect = TRUE;
282 signal_emit("session restore server", 2, server, node);
286 static void sig_session_save(CONFIG_REC *config)
293 node = config_node_traverse(config, "(servers", TRUE);
294 while (servers != NULL)
295 session_save_server(servers->data, config, node);
298 str = g_string_new(NULL);
299 for (tmp = pidwait_get_pids(); tmp != NULL; tmp = tmp->next)
300 g_string_sprintfa(str, "%d ", GPOINTER_TO_INT(tmp->data));
301 config_node_set_str(config, config->mainnode, "pids", str->str);
302 g_string_free(str, TRUE);
305 static void sig_session_restore(CONFIG_REC *config)
311 /* restore servers */
312 node = config_node_traverse(config, "(servers", FALSE);
314 tmp = config_node_first(node->value);
315 for (; tmp != NULL; tmp = config_node_next(tmp))
316 session_restore_server(tmp->data);
319 /* restore pids (so we don't leave zombies) */
320 pids = g_strsplit(config_node_get_str(config->mainnode, "pids", ""), " ", -1);
321 for (pid = pids; *pid != NULL; pid++)
322 pidwait_add(atoi(*pid));
326 static void sig_init_finished(void)
330 if (session_file == NULL)
333 session = config_open(session_file, -1);
337 config_parse(session);
338 signal_emit("session restore", 1, session);
339 config_close(session);
341 unlink(session_file);
345 void session_init(void)
347 static struct poptOption options[] = {
348 { "session", 0, POPT_ARG_STRING, &session_file, 0, "Used by /UPGRADE command", "PATH" },
349 { NULL, '\0', 0, NULL }
353 args_register(options);
355 command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);
357 signal_add("session save", (SIGNAL_FUNC) sig_session_save);
358 signal_add("session restore", (SIGNAL_FUNC) sig_session_restore);
359 signal_add("session save server", (SIGNAL_FUNC) session_save_server_channels);
360 signal_add("session restore server", (SIGNAL_FUNC) session_restore_server_channels);
361 signal_add("session save channel", (SIGNAL_FUNC) session_save_channel_nicks);
362 signal_add("session restore channel", (SIGNAL_FUNC) session_restore_channel_nicks);
363 signal_add("irssi init finished", (SIGNAL_FUNC) sig_init_finished);
366 void session_deinit(void)
368 g_free_not_null(irssi_binary);
370 command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);
372 signal_remove("session save", (SIGNAL_FUNC) sig_session_save);
373 signal_remove("session restore", (SIGNAL_FUNC) sig_session_restore);
374 signal_remove("session save server", (SIGNAL_FUNC) session_save_server_channels);
375 signal_remove("session restore server", (SIGNAL_FUNC) session_restore_server_channels);
376 signal_remove("session save channel", (SIGNAL_FUNC) session_save_channel_nicks);
377 signal_remove("session restore channel", (SIGNAL_FUNC) session_restore_channel_nicks);
378 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_init_finished);