Merged Irssi SVN (upcoming irssi 0.8.11).
[silc.git] / apps / irssi / src / core / session.c
index 40c4f5474938848d41100f3e2a4f75b8d67afd93..f3553634ba7f01841cbd34bdb4dc2a1c49b3f990 100644 (file)
 #include "nicklist.h"
 
 static char *session_file;
-static char *irssi_binary;
+char *irssi_binary = NULL;
 
 static char **session_args;
 
-void session_set_binary(const char *path)
+#ifndef HAVE_GLIB2
+static char *g_find_program_in_path(const char *path)
 {
        const char *envpath;
        char **paths, **tmp;
         char *str;
-
-       g_free_and_null(irssi_binary);
+       char *result = NULL;
 
        if (g_path_is_absolute(path)) {
                 /* full path - easy */
-               irssi_binary = g_strdup(path);
-                return;
+               if(access(path, X_OK) == -1)
+                       return NULL;
+               else
+                       return g_strdup(path);
        }
 
        if (strchr(path, G_DIR_SEPARATOR) != NULL) {
                /* relative path */
                 str = g_get_current_dir();
-               irssi_binary = g_strconcat(str, G_DIR_SEPARATOR_S, path, NULL);
+               result = g_strconcat(str, G_DIR_SEPARATOR_S, path, NULL);
                g_free(str);
-                return;
+               if (access(result, X_OK) == -1) {
+                       g_free(result);
+                       return NULL;
+               }
+               else
+                       return result;
        }
 
        /* we'll need to find it from path. */
        envpath = g_getenv("PATH");
-       if (envpath == NULL) return;
+       if (envpath == NULL) return NULL;
 
        paths = g_strsplit(envpath, ":", -1);
        for (tmp = paths; *tmp != NULL; tmp++) {
                 str = g_strconcat(*tmp, G_DIR_SEPARATOR_S, path, NULL);
                if (access(str, X_OK) == 0) {
-                       irssi_binary = str;
+                       result = str;
                         break;
                }
                 g_free(str);
        }
        g_strfreev(paths);
+
+       return result;
+}
+#endif
+
+void session_set_binary(const char *path)
+{
+       g_free_and_null(irssi_binary);
+
+       irssi_binary = g_find_program_in_path(path);
 }
 
 void session_upgrade(void)
@@ -80,7 +97,7 @@ void session_upgrade(void)
        if (session_args == NULL)
                 return;
 
-       execvp(session_args[0], session_args);
+       execv(session_args[0], session_args);
        fprintf(stderr, "exec failed: %s: %s\n",
                session_args[0], g_strerror(errno));
 }
@@ -90,11 +107,13 @@ static void cmd_upgrade(const char *data)
 {
        CONFIG_REC *session;
        char *session_file, *str;
+       char *binary;
 
        if (*data == '\0')
                data = irssi_binary;
-       if (data == NULL)
-                cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
+
+       if ((binary = g_find_program_in_path(data)) == NULL)
+               cmd_return_error(CMDERR_PROGRAM_NOT_FOUND);
 
        /* save the session */
         session_file = g_strdup_printf("%s/session", get_irssi_dir());
@@ -108,7 +127,9 @@ static void cmd_upgrade(const char *data)
        /* data may contain some other program as well, like
           /UPGRADE /usr/bin/screen irssi */
        str = g_strdup_printf("%s --noconnect --session=%s --home=%s --config=%s",
-                             data, session_file, get_irssi_dir(), get_irssi_config());
+                             binary, session_file, get_irssi_dir(), get_irssi_config());
+       g_free(binary);
+       g_free(session_file);
         session_args = g_strsplit(str, " ", -1);
         g_free(str);
 
@@ -118,12 +139,17 @@ static void cmd_upgrade(const char *data)
 static void session_save_nick(CHANNEL_REC *channel, NICK_REC *nick,
                              CONFIG_REC *config, CONFIG_NODE *node)
 {
+       static char other[2];
        node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
 
        config_node_set_str(config, node, "nick", nick->nick);
        config_node_set_bool(config, node, "op", nick->op);
        config_node_set_bool(config, node, "halfop", nick->halfop);
        config_node_set_bool(config, node, "voice", nick->voice);
+       
+       other[0] = nick->other;
+       other[1] = '\0';
+       config_node_set_str(config, node, "other", other);
 
        signal_emit("session save nick", 4, channel, nick, config, node);
 }
@@ -181,8 +207,14 @@ static void session_save_server(SERVER_REC *server, CONFIG_REC *config,
        config_node_set_str(config, node, "chatnet", server->connrec->chatnet);
        config_node_set_str(config, node, "password", server->connrec->password);
        config_node_set_str(config, node, "nick", server->nick);
+       config_node_set_str(config, node, "version", server->version);
 
        config_node_set_bool(config, node, "use_ssl", server->connrec->use_ssl);
+       config_node_set_str(config, node, "ssl_cert", server->connrec->ssl_cert);
+       config_node_set_str(config, node, "ssl_pkey", server->connrec->ssl_pkey);
+       config_node_set_bool(config, node, "ssl_verify", server->connrec->ssl_verify);
+       config_node_set_str(config, node, "ssl_cafile", server->connrec->ssl_cafile);
+       config_node_set_str(config, node, "ssl_capath", server->connrec->ssl_capath);
 
        handle = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
        config_node_set_int(config, node, "handle", handle);
@@ -281,6 +313,7 @@ static void session_restore_server(CONFIG_NODE *node)
                conn->connect_handle = g_io_channel_unix_new(handle);
 
                server = proto->server_init_connect(conn);
+               server->version = g_strdup(config_node_get_str(node, "version", NULL));         
                server->session_reconnect = TRUE;
                signal_emit("session restore server", 2, server, node);
 
@@ -350,16 +383,14 @@ static void sig_init_finished(void)
 void session_init(void)
 {
        static struct poptOption options[] = {
-#if 0 /* --session is not available in SILC Client */
                { "session", 0, POPT_ARG_STRING, &session_file, 0, "Used by /UPGRADE command", "PATH" },
-#endif
                { NULL, '\0', 0, NULL }
        };
 
-        session_file = NULL;
+       session_file = NULL;
        args_register(options);
 
-       /*command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);*/
+       command_bind("upgrade", NULL, (SIGNAL_FUNC) cmd_upgrade);
 
        signal_add("session save", (SIGNAL_FUNC) sig_session_save);
        signal_add("session restore", (SIGNAL_FUNC) sig_session_restore);
@@ -374,7 +405,7 @@ void session_deinit(void)
 {
        g_free_not_null(irssi_binary);
 
-        /*command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);*/
+        command_unbind("upgrade", (SIGNAL_FUNC) cmd_upgrade);
 
        signal_remove("session save", (SIGNAL_FUNC) sig_session_save);
        signal_remove("session restore", (SIGNAL_FUNC) sig_session_restore);