Improved UTF-8 encoding and decoding, improved toolkit doc,
[silc.git] / apps / irssi / src / silc / core / silc-channels.c
index 870c1f4343e1e862e36af5b9f2df353fa97dcf24..afff90e60b28a94dbbe4f66297d476385b42e5cb 100644 (file)
@@ -43,6 +43,8 @@
 #include "fe-common/core/printtext.h"
 #include "fe-common/silc/module-formats.h"
 
+#include "silc-commands.h"
+
 SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server,
                                      const char *name, int automatic)
 {
@@ -64,11 +66,13 @@ static void sig_channel_destroyed(SILC_CHANNEL_REC *channel)
 {
   if (!IS_SILC_CHANNEL(channel))
     return;
+  if (channel->server && channel->server->disconnected)
+    return;
 
   if (channel->server != NULL && !channel->left && !channel->kicked) {
     /* destroying channel record without actually
        having left the channel yet */
-    silc_command_exec(channel->server, "PART", channel->name);
+    silc_command_exec(channel->server, "LEAVE", channel->name);
   }
 }
 
@@ -132,6 +136,8 @@ static void command_part(const char *data, SILC_SERVER_REC *server,
   SILC_CHANNEL_REC *chanrec;
   char userhost[256];
   
+  CMD_SILC_SERVER(server);
+
   if (!IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -152,6 +158,7 @@ static void command_part(const char *data, SILC_SERVER_REC *server,
   signal_emit("message part", 5, server, chanrec->name,
              server->nick, userhost, "");
   
+  chanrec->left = TRUE;
   silc_command_exec(server, "LEAVE", chanrec->name);
   signal_stop();
   
@@ -165,11 +172,14 @@ static void command_me(const char *data, SILC_SERVER_REC *server,
 {
   SILC_CHANNEL_REC *chanrec;
   char *tmpcmd = "ME", *tmp;
-  uint32 argc = 0;
+  SilcUInt32 argc = 0;
+  unsigned char *message = NULL;
   unsigned char **argv;
-  uint32 *argv_lens, *argv_types;
+  SilcUInt32 *argv_lens, *argv_types;
   int i;
  
+  CMD_SILC_SERVER(server);
+
   if (!IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -189,11 +199,23 @@ static void command_me(const char *data, SILC_SERVER_REC *server,
   if (chanrec == NULL) 
     cmd_return_error(CMDERR_CHAN_NOT_FOUND);
 
+  if (!silc_term_utf8()) {
+    int len = silc_utf8_encoded_len(argv[1], argv_lens[1],
+                                   SILC_STRING_LANGUAGE);
+    message = silc_calloc(len + 1, sizeof(*message));
+    g_return_if_fail(message != NULL);
+    silc_utf8_encode(argv[1], argv_lens[1], SILC_STRING_LANGUAGE,
+                    message, len);
+  }
+
   /* Send the action message */
   silc_client_send_channel_message(silc_client, server->conn, 
                                   chanrec->entry, NULL,
-                                  SILC_MESSAGE_FLAG_ACTION, 
-                                  argv[1], argv_lens[1], TRUE);
+                                  SILC_MESSAGE_FLAG_ACTION |
+                                  SILC_MESSAGE_FLAG_UTF8,
+                                  message ? message : argv[1],
+                                  message ? strlen(message) : argv_lens[1],
+                                  TRUE);
 
   printformat_module("fe-common/silc", server, chanrec->entry->channel_name,
                     MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_OWNACTION, 
@@ -203,6 +225,7 @@ static void command_me(const char *data, SILC_SERVER_REC *server,
     silc_free(argv[i]);
   silc_free(argv_lens);
   silc_free(argv_types);
+  silc_free(message);
 }
 
 /* ACTION local command. Same as ME but takes the channel as mandatory
@@ -213,11 +236,13 @@ static void command_action(const char *data, SILC_SERVER_REC *server,
 {
   SILC_CHANNEL_REC *chanrec;
   char *tmpcmd = "ME", *tmp;
-  uint32 argc = 0;
+  SilcUInt32 argc = 0;
+  unsigned char *message = NULL;
   unsigned char **argv;
-  uint32 *argv_lens, *argv_types;
+  SilcUInt32 *argv_lens, *argv_types;
   int i;
  
+  CMD_SILC_SERVER(server);
   if (!IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -237,11 +262,23 @@ static void command_action(const char *data, SILC_SERVER_REC *server,
   if (chanrec == NULL) 
     cmd_return_error(CMDERR_CHAN_NOT_FOUND);
 
+  if (!silc_term_utf8()) {
+    int len = silc_utf8_encoded_len(argv[2], argv_lens[2],
+                                   SILC_STRING_LANGUAGE);
+    message = silc_calloc(len + 1, sizeof(*message));
+    g_return_if_fail(message != NULL);
+    silc_utf8_encode(argv[2], argv_lens[2], SILC_STRING_LANGUAGE,
+                    message, len);
+  }
+
   /* Send the action message */
   silc_client_send_channel_message(silc_client, server->conn, 
                                   chanrec->entry, NULL,
-                                  SILC_MESSAGE_FLAG_ACTION, 
-                                  argv[2], argv_lens[2], TRUE);
+                                  SILC_MESSAGE_FLAG_ACTION |
+                                  SILC_MESSAGE_FLAG_UTF8,
+                                  message ? message : argv[2],
+                                  message ? strlen(message) : argv_lens[2],
+                                  TRUE);
 
   printformat_module("fe-common/silc", server, chanrec->entry->channel_name,
                     MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_OWNACTION, 
@@ -251,6 +288,7 @@ static void command_action(const char *data, SILC_SERVER_REC *server,
     silc_free(argv[i]);
   silc_free(argv_lens);
   silc_free(argv_types);
+  silc_free(message);
 }
 
 /* NOTICE local command. */
@@ -260,11 +298,13 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
 {
   SILC_CHANNEL_REC *chanrec;
   char *tmpcmd = "ME", *tmp;
-  uint32 argc = 0;
+  SilcUInt32 argc = 0;
+  unsigned char *message = NULL;
   unsigned char **argv;
-  uint32 *argv_lens, *argv_types;
+  SilcUInt32 *argv_lens, *argv_types;
   int i;
  
+  CMD_SILC_SERVER(server);
   if (!IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -284,11 +324,23 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
   if (chanrec == NULL) 
     cmd_return_error(CMDERR_CHAN_NOT_FOUND);
 
+  if (!silc_term_utf8()) {
+    int len = silc_utf8_encoded_len(argv[1], argv_lens[1],
+                                   SILC_STRING_LANGUAGE);
+    message = silc_calloc(len + 1, sizeof(*message));
+    g_return_if_fail(message != NULL);
+    silc_utf8_encode(argv[1], argv_lens[1], SILC_STRING_LANGUAGE,
+                    message, len);
+  }
+
   /* Send the action message */
   silc_client_send_channel_message(silc_client, server->conn, 
                                   chanrec->entry, NULL,
-                                  SILC_MESSAGE_FLAG_NOTICE, 
-                                  argv[1], argv_lens[1], TRUE);
+                                  SILC_MESSAGE_FLAG_NOTICE |
+                                  SILC_MESSAGE_FLAG_UTF8,
+                                  message ? message : argv[1],
+                                  message ? strlen(message) : argv_lens[1],
+                                  TRUE);
 
   printformat_module("fe-common/silc", server, chanrec->entry->channel_name,
                     MSGLEVEL_NOTICES, SILCTXT_CHANNEL_OWNNOTICE, 
@@ -298,6 +350,7 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
     silc_free(argv[i]);
   silc_free(argv_lens);
   silc_free(argv_types);
+  silc_free(message);
 }
 
 /* AWAY local command.  Sends UMODE command that sets the SILC_UMODE_GONE
@@ -308,6 +361,8 @@ static void command_away(const char *data, SILC_SERVER_REC *server,
 {
   bool set;
 
+  CMD_SILC_SERVER(server);
+
   if (!IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -327,6 +382,9 @@ static void command_away(const char *data, SILC_SERVER_REC *server,
                       SILCTXT_SET_AWAY, data);
   }
 
+  server->usermode_away = set;
+  if (set)
+    server->away_reason = g_strdup((char *)data);
   signal_emit("away mode changed", 1, server);
 
   silc_command_exec(server, "UMODE", set ? "+g" : "-g");
@@ -392,6 +450,17 @@ static void keyagr_completion(SilcClient client,
     printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
                       SILCTXT_KEY_AGREEMENT_ABORTED, client_entry->nickname);
     break;
+
+  case SILC_KEY_AGREEMENT_ALREADY_STARTED:
+    printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
+                      SILCTXT_KEY_AGREEMENT_ALREADY_STARTED,
+                      client_entry->nickname);
+    break;
+    
+  case SILC_KEY_AGREEMENT_SELF_DENIED:
+    printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
+                      SILCTXT_KEY_AGREEMENT_SELF_DENIED);
+    break;
     
   default:
     break;
@@ -422,7 +491,7 @@ typedef struct {
 static void silc_client_command_key_get_clients(SilcClient client,
                                                SilcClientConnection conn,
                                                SilcClientEntry *clients,
-                                               uint32 clients_count,
+                                               SilcUInt32 clients_count,
                                                void *context)
 {
   KeyGetClients internal = (KeyGetClients)context;
@@ -449,17 +518,20 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 {
   SilcClientConnection conn;
   SilcClientEntry *entrys, client_entry = NULL;
-  uint32 entry_count;
+  SilcUInt32 entry_count;
+  SILC_CHANNEL_REC *chanrec = NULL;
   SilcChannelEntry channel_entry = NULL;
   char *nickname = NULL, *tmp;
   int command = 0, port = 0, type = 0;
   char *hostname = NULL;
   KeyInternal internal = NULL;
-  uint32 argc = 0;
+  SilcUInt32 argc = 0;
   unsigned char **argv;
-  uint32 *argv_lens, *argv_types;
+  SilcUInt32 *argv_lens, *argv_types;
   char *bindhost = NULL;
  
+  CMD_SILC_SERVER(server);
+
   if (!server || !IS_SILC_SERVER(server) || !server->connected)
     cmd_return_error(CMDERR_NOT_CONNECTED);
 
@@ -525,11 +597,12 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
       name = argv[2];
     }
 
-    channel_entry = silc_client_get_channel(silc_client, conn, name);
-    if (!channel_entry) {
+    chanrec = silc_channel_find(server, name);
+    if (chanrec == NULL) {
       silc_free(nickname);
-      cmd_return_error(CMDERR_NOT_JOINED);
+      cmd_return_error(CMDERR_CHAN_NOT_FOUND);
     }
+    channel_entry = chanrec->entry;
   }
 
   /* Set command */
@@ -576,7 +649,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
          hmac = argv[6];
 
        if (!silc_client_add_channel_private_key(silc_client, conn, 
-                                                channel_entry,
+                                                channel_entry, NULL,
                                                 cipher, hmac,
                                                 argv[4],
                                                 argv_lens[4])) {
@@ -605,7 +678,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     } else if (type == 2) {
       /* Unset channel key(s) */
       SilcChannelPrivateKey *keys;
-      uint32 keys_count;
+      SilcUInt32 keys_count;
       int number;
 
       if (argc == 4)
@@ -640,7 +713,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     if (type == 1) {
       SilcPrivateMessageKeys keys;
-      uint32 keys_count;
+      SilcUInt32 keys_count;
       int k, i, len;
       char buf[1024];
 
@@ -714,7 +787,7 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
 
     } else if (type == 2) {
       SilcChannelPrivateKey *keys;
-      uint32 keys_count;
+      SilcUInt32 keys_count;
       int k, i, len;
       char buf[1024];
 
@@ -776,22 +849,18 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
        
         hostname = (char *)settings_get_str("auto_public_ip");
 
-/* If the hostname isn't set, treat this case as if auto_public_ip wasn't
- * set.
- */
+       /* If the hostname isn't set, treat this case as if auto_public_ip 
+          wasn't set. */
         if ((hostname) && (*hostname == '\0')) {
            hostname = NULL;
-        }
-        else {
+        } else {
           bindhost = (char *)settings_get_str("auto_bind_ip");
             
-/* if the bind_ip isn't set, but the public_ip IS, then assume then
- * public_ip is the same value as the bind_ip.
- */
-          if ((bindhost) && (*bindhost == '\0')) {
+         /* if the bind_ip isn't set, but the public_ip IS, then assume then
+            public_ip is the same value as the bind_ip. */
+          if ((bindhost) && (*bindhost == '\0'))
             bindhost = hostname;
-          }
-           port = settings_get_int("auto_bind_port");
+         port = settings_get_int("auto_bind_port");
         }
       }  /* if use_auto_addr */
     }
@@ -812,6 +881,48 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     internal->server = server;
   }
 
+  /* Change current channel private key */
+  if (!strcasecmp(argv[3], "change")) {
+    command = 6;
+    if (type == 2) {
+      /* Unset channel key(s) */
+      SilcChannelPrivateKey *keys;
+      SilcUInt32 keys_count;
+      int number;
+
+      keys = silc_client_list_channel_private_keys(silc_client, conn, 
+                                                  channel_entry,
+                                                  &keys_count);
+      if (!keys)
+       goto out;
+
+      if (argc == 4) {
+       chanrec->cur_key++;
+       if (chanrec->cur_key >= keys_count)
+         chanrec->cur_key = 0;
+      }
+
+      if (argc > 4) {
+       number = atoi(argv[4]);
+       if (!number || number > keys_count)
+         chanrec->cur_key = 0;
+       else
+         chanrec->cur_key = number - 1;
+      }
+
+      /* Set the current channel private key */
+      silc_client_current_channel_private_key(silc_client, conn, 
+                                             channel_entry, 
+                                             keys[chanrec->cur_key]);
+      printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
+                        SILCTXT_CH_PRIVATE_KEY_CHANGE, chanrec->cur_key + 1,
+                        channel_entry->channel_name);
+
+      silc_client_free_channel_private_keys(keys, keys_count);
+      goto out;
+    }
+  }
+
   if (command == 0) {
     silc_say(silc_client, conn, SILC_CLIENT_MESSAGE_INFO,
             "Usage: /KEY msg|channel <nickname|channel> "
@@ -861,13 +972,13 @@ void silc_channels_init(void)
   signal_add("server connected", (SIGNAL_FUNC) sig_connected);
   signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
 
-  command_bind("part", MODULE_NAME, (SIGNAL_FUNC) command_part);
-  command_bind("me", MODULE_NAME, (SIGNAL_FUNC) command_me);
-  command_bind("action", MODULE_NAME, (SIGNAL_FUNC) command_action);
-  command_bind("notice", MODULE_NAME, (SIGNAL_FUNC) command_notice);
-  command_bind("away", MODULE_NAME, (SIGNAL_FUNC) command_away);
-  command_bind("key", MODULE_NAME, (SIGNAL_FUNC) command_key);
-  command_bind("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys);
+  command_bind_silc("part", MODULE_NAME, (SIGNAL_FUNC) command_part);
+  command_bind_silc("me", MODULE_NAME, (SIGNAL_FUNC) command_me);
+  command_bind_silc("action", MODULE_NAME, (SIGNAL_FUNC) command_action);
+  command_bind_silc("notice", MODULE_NAME, (SIGNAL_FUNC) command_notice);
+  command_bind_silc("away", MODULE_NAME, (SIGNAL_FUNC) command_away);
+  command_bind_silc("key", MODULE_NAME, (SIGNAL_FUNC) command_key);
+  command_bind_silc("listkeys", MODULE_NAME, (SIGNAL_FUNC) command_listkeys);
 
   silc_nicklist_init();
 }