Merged silc_1_0_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / silc-channels.c
index c02d61d2fc4e9724538a831d38b83fd3221db2ad..2ebdcc7ab42de108834ae6b9405a080ad8b704ba 100644 (file)
 #include "fe-common/core/printtext.h"
 #include "fe-common/silc/module-formats.h"
 
+#include "silc-commands.h"
+
+void sig_mime(SILC_SERVER_REC *server, SILC_CHANNEL_REC *channel,
+               const char *blob, const char *enc, const char *type,
+               const char *nick)
+{
+
+  if (!(IS_SILC_SERVER(server)))
+    return;
+  
+  printformat_module("fe-common/silc", server, 
+                        channel == NULL ? NULL : channel->name,
+                        MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA,
+                        nick == NULL ? "[<unknown>]" : nick, type);
+
+}
+
 SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server,
-                                     const char *name, int automatic)
+                                     const char *name,
+                                     const char *visible_name,
+                                     int automatic)
 {
   SILC_CHANNEL_REC *rec;
 
@@ -53,10 +72,8 @@ SILC_CHANNEL_REC *silc_channel_create(SILC_SERVER_REC *server,
 
   rec = g_new0(SILC_CHANNEL_REC, 1);
   rec->chat_type = SILC_PROTOCOL;
-  rec->name = g_strdup(name);
-  rec->server = server;
-
-  channel_init((CHANNEL_REC *) rec, automatic);
+  channel_init((CHANNEL_REC *)rec, (SERVER_REC *)server, name, name,
+              automatic);
   return rec;
 }
 
@@ -64,33 +81,29 @@ 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);
   }
 }
 
 static void silc_channels_join(SILC_SERVER_REC *server,
                               const char *channels, int automatic)
 {
-  char **list, **tmp, *channel;
+  char **list, **tmp;
   SILC_CHANNEL_REC *chanrec;
 
   list = g_strsplit(channels, ",", -1);
   for (tmp = list; *tmp != NULL; tmp++) {
-    channel = **tmp == '#' ? g_strdup(*tmp) :
-      g_strconcat("#", *tmp, NULL);
-
-    chanrec = silc_channel_find(server, channel);
-    if (chanrec) {
-      g_free(channel);
+    chanrec = silc_channel_find(server, *tmp);
+    if (chanrec)
       continue;
-    }
 
-    silc_command_exec(server, "JOIN", channel);
-    g_free(channel);
+    silc_command_exec(server, "JOIN", *tmp);
   }
 
   g_strfreev(list);
@@ -111,9 +124,12 @@ static void sig_server_quit(SILC_SERVER_REC *server, const char *msg)
     silc_command_exec(server, "QUIT", msg);
 }
 
-/*
- * "event join". Joined to a channel.
- */
+static void sig_gui_quit(SILC_SERVER_REC *server, const char *msg)
+{
+  silc_client_stop(silc_client);
+}
+
+/* Find Irssi channel entry by SILC channel entry */
 
 SILC_CHANNEL_REC *silc_channel_find_entry(SILC_SERVER_REC *server,
                                          SilcChannelEntry entry)
@@ -132,406 +148,6 @@ SILC_CHANNEL_REC *silc_channel_find_entry(SILC_SERVER_REC *server,
   return NULL;
 }
 
-static void event_join(SILC_SERVER_REC *server, va_list va)
-{
-  SILC_CHANNEL_REC *chanrec;
-  SILC_NICK_REC *nickrec;
-  SilcClientEntry client;
-  SilcChannelEntry channel;
-  char userhost[256];
-
-  client = va_arg(va, SilcClientEntry);
-  channel = va_arg(va, SilcChannelEntry);
-
-  if (client == server->conn->local_entry) {
-    /* You joined to channel */
-    chanrec = silc_channel_find(server, channel->channel_name);
-    if (chanrec != NULL && !chanrec->joined)
-      chanrec->entry = channel;
-  } else {
-    chanrec = silc_channel_find_entry(server, channel);
-    if (chanrec != NULL) {
-      SilcChannelUser user;
-
-      silc_list_start(chanrec->entry->clients);
-      while ((user = silc_list_get(chanrec->entry->clients)) != NULL)
-       if (user->client == client) {
-         nickrec = silc_nicklist_insert(chanrec, user, TRUE);
-         break;
-       }
-    }
-  }
-
-  memset(userhost, 0, sizeof(userhost));
-  if (client->username)
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-            client->username, client->hostname);
-  signal_emit("message join", 4, server, channel->channel_name,
-             client->nickname,
-             client->username == NULL ? "" : userhost);
-}
-
-/*
- * "event leave". Left a channel.
- */
-
-static void event_leave(SILC_SERVER_REC *server, va_list va)
-{
-  SILC_CHANNEL_REC *chanrec;
-  SILC_NICK_REC *nickrec;
-  SilcClientEntry client;
-  SilcChannelEntry channel;
-  char userhost[256];
-
-  client = va_arg(va, SilcClientEntry);
-  channel = va_arg(va, SilcChannelEntry);
-
-  memset(userhost, 0, sizeof(userhost));
-  if (client->username)
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-            client->username, client->hostname);
-  signal_emit("message part", 5, server, channel->channel_name,
-             client->nickname,  client->username ?  userhost : "", 
-             client->nickname);
-
-  chanrec = silc_channel_find_entry(server, channel);
-  if (chanrec != NULL) {
-    nickrec = silc_nicklist_find(chanrec, client);
-    if (nickrec != NULL)
-      nicklist_remove(CHANNEL(chanrec), NICK(nickrec));
-  }
-}
-
-/*
- * "event signoff". Left the network.
- */
-
-static void event_signoff(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientEntry client;
-  GSList *nicks, *tmp;
-  char *message;
-  char userhost[256];
-
-  client = va_arg(va, SilcClientEntry);
-  message = va_arg(va, char *);
-
-  silc_server_free_ftp(server, client);
-
-  memset(userhost, 0, sizeof(userhost));
-  if (client->username)
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-            client->username, client->hostname);
-  signal_emit("message quit", 4, server, client->nickname,
-             client->username ? userhost : "", 
-             message ? message : "");
-
-  nicks = nicklist_get_same_unique(SERVER(server), client);
-  for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
-    CHANNEL_REC *channel = tmp->data;
-    NICK_REC *nickrec = tmp->next->data;
-    
-    nicklist_remove(channel, nickrec);
-  }
-}
-
-/*
- * "event topic". Changed topic.
- */
-
-static void event_topic(SILC_SERVER_REC *server, va_list va)
-{
-  SILC_CHANNEL_REC *chanrec;
-  SilcClientEntry client;
-  SilcChannelEntry channel;
-  char *topic;
-  char userhost[256];
-
-  client = va_arg(va, SilcClientEntry);
-  topic = va_arg(va, char *);
-  channel = va_arg(va, SilcChannelEntry);
-
-  silc_server_free_ftp(server, client);
-
-  chanrec = silc_channel_find_entry(server, channel);
-  if (chanrec != NULL) {
-    g_free_not_null(chanrec->topic);
-    chanrec->topic = *topic == '\0' ? NULL : g_strdup(topic);
-    signal_emit("channel topic changed", 1, chanrec);
-  }
-
-  memset(userhost, 0, sizeof(userhost));
-  snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-          client->username, client->hostname);
-  signal_emit("message topic", 5, server, channel->channel_name,
-             topic, client->nickname, userhost);
-}
-
-/*
- * "event invite". Invited or modified invite list.
- */
-
-static void event_invite(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientEntry client;
-  SilcChannelEntry channel;
-  char *channel_name;
-  char userhost[256];
-  
-  channel = va_arg(va, SilcChannelEntry);
-  channel_name = va_arg(va, char *);
-  client = va_arg(va, SilcClientEntry);
-
-  memset(userhost, 0, sizeof(userhost));
-  snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-          client->username, client->hostname);
-  signal_emit("message invite", 4, server, channel ? channel->channel_name :
-             channel_name, client->nickname, userhost);
-}
-
-/*
- * "event nick". Changed nickname.
- */
-
-static void event_nick(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientEntry oldclient, newclient;
-  char userhost[256];
-
-  oldclient = va_arg(va, SilcClientEntry);
-  newclient = va_arg(va, SilcClientEntry);
-
-  nicklist_rename_unique(SERVER(server),
-                        oldclient, oldclient->nickname,
-                        newclient, newclient->nickname);
-
-  memset(userhost, 0, sizeof(userhost));
-  snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-          newclient->username, newclient->hostname);
-  signal_emit("message nick", 4, server, newclient->nickname, 
-             oldclient->nickname, userhost);
-}
-
-/*
- * "event cmode". Changed channel mode.
- */
-
-static void event_cmode(SILC_SERVER_REC *server, va_list va)
-{
-  SILC_CHANNEL_REC *chanrec;
-  void *entry;
-  SilcClientEntry client;
-  SilcServerEntry server_entry;
-  SilcChannelEntry channel;
-  char *mode;
-  uint32 modei;
-  SilcIdType idtype;
-
-  idtype = va_arg(va, int);
-  entry = va_arg(va, void *);
-  modei = va_arg(va, uint32);
-  (void)va_arg(va, char *);
-  (void)va_arg(va, char *);
-  channel = va_arg(va, SilcChannelEntry);
-
-  mode = silc_client_chmode(modei, 
-                           channel->channel_key->cipher->name,
-                           silc_hmac_get_name(channel->hmac));
-  
-  chanrec = silc_channel_find_entry(server, channel);
-  if (chanrec != NULL) {
-    g_free_not_null(chanrec->mode);
-    chanrec->mode = g_strdup(mode == NULL ? "" : mode);
-    signal_emit("channel mode changed", 1, chanrec);
-  }
-  
-  if (idtype == SILC_ID_CLIENT) {
-    client = (SilcClientEntry)entry;
-    printformat_module("fe-common/silc", server, channel->channel_name,
-                      MSGLEVEL_MODES, SILCTXT_CHANNEL_CMODE,
-                      channel->channel_name, mode ? mode : "removed all",
-                      client->nickname);
-  } else {
-    server_entry = (SilcServerEntry)entry;
-    printformat_module("fe-common/silc", server, channel->channel_name,
-                      MSGLEVEL_MODES, SILCTXT_CHANNEL_CMODE,
-                      channel->channel_name, mode ? mode : "removed all",
-                      server_entry->server_name);
-  }
-  
-  g_free(mode);
-}
-
-/*
- * "event cumode". Changed user's mode on channel.
- */
-
-static void event_cumode(SILC_SERVER_REC *server, va_list va)
-{
-  SILC_CHANNEL_REC *chanrec;
-  SilcClientEntry client, destclient;
-  SilcChannelEntry channel;
-  int mode;
-  char *modestr;
-  
-  client = va_arg(va, SilcClientEntry);
-  mode = va_arg(va, uint32);
-  destclient = va_arg(va, SilcClientEntry);
-  channel = va_arg(va, SilcChannelEntry);
-  
-  modestr = silc_client_chumode(mode);
-  chanrec = silc_channel_find_entry(server, channel);
-  if (chanrec != NULL) {
-    SILC_NICK_REC *nick;
-    
-    if (destclient == server->conn->local_entry) {
-      chanrec->chanop =
-       (mode & SILC_CHANNEL_UMODE_CHANOP) != 0;
-    }
-
-    nick = silc_nicklist_find(chanrec, destclient);
-    if (nick != NULL) {
-      nick->op = (mode & SILC_CHANNEL_UMODE_CHANOP) != 0;
-      nick->founder = (mode & SILC_CHANNEL_UMODE_CHANFO) != 0;
-      signal_emit("nick mode changed", 2, chanrec, nick);
-    }
-  }
-  
-  printformat_module("fe-common/silc", server, channel->channel_name,
-                    MSGLEVEL_MODES, SILCTXT_CHANNEL_CUMODE,
-                    channel->channel_name, destclient->nickname, 
-                    modestr ? modestr : "removed all",
-                    client->nickname);
-
-  if (mode & SILC_CHANNEL_UMODE_CHANFO)
-    printformat_module("fe-common/silc", 
-                      server, channel->channel_name, MSGLEVEL_CRAP,
-                      SILCTXT_CHANNEL_FOUNDER,
-                      channel->channel_name, destclient->nickname);
-
-  g_free(modestr);
-}
-
-/*
- * "event motd". Received MOTD.
- */
-
-static void event_motd(SILC_SERVER_REC *server, va_list va)
-{
-  char *text = va_arg(va, char *);
-
-  if (!settings_get_bool("skip_motd"))
-    printtext_multiline(server, NULL, MSGLEVEL_CRAP, "%s", text);
-}
-
-/*
- * "event channel_change". Channel ID has changed.
- */
-
-static void event_channel_change(SILC_SERVER_REC *server, va_list va)
-{
-  /* Nothing interesting to do */
-}
-
-/*
- * "event server_signoff". Server has quit the network.
- */
-
-static void event_server_signoff(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientEntry *clients;
-  uint32 clients_count;
-  int i;
-  char userhost[256];
-  
-  (void)va_arg(va, void *);
-  clients = va_arg(va, SilcClientEntry *);
-  clients_count = va_arg(va, uint32);
-  
-  for (i = 0; i < clients_count; i++) {
-    memset(userhost, 0, sizeof(userhost));
-    if (clients[i]->username)
-      snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-              clients[i]->username, clients[i]->hostname);
-    signal_emit("message quit", 4, server, clients[i]->nickname,
-               clients[i]->username ? userhost : "", 
-               "server signoff");
-  }
-}
-
-/*
- * "event kick". Someone was kicked from channel.
- */
-
-static void event_kick(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientConnection conn = server->conn;
-  SilcClientEntry client_entry;
-  SilcChannelEntry channel_entry;
-  char *tmp;
-  SILC_CHANNEL_REC *chanrec;
-
-  client_entry = va_arg(va, SilcClientEntry);
-  tmp = va_arg(va, char *);
-  channel_entry = va_arg(va, SilcChannelEntry);
-
-  chanrec = silc_channel_find_entry(server, channel_entry);
-  
-  if (client_entry == conn->local_entry) {
-    printformat_module("fe-common/silc", server, channel_entry->channel_name,
-                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED_YOU, 
-                      channel_entry->channel_name, tmp ? tmp : "");
-    if (chanrec) {
-      chanrec->kicked = TRUE;
-      channel_destroy((CHANNEL_REC *)chanrec);
-    }
-  } else {
-    printformat_module("fe-common/silc", server, channel_entry->channel_name,
-                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED, 
-                      client_entry->nickname,
-                      channel_entry->channel_name, tmp ? tmp : "");
-
-    if (chanrec) {
-      SILC_NICK_REC *nickrec = silc_nicklist_find(chanrec, client_entry);
-      if (nickrec != NULL)
-       nicklist_remove(CHANNEL(chanrec), NICK(nickrec));
-    }
-  }
-}
-
-/*
- * "event kill". Someone was killed from the network.
- */
-
-static void event_kill(SILC_SERVER_REC *server, va_list va)
-{
-  SilcClientConnection conn = server->conn;
-  SilcClientEntry client_entry;
-  char *tmp;
-
-  client_entry = va_arg(va, SilcClientEntry);
-  tmp = va_arg(va, char *);
-  
-  if (client_entry == conn->local_entry) {
-    printformat_module("fe-common/silc", server, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU, 
-                      tmp ? tmp : "");
-  } else {
-    GSList *nicks, *tmpn;
-    nicks = nicklist_get_same_unique(SERVER(server), client_entry);
-    for (tmpn = nicks; tmpn != NULL; tmpn = tmpn->next->next) {
-      CHANNEL_REC *channel = tmpn->data;
-      NICK_REC *nickrec = tmpn->next->data;
-      nicklist_remove(channel, nickrec);
-    }
-
-    printformat_module("fe-common/silc", server, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED, 
-                      client_entry->nickname,
-                      tmp ? tmp : "");
-  }
-}
-
 /* PART (LEAVE) command. */
 
 static void command_part(const char *data, SILC_SERVER_REC *server,
@@ -540,13 +156,15 @@ 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);
 
   if (!strcmp(data, "*") || *data == '\0') {
     if (!IS_SILC_CHANNEL(item))
       cmd_return_error(CMDERR_NOT_JOINED);
-    data = item->name;
+    data = item->visible_name;
   }
 
   chanrec = silc_channel_find(server, data);
@@ -560,9 +178,10 @@ 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();
-  
+
   channel_destroy(CHANNEL(chanrec));
 }
 
@@ -573,11 +192,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);
 
@@ -593,15 +215,27 @@ static void command_me(const char *data, SILC_SERVER_REC *server,
   if (argc < 2)
     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
 
-  chanrec = silc_channel_find(server, item->name);
+  chanrec = silc_channel_find(server, item->visible_name);
   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, 
@@ -611,6 +245,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
@@ -621,11 +256,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);
 
@@ -645,11 +282,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, 
@@ -659,6 +308,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. */
@@ -668,11 +318,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);
 
@@ -688,15 +340,27 @@ static void command_notice(const char *data, SILC_SERVER_REC *server,
   if (argc < 2)
     cmd_return_error(CMDERR_NOT_ENOUGH_PARAMS);
 
-  chanrec = silc_channel_find(server, item->name);
+  chanrec = silc_channel_find(server, item->visible_name);
   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, 
@@ -706,20 +370,20 @@ 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
    flag. */
 
-static void command_away(const char *data, SILC_SERVER_REC *server,
-                        WI_ITEM_REC *item)
+bool silc_set_away(const char *reason, SILC_SERVER_REC *server)
 {
   bool set;
-
+  
   if (!IS_SILC_SERVER(server) || !server->connected)
-    cmd_return_error(CMDERR_NOT_CONNECTED);
-
-  if (*data == '\0') {
+    return FALSE;
+  
+  if (*reason == '\0') {
     /* Remove any possible away message */
     silc_client_set_away_message(silc_client, server->conn, NULL);
     set = FALSE;
@@ -728,16 +392,37 @@ static void command_away(const char *data, SILC_SERVER_REC *server,
                       SILCTXT_UNSET_AWAY);
   } else {
     /* Set the away message */
-    silc_client_set_away_message(silc_client, server->conn, (char *)data);
+    silc_client_set_away_message(silc_client, server->conn, (char *)reason);
     set = TRUE;
 
     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP, 
-                      SILCTXT_SET_AWAY, data);
+                      SILCTXT_SET_AWAY, reason);
   }
 
+  server->usermode_away = set;
+  g_free_and_null(server->away_reason);
+  if (set)
+    server->away_reason = g_strdup((char *)reason);
+
   signal_emit("away mode changed", 1, server);
 
-  silc_command_exec(server, "UMODE", set ? "+g" : "-g");
+  return set;
+}
+
+static void command_away(const char *data, SILC_SERVER_REC *server,
+                        WI_ITEM_REC *item)
+{
+  CMD_SILC_SERVER(server);
+
+  if (!IS_SILC_SERVER(server) || !server->connected)
+    cmd_return_error(CMDERR_NOT_CONNECTED);
+
+  g_free_and_null(server->away_reason);
+  if ((data) && (*data != '\0'))
+    server->away_reason = g_strdup(data);
+  
+  silc_command_exec(server, "UMODE", 
+                   (server->away_reason != NULL) ? "+g" : "-g");
 }
 
 typedef struct {
@@ -772,7 +457,7 @@ static void keyagr_completion(SilcClient client,
       /* Set the private key for this client */
       silc_client_del_private_message_key(client, conn, client_entry);
       silc_client_add_private_message_key_ske(client, conn, client_entry,
-                                             NULL, key, i->responder);
+                                             NULL, NULL, key, i->responder);
       printformat_module("fe-common/silc", i->server, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KEY_AGREEMENT_PRIVMSG, 
                         client_entry->nickname);
@@ -796,6 +481,22 @@ static void keyagr_completion(SilcClient client,
                       SILCTXT_KEY_AGREEMENT_TIMEOUT, client_entry->nickname);
     break;
     
+  case SILC_KEY_AGREEMENT_ABORTED:
+    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;
   } 
@@ -825,7 +526,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;
@@ -852,17 +553,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);
 
@@ -928,11 +632,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 */
@@ -940,32 +645,46 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     command = 1;
 
     if (argc >= 5) {
+      char *cipher = NULL, *hmac = NULL;
+
       if (type == 1 && client_entry) {
        /* Set private message key */
+       bool responder = FALSE;
        
        silc_client_del_private_message_key(silc_client, conn, client_entry);
 
-       if (argc >= 6)
-         silc_client_add_private_message_key(silc_client, conn, client_entry,
-                                             argv[5], argv[4],
-                                             argv_lens[4],
-                                             (argv[4][0] == '*' ?
-                                              TRUE : FALSE), FALSE);
-       else
-         silc_client_add_private_message_key(silc_client, conn, client_entry,
-                                             NULL, argv[4],
-                                             argv_lens[4],
-                                             (argv[4][0] == '*' ?
-                                              TRUE : FALSE), FALSE);
+       if (argc >= 6) {
+         if (!strcasecmp(argv[5], "-responder"))
+           responder = TRUE;
+         else
+           cipher = argv[5];
+       }
+       if (argc >= 7) {
+         if (!strcasecmp(argv[6], "-responder"))
+           responder = TRUE;
+         else
+           hmac = argv[6];
+       }
+       if (argc >= 8) {
+         if (!strcasecmp(argv[7], "-responder"))
+           responder = TRUE;
+       }
+
+       silc_client_add_private_message_key(silc_client, conn, client_entry,
+                                           cipher, hmac,
+                                           argv[4], argv_lens[4],
+                                           (argv[4][0] == '*' ?
+                                            TRUE : FALSE), responder);
 
        /* Send the key to the remote client so that it starts using it
           too. */
+       /* XXX for now we don't do this.  This feature is pretty stupid
+          and should perhaps be removed altogether from SILC.
        silc_client_send_private_message_key(silc_client, conn, 
                                             client_entry, TRUE);
+       */
       } else if (type == 2) {
        /* Set private channel key */
-       char *cipher = NULL, *hmac = NULL;
-
        if (!(channel_entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
          printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                             SILCTXT_CH_PRIVATE_KEY_NOMODE, 
@@ -979,7 +698,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])) {
@@ -1008,7 +727,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)
@@ -1043,7 +762,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];
 
@@ -1117,7 +836,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];
 
@@ -1136,8 +855,9 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
        memset(buf, 0, sizeof(buf));
        strncat(buf, "  ", 2);
 
-       len = strlen(keys[k]->cipher->cipher->name);
-       strncat(buf, keys[k]->cipher->cipher->name, len > 16 ? 16 : len);
+       len = strlen(silc_cipher_get_name(keys[k]->cipher));
+       strncat(buf, silc_cipher_get_name(keys[k]->cipher),
+               len > 16 ? 16 : len);
        if (len < 16)
          for (i = 0; i < 16 - len; i++)
            strcat(buf, " ");
@@ -1179,22 +899,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 */
     }
@@ -1215,6 +931,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> "
@@ -1226,9 +984,11 @@ static void command_key(const char *data, SILC_SERVER_REC *server,
     printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                       SILCTXT_KEY_AGREEMENT, argv[2]);
     internal->responder = TRUE;
-    silc_client_send_key_agreement(silc_client, conn, client_entry, hostname, 
-                                  bindhost, port, 120, keyagr_completion, 
-                                  internal);
+    silc_client_send_key_agreement(
+                          silc_client, conn, client_entry, hostname, 
+                          bindhost, port, 
+                          settings_get_int("key_exchange_timeout_secs"), 
+                          keyagr_completion, internal);
     if (!hostname)
       silc_free(internal);
     goto out;
@@ -1261,28 +1021,16 @@ void silc_channels_init(void)
   signal_add("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
   signal_add("server connected", (SIGNAL_FUNC) sig_connected);
   signal_add("server quit", (SIGNAL_FUNC) sig_server_quit);
+  signal_add("gui exit", (SIGNAL_FUNC) sig_gui_quit);
+  signal_add("mime", (SIGNAL_FUNC) sig_mime);
 
-  signal_add("silc event join", (SIGNAL_FUNC) event_join);
-  signal_add("silc event leave", (SIGNAL_FUNC) event_leave);
-  signal_add("silc event signoff", (SIGNAL_FUNC) event_signoff);
-  signal_add("silc event topic", (SIGNAL_FUNC) event_topic);
-  signal_add("silc event invite", (SIGNAL_FUNC) event_invite);
-  signal_add("silc event nick", (SIGNAL_FUNC) event_nick);
-  signal_add("silc event cmode", (SIGNAL_FUNC) event_cmode);
-  signal_add("silc event cumode", (SIGNAL_FUNC) event_cumode);
-  signal_add("silc event motd", (SIGNAL_FUNC) event_motd);
-  signal_add("silc event channel_change", (SIGNAL_FUNC) event_channel_change);
-  signal_add("silc event server_signoff", (SIGNAL_FUNC) event_server_signoff);
-  signal_add("silc event kick", (SIGNAL_FUNC) event_kick);
-  signal_add("silc event kill", (SIGNAL_FUNC) event_kill);
-  
-  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();
 }
@@ -1292,30 +1040,16 @@ void silc_channels_deinit(void)
   signal_remove("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
   signal_remove("server quit", (SIGNAL_FUNC) sig_server_quit);
+  signal_remove("gui exit", (SIGNAL_FUNC) sig_gui_quit);
+  signal_remove("mime", (SIGNAL_FUNC) sig_mime);
 
-  signal_remove("silc event join", (SIGNAL_FUNC) event_join);
-  signal_remove("silc event leave", (SIGNAL_FUNC) event_leave);
-  signal_remove("silc event signoff", (SIGNAL_FUNC) event_signoff);
-  signal_remove("silc event topic", (SIGNAL_FUNC) event_topic);
-  signal_remove("silc event invite", (SIGNAL_FUNC) event_invite);
-  signal_remove("silc event nick", (SIGNAL_FUNC) event_nick);
-  signal_remove("silc event cmode", (SIGNAL_FUNC) event_cmode);
-  signal_remove("silc event cumode", (SIGNAL_FUNC) event_cumode);
-  signal_remove("silc event motd", (SIGNAL_FUNC) event_motd);
-  signal_remove("silc event channel_change", 
-               (SIGNAL_FUNC) event_channel_change);
-  signal_remove("silc event server_signoff", 
-               (SIGNAL_FUNC) event_server_signoff);
-  signal_remove("silc event kick", (SIGNAL_FUNC) event_kick);
-  signal_remove("silc event kill", (SIGNAL_FUNC) event_kill);
-  
   command_unbind("part", (SIGNAL_FUNC) command_part);
   command_unbind("me", (SIGNAL_FUNC) command_me);
   command_unbind("action", (SIGNAL_FUNC) command_action);
   command_unbind("notice", (SIGNAL_FUNC) command_notice);
   command_unbind("away", (SIGNAL_FUNC) command_away);
   command_unbind("key", (SIGNAL_FUNC) command_key);
-  command_unbind("listkeys", (SIGNAL_FUNC) command_listkeys);
+/*  command_unbind("listkeys", (SIGNAL_FUNC) command_listkeys); */
 
   silc_nicklist_deinit();
 }