Merged from silc_1_0_branch.
[silc.git] / apps / irssi / src / silc / core / client_ops.c
index d2807c47ce8ea7b5b9320999b24a282f60641a9e..b55b0d31a7c859a9aa96d21fb73d0cdce191311c 100644 (file)
 #include "signals.h"
 #include "levels.h"
 #include "settings.h"
+#include "ignore.h"
 #include "fe-common/core/printtext.h"
 #include "fe-common/core/fe-channels.h"
 #include "fe-common/core/keyboard.h"
+#include "fe-common/core/window-items.h"
 #include "fe-common/silc/module-formats.h"
 
 #include "core.h"
@@ -47,6 +49,81 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
                                SilcSKEPKType pk_type,
                                SilcVerifyPublicKey completion, void *context);
 
+static void silc_get_umode_string(SilcUInt32 mode, char *buf, 
+                                 SilcUInt32 buf_size)
+{
+  if ((mode & SILC_UMODE_SERVER_OPERATOR) ||
+      (mode & SILC_UMODE_ROUTER_OPERATOR)) {
+    strcat(buf, (mode & SILC_UMODE_SERVER_OPERATOR) ?
+          "[server operator]" :
+          (mode & SILC_UMODE_ROUTER_OPERATOR) ?
+          "[SILC operator]" : "[unknown mode]");
+  }
+  if (mode & SILC_UMODE_GONE)
+    strcat(buf, " [away]");
+  if (mode & SILC_UMODE_INDISPOSED)
+    strcat(buf, " [indisposed]");
+  if (mode & SILC_UMODE_BUSY)
+    strcat(buf, " [busy]");
+  if (mode & SILC_UMODE_PAGE)
+    strcat(buf, " [page to reach]");
+  if (mode & SILC_UMODE_HYPER)
+    strcat(buf, " [hyper active]");
+  if (mode & SILC_UMODE_ROBOT)
+    strcat(buf, " [robot]");
+  if (mode & SILC_UMODE_ANONYMOUS)
+    strcat(buf, " [anonymous]");
+  if (mode & SILC_UMODE_BLOCK_PRIVMSG)
+    strcat(buf, " [blocks private messages]");
+  if (mode & SILC_UMODE_DETACHED)
+    strcat(buf, " [detached]");
+  if (mode & SILC_UMODE_REJECT_WATCHING)
+    strcat(buf, " [rejects watching]");
+  if (mode & SILC_UMODE_BLOCK_INVITE)
+    strcat(buf, " [blocks invites]");
+}
+
+/* print "nick appears as" message to every channel of a server */
+static void 
+silc_print_nick_change_channel(SILC_SERVER_REC *server, const char *channel,
+                             const char *newnick, const char *oldnick,
+                             const char *address)
+{
+  if (ignore_check(SERVER(server), oldnick, address,
+                  channel, newnick, MSGLEVEL_NICKS))
+    return;
+  
+  printformat_module("fe-common/silc", server, channel, MSGLEVEL_NICKS,
+                    SILCTXT_CHANNEL_APPEARS,
+                    oldnick, newnick, channel, address);
+}
+
+static void
+silc_print_nick_change(SILC_SERVER_REC *server, const char *newnick,
+                      const char *oldnick, const char *address)
+{
+  GSList *tmp, *windows;
+
+  /* Print to each channel/query where the nick is.
+     Don't print more than once to the same window. */
+  windows = NULL;
+    
+  for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
+    CHANNEL_REC *channel = tmp->data;
+    WINDOW_REC *window = window_item_window((WI_ITEM_REC *) channel);
+
+    if (nicklist_find(channel, newnick) == NULL ||
+       g_slist_find(windows, window) != NULL)
+      continue;
+
+    windows = g_slist_append(windows, window);
+    silc_print_nick_change_channel(server, channel->visible_name,
+                                  newnick, oldnick, address);
+  }
+
+  g_slist_free(windows);
+}
+
 void silc_say(SilcClient client, SilcClientConnection conn,
              SilcClientMessageType type, char *msg, ...)
 {
@@ -76,17 +153,107 @@ void silc_say_error(char *msg, ...)
   va_end(va);
 }
 
+/* try to verify a message using locally stored public key data */
+int verify_message_signature(SilcClientEntry sender,
+                            SilcMessageSignedPayload sig,
+                            SilcMessagePayload message)
+{
+  SilcPublicKey pk;
+  char file[256], filename[256];
+  char *fingerprint, *fingerprint2;
+  unsigned char *pk_data;
+  SilcUInt32 pk_datalen;
+  struct stat st;
+  int ret = SILC_MSG_SIGNED_VERIFIED, i;
+
+  if (sig == NULL)
+    return SILC_MSG_SIGNED_UNKNOWN;
+
+  /* get public key from the signature payload and compare it with the
+     one stored in the client entry */
+  pk = silc_message_signed_get_public_key(sig, &pk_data, &pk_datalen);
+
+  if (pk != NULL) {
+    fingerprint = silc_hash_fingerprint(NULL, pk_data, pk_datalen);
+
+    if (sender->fingerprint) {
+      fingerprint2 = silc_fingerprint(sender->fingerprint,
+                                     sender->fingerprint_len);
+      if (strcmp(fingerprint, fingerprint2)) {
+        /* since the public key differs from the senders public key, the
+           verification _failed_ */
+        silc_pkcs_public_key_free(pk);
+        silc_free(fingerprint);
+        ret = SILC_MSG_SIGNED_UNKNOWN;
+      }
+      silc_free(fingerprint2);
+    }
+  } else if (sender->fingerprint)
+    fingerprint = silc_fingerprint(sender->fingerprint,
+                                  sender->fingerprint_len);
+  else
+    /* no idea, who or what signed that message ... */
+    return SILC_MSG_SIGNED_UNKNOWN;
+  
+  /* search our local client key cache */
+  for (i = 0; i < strlen(fingerprint); i++)
+    if (fingerprint[i] == ' ')
+      fingerprint[i] = '_';
+    
+  snprintf(file, sizeof(file) - 1, "clientkey_%s.pub", fingerprint);
+  snprintf(filename, sizeof(filename) - 1, "%s/clientkeys/%s", 
+          get_irssi_dir(), file);
+  silc_free(fingerprint);
+  
+  if (stat(filename, &st) < 0)
+    /* we don't have the public key cached ... use the one from the sig */
+    ret = SILC_MSG_SIGNED_UNKNOWN;
+  else {
+    SilcPublicKey cached_pk=NULL;
+
+    /* try to load the file */
+    if (!silc_pkcs_load_public_key(filename, &cached_pk, SILC_PKCS_FILE_PEM) &&
+       !silc_pkcs_load_public_key(filename, &cached_pk, 
+                                  SILC_PKCS_FILE_BIN)) {
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+                        SILCTXT_PUBKEY_COULD_NOT_LOAD, "client");
+      if (pk == NULL)
+       return SILC_MSG_SIGNED_UNKNOWN;
+      else
+       ret = SILC_MSG_SIGNED_UNKNOWN;
+    }
+
+    if (cached_pk) {
+      if (pk)
+        silc_pkcs_public_key_free(pk);
+      pk = cached_pk; 
+    }
+  }
+
+  /* the public key is now in pk, our "level of trust" in ret */
+  if ((pk) && silc_message_signed_verify(sig, message, pk, 
+                                        silc_client->sha1hash)!= SILC_AUTH_OK)
+    ret = SILC_MSG_SIGNED_FAILED;
+
+  if (pk)
+    silc_pkcs_public_key_free(pk);
+
+  return ret;
+}
+
 /* Message for a channel. The `sender' is the nickname of the sender 
    received in the packet. The `channel_name' is the name of the channel. */
 
 void silc_channel_message(SilcClient client, SilcClientConnection conn,
                          SilcClientEntry sender, SilcChannelEntry channel,
+                         SilcMessagePayload payload,
                          SilcMessageFlags flags, const unsigned char *message,
                          SilcUInt32 message_len)
 {
   SILC_SERVER_REC *server;
   SILC_NICK_REC *nick;
   SILC_CHANNEL_REC *chanrec;
+  int verified = 0;
   
   SILC_LOG_DEBUG(("Start"));
 
@@ -106,14 +273,26 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
       nick = silc_nicklist_insert(chanrec, chu, FALSE);
   }
 
+  /* If the messages is digitally signed, verify it, if possible. */
+  if (flags & SILC_MESSAGE_FLAG_SIGNED) {
+    if (!settings_get_bool("ignore_message_signatures")) {
+      SilcMessageSignedPayload sig = silc_message_get_signature(payload);
+      verified = verify_message_signature(sender, sig, payload);
+    } else {
+      flags &= ~SILC_MESSAGE_FLAG_SIGNED;
+    }
+  }
+  
   if (flags & SILC_MESSAGE_FLAG_DATA) {
     /* MIME object received, try to display it as well as we can */
-    char type[128];
+    char type[128], enc[128];
     unsigned char *data;
+    SilcUInt32 data_len;
 
     memset(type, 0, sizeof(type));
+    memset(enc, 0, sizeof(enc));
     if (!silc_mime_parse(message, message_len, NULL, 0, type, sizeof(type) - 1,
-                        NULL, 0, &data, NULL))
+                        enc, sizeof(enc) - 1, &data, &data_len))
       return;
 
     /* Then figure out what we can display */
@@ -122,6 +301,9 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
       /* It is something textual, display it */
       message = (const unsigned char *)data;
     } else {
+      printformat_module("fe-common/silc", server, channel->channel_name,
+                        MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA,
+                        nick == NULL ? "[<unknown>]" : nick->nick, type);
       message = NULL;
     }
   }
@@ -129,6 +311,8 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
   if (!message)
     return;
 
+  /* FIXME: replace those printformat calls with signals and add signature
+            information to them (if present) */
   if (flags & SILC_MESSAGE_FLAG_ACTION)
     printformat_module("fe-common/silc", server, channel->channel_name,
                       MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_ACTION, 
@@ -137,23 +321,58 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
     printformat_module("fe-common/silc", server, channel->channel_name,
                       MSGLEVEL_NOTICES, SILCTXT_CHANNEL_NOTICE, 
                        nick == NULL ? "[<unknown>]" : nick->nick, message);
-  else
-    signal_emit("message public", 6, server, message,
-               nick == NULL ? "[<unknown>]" : nick->nick,
-               nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
-               chanrec->name, nick);
+  else {
+    if (flags & SILC_MESSAGE_FLAG_UTF8 && !silc_term_utf8()) {
+      char tmp[256], *cp, *dm = NULL;
+
+      memset(tmp, 0, sizeof(tmp));
+      cp = tmp;
+      if (message_len > sizeof(tmp) - 1) {
+       dm = silc_calloc(message_len + 1, sizeof(*dm));
+       cp = dm;
+      }
+
+      silc_utf8_decode(message, message_len, SILC_STRING_LANGUAGE,
+                      cp, message_len);
+      if (flags & SILC_MESSAGE_FLAG_SIGNED)
+        signal_emit("message signed_public", 6, server, cp,
+                   nick == NULL ? "[<unknown>]" : nick->nick,
+                   nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
+                   chanrec->name, verified);
+      else
+        signal_emit("message public", 6, server, cp,
+                   nick == NULL ? "[<unknown>]" : nick->nick,
+                   nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
+                   chanrec->name, nick);
+      silc_free(dm);
+      return;
+    }
+
+    if (flags & SILC_MESSAGE_FLAG_SIGNED)
+      signal_emit("message signed_public", 6, server, message,
+                 nick == NULL ? "[<unknown>]" : nick->nick,
+                 nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
+                 chanrec->name, verified);
+    else
+      signal_emit("message public", 6, server, message,
+                 nick == NULL ? "[<unknown>]" : nick->nick,
+                 nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
+                 chanrec->name, nick);
+  }
 }
 
 /* Private message to the client. The `sender' is the nickname of the
    sender received in the packet. */
 
 void silc_private_message(SilcClient client, SilcClientConnection conn,
-                         SilcClientEntry sender, SilcMessageFlags flags,
+                         SilcClientEntry sender, SilcMessagePayload payload,
+                         SilcMessageFlags flags,
                          const unsigned char *message,
                          SilcUInt32 message_len)
 {
   SILC_SERVER_REC *server;
   char userhost[256];
+  int verified = 0;
   
   SILC_LOG_DEBUG(("Start"));
 
@@ -162,9 +381,78 @@ void silc_private_message(SilcClient client, SilcClientConnection conn,
   if (sender->username)
     snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
             sender->username, sender->hostname);
-  signal_emit("message private", 4, server, message,
-             sender->nickname ? sender->nickname : "[<unknown>]",
-             sender->username ? userhost : NULL);
+
+  /* If the messages is digitally signed, verify it, if possible. */
+  if (flags & SILC_MESSAGE_FLAG_SIGNED) {
+    if (!settings_get_bool("ignore_message_signatures")) {
+      SilcMessageSignedPayload sig = silc_message_get_signature(payload);
+      verified = verify_message_signature(sender, sig, payload);
+    } else {
+      flags &= ~SILC_MESSAGE_FLAG_SIGNED;
+    }
+  }
+  
+  if (flags & SILC_MESSAGE_FLAG_DATA) {
+    /* MIME object received, try to display it as well as we can */
+    char type[128], enc[128];
+    unsigned char *data;
+    SilcUInt32 data_len;
+
+    memset(type, 0, sizeof(type));
+    memset(enc, 0, sizeof(enc));
+    if (!silc_mime_parse(message, message_len, NULL, 0, type, sizeof(type) - 1,
+                        enc, sizeof(enc) - 1, &data, &data_len))
+      return;
+
+    /* Then figure out what we can display */
+    if (strstr(type, "text/") && !strstr(type, "text/t140") &&
+       !strstr(type, "text/vnd")) {
+      /* It is something textual, display it */
+      message = (const unsigned char *)data;
+    } else {
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_MESSAGE_DATA,
+                        sender->nickname ? sender->nickname : "[<unknown>]",
+                        type);
+      message = NULL;
+    }
+  }
+
+  if (!message)
+    return;
+
+  if (flags & SILC_MESSAGE_FLAG_UTF8 && !silc_term_utf8()) {
+    char tmp[256], *cp, *dm = NULL;
+
+    memset(tmp, 0, sizeof(tmp));
+    cp = tmp;
+    if (message_len > sizeof(tmp) - 1) {
+      dm = silc_calloc(message_len + 1, sizeof(*dm));
+      cp = dm;
+    }
+
+    silc_utf8_decode(message, message_len, SILC_STRING_LANGUAGE,
+                    cp, message_len);
+    if (flags & SILC_MESSAGE_FLAG_SIGNED)
+      signal_emit("message signed_private", 5, server, cp,
+                 sender->nickname ? sender->nickname : "[<unknown>]",
+                 sender->username ? userhost : NULL, verified);
+    else
+      signal_emit("message private", 4, server, cp,
+                 sender->nickname ? sender->nickname : "[<unknown>]",
+                 sender->username ? userhost : NULL);
+    silc_free(dm);
+    return;
+  }
+
+  if (flags & SILC_MESSAGE_FLAG_SIGNED)
+    signal_emit("message signed_private", 5, server, message,
+               sender->nickname ? sender->nickname : "[<unknown>]",
+               sender->username ? userhost : NULL, verified);
+  else
+    signal_emit("message private", 4, server, message,
+               sender->nickname ? sender->nickname : "[<unknown>]",
+               sender->username ? userhost : NULL);
 }
 
 /* Notify message to the client. The notify arguments are sent in the
@@ -188,7 +476,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
   SilcIdType idtype;
   void *entry;
   SilcUInt32 mode;
-  char userhost[512];
+  char buf[512];
   char *name, *tmp;
   GSList *list1, *list_tmp;
 
@@ -215,11 +503,11 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     name = va_arg(va, char *);
     client_entry = va_arg(va, SilcClientEntry);
 
-    memset(userhost, 0, sizeof(userhost));
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
+    memset(buf, 0, sizeof(buf));
+    snprintf(buf, sizeof(buf) - 1, "%s@%s",
             client_entry->username, client_entry->hostname);
     signal_emit("message invite", 4, server, channel ? channel->channel_name :
-               name, client_entry->nickname, userhost);
+               name, client_entry->nickname, buf);
     break;
 
   case SILC_NOTIFY_TYPE_JOIN:
@@ -246,13 +534,13 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       }
     }
     
-    memset(userhost, 0, sizeof(userhost));
+    memset(buf, 0, sizeof(buf));
     if (client_entry->username)
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
+    snprintf(buf, sizeof(buf) - 1, "%s@%s",
             client_entry->username, client_entry->hostname);
     signal_emit("message join", 4, server, channel->channel_name,
                client_entry->nickname,
-               client_entry->username == NULL ? "" : userhost);
+               client_entry->username == NULL ? "" : buf);
     break;
 
   case SILC_NOTIFY_TYPE_LEAVE:
@@ -265,14 +553,14 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     client_entry = va_arg(va, SilcClientEntry);
     channel = va_arg(va, SilcChannelEntry);
     
-    memset(userhost, 0, sizeof(userhost));
+    memset(buf, 0, sizeof(buf));
     if (client_entry->username)
-      snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
+      snprintf(buf, sizeof(buf) - 1, "%s@%s",
               client_entry->username, client_entry->hostname);
     signal_emit("message part", 5, server, channel->channel_name,
-               client_entry->nickname,  client_entry->username ? 
-               userhost : "", client_entry->nickname);
-    
+               client_entry->nickname,  client_entry->username ? 
+               buf : "", client_entry->nickname);
+
     chanrec = silc_channel_find_entry(server, channel);
     if (chanrec != NULL) {
       nickrec = silc_nicklist_find(chanrec, client_entry);
@@ -292,15 +580,20 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     tmp = va_arg(va, char *);
     
     silc_server_free_ftp(server, client_entry);
-    
-    memset(userhost, 0, sizeof(userhost));
-    if (client_entry->username)
-      snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
-              client_entry->username, client_entry->hostname);
-    signal_emit("message quit", 4, server, client_entry->nickname,
-               client_entry->username ? userhost : "", 
-               tmp ? tmp : "");
-    
+
+    /* Print only if we have the nickname.  If this cliente has just quit
+       when we were only resolving it, it is possible we don't have the
+       nickname. */
+    if (client_entry->nickname) {
+      memset(buf, 0, sizeof(buf));
+      if (client_entry->username)
+        snprintf(buf, sizeof(buf) - 1, "%s@%s",
+                client_entry->username, client_entry->hostname);
+      signal_emit("message quit", 4, server, client_entry->nickname,
+                 client_entry->username ? buf : "", 
+                 tmp ? tmp : "");
+    }
+
     list1 = nicklist_get_same_unique(SERVER(server), client_entry);
     for (list_tmp = list1; list_tmp != NULL; list_tmp = 
           list_tmp->next->next) {
@@ -325,18 +618,35 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     
     chanrec = silc_channel_find_entry(server, channel);
     if (chanrec != NULL) {
+      char tmp2[256], *cp, *dm = NULL;
+
       g_free_not_null(chanrec->topic);
+      if (tmp && !silc_term_utf8() && silc_utf8_valid(tmp, strlen(tmp))) {
+       memset(tmp2, 0, sizeof(tmp2));
+       cp = tmp2;
+       if (strlen(tmp) > sizeof(tmp2) - 1) {
+         dm = silc_calloc(strlen(tmp) + 1, sizeof(*dm));
+         cp = dm;
+       }
+
+       silc_utf8_decode(tmp, strlen(tmp), SILC_STRING_LANGUAGE,
+                        cp, strlen(tmp));
+       tmp = cp;
+      }
+
       chanrec->topic = *tmp == '\0' ? NULL : g_strdup(tmp);
       signal_emit("channel topic changed", 1, chanrec);
+
+      silc_free(dm);
     }
     
     if (idtype == SILC_ID_CLIENT) {
       client_entry = (SilcClientEntry)entry;
-      memset(userhost, 0, sizeof(userhost));
-      snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
+      memset(buf, 0, sizeof(buf));
+      snprintf(buf, sizeof(buf) - 1, "%s@%s",
               client_entry->username, client_entry->hostname);
       signal_emit("message topic", 5, server, channel->channel_name,
-                 tmp, client_entry->nickname, userhost);
+                 tmp, client_entry->nickname, buf);
     } else if (idtype == SILC_ID_SERVER) {
       server_entry = (SilcServerEntry)entry;
       signal_emit("message topic", 5, server, channel->channel_name,
@@ -362,14 +672,14 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     if (!strcmp(client_entry->nickname, client_entry2->nickname))
       break;
     
-    memset(userhost, 0, sizeof(userhost));
-    snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
+    memset(buf, 0, sizeof(buf));
+    snprintf(buf, sizeof(buf) - 1, "%s@%s",
             client_entry2->username, client_entry2->hostname);
     nicklist_rename_unique(SERVER(server),
                           client_entry, client_entry->nickname,
                           client_entry2, client_entry2->nickname);
     signal_emit("message nick", 4, server, client_entry2->nickname, 
-               client_entry->nickname, userhost);
+               client_entry->nickname, buf);
     break;
 
   case SILC_NOTIFY_TYPE_CMODE_CHANGE:
@@ -388,7 +698,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
     tmp = silc_client_chmode(mode,
                             channel->channel_key ? 
-                            channel->channel_key->cipher->name : "",
+                            silc_cipher_get_name(channel->channel_key) : "",
                             channel->hmac ? 
                             silc_hmac_get_name(channel->hmac) : "");
     
@@ -480,6 +790,11 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
                         SILCTXT_CHANNEL_FOUNDER,
                         channel->channel_name, client_entry2->nickname);
 
+    if (mode & SILC_CHANNEL_UMODE_QUIET && conn->local_entry == client_entry2)
+      printformat_module("fe-common/silc", 
+                        server, channel->channel_name, MSGLEVEL_CRAP,
+                        SILCTXT_CHANNEL_QUIETED, channel->channel_name);
+
     silc_free(tmp);
     break;
 
@@ -616,13 +931,19 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       clients_count = va_arg(va, SilcUInt32);
   
       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");
+       memset(buf, 0, sizeof(buf));
+
+       /* Print only if we have the nickname.  If this client has just quit
+          when we were only resolving it, it is possible we don't have the
+          nickname. */
+       if (clients[i]->nickname) {
+         if (clients[i]->username)
+           snprintf(buf, sizeof(buf) - 1, "%s@%s",
+                    clients[i]->username, clients[i]->hostname);
+         signal_emit("message quit", 4, server, clients[i]->nickname,
+                     clients[i]->username ? buf : "", 
+                     "server signoff");
+       }
 
        silc_server_free_ftp(server, clients[i]);
        
@@ -642,7 +963,65 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       SilcStatus error = va_arg(va, int);
 
       silc_say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
-               "%s", silc_client_status_message(error));
+               "%s", silc_get_status_message(error));
+    }
+    break;
+
+  case SILC_NOTIFY_TYPE_WATCH:
+    {
+      SilcNotifyType notify;
+
+      client_entry = va_arg(va, SilcClientEntry);
+      name = va_arg(va, char *);          /* Maybe NULL */
+      mode = va_arg(va, SilcUInt32);
+      notify = va_arg(va, int);
+
+      if (notify == SILC_NOTIFY_TYPE_NICK_CHANGE) {
+       if (name)
+         printformat_module("fe-common/silc", server, NULL,
+                            MSGLEVEL_CRAP, SILCTXT_WATCH_NICK_CHANGE,
+                            client_entry->nickname, name);
+       else
+         printformat_module("fe-common/silc", server, NULL,
+                            MSGLEVEL_CRAP, SILCTXT_WATCH_PRESENT,
+                            client_entry->nickname);
+      } else if (notify == SILC_NOTIFY_TYPE_UMODE_CHANGE) {
+       /* See if client was away and is now present */
+       if (!(mode & (SILC_UMODE_GONE | SILC_UMODE_INDISPOSED |
+                     SILC_UMODE_BUSY | SILC_UMODE_PAGE |
+                     SILC_UMODE_DETACHED)) &&
+           (client_entry->mode & SILC_UMODE_GONE ||
+            client_entry->mode & SILC_UMODE_INDISPOSED ||
+            client_entry->mode & SILC_UMODE_BUSY ||
+            client_entry->mode & SILC_UMODE_PAGE ||
+            client_entry->mode & SILC_UMODE_DETACHED)) {
+         printformat_module("fe-common/silc", server, NULL,
+                            MSGLEVEL_CRAP, SILCTXT_WATCH_PRESENT,
+                            client_entry->nickname);
+       }
+
+       if (mode) {
+         memset(buf, 0, sizeof(buf));
+         silc_get_umode_string(mode, buf, sizeof(buf) - 1);
+         printformat_module("fe-common/silc", server, NULL,
+                            MSGLEVEL_CRAP, SILCTXT_WATCH_UMODE_CHANGE,
+                            client_entry->nickname, buf);
+       }
+      } else if (notify == SILC_NOTIFY_TYPE_KILLED) {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_WATCH_KILLED,
+                          client_entry->nickname);
+      } else if (notify == SILC_NOTIFY_TYPE_SIGNOFF ||
+                notify == SILC_NOTIFY_TYPE_SERVER_SIGNOFF) {
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_WATCH_SIGNOFF,
+                          client_entry->nickname);
+      } else if (notify == SILC_NOTIFY_TYPE_NONE) {
+       /* Client logged in to the network */
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_WATCH_PRESENT,
+                          client_entry->nickname);
+      }
     }
     break;
 
@@ -665,7 +1044,7 @@ void silc_connect(SilcClient client, SilcClientConnection conn,
 {
   SILC_SERVER_REC *server = conn->context;
 
-  if (!server) {
+  if (!server || server->disconnected) {
     silc_client_close_connection(client, conn);
     return;
   }
@@ -707,7 +1086,8 @@ void silc_connect(SilcClient client, SilcClientConnection conn,
 
 /* Called to indicate that connection was disconnected to the server. */
 
-void silc_disconnect(SilcClient client, SilcClientConnection conn)
+void silc_disconnect(SilcClient client, SilcClientConnection conn,
+                    SilcStatus status, const char *message)
 {
   SILC_SERVER_REC *server = conn->context;
 
@@ -724,6 +1104,12 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn)
     silc_change_nick(server, silc_client->username);
   }
 
+  if (message)
+    silc_say(client, conn, SILC_CLIENT_MESSAGE_AUDIT,
+            "Server closed connection: %s (%d) %s",
+            silc_get_status_message(status), status,
+            message ? message : "");
+
   server->conn->context = NULL;
   server->conn = NULL;
   server->connection_lost = TRUE;
@@ -740,25 +1126,34 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn)
    that the command really was processed. */
 
 void silc_command(SilcClient client, SilcClientConnection conn, 
-                 SilcClientCommandContext cmd_context, int success,
-                 SilcCommand command)
+                 SilcClientCommandContext cmd_context, bool success,
+                 SilcCommand command, SilcStatus status)
 {
   SILC_SERVER_REC *server = conn->context;
 
   SILC_LOG_DEBUG(("Start"));
 
-  if (!success)
+  if (!success) {
+    silc_say_error("%s", silc_get_status_message(status));
     return;
+  }
+
+  switch (command) {
 
-  switch(command) {
   case SILC_COMMAND_INVITE:
-    printformat_module("fe-common/silc", server, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITING,
-                      cmd_context->argv[2], 
-                      (cmd_context->argv[1][0] == '*' ?
-                       (char *)conn->current_channel->channel_name :
-                       (char *)cmd_context->argv[1]));
+    if (cmd_context->argc > 2)
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITING,
+                        cmd_context->argv[2], 
+                        (cmd_context->argv[1][0] == '*' ?
+                         (char *)conn->current_channel->channel_name :
+                         (char *)cmd_context->argv[1]));
+    break;
+
+  case SILC_COMMAND_DETACH:
+    server->no_reconnect = TRUE;
     break;
+
   default:
     break;
   }
@@ -811,15 +1206,14 @@ static void silc_client_join_get_users(SilcClient client,
                       MSGLEVEL_CRAP, SILCTXT_CHANNEL_TOPIC,
                       channel->channel_name, chanrec->topic);
 
-  fe_channels_nicklist(CHANNEL(chanrec), CHANNEL_NICKLIST_FLAG_ALL);
-
   if (founder) {
-    if (founder == conn->local_entry)
+    if (founder == conn->local_entry) {
       printformat_module("fe-common/silc", 
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         SILCTXT_CHANNEL_FOUNDER_YOU,
                         channel->channel_name);
-    else
+      signal_emit("nick mode changed", 2, chanrec, ownnick);
+    } else
       printformat_module("fe-common/silc", 
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         SILCTXT_CHANNEL_FOUNDER,
@@ -845,16 +1239,130 @@ void silc_getkey_cb(bool success, void *context)
 
   if (success) {
     printformat_module("fe-common/silc", NULL, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_GETKEY_VERIFIED, entity, name);
+                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_VERIFIED, entity, name);
   } else {
     printformat_module("fe-common/silc", NULL, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_GETKEY_DISCARD, entity, name);
+                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_NOTVERIFIED,
+                      entity, name);
   }
 
   silc_free(getkey->fingerprint);
   silc_free(getkey);
 }
 
+/* Parse an invite or ban list */
+void  silc_parse_inviteban_list(SilcClient client,
+                               SilcClientConnection conn,
+                               SILC_SERVER_REC *server, 
+                               SilcChannelEntry channel, 
+                               const char *list_type,
+                               SilcArgumentPayload list)
+{
+  unsigned char *tmp;
+  SilcUInt32 type, len;
+  SILC_CHANNEL_REC *chanrec = silc_channel_find_entry(server, channel);
+  int counter=0, resolving = FALSE;
+
+  if (!silc_argument_get_arg_num(list)) {
+    printformat_module("fe-common/silc", server,
+                      (chanrec ? chanrec->visible_name : NULL),
+                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_NO_INVITEBAN_LIST,
+                      channel->channel_name, list_type);
+    return;
+  }
+  
+  printformat_module("fe-common/silc", server,
+                    (chanrec ? chanrec->visible_name : NULL),
+                    MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITEBAN_LIST,
+                    channel->channel_name, list_type);
+
+  /* parse the list */
+  tmp = silc_argument_get_first_arg(list, &type, &len);
+  while (tmp) {
+    switch (type) {
+      case 1:
+       {
+         /* an invite string */
+         char **list;
+         int i=0;
+               
+         if (tmp[len-1] == ',')
+           tmp[len-1] = '\0';
+         
+         list = g_strsplit(tmp, ",", -1);
+         while (list[i])
+           printformat_module("fe-common/silc", server,
+                              (chanrec ? chanrec->visible_name : NULL),
+                              MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITEBAN_STRING,
+                              ++counter, channel->channel_name, list_type,
+                              list[i++]);
+         g_strfreev(list);
+       }
+       break;
+
+      case 2:
+       {
+         /* a public key */
+         char *fingerprint, *babbleprint;
+
+         /* tmp is Public Key Payload, take public key from it. */
+         fingerprint = silc_hash_fingerprint(NULL, tmp + 4, len - 4);
+         babbleprint = silc_hash_babbleprint(NULL, tmp + 4, len - 4);
+         
+         printformat_module("fe-common/silc", server,
+                            (chanrec ? chanrec->visible_name : NULL),
+                            MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITEBAN_PUBKEY,
+                            ++counter, channel->channel_name, list_type,
+                            fingerprint, babbleprint);
+       }
+       break;
+       
+      case 3:
+       {
+         /* a client ID */
+         SilcClientID *client_id;
+         SilcClientEntry client_entry;
+         
+         client_id = silc_id_payload_parse_id(tmp, len, NULL);
+                         
+         if (client_id == NULL) {
+           silc_say_error("Invalid data in %s list encountered", list_type);
+           break;
+         }
+
+         client_entry = silc_client_get_client_by_id(client, conn, client_id);
+
+         if (client_entry) {
+           printformat_module("fe-common/silc", server,
+                              (chanrec ? chanrec->visible_name : NULL),
+                              MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITEBAN_STRING,
+                              ++counter, channel->channel_name, list_type,
+                              client_entry->nickname);
+         } else {
+           resolving = TRUE;
+           silc_client_get_client_by_id_resolve(client, conn, client_id,
+                                                NULL, NULL, NULL);
+         }
+
+         silc_free(client_id);
+       }
+       break;
+       
+      default:
+       /* "trash" */
+       silc_say_error("Unkown type in %s list: %u (len %u)",
+                      list_type, type, len);
+    }
+    tmp = silc_argument_get_next_arg(list, &type, &len);
+  }
+
+  if (resolving)
+    printformat_module("fe-common/silc", server, 
+                      (chanrec ? chanrec->visible_name : NULL),
+                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITEBAN_REGET,
+                      list_type, channel->channel_name);
+}
+
 /* Command reply handler. This function is called always in the command reply
    function. If error occurs it will be called as well. Normal scenario
    is that it will be called after the received command data has been parsed
@@ -873,7 +1381,7 @@ void silc_getkey_cb(bool success, void *context)
 
 void 
 silc_command_reply(SilcClient client, SilcClientConnection conn,
-                  SilcCommandPayload cmd_payload, int success,
+                  SilcCommandPayload cmd_payload, bool success,
                   SilcCommand command, SilcStatus status, ...)
 
 {
@@ -893,6 +1401,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       SilcUInt32 idle, mode;
       SilcBuffer channels, user_modes;
       SilcClientEntry client_entry;
+      SilcDList attrs;
       
       if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
        /* Print the unknown nick for user */
@@ -901,7 +1410,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                     3, NULL);
        if (tmp)
          silc_say_error("%s: %s", tmp, 
-                        silc_client_status_message(status));
+                        silc_get_status_message(status));
        break;
       } else if (status == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
        /* Try to find the entry for the unknown client ID, since we
@@ -918,16 +1427,16 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                                        client_id);
            if (client_entry && client_entry->nickname)
              silc_say_error("%s: %s", client_entry->nickname,
-                            silc_client_status_message(status));
+                            silc_get_status_message(status));
            silc_free(client_id);
          }
        }
        break;
-      }
-      
-      if (!success)
+      } else if (!success) {
+       silc_say_error("WHOIS: %s", silc_get_status_message(status));
        return;
-      
+      }
+
       client_entry = va_arg(vp, SilcClientEntry);
       nickname = va_arg(vp, char *);
       username = va_arg(vp, char *);
@@ -937,6 +1446,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       idle = va_arg(vp, SilcUInt32);
       fingerprint = va_arg(vp, unsigned char *);
       user_modes = va_arg(vp, SilcBuffer);
+      attrs = va_arg(vp, SilcDList);
       
       silc_parse_userfqdn(nickname, &nick, NULL);
       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
@@ -979,33 +1489,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       
       if (mode) {
        memset(buf, 0, sizeof(buf));
-
-       if ((mode & SILC_UMODE_SERVER_OPERATOR) ||
-           (mode & SILC_UMODE_ROUTER_OPERATOR)) {
-         strcat(buf, (mode & SILC_UMODE_SERVER_OPERATOR) ?
-                "Server Operator" :
-                (mode & SILC_UMODE_ROUTER_OPERATOR) ?
-                "SILC Operator" : "[Unknown mode]");
-       }
-       if (mode & SILC_UMODE_GONE)
-         strcat(buf, " [away]");
-       if (mode & SILC_UMODE_INDISPOSED)
-         strcat(buf, " [indisposed]");
-       if (mode & SILC_UMODE_BUSY)
-         strcat(buf, " [busy]");
-       if (mode & SILC_UMODE_PAGE)
-         strcat(buf, " [page to reach]");
-       if (mode & SILC_UMODE_HYPER)
-         strcat(buf, " [hyper active]");
-       if (mode & SILC_UMODE_ROBOT)
-         strcat(buf, " [robot]");
-       if (mode & SILC_UMODE_ANONYMOUS)
-         strcat(buf, " [anonymous]");
-       if (mode & SILC_UMODE_BLOCK_PRIVMSG)
-         strcat(buf, " [blocks private messages]");
-       if (mode & SILC_UMODE_DETACHED)
-         strcat(buf, " [detached]");
-
+       silc_get_umode_string(mode, buf, sizeof(buf - 1));
        printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
                           SILCTXT_WHOIS_MODES, buf);
       }
@@ -1026,6 +1510,10 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                           SILCTXT_WHOIS_FINGERPRINT, fingerprint);
        silc_free(fingerprint);
       }
+
+      if (attrs)
+       silc_query_attributes_print(server, silc_client, conn, attrs,
+                                   client_entry);
     }
     break;
     
@@ -1040,7 +1528,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                     3, NULL);
        if (tmp)
          silc_say_error("%s: %s", tmp, 
-                        silc_client_status_message(status));
+                        silc_get_status_message(status));
        break;
       } else if (status == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
        /* Try to find the entry for the unknown client ID, since we
@@ -1057,7 +1545,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                                        client_id);
            if (client_entry && client_entry->nickname)
              silc_say_error("%s: %s", client_entry->nickname,
-                            silc_client_status_message(status));
+                            silc_get_status_message(status));
            silc_free(client_id);
          }
        }
@@ -1078,12 +1566,12 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                         3, NULL);
        if (tmp)
          silc_say_error("%s: %s", tmp, 
-                        silc_client_status_message(status));
+                        silc_get_status_message(status));
        break;
-      }
-      
-      if (!success)
+      } else if (!success) {
+       silc_say_error("WHOWAS: %s", silc_get_status_message(status));
        return;
+      }
       
       (void)va_arg(vp, SilcClientEntry);
       nickname = va_arg(vp, char *);
@@ -1099,28 +1587,26 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
   case SILC_COMMAND_INVITE:
     {
       SilcChannelEntry channel;
-      char *invite_list;
-      SilcArgumentPayload args;
-      int argc = 0;
+      SilcBuffer payload;
+      SilcArgumentPayload invite_list;
+      SilcUInt32 argc;
       
       if (!success)
        return;
       
       channel = va_arg(vp, SilcChannelEntry);
-      invite_list = va_arg(vp, char *);
-
-      args = silc_command_get_args(cmd_payload);
-      if (args)
-       argc = silc_argument_get_arg_num(args);
-
-      if (invite_list)
-       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                          SILCTXT_CHANNEL_INVITE_LIST, channel->channel_name,
-                          invite_list);
-      else if (argc == 3)
-       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                          SILCTXT_CHANNEL_NO_INVITE_LIST, 
-                          channel->channel_name);
+      payload = va_arg(vp, SilcBuffer);
+
+      if (payload) {
+       SILC_GET16_MSB(argc, payload->data);
+       invite_list = silc_argument_payload_parse(payload->data + 2, 
+                                                 payload->len - 2, argc);
+       if (invite_list) {
+         silc_parse_inviteban_list(client, conn, server, channel, 
+                                   "invite", invite_list);
+         silc_argument_payload_free(invite_list);
+       }
+      }
     }
     break;
 
@@ -1149,17 +1635,35 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
       chanrec = silc_channel_find(server, channel);
       if (!chanrec)
-       chanrec = silc_channel_create(server, channel, TRUE);
+       chanrec = silc_channel_create(server, channel, channel, TRUE);
 
       if (topic) {
+       char tmp[256], *cp, *dm = NULL;
        g_free_not_null(chanrec->topic);
+
+       if (!silc_term_utf8() && silc_utf8_valid(topic, strlen(topic))) {
+         memset(tmp, 0, sizeof(tmp));
+         cp = tmp;
+         if (strlen(topic) > sizeof(tmp) - 1) {
+           dm = silc_calloc(strlen(topic) + 1, sizeof(*dm));
+           cp = dm;
+         }
+
+         silc_utf8_decode(topic, strlen(topic), SILC_STRING_LANGUAGE,
+                          cp, strlen(topic));
+         topic = cp;
+       }
+
        chanrec->topic = *topic == '\0' ? NULL : g_strdup(topic);
        signal_emit("channel topic changed", 1, chanrec);
+
+       silc_free(dm);
       }
 
       mode = silc_client_chmode(modei, 
                                channel_entry->channel_key ? 
-                               channel_entry->channel_key->cipher->name : "",
+                               silc_cipher_get_name(channel_entry->
+                                                    channel_key) : "",
                                channel_entry->hmac ? 
                                silc_hmac_get_name(channel_entry->hmac) : "");
       g_free_not_null(chanrec->mode);
@@ -1176,17 +1680,38 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
   case SILC_COMMAND_NICK: 
     {
-      SilcClientEntry client = va_arg(vp, SilcClientEntry);
       char *old;
+      SilcClientEntry client_entry = va_arg(vp, SilcClientEntry);
+      GSList *nicks;
       
       if (!success)
        return;
 
+      nicks = nicklist_get_same(SERVER(server), client_entry->nickname);
+      if (nicks != NULL) {
+       char buf[512];
+       SilcClientEntry collider, old;
+
+       old = ((SILC_NICK_REC *)(nicks->next->data))->silc_user->client;
+       collider = silc_client_get_client_by_id(client, conn,
+                                               old->id);
+       
+        memset(buf, 0, sizeof(buf));
+        snprintf(buf, sizeof(buf) - 1, "%s@%s",
+                collider->username, collider->hostname);
+       nicklist_rename_unique(SERVER(server),
+                              old, old->nickname,
+                              collider, collider->nickname);
+       silc_print_nick_change(server, collider->nickname,
+                              client_entry->nickname, buf);
+       g_slist_free(nicks);
+      }
+
       old = g_strdup(server->nick);
-      server_change_nick(SERVER(server), client->nickname);
+      server_change_nick(SERVER(server), client_entry->nickname);
       nicklist_rename_unique(SERVER(server),
                             server->conn->local_entry, server->nick,
-                            client, client->nickname);
+                            client_entry, client_entry->nickname);
       signal_emit("message own_nick", 4, server, server->nick, old, "");
       g_free(old);
       break;
@@ -1197,6 +1722,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       char *topic, *name;
       int usercount;
       char users[20];
+      char tmp[256], *cp, *dm = NULL;
       
       if (!success)
        return;
@@ -1205,6 +1731,20 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       name = va_arg(vp, char *);
       topic = va_arg(vp, char *);
       usercount = va_arg(vp, int);
+
+      if (topic && !silc_term_utf8() &&
+         silc_utf8_valid(topic, strlen(topic))) {
+       memset(tmp, 0, sizeof(tmp));
+       cp = tmp;
+       if (strlen(topic) > sizeof(tmp) - 1) {
+         dm = silc_calloc(strlen(topic) + 1, sizeof(*dm));
+         cp = dm;
+       }
+
+       silc_utf8_decode(topic, strlen(topic), SILC_STRING_LANGUAGE,
+                        cp, strlen(topic));
+       topic = cp;
+      }
       
       if (status == SILC_STATUS_LIST_START ||
          status == SILC_STATUS_OK)
@@ -1218,12 +1758,14 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       printformat_module("fe-common/silc", server, NULL,
                         MSGLEVEL_CRAP, SILCTXT_LIST,
                         name, users, topic ? topic : "");
+      silc_free(dm);
     }
     break;
     
   case SILC_COMMAND_UMODE:
     {
       SilcUInt32 mode;
+      char *reason;
       
       if (!success)
        return;
@@ -1240,6 +1782,20 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
        printformat_module("fe-common/silc", server, NULL,
                           MSGLEVEL_CRAP, SILCTXT_ROUTER_OPER);
 
+      if ((mode & SILC_UMODE_GONE) != (server->umode & SILC_UMODE_GONE)) {
+       if (mode & SILC_UMODE_GONE) {      
+         if ((server->away_reason != NULL) && (server->away_reason[0] != '\0'))
+           reason = g_strdup(server->away_reason);
+         else
+           reason = g_strdup("away");
+       } else
+         reason = g_strdup("");
+
+       silc_set_away(reason, server);
+
+       g_free(reason);
+      }
+
       server->umode = mode;
       signal_emit("user mode changed", 2, server, NULL);
     }
@@ -1327,22 +1883,26 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
   case SILC_COMMAND_BAN:
     {
       SilcChannelEntry channel;
-      char *ban_list;
+      SilcBuffer payload;
+      SilcArgumentPayload ban_list;
+      SilcUInt32 argc;
       
       if (!success)
        return;
       
       channel = va_arg(vp, SilcChannelEntry);
-      ban_list = va_arg(vp, char *);
-      
-      if (ban_list)
-       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                          SILCTXT_CHANNEL_BAN_LIST, channel->channel_name,
-                          ban_list);
-      else
-       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                          SILCTXT_CHANNEL_NO_BAN_LIST, 
-                          channel->channel_name);
+      payload = va_arg(vp, SilcBuffer);
+
+      if (payload) {
+       SILC_GET16_MSB(argc, payload->data);
+       ban_list = silc_argument_payload_parse(payload->data + 2, 
+                                              payload->len - 2, argc);
+       if (ban_list) {
+         silc_parse_inviteban_list(client, conn, server, channel, 
+                                   "ban", ban_list);
+         silc_argument_payload_free(ban_list);
+       }
+      }
     }
     break;
     
@@ -1386,7 +1946,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
        silc_free(pk);
       } else {
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_GETKEY_NOKEY);
+                          MSGLEVEL_CRAP, SILCTXT_PUBKEY_NOKEY);
       }
     }
     break;
@@ -1416,13 +1976,28 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
     {
       SilcChannelEntry channel;
       char *topic;
+      char tmp[256], *cp, *dm = NULL;
       
       if (!success)
        return;
       
       channel = va_arg(vp, SilcChannelEntry);
       topic = va_arg(vp, char *);
-      
+
+      if (topic && !silc_term_utf8() &&
+         silc_utf8_valid(topic, strlen(topic))) {
+       memset(tmp, 0, sizeof(tmp));
+       cp = tmp;
+       if (strlen(topic) > sizeof(tmp) - 1) {
+         dm = silc_calloc(strlen(topic) + 1, sizeof(*dm));
+         cp = dm;
+       }
+
+       silc_utf8_decode(topic, strlen(topic), SILC_STRING_LANGUAGE,
+                        cp, strlen(topic));
+       topic = cp;
+      }
+
       if (topic) {
        chanrec = silc_channel_find_entry(server, channel);
        if (chanrec) {
@@ -1438,6 +2013,138 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                           MSGLEVEL_CRAP, SILCTXT_CHANNEL_TOPIC_NOT_SET,
                           channel->channel_name);
       }
+      silc_free(dm);
+    }
+    break;
+
+  case SILC_COMMAND_WATCH:
+    break;
+  
+  case SILC_COMMAND_STATS:
+    {
+      SilcUInt32 starttime, uptime, my_clients, my_channels, my_server_ops,
+                my_router_ops, cell_clients, cell_channels, cell_servers,
+                clients, channels, servers, routers, server_ops, router_ops;
+      SilcUInt32 buf_len;
+      SilcBufferStruct buf;
+      unsigned char *tmp_buf;
+      char tmp[40];
+      const char *tmptime;
+      int days, hours, mins, secs;
+
+      if (!success)
+       return;
+
+      tmp_buf = va_arg(vp, unsigned char *);
+      buf_len = va_arg(vp, SilcUInt32);
+
+      if (!tmp_buf || !buf_len) {
+       printtext(server, NULL, MSGLEVEL_CRAP, "No statistics available");
+       return;
+      }
+
+      /* Get statistics structure */
+      silc_buffer_set(&buf, tmp_buf, buf_len);
+      silc_buffer_unformat(&buf,
+                          SILC_STR_UI_INT(&starttime),
+                          SILC_STR_UI_INT(&uptime),
+                          SILC_STR_UI_INT(&my_clients),
+                          SILC_STR_UI_INT(&my_channels),
+                          SILC_STR_UI_INT(&my_server_ops),
+                          SILC_STR_UI_INT(&my_router_ops),
+                          SILC_STR_UI_INT(&cell_clients),
+                          SILC_STR_UI_INT(&cell_channels),
+                          SILC_STR_UI_INT(&cell_servers),
+                          SILC_STR_UI_INT(&clients),
+                          SILC_STR_UI_INT(&channels),
+                          SILC_STR_UI_INT(&servers),
+                          SILC_STR_UI_INT(&routers),
+                          SILC_STR_UI_INT(&server_ops),
+                          SILC_STR_UI_INT(&router_ops),
+                          SILC_STR_END);
+
+      tmptime = silc_get_time(starttime);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local server start time", tmptime);
+
+      days = uptime / (24 * 60 * 60);
+      uptime -= days * (24 * 60 * 60);
+      hours = uptime / (60 * 60);
+      uptime -= hours * (60 * 60);
+      mins = uptime / 60;
+      uptime -= mins * 60;
+      secs = uptime;
+      snprintf(tmp, sizeof(tmp) - 1, "%d days %d hours %d mins %d secs",
+              days, hours, mins, secs);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local server uptime", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)my_clients);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local server clients", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)my_channels);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local server channels", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)my_server_ops);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local server operators", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)my_router_ops);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local router operators", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)cell_clients);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local cell clients", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)cell_channels);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local cell channels", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)cell_servers);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Local cell servers", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)clients);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Total clients", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)channels);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Total channels", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)servers);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Total servers", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)routers);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Total routers", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)server_ops);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                          "Total server operators", tmp);
+
+      snprintf(tmp, sizeof(tmp) - 1, "%d", (int)router_ops);
+      printformat_module("fe-common/silc", server, NULL,
+                        MSGLEVEL_CRAP, SILCTXT_STATS,
+                        "Total router operators", tmp);
     }
     break;
 
@@ -1581,8 +2288,7 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   verify->entity_name = (conn_type != SILC_SOCKET_TYPE_CLIENT ?
                         (name ? strdup(name) : strdup(conn->sock->hostname))
                         : NULL);
-  verify->pk = silc_calloc(pk_len, sizeof(*verify->pk));
-  memcpy(verify->pk, pk, pk_len);
+  verify->pk = silc_memdup(pk, pk_len);
   verify->pk_len = pk_len;
   verify->pk_type = pk_type;
   verify->completion = completion;
@@ -1690,6 +2396,11 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
     if (completion)
       completion(TRUE, context);
     silc_free(fingerprint);
+    silc_free(verify->filename);
+    silc_free(verify->entity);
+    silc_free(verify->entity_name);
+    silc_free(verify->pk);
+    silc_free(verify);
   }
 }
 
@@ -1719,6 +2430,8 @@ typedef struct {
 void ask_passphrase_completion(const char *passphrase, void *context)
 {
   AskPassphrase p = (AskPassphrase)context;
+  if (passphrase && passphrase[0] == '\0')
+    passphrase = NULL;
   p->completion((unsigned char *)passphrase, 
                passphrase ? strlen(passphrase) : 0, p->context);
   silc_free(p);
@@ -1759,9 +2472,20 @@ static void silc_get_auth_method_callback(SilcClient client,
     (*internal->completion)(TRUE, auth_meth, NULL, 0, internal->context);
     break;
   case SILC_AUTH_PASSWORD:
-    /* Do not ask the passphrase from user, the library will ask it if
-       we do not provide it here. */
-    (*internal->completion)(TRUE, auth_meth, NULL, 0, internal->context);
+    {
+      /* Check whether we find the password for this server in our
+        configuration.  If not, then don't provide so library will ask
+        it from the user. */
+      SERVER_SETUP_REC *setup = server_setup_find_port(conn->remote_host,
+                                                      conn->remote_port);
+      if (!setup || !setup->password) {
+       (*internal->completion)(TRUE, auth_meth, NULL, 0, internal->context);
+       break;
+      }
+      
+      (*internal->completion)(TRUE, auth_meth, setup->password,
+                             strlen(setup->password), internal->context);
+    }
     break;
   case SILC_AUTH_PUBLIC_KEY:
     /* Do not get the authentication data now, the library will generate
@@ -1789,9 +2513,6 @@ void silc_get_auth_method(SilcClient client, SilcClientConnection conn,
 
   SILC_LOG_DEBUG(("Start"));
 
-  /* XXX must resolve from configuration whether this connection has
-     any specific authentication data */
-
   /* If we do not have this connection configured by the user in a
      configuration file then resolve the authentication method from the
      server for this session. */
@@ -1864,10 +2585,10 @@ void silc_failure(SilcClient client, SilcClientConnection conn,
    desired (application may start it later by calling the function
    silc_client_perform_key_agreement). */
 
-int silc_key_agreement(SilcClient client, SilcClientConnection conn,
-                      SilcClientEntry client_entry, const char *hostname,
-                      SilcUInt16 port, SilcKeyAgreementCallback *completion,
-                      void **context)
+bool silc_key_agreement(SilcClient client, SilcClientConnection conn,
+                       SilcClientEntry client_entry, const char *hostname,
+                       SilcUInt16 port, SilcKeyAgreementCallback *completion,
+                       void **context)
 {
   char portstr[12];
 
@@ -1906,18 +2627,29 @@ void silc_ftp(SilcClient client, SilcClientConnection conn,
 {
   SILC_SERVER_REC *server;
   char portstr[12];
-  FtpSession ftp = silc_calloc(1, sizeof(*ftp));
+  FtpSession ftp = NULL;
 
   SILC_LOG_DEBUG(("Start"));
 
   server = conn->context;
 
-  ftp->client_entry = client_entry;
-  ftp->session_id = session_id;
-  ftp->send = FALSE;
-  ftp->conn = conn;
-  silc_dlist_add(server->ftp_sessions, ftp);
-  server->current_session = ftp;
+  silc_dlist_start(server->ftp_sessions);
+  while ((ftp = silc_dlist_get(server->ftp_sessions)) != SILC_LIST_END) {
+    if (ftp->client_entry == client_entry &&
+       ftp->session_id == session_id) {
+      server->current_session = ftp;
+      break;
+    }
+  }
+  if (ftp == SILC_LIST_END) {
+    ftp = silc_calloc(1, sizeof(*ftp));
+    ftp->client_entry = client_entry;
+    ftp->session_id = session_id;
+    ftp->send = FALSE;
+    ftp->conn = conn;
+    silc_dlist_add(server->ftp_sessions, ftp);
+    server->current_session = ftp;
+  }
 
   if (hostname) 
     snprintf(portstr, sizeof(portstr) - 1, "%d", port);