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, "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);
155 signal_emit("session save channel", 3, channel, config, node);
158 static void session_save_server_channels(SERVER_REC *server,
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);
170 static void session_save_server(SERVER_REC *server, CONFIG_REC *config,
175 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
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);
185 config_node_set_bool(config, node, "use_ssl", server->connrec->use_ssl);
187 handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
188 config_node_set_int(config, node, "handle", handle);
190 signal_emit("session save server", 3, server, config, node);
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;
197 server->connection_lost = TRUE;
198 server->no_reconnect = TRUE;
199 server_disconnect(server);
202 static void session_restore_channel_nicks(CHANNEL_REC *channel,
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,
218 static void session_restore_channel(SERVER_REC *server, CONFIG_NODE *node)
220 CHANNEL_REC *channel;
221 const char *name, *visible_name;
223 name = config_node_get_str(node, "name", NULL);
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;
235 signal_emit("session restore channel", 2, channel, node);
238 static void session_restore_server_channels(SERVER_REC *server,
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);
252 static void session_restore_server(CONFIG_NODE *node)
254 CHAT_PROTOCOL_REC *proto;
255 SERVER_CONNECT_REC *conn;
257 const char *chat_type, *address, *chatnet, *password, *nick;
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);
268 if (chat_type == NULL || address == NULL || nick == NULL || handle < 0)
271 proto = chat_protocol_find(chat_type);
272 if (proto == NULL || proto->not_initialized) {
273 if (handle < 0) close(handle);
277 conn = server_create_conn(proto->id, address, port,
278 chatnet, password, nick);
280 conn->reconnection = TRUE;
281 conn->connect_handle = g_io_channel_unix_new(handle);
283 server = proto->server_init_connect(conn);
284 server->session_reconnect = TRUE;
285 signal_emit("session restore server", 2, server, node);
287 proto->server_connect(server);
291 static void sig_session_save(CONFIG_REC *config)
298 node = config_node_traverse(config, "(servers", TRUE);
299 while (servers != NULL)
300 session_save_server(servers->data, config, node);
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);
310 static void sig_session_restore(CONFIG_REC *config)
316 /* restore servers */
317 node = config_node_traverse(config, "(servers", FALSE);
319 tmp = config_node_first(node->value);
320 for (; tmp != NULL; tmp = config_node_next(tmp))
321 session_restore_server(tmp->data);
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));
331 static void sig_init_finished(void)
335 if (session_file == NULL)
338 session = config_open(session_file, -1);
342 config_parse(session);
343 signal_emit("session restore", 1, session);
344 config_close(session);
346 unlink(session_file);
350 void session_init(void)
352 static struct poptOption options[] = {
353 { "session", 0, POPT_ARG_STRING, &session_file, 0, "Used by /UPGRADE command", "PATH" },
354 { NULL, '\0', 0, NULL }
358 args_register(options);
360 /*command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);*/
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);
371 void session_deinit(void)
373 g_free_not_null(irssi_binary);
375 /*command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);*/
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);