Improved UTF-8 encoding and decoding, improved toolkit doc,
[silc.git] / apps / irssi / src / silc / core / silc-servers.c
index f24ba50828474763a75479740c7b2ab8de05fe34..5b5eb384ac6af9cb8a8c5b7dce88b0fb8a31030c 100644 (file)
@@ -41,6 +41,8 @@
 #include "window-item-def.h"
 
 #include "fe-common/core/printtext.h"
+#include "fe-common/core/fe-channels.h"
+#include "fe-common/core/keyboard.h"
 #include "fe-common/silc/module-formats.h"
 
 #include "silc-commands.h"
@@ -54,11 +56,14 @@ static void silc_send_channel(SILC_SERVER_REC *server,
   SILC_CHANNEL_REC *rec;
   
   rec = silc_channel_find(server, channel);
-  if (rec == NULL || rec->entry == NULL)
+  if (rec == NULL || rec->entry == NULL) {
+    cmd_return_error(CMDERR_NOT_JOINED);
     return;
-  
+  }
+
   silc_client_send_channel_message(silc_client, server->conn, rec->entry, 
-                                  NULL, 0, msg, strlen(msg), TRUE);
+                                  NULL, SILC_MESSAGE_FLAG_UTF8,
+                                  msg, strlen(msg), TRUE);
 }
 
 typedef struct {
@@ -115,7 +120,8 @@ static void silc_send_msg_clients(SilcClient client,
     }
 
     /* Send the private message */
-    silc_client_send_private_message(client, conn, target, 0,
+    silc_client_send_private_message(client, conn, target, 
+                                    SILC_MESSAGE_FLAG_UTF8,
                                     rec->msg, strlen(rec->msg),
                                     TRUE);
   }
@@ -158,7 +164,8 @@ static void silc_send_msg(SILC_SERVER_REC *server, char *nick, char *msg)
   /* Send the private message directly */
   silc_free(nickname);
   silc_client_send_private_message(silc_client, server->conn, 
-                                  clients[0], 0, msg, strlen(msg), TRUE);
+                                  clients[0], SILC_MESSAGE_FLAG_UTF8,
+                                  msg, strlen(msg), TRUE);
 }
 
 static int isnickflag_func(char flag)
@@ -179,31 +186,62 @@ const char *get_nick_flags(void)
 static void send_message(SILC_SERVER_REC *server, char *target,
                         char *msg, int target_type)
 {
+  char *message = NULL;
+  int len;
+
   g_return_if_fail(server != NULL);
   g_return_if_fail(target != NULL);
   g_return_if_fail(msg != NULL);
 
+  if (!silc_term_utf8()) {
+    len = silc_utf8_encoded_len(msg, strlen(msg), SILC_STRING_LANGUAGE);
+    message = silc_calloc(len + 1, sizeof(*message));
+    g_return_if_fail(message != NULL);
+    silc_utf8_encode(msg, strlen(msg), SILC_STRING_LANGUAGE, message, len);
+  }
+
   if (target_type == SEND_TARGET_CHANNEL)
-    silc_send_channel(server, target, msg);
+    silc_send_channel(server, target, message ? message : msg);
   else
-    silc_send_msg(server, target, msg);
+    silc_send_msg(server, target, message ? message : msg);
+
+  silc_free(message);
 }
 
 static void sig_connected(SILC_SERVER_REC *server)
 {
   SilcClientConnection conn;
+  SilcClientConnectionParams params;
+  char file[256];
   int fd;
 
   if (!IS_SILC_SERVER(server))
     return;
 
-  conn = silc_client_add_connection(silc_client,
+  /* Try to read detached session data and use it if found. */
+  memset(&params, 0, sizeof(params));
+  memset(file, 0, sizeof(file));
+  snprintf(file, sizeof(file) - 1, "%s/session", get_irssi_dir());
+  params.detach_data = silc_file_readfile(file, &params.detach_data_len);
+
+  /* Add connection to the client library */
+  conn = silc_client_add_connection(silc_client, &params,
                                    server->connrec->address,
                                    server->connrec->port,
                                    server);
   server->conn = conn;
-       
+
+  if (params.detach_data)
+    keyboard_entry_redirect(NULL,
+                           "-- Resuming old session, may take a while ...",
+                           ENTRY_REDIRECT_FLAG_HIDDEN, server);
+
+  silc_free(params.detach_data);
+  unlink(file);
+
   fd = g_io_channel_unix_get_fd(net_sendbuffer_handle(server->handle));
+
+  /* Start key exchange with the server */
   silc_client_start_key_exchange(silc_client, conn, fd);
 
   server->ftp_sessions = silc_dlist_init();
@@ -222,7 +260,7 @@ static void sig_disconnected(SILC_SERVER_REC *server)
 
   if (server->conn && server->conn->sock != NULL) {
     silc_client_close_connection(silc_client, server->conn);
-    
+
     /* SILC closes the handle */
     g_io_channel_unref(net_sendbuffer_handle(server->handle));
     net_sendbuffer_destroy(server->handle, FALSE);
@@ -230,8 +268,7 @@ static void sig_disconnected(SILC_SERVER_REC *server)
   }
 }
 
-SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn)
-{
+SILC_SERVER_REC *silc_server_connect(SILC_SERVER_CONNECT_REC *conn){
   SILC_SERVER_REC *server;
 
   g_return_val_if_fail(IS_SILC_SERVER_CONNECT(conn), NULL);
@@ -292,7 +329,7 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 
 /* SYNTAX: BAN <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
 /* SYNTAX: CMODE <channel> +|-<modes> [{ <arguments>}] */
-/* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] [-pubkey|<passwd>] */
+/* SYNTAX: CUMODE <channel> +|-<modes> <nickname>[@<hostname>] */
 /* SYNTAX: GETKEY <nickname or server name> */
 /* SYNTAX: INVITE <channel> [<nickname>[@hostname>] */
 /* SYNTAX: INVITE <channel> [+|-[<nickname>[@<server>[!<username>[@hostname>]]]]] */
@@ -324,7 +361,9 @@ char *silc_server_get_channels(SILC_SERVER_REC *server)
 /* SYNTAX: FILE RECEIVE [<nickname>] */
 /* SYNTAX: FILE CLOSE [<nickname>] */
 /* SYNTAX: FILE */
-/* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder <-pubkey|passwd>] */
+/* SYNTAX: JOIN <channel> [<passphrase>] [-cipher <cipher>] [-hmac <hmac>] [-founder] */
+/* SYNTAX: DETACH */
+/* SYNTAX: WATCH [<-add | -del> <nickname>] */
 
 void silc_command_exec(SILC_SERVER_REC *server,
                       const char *command, const char *args)
@@ -698,7 +737,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
       }
 
       ret = silc_client_file_receive(silc_client, conn, 
-                                    silc_client_file_monitor, server,
+                                    silc_client_file_monitor, server, NULL,
                                     server->current_session->session_id);
       if (ret != SILC_CLIENT_FILE_OK) {
        if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
@@ -719,7 +758,7 @@ static void command_file(const char *data, SILC_SERVER_REC *server,
       if (ftp->client_entry == client_entry && !ftp->filepath) {
        ret = silc_client_file_receive(silc_client, conn, 
                                       silc_client_file_monitor, server,
-                                      ftp->session_id);
+                                      NULL, ftp->session_id);
        if (ret != SILC_CLIENT_FILE_OK) {
          if (ret == SILC_CLIENT_FILE_ALREADY_STARTED)
            printformat_module("fe-common/silc", server, NULL,
@@ -874,6 +913,8 @@ void silc_server_init(void)
   command_bind_silc("getkey", MODULE_NAME, (SIGNAL_FUNC) command_self);
   command_bind_silc("sconnect", MODULE_NAME, (SIGNAL_FUNC) command_sconnect);
   command_bind_silc("file", MODULE_NAME, (SIGNAL_FUNC) command_file);
+  command_bind_silc("detach", MODULE_NAME, (SIGNAL_FUNC) command_self);
+  command_bind_silc("watch", MODULE_NAME, (SIGNAL_FUNC) command_self);
 
   command_set_options("connect", "+silcnet");
 }
@@ -907,6 +948,8 @@ void silc_server_deinit(void)
   command_unbind("getkey", (SIGNAL_FUNC) command_self);
   command_unbind("sconnect", (SIGNAL_FUNC) command_sconnect);
   command_unbind("file", (SIGNAL_FUNC) command_file);
+  command_unbind("detach", (SIGNAL_FUNC) command_self);
+  command_unbind("watch", (SIGNAL_FUNC) command_self);
 }
 
 void silc_server_free_ftp(SILC_SERVER_REC *server,
@@ -923,3 +966,13 @@ void silc_server_free_ftp(SILC_SERVER_REC *server,
     }
   }
 }
+
+bool silc_term_utf8(void)
+{
+  const char *str;
+  str = settings_get_str("term_type");
+  if (str)
+    if (g_strcasecmp(str, "utf-8") == 0)
+      return TRUE;
+  return FALSE;
+}