Merged silc_1_0_branch to trunk.
[silc.git] / apps / irssi / src / silc / core / client_ops.c
index 1a539869022a2bb85c820cbbe375856b66dbe1ef..476fbeda80d86fcaf14452563f78aa176807ef19 100644 (file)
@@ -4,13 +4,13 @@
 
   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
 
-  Copyright (C) 2001 Pekka Riikonen
+  Copyright (C) 2001 - 2003 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
-  
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #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"
 
-static void 
+static void
 silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
-                               const char *name, SilcSocketType conn_type, 
-                               unsigned char *pk, SilcUInt32 pk_len, 
+                               const char *name, SilcSocketType conn_type,
+                               unsigned char *pk, SilcUInt32 pk_len,
                                SilcSKEPKType pk_type,
                                SilcVerifyPublicKey completion, void *context);
 
-static void silc_get_umode_string(SilcUInt32 mode, char *buf, 
+static void silc_get_umode_string(SilcUInt32 mode, char *buf,
                                  SilcUInt32 buf_size)
 {
   if ((mode & SILC_UMODE_SERVER_OPERATOR) ||
@@ -81,6 +83,94 @@ static void silc_get_umode_string(SilcUInt32 mode, char *buf,
     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);
+}
+
+static void silc_parse_channel_public_keys(SILC_SERVER_REC *server,
+                                          SilcChannelEntry channel_entry,
+                                          SilcBuffer channel_pubkeys)
+{
+  SilcUInt16 argc;
+  SilcArgumentPayload chpks;
+  unsigned char *pk;
+  SilcUInt32 pk_len, type;
+  int c = 1;
+  char *fingerprint, *babbleprint;
+  SilcPublicKey pubkey;
+  SilcPublicKeyIdentifier ident;
+
+  printformat_module("fe-common/silc", server, NULL,
+                    MSGLEVEL_CRAP, SILCTXT_CHANNEL_PK_LIST,
+                    channel_entry->channel_name);
+
+  SILC_GET16_MSB(argc, channel_pubkeys->data);
+  chpks = silc_argument_payload_parse(channel_pubkeys->data + 2,
+                                     channel_pubkeys->len - 2, argc);
+  if (!chpks)
+    return;
+
+  pk = silc_argument_get_first_arg(chpks, &type, &pk_len);
+  while (pk) {
+    fingerprint = silc_hash_fingerprint(NULL, pk + 4, pk_len - 4);
+    babbleprint = silc_hash_babbleprint(NULL, pk + 4, pk_len - 4);
+    silc_pkcs_public_key_payload_decode(pk, pk_len, &pubkey);
+    ident = silc_pkcs_decode_identifier(pubkey->identifier);
+
+    printformat_module("fe-common/silc", server, NULL,
+                      MSGLEVEL_CRAP, SILCTXT_CHANNEL_PK_LIST_ENTRY,
+                      c++, channel_entry->channel_name,
+                      type == 0x00 ? "Added" : "Removed",
+                      ident->realname ? ident->realname : "",
+                      fingerprint, babbleprint);
+
+    silc_free(fingerprint);
+    silc_free(babbleprint);
+    silc_pkcs_public_key_free(pubkey);
+    silc_pkcs_free_identifier(ident);
+    pk = silc_argument_get_next_arg(chpks, &type, &pk_len);
+  }
+
+  silc_argument_payload_free(chpks);
+}
+
 void silc_say(SilcClient client, SilcClientConnection conn,
              SilcClientMessageType type, char *msg, ...)
 {
@@ -89,7 +179,7 @@ void silc_say(SilcClient client, SilcClientConnection conn,
   char *str;
 
   server = conn == NULL ? NULL : conn->context;
-  
+
   va_start(va, msg);
   str = g_strdup_vprintf(msg, va);
   printtext(server, NULL, MSGLEVEL_CRAP, "%s", str);
@@ -110,18 +200,170 @@ void silc_say_error(char *msg, ...)
   va_end(va);
 }
 
-/* Message for a channel. The `sender' is the nickname of the sender 
+/* 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;
+}
+
+char * silc_unescape_data(const char *escaped_data, SilcUInt32 *length)
+{
+  SilcUInt32 ctr, dest=0;
+  char *data;
+
+  data = silc_calloc(strlen(escaped_data), sizeof(char));
+  
+  for (ctr = 0; ctr < strlen(escaped_data); ctr++)
+    if (escaped_data[ctr] == 1)
+      data[dest++] = escaped_data[++ctr] - 1;
+    else
+      data[dest++] = escaped_data[ctr];
+
+  *length = dest;
+  return data;
+}
+
+char * silc_escape_data(const char *data, SilcUInt32 len)
+{
+  char *escaped_data;
+  SilcUInt32 ctr, zeros=0;
+
+  for (ctr = 0; ctr < len; ctr++)
+    if (data[ctr] == 0 || data[ctr] == 1)
+      zeros++;
+
+  escaped_data = silc_calloc(zeros + len, sizeof(char));
+
+  zeros=0;
+  for (ctr = 0; ctr < len; ctr++)
+    switch (data[ctr]) {
+      case 0:
+       escaped_data[zeros++] = 1;
+       escaped_data[zeros++] = 1;
+       break;
+
+      case 1:
+       escaped_data[zeros++] = 1;
+       escaped_data[zeros++] = 2;
+       break;
+
+      default:
+       escaped_data[zeros++] = data[ctr];
+    } 
+
+    return escaped_data;
+}
+
+void silc_emit_mime_sig(SILC_SERVER_REC *server, SILC_CHANNEL_REC *channel,
+               const char *data, SilcUInt32 data_len,
+               const char *encoding, const char *type, const char *nick)
+{
+   char *escaped_data;
+
+   escaped_data = silc_escape_data(data, data_len);
+
+   signal_emit("mime", 6, server, channel, escaped_data, encoding, type, nick);
+   silc_free(escaped_data);
+}
+
+
+/* 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"));
 
   if (!message)
@@ -131,7 +373,7 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
   chanrec = silc_channel_find_entry(server, channel);
   if (!chanrec)
     return;
-  
+
   nick = silc_nicklist_find(chanrec, sender);
   if (!nick) {
     /* We didn't find client but it clearly exists, add it. */
@@ -140,6 +382,16 @@ 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], enc[128];
@@ -158,9 +410,8 @@ 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);
+      silc_emit_mime_sig(server, chanrec, data, data_len,
+               enc, type, nick == NULL ? NULL : nick->nick);
       message = NULL;
     }
   }
@@ -168,14 +419,50 @@ 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, 
-                       nick == NULL ? "[<unknown>]" : nick->nick, message);
+    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);
+      printformat_module("fe-common/silc", server, channel->channel_name,
+                         MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_ACTION,
+                         nick == NULL ? "[<unknown>]" : nick->nick, cp);
+      silc_free(dm);
+    } else {
+      printformat_module("fe-common/silc", server, channel->channel_name,
+                         MSGLEVEL_ACTIONS, SILCTXT_CHANNEL_ACTION,
+                         nick == NULL ? "[<unknown>]" : nick->nick,
+                         message);
+    }
   else if (flags & SILC_MESSAGE_FLAG_NOTICE)
-    printformat_module("fe-common/silc", server, channel->channel_name,
-                      MSGLEVEL_NOTICES, SILCTXT_CHANNEL_NOTICE, 
-                       nick == NULL ? "[<unknown>]" : nick->nick, message);
+    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);
+      printformat_module("fe-common/silc", server, channel->channel_name,
+                         MSGLEVEL_NOTICES, SILCTXT_CHANNEL_NOTICE,
+                         nick == NULL ? "[<unknown>]" : nick->nick, cp);
+      silc_free(dm);
+    } else {
+      printformat_module("fe-common/silc", server, channel->channel_name,
+                         MSGLEVEL_NOTICES, SILCTXT_CHANNEL_NOTICE,
+                         nick == NULL ? "[<unknown>]" : nick->nick,
+                         message);
+    }
   else {
     if (flags & SILC_MESSAGE_FLAG_UTF8 && !silc_term_utf8()) {
       char tmp[256], *cp, *dm = NULL;
@@ -189,18 +476,30 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
 
       silc_utf8_decode(message, message_len, SILC_STRING_LANGUAGE,
                       cp, message_len);
-      signal_emit("message public", 6, server, cp,
-                 nick == NULL ? "[<unknown>]" : nick->nick,
-                 nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
-                 chanrec->name, nick);
+      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;
     }
 
-    signal_emit("message public", 6, server, message,
-               nick == NULL ? "[<unknown>]" : nick->nick,
-               nick == NULL ? "" : nick->host == NULL ? "" : nick->host,
-               chanrec->name, nick);
+    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);
   }
 }
 
@@ -208,13 +507,15 @@ void silc_channel_message(SilcClient client, SilcClientConnection conn,
    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"));
 
   server = conn == NULL ? NULL : conn->context;
@@ -223,6 +524,16 @@ void silc_private_message(SilcClient client, SilcClientConnection conn,
     snprintf(userhost, sizeof(userhost) - 1, "%s@%s",
             sender->username, sender->hostname);
 
+  /* 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];
@@ -241,10 +552,9 @@ void silc_private_message(SilcClient client, SilcClientConnection conn,
       /* 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);
+      silc_emit_mime_sig(server, NULL, data, data_len,
+                       enc, type, sender->nickname ? sender->nickname :
+                                  "[<unknown>]");
       message = NULL;
     }
   }
@@ -264,16 +574,26 @@ void silc_private_message(SilcClient client, SilcClientConnection conn,
 
     silc_utf8_decode(message, message_len, SILC_STRING_LANGUAGE,
                     cp, message_len);
-    signal_emit("message private", 4, server, cp,
-               sender->nickname ? sender->nickname : "[<unknown>]",
-               sender->username ? userhost : NULL);
+    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;
   }
 
-  signal_emit("message private", 4, server, message,
-             sender->nickname ? sender->nickname : "[<unknown>]",
-             sender->username ? userhost : NULL);
+  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
@@ -300,13 +620,14 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
   char buf[512];
   char *name, *tmp;
   GSList *list1, *list_tmp;
+  SilcBuffer buffer;
 
   SILC_LOG_DEBUG(("Start"));
 
   va_start(va, type);
 
   server = conn == NULL ? NULL : conn->context;
-  
+
   switch(type) {
   case SILC_NOTIFY_TYPE_NONE:
     /* Some generic notice from server */
@@ -335,7 +656,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     /*
      * Joined channel.
      */
+
     SILC_LOG_DEBUG(("Notify: JOIN"));
 
     client_entry = va_arg(va, SilcClientEntry);
@@ -354,7 +675,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
          nickrec = silc_nicklist_insert(chanrec, chu, TRUE);
       }
     }
-    
+
     memset(buf, 0, sizeof(buf));
     if (client_entry->username)
     snprintf(buf, sizeof(buf) - 1, "%s@%s",
@@ -373,15 +694,15 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
     client_entry = va_arg(va, SilcClientEntry);
     channel = va_arg(va, SilcChannelEntry);
-    
+
     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 part", 5, server, channel->channel_name,
-               client_entry->nickname,  client_entry->username ? 
+               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);
@@ -399,23 +720,28 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
     client_entry = va_arg(va, SilcClientEntry);
     tmp = va_arg(va, char *);
-    
+
     silc_server_free_ftp(server, client_entry);
-    
-    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 : "");
-    
+
+    /* 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 = 
+    for (list_tmp = list1; list_tmp != NULL; list_tmp =
           list_tmp->next->next) {
       CHANNEL_REC *channel = list_tmp->data;
       NICK_REC *nickrec = list_tmp->next->data;
-      
+
       nicklist_remove(channel, nickrec);
     }
     break;
@@ -431,14 +757,31 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     entry = va_arg(va, void *);
     tmp = va_arg(va, char *);
     channel = va_arg(va, SilcChannelEntry);
-    
+
     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(buf, 0, sizeof(buf));
@@ -449,7 +792,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     } else if (idtype == SILC_ID_SERVER) {
       server_entry = (SilcServerEntry)entry;
       signal_emit("message topic", 5, server, channel->channel_name,
-                 tmp, server_entry->server_name, 
+                 tmp, server_entry->server_name,
                  server_entry->server_name);
     } else if (idtype == SILC_ID_CHANNEL) {
       channel = (SilcChannelEntry)entry;
@@ -470,14 +813,14 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
     if (!strcmp(client_entry->nickname, client_entry2->nickname))
       break;
-    
+
     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, 
+    signal_emit("message nick", 4, server, client_entry2->nickname,
                client_entry->nickname, buf);
     break;
 
@@ -491,23 +834,26 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     idtype = va_arg(va, int);
     entry = va_arg(va, void *);
     mode = va_arg(va, SilcUInt32);
-    (void)va_arg(va, char *);
-    (void)va_arg(va, char *);
+    (void)va_arg(va, char *);                 /* cipher */
+    (void)va_arg(va, char *);                 /* hmac */
+    (void)va_arg(va, char *);                 /* passphrase */
+    (void)va_arg(va, SilcPublicKey);          /* founder key */
+    buffer = va_arg(va, SilcBuffer);          /* channel public keys */
     channel = va_arg(va, SilcChannelEntry);
 
     tmp = silc_client_chmode(mode,
-                            channel->channel_key ? 
+                            channel->channel_key ?
                             silc_cipher_get_name(channel->channel_key) : "",
-                            channel->hmac ? 
+                            channel->hmac ?
                             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(tmp == NULL ? "" : tmp);
       signal_emit("channel mode changed", 1, chanrec);
     }
-    
+
     if (idtype == SILC_ID_CLIENT) {
       client_entry = (SilcClientEntry)entry;
       printformat_module("fe-common/silc", server, channel->channel_name,
@@ -528,6 +874,10 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
                         channel2->channel_name);
     }
 
+    /* Print the channel public key list */
+    if (buffer)
+      silc_parse_channel_public_keys(server, channel, buffer);
+
     silc_free(tmp);
     break;
 
@@ -551,7 +901,7 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
 
       if (client_entry2 == server->conn->local_entry)
        chanrec->chanop = (mode & SILC_CHANNEL_UMODE_CHANOP) != 0;
-      
+
       nick = silc_nicklist_find(chanrec, client_entry2);
       if (nick != NULL) {
        nick->op = (mode & SILC_CHANNEL_UMODE_CHANOP) != 0;
@@ -564,33 +914,33 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       client_entry = (SilcClientEntry)entry;
       printformat_module("fe-common/silc", server, channel->channel_name,
                         MSGLEVEL_MODES, SILCTXT_CHANNEL_CUMODE,
-                        channel->channel_name, client_entry2->nickname, 
+                        channel->channel_name, client_entry2->nickname,
                         tmp ? tmp : "removed all",
                         client_entry->nickname);
     } else if (idtype == SILC_ID_SERVER) {
       server_entry = (SilcServerEntry)entry;
       printformat_module("fe-common/silc", server, channel->channel_name,
                         MSGLEVEL_MODES, SILCTXT_CHANNEL_CUMODE,
-                        channel->channel_name, client_entry2->nickname, 
+                        channel->channel_name, client_entry2->nickname,
                         tmp ? tmp : "removed all",
                         server_entry->server_name);
     } else if (idtype == SILC_ID_CHANNEL) {
       channel2 = (SilcChannelEntry)entry;
       printformat_module("fe-common/silc", server, channel->channel_name,
                         MSGLEVEL_MODES, SILCTXT_CHANNEL_CUMODE,
-                        channel->channel_name, client_entry2->nickname, 
+                        channel->channel_name, client_entry2->nickname,
                         tmp ? tmp : "removed all",
                         channel2->channel_name);
     }
 
     if (mode & SILC_CHANNEL_UMODE_CHANFO)
-      printformat_module("fe-common/silc", 
+      printformat_module("fe-common/silc",
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         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", 
+      printformat_module("fe-common/silc",
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         SILCTXT_CHANNEL_QUIETED, channel->channel_name);
 
@@ -623,11 +973,11 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     channel = va_arg(va, SilcChannelEntry);
 
     chanrec = silc_channel_find_entry(server, channel);
-  
+
     if (client_entry == conn->local_entry) {
       printformat_module("fe-common/silc", server, channel->channel_name,
-                        MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED_YOU, 
-                        channel->channel_name, 
+                        MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED_YOU,
+                        channel->channel_name,
                         client_entry ? client_entry2->nickname : "",
                         tmp ? tmp : "");
       if (chanrec) {
@@ -636,9 +986,9 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       }
     } else {
       printformat_module("fe-common/silc", server, channel->channel_name,
-                        MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED, 
-                        client_entry->nickname, channel->channel_name, 
-                        client_entry2 ? client_entry2->nickname : "", 
+                        MSGLEVEL_CRAP, SILCTXT_CHANNEL_KICKED,
+                        client_entry->nickname, channel->channel_name,
+                        client_entry2 ? client_entry2->nickname : "",
                         tmp ? tmp : "");
 
       if (chanrec) {
@@ -660,28 +1010,28 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
     tmp = va_arg(va, char *);
     idtype = va_arg(va, int);
     entry = va_arg(va, SilcClientEntry);
-  
+
     if (client_entry == conn->local_entry) {
       if (idtype == SILC_ID_CLIENT) {
        client_entry2 = (SilcClientEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU,
                           client_entry2 ? client_entry2->nickname : "",
                           tmp ? tmp : "");
       } else if (idtype == SILC_ID_SERVER) {
        server_entry = (SilcServerEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU,
                           server_entry->server_name, tmp ? tmp : "");
       } else if (idtype == SILC_ID_CHANNEL) {
        channel = (SilcChannelEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED_YOU,
                           channel->channel_name, tmp ? tmp : "");
       }
     } else {
       list1 = nicklist_get_same_unique(SERVER(server), client_entry);
-      for (list_tmp = list1; list_tmp != NULL; list_tmp = 
+      for (list_tmp = list1; list_tmp != NULL; list_tmp =
             list_tmp->next->next) {
        CHANNEL_REC *channel = list_tmp->data;
        NICK_REC *nickrec = list_tmp->next->data;
@@ -691,20 +1041,20 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       if (idtype == SILC_ID_CLIENT) {
        client_entry2 = (SilcClientEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED,
                           client_entry->nickname,
                           client_entry2 ? client_entry2->nickname : "",
                           tmp ? tmp : "");
       } else if (idtype == SILC_ID_SERVER) {
        server_entry = (SilcServerEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED,
                           client_entry->nickname,
                           server_entry->server_name, tmp ? tmp : "");
       } else if (idtype == SILC_ID_CHANNEL) {
        channel = (SilcChannelEntry)entry;
        printformat_module("fe-common/silc", server, NULL,
-                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED, 
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_KILLED,
                           client_entry->nickname,
                           channel->channel_name, tmp ? tmp : "");
       }
@@ -724,24 +1074,30 @@ void silc_notify(SilcClient client, SilcClientConnection conn,
       SilcUInt32 clients_count;
 
       SILC_LOG_DEBUG(("Notify: SIGNOFF"));
-      
+
       (void)va_arg(va, void *);
       clients = va_arg(va, SilcClientEntry *);
       clients_count = va_arg(va, SilcUInt32);
-  
+
       for (i = 0; i < clients_count; i++) {
        memset(buf, 0, sizeof(buf));
-       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");
+
+       /* 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]);
-       
+
        list1 = nicklist_get_same_unique(SERVER(server), clients[i]);
-       for (list_tmp = list1; list_tmp != NULL; list_tmp = 
+       for (list_tmp = list1; list_tmp != NULL; list_tmp =
               list_tmp->next->next) {
          CHANNEL_REC *channel = list_tmp->data;
          NICK_REC *nickrec = list_tmp->next->data;
@@ -854,13 +1210,13 @@ void silc_connect(SilcClient client, SilcClientConnection conn,
     server->connected = TRUE;
     signal_emit("event connected", 1, server);
 
-    /* If we resumed old session check whether we need to update 
+    /* If we resumed old session check whether we need to update
        our nickname */
     if (strcmp(server->nick, conn->local_entry->nickname)) {
       char *old;
       old = g_strdup(server->nick);
       server_change_nick(SERVER(server), conn->local_entry->nickname);
-      nicklist_rename_unique(SERVER(server), 
+      nicklist_rename_unique(SERVER(server),
                             conn->local_entry, server->nick,
                             conn->local_entry, conn->local_entry->nickname);
       signal_emit("message own_nick", 4, server, server->nick, old, "");
@@ -880,7 +1236,7 @@ void silc_connect(SilcClient client, SilcClientConnection conn,
 /* Called to indicate that connection was disconnected to the server. */
 
 void silc_disconnect(SilcClient client, SilcClientConnection conn,
-                    SilcStatus status, const char *message)
+                    SilcStatus status, const char *message)
 {
   SILC_SERVER_REC *server = conn->context;
 
@@ -892,7 +1248,7 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn,
   if (server->conn && server->conn->local_entry) {
     nicklist_rename_unique(SERVER(server),
                           server->conn->local_entry, server->nick,
-                          server->conn->local_entry, 
+                          server->conn->local_entry,
                           silc_client->username);
     silc_change_nick(server, silc_client->username);
   }
@@ -903,7 +1259,8 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn,
             silc_get_status_message(status), status,
             message ? message : "");
 
-  server->conn->context = NULL;
+  if (server->conn)
+    server->conn->context = NULL;
   server->conn = NULL;
   server->connection_lost = TRUE;
   server_disconnect(SERVER(server));
@@ -918,7 +1275,9 @@ void silc_disconnect(SilcClient client, SilcClientConnection conn,
    after application has called the command. Just to tell application
    that the command really was processed. */
 
-void silc_command(SilcClient client, SilcClientConnection conn, 
+static bool cmode_list_chpks = FALSE;
+
+void silc_command(SilcClient client, SilcClientConnection conn,
                  SilcClientCommandContext cmd_context, bool success,
                  SilcCommand command, SilcStatus status)
 {
@@ -937,7 +1296,7 @@ void silc_command(SilcClient client, SilcClientConnection conn,
     if (cmd_context->argc > 2)
       printformat_module("fe-common/silc", server, NULL,
                         MSGLEVEL_CRAP, SILCTXT_CHANNEL_INVITING,
-                        cmd_context->argv[2], 
+                        cmd_context->argv[2],
                         (cmd_context->argv[1][0] == '*' ?
                          (char *)conn->current_channel->channel_name :
                          (char *)cmd_context->argv[1]));
@@ -947,11 +1306,24 @@ void silc_command(SilcClient client, SilcClientConnection conn,
     server->no_reconnect = TRUE;
     break;
 
+  case SILC_COMMAND_CMODE:
+    if (cmd_context->argc == 3 &&
+       !strcmp(cmd_context->argv[2], "+C"))
+      cmode_list_chpks = TRUE;
+    else
+      cmode_list_chpks = FALSE;
+    break;
+
   default:
     break;
   }
 }
 
+typedef struct {
+  SilcChannelEntry channel;
+  bool retry;
+} *SilcJoinResolve;
+
 /* Client info resolving callback when JOIN command reply is received.
    This will cache all users on the channel. */
 
@@ -961,7 +1333,8 @@ static void silc_client_join_get_users(SilcClient client,
                                       SilcUInt32 clients_count,
                                       void *context)
 {
-  SilcChannelEntry channel = (SilcChannelEntry)context;
+  SilcJoinResolve r = context;
+  SilcChannelEntry channel = r->channel;
   SilcHashTableList htl;
   SilcChannelUser chu;
   SILC_SERVER_REC *server = conn->context;
@@ -972,8 +1345,13 @@ static void silc_client_join_get_users(SilcClient client,
   SILC_LOG_DEBUG(("Start, channel %s, %d users", channel->channel_name,
                  silc_hash_table_count(channel->user_list)));
 
-  if (!clients)
+  if (!clients && r->retry < 1) {
+    /* Retry to resolve */
+    r->retry++;
+    silc_client_get_clients_by_channel(client, conn, channel,
+                                      silc_client_join_get_users, context);
     return;
+  }
 
   chanrec = silc_channel_find(server, channel->channel_name);
   if (chanrec == NULL)
@@ -1000,13 +1378,14 @@ static void silc_client_join_get_users(SilcClient client,
                       channel->channel_name, chanrec->topic);
 
   if (founder) {
-    if (founder == conn->local_entry)
-      printformat_module("fe-common/silc", 
+    if (founder == conn->local_entry) {
+      printformat_module("fe-common/silc",
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         SILCTXT_CHANNEL_FOUNDER_YOU,
                         channel->channel_name);
-    else
-      printformat_module("fe-common/silc", 
+      signal_emit("nick mode changed", 2, chanrec, ownnick);
+    } else
+      printformat_module("fe-common/silc",
                         server, channel->channel_name, MSGLEVEL_CRAP,
                         SILCTXT_CHANNEL_FOUNDER,
                         channel->channel_name, founder->nickname);
@@ -1025,7 +1404,7 @@ void silc_getkey_cb(bool success, void *context)
 {
   GetkeyContext getkey = (GetkeyContext)context;
   char *entity = (getkey->id_type == SILC_ID_CLIENT ? "user" : "server");
-  char *name = (getkey->id_type == SILC_ID_CLIENT ? 
+  char *name = (getkey->id_type == SILC_ID_CLIENT ?
                ((SilcClientEntry)getkey->entry)->nickname :
                ((SilcServerEntry)getkey->entry)->server_name);
 
@@ -1042,11 +1421,124 @@ void silc_getkey_cb(bool success, void *context)
   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
    and processed. The function is used to pass the received command data to
-   the application. 
+   the application.
 
    `conn' is the associated client connection. `cmd_payload' is the command
    payload data received from server and it can be ignored. It is provided
@@ -1058,7 +1550,7 @@ void silc_getkey_cb(bool success, void *context)
    and each command defines the number and type of arguments it passes to the
    application (on error they are not sent). */
 
-void 
+void
 silc_command_reply(SilcClient client, SilcClientConnection conn,
                   SilcCommandPayload cmd_payload, bool success,
                   SilcCommand command, SilcStatus status, ...)
@@ -1081,14 +1573,14 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       SilcBuffer channels, user_modes;
       SilcClientEntry client_entry;
       SilcDList attrs;
-      
+
       if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
        /* Print the unknown nick for user */
        unsigned char *tmp =
          silc_argument_get_arg_type(silc_command_get_args(cmd_payload),
                                     3, NULL);
        if (tmp)
-         silc_say_error("%s: %s", tmp, 
+         silc_say_error("%s: %s", tmp,
                         silc_get_status_message(status));
        break;
       } else if (status == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
@@ -1099,7 +1591,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
          silc_argument_get_arg_type(silc_command_get_args(cmd_payload),
                                     2, &tmp_len);
        if (tmp) {
-         SilcClientID *client_id = silc_id_payload_parse_id(tmp, tmp_len, 
+         SilcClientID *client_id = silc_id_payload_parse_id(tmp, tmp_len,
                                                             NULL);
          if (client_id) {
            client_entry = silc_client_get_client_by_id(client, conn,
@@ -1126,10 +1618,10 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       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,
-                        SILCTXT_WHOIS_USERINFO, nickname, 
+                        SILCTXT_WHOIS_USERINFO, nickname,
                         client_entry->username, client_entry->hostname,
                         nick, client_entry->nickname);
       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
@@ -1151,11 +1643,11 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
            SilcUInt32 name_len;
            char *m = silc_client_chumode_char(umodes[i++]);
            char *name = silc_channel_get_name(entry, &name_len);
-           
+
            if (m)
-             strncat(buf, m, strlen(m));
-           strncat(buf, name, name_len);
-           strncat(buf, " ", 1);
+             silc_strncat(buf, sizeof(buf) - 1, m, strlen(m));
+           silc_strncat(buf, sizeof(buf) - 1, name, name_len);
+           silc_strncat(buf, sizeof(buf) - 1, " ", 1);
            silc_free(m);
          }
 
@@ -1165,14 +1657,14 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
          silc_free(umodes);
        }
       }
-      
+
       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_WHOIS_MODES, buf);
       }
-      
+
       if (idle && nickname) {
        memset(buf, 0, sizeof(buf));
        snprintf(buf, sizeof(buf) - 1, "%lu %s",
@@ -1195,18 +1687,18 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                                    client_entry);
     }
     break;
-    
+
   case SILC_COMMAND_IDENTIFY:
     {
       SilcClientEntry client_entry;
-      
+
       if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
        /* Print the unknown nick for user */
        unsigned char *tmp =
          silc_argument_get_arg_type(silc_command_get_args(cmd_payload),
                                     3, NULL);
        if (tmp)
-         silc_say_error("%s: %s", tmp, 
+         silc_say_error("%s: %s", tmp,
                         silc_get_status_message(status));
        break;
       } else if (status == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
@@ -1237,61 +1729,59 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
   case SILC_COMMAND_WHOWAS:
     {
       char *nickname, *username, *realname;
-      
+
       if (status == SILC_STATUS_ERR_NO_SUCH_NICK ||
          status == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID) {
        char *tmp;
        tmp = silc_argument_get_arg_type(silc_command_get_args(cmd_payload),
                                         3, NULL);
        if (tmp)
-         silc_say_error("%s: %s", tmp, 
+         silc_say_error("%s: %s", tmp,
                         silc_get_status_message(status));
        break;
       } else if (!success) {
        silc_say_error("WHOWAS: %s", silc_get_status_message(status));
        return;
       }
-      
+
       (void)va_arg(vp, SilcClientEntry);
       nickname = va_arg(vp, char *);
       username = va_arg(vp, char *);
       realname = va_arg(vp, char *);
-      
+
       printformat_module("fe-common/silc", server, NULL, MSGLEVEL_CRAP,
-                        SILCTXT_WHOWAS_USERINFO, nickname, username, 
+                        SILCTXT_WHOWAS_USERINFO, nickname, username,
                         realname ? realname : "");
     }
     break;
-    
+
   case SILC_COMMAND_INVITE:
     {
       SilcChannelEntry channel;
-      char *invite_list;
-      SilcArgumentPayload args;
-      int argc = 0;
-      
+      SilcBuffer payload;
+      SilcArgumentPayload invite_list;
+      SilcUInt16 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);
+      channel = va_arg(vp, SilcChannelEntry);
+      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;
 
-  case SILC_COMMAND_JOIN: 
+  case SILC_COMMAND_JOIN:
     {
       char *channel, *mode, *topic;
       SilcUInt32 modei;
@@ -1319,57 +1809,100 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
        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 ? 
+      mode = silc_client_chmode(modei,
+                               channel_entry->channel_key ?
                                silc_cipher_get_name(channel_entry->
                                                     channel_key) : "",
-                               channel_entry->hmac ? 
+                               channel_entry->hmac ?
                                silc_hmac_get_name(channel_entry->hmac) : "");
       g_free_not_null(chanrec->mode);
       chanrec->mode = g_strdup(mode == NULL ? "" : mode);
       signal_emit("channel mode changed", 1, chanrec);
 
       /* Resolve the client information */
-      silc_client_get_clients_by_list(client, conn, list_count, client_id_list,
-                                     silc_client_join_get_users, 
-                                     channel_entry);
+      {
+       SilcJoinResolve r = silc_calloc(1, sizeof(*r));
+       r->channel = channel_entry;
+       silc_client_get_clients_by_list(client, conn, list_count,
+                                       client_id_list,
+                                       silc_client_join_get_users, r);
+      }
 
       break;
     }
 
-  case SILC_COMMAND_NICK: 
+  case SILC_COMMAND_NICK:
     {
       char *old;
-      SilcClientEntry client = va_arg(vp, SilcClientEntry);
-      
+      SilcClientEntry client_entry = va_arg(vp, SilcClientEntry);
+      GSList *nicks;
+
       if (!success)
        return;
 
+      nicks = nicklist_get_same(SERVER(server), client_entry->nickname);
+      if ((nicks != NULL) &&
+        (strcmp(SERVER(server)->nick, client_entry->nickname))) {
+       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;
     }
-    
+
   case SILC_COMMAND_LIST:
     {
       char *topic, *name;
       int usercount;
       char users[20];
       char tmp[256], *cp, *dm = NULL;
-      
+
       if (!success)
        return;
-      
+
       (void)va_arg(vp, SilcChannelEntry);
       name = va_arg(vp, char *);
       topic = va_arg(vp, char *);
@@ -1388,7 +1921,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
                         cp, strlen(topic));
        topic = cp;
       }
-      
+
       if (status == SILC_STATUS_LIST_START ||
          status == SILC_STATUS_OK)
        printformat_module("fe-common/silc", server, NULL,
@@ -1404,16 +1937,17 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       silc_free(dm);
     }
     break;
-    
+
   case SILC_COMMAND_UMODE:
     {
       SilcUInt32 mode;
-      
+      char *reason;
+
       if (!success)
        return;
-      
+
       mode = va_arg(vp, SilcUInt32);
-      
+
       if (mode & SILC_UMODE_SERVER_OPERATOR &&
          !(server->umode & SILC_UMODE_SERVER_OPERATOR))
        printformat_module("fe-common/silc", server, NULL,
@@ -1424,11 +1958,25 @@ 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);
     }
     break;
-    
+
   case SILC_COMMAND_OPER:
     if (!success)
       return;
@@ -1439,7 +1987,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
     printformat_module("fe-common/silc", server, NULL,
                       MSGLEVEL_CRAP, SILCTXT_SERVER_OPER);
     break;
-    
+
   case SILC_COMMAND_SILCOPER:
     if (!success)
       return;
@@ -1450,18 +1998,18 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
     printformat_module("fe-common/silc", server, NULL,
                       MSGLEVEL_CRAP, SILCTXT_ROUTER_OPER);
     break;
-    
-  case SILC_COMMAND_USERS: 
+
+  case SILC_COMMAND_USERS:
     {
       SilcHashTableList htl;
       SilcChannelEntry channel;
       SilcChannelUser chu;
-      
+
       if (!success)
        return;
-      
+
       channel = va_arg(vp, SilcChannelEntry);
-      
+
       printformat_module("fe-common/silc", server, channel->channel_name,
                         MSGLEVEL_CRAP, SILCTXT_USERS_HEADER,
                         channel->channel_name);
@@ -1473,7 +2021,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
        if (!e->nickname)
          continue;
-       
+
        memset(stat, 0, sizeof(stat));
        mode = silc_client_chumode_char(chu->mode);
        if (e->mode & SILC_UMODE_GONE)
@@ -1497,7 +2045,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
        printformat_module("fe-common/silc", server, channel->channel_name,
                           MSGLEVEL_CRAP, SILCTXT_USERS,
-                          e->nickname, stat, 
+                          e->nickname, stat,
                           e->username ? e->username : "",
                           e->hostname ? e->hostname : "",
                           e->realname ? e->realname : "");
@@ -1511,25 +2059,29 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
   case SILC_COMMAND_BAN:
     {
       SilcChannelEntry channel;
-      char *ban_list;
-      
+      SilcBuffer payload;
+      SilcArgumentPayload ban_list;
+      SilcUInt16 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;
-    
+
   case SILC_COMMAND_GETKEY:
     {
       SilcIdType id_type;
@@ -1539,10 +2091,10 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
       SilcUInt32 pk_len;
       GetkeyContext getkey;
       char *name;
-      
+
       if (!success)
        return;
-      
+
       id_type = va_arg(vp, SilcUInt32);
       entry = va_arg(vp, void *);
       public_key = va_arg(vp, SilcPublicKey);
@@ -1557,7 +2109,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
        getkey->conn = conn;
        getkey->fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
 
-       name = (id_type == SILC_ID_CLIENT ? 
+       name = (id_type == SILC_ID_CLIENT ?
                ((SilcClientEntry)entry)->nickname :
                ((SilcServerEntry)entry)->server_name);
 
@@ -1583,7 +2135,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
       if (!success)
        return;
-      
+
       server_entry = va_arg(vp, SilcServerEntry);
       server_name = va_arg(vp, char *);
       server_info = va_arg(vp, char *);
@@ -1595,16 +2147,16 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
        }
     }
     break;
-    
+
   case SILC_COMMAND_TOPIC:
     {
       SilcChannelEntry channel;
       char *topic;
       char tmp[256], *cp, *dm = NULL;
-      
+
       if (!success)
        return;
-      
+
       channel = va_arg(vp, SilcChannelEntry);
       topic = va_arg(vp, char *);
 
@@ -1643,7 +2195,7 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
 
   case SILC_COMMAND_WATCH:
     break;
-  
+
   case SILC_COMMAND_STATS:
     {
       SilcUInt32 starttime, uptime, my_clients, my_channels, my_server_ops,
@@ -1772,6 +2324,30 @@ silc_command_reply(SilcClient client, SilcClientConnection conn,
     }
     break;
 
+  case SILC_COMMAND_CMODE:
+    {
+      SilcChannelEntry channel_entry;
+      SilcBuffer channel_pubkeys;
+
+      channel_entry = va_arg(vp, SilcChannelEntry);
+      (void)va_arg(vp, SilcUInt32);
+      (void)va_arg(vp, SilcPublicKey);
+      channel_pubkeys = va_arg(vp, SilcBuffer);
+
+      if (!success || !cmode_list_chpks ||
+         !channel_entry || !channel_entry->channel_name)
+       return;
+
+      /* Print the channel public key list */
+      if (channel_pubkeys)
+       silc_parse_channel_public_keys(server, channel_entry, channel_pubkeys);
+      else
+       printformat_module("fe-common/silc", server, NULL,
+                          MSGLEVEL_CRAP, SILCTXT_CHANNEL_PK_NO_LIST,
+                          channel_entry->channel_name);
+    }
+    break;
+
   }
 
   va_end(vp);
@@ -1800,7 +2376,7 @@ static void verify_public_key_completion(const char *line, void *context)
       verify->completion(TRUE, verify->context);
 
     /* Save the key for future checking */
-    silc_pkcs_save_public_key_data(verify->filename, verify->pk, 
+    silc_pkcs_save_public_key_data(verify->filename, verify->pk,
                                   verify->pk_len, SILC_PKCS_FILE_PEM);
   } else {
     /* Call the completion */
@@ -1808,7 +2384,7 @@ static void verify_public_key_completion(const char *line, void *context)
       verify->completion(FALSE, verify->context);
 
     printformat_module("fe-common/silc", NULL, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_DISCARD, 
+                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_DISCARD,
                       verify->entity_name ? verify->entity_name :
                       verify->entity);
   }
@@ -1825,10 +2401,10 @@ static void verify_public_key_completion(const char *line, void *context)
    server/router public key this will check for filename that includes the
    remote host's IP address and remote host's hostname. */
 
-static void 
+static void
 silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
-                               const char *name, SilcSocketType conn_type, 
-                               unsigned char *pk, SilcUInt32 pk_len, 
+                               const char *name, SilcSocketType conn_type,
+                               unsigned char *pk, SilcUInt32 pk_len,
                                SilcSKEPKType pk_type,
                                SilcVerifyPublicKey completion, void *context)
 {
@@ -1838,13 +2414,13 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   struct passwd *pw;
   struct stat st;
   char *entity = ((conn_type == SILC_SOCKET_TYPE_SERVER ||
-                  conn_type == SILC_SOCKET_TYPE_ROUTER) ? 
+                  conn_type == SILC_SOCKET_TYPE_ROUTER) ?
                  "server" : "client");
   PublicKeyVerify verify;
 
   if (pk_type != SILC_SKE_PK_TYPE_SILC) {
     printformat_module("fe-common/silc", NULL, NULL,
-                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_UNSUPPORTED, 
+                      MSGLEVEL_CRAP, SILCTXT_PUBKEY_UNSUPPORTED,
                       entity, pk_type);
     if (completion)
       completion(FALSE, context);
@@ -1865,24 +2441,24 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   if (conn_type == SILC_SOCKET_TYPE_SERVER ||
       conn_type == SILC_SOCKET_TYPE_ROUTER) {
     if (!name) {
-      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity, 
+      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity,
               conn->sock->ip, conn->sock->port);
-      snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s", 
+      snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s",
               get_irssi_dir(), entity, file);
-      
-      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity, 
+
+      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity,
               conn->sock->hostname, conn->sock->port);
-      snprintf(filename2, sizeof(filename2) - 1, "%s/%skeys/%s", 
+      snprintf(filename2, sizeof(filename2) - 1, "%s/%skeys/%s",
               get_irssi_dir(), entity, file);
-      
+
       ipf = filename;
       hostf = filename2;
     } else {
-      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity, 
+      snprintf(file, sizeof(file) - 1, "%skey_%s_%d.pub", entity,
               name, conn->sock->port);
-      snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s", 
+      snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s",
               get_irssi_dir(), entity, file);
-      
+
       ipf = filename;
     }
   } else {
@@ -1891,9 +2467,9 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
     for (i = 0; i < strlen(fingerprint); i++)
       if (fingerprint[i] == ' ')
        fingerprint[i] = '_';
-    
+
     snprintf(file, sizeof(file) - 1, "%skey_%s.pub", entity, fingerprint);
-    snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s", 
+    snprintf(filename, sizeof(filename) - 1, "%s/%skeys/%s",
             get_irssi_dir(), entity, file);
     silc_free(fingerprint);
 
@@ -1912,8 +2488,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;
@@ -1923,12 +2498,12 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
   if (stat(ipf, &st) < 0 && (!hostf || stat(hostf, &st) < 0)) {
     /* Key does not exist, ask user to verify the key and save it */
 
-    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
-                      SILCTXT_PUBKEY_RECEIVED,verify->entity_name ? 
+    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                      SILCTXT_PUBKEY_RECEIVED,verify->entity_name ?
                       verify->entity_name : entity);
-    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                       SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
-    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+    printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                       SILCTXT_PUBKEY_BABBLEPRINT, babbleprint);
     format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                             SILCTXT_PUBKEY_ACCEPT);
@@ -1944,22 +2519,22 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
     SilcUInt32 encpk_len;
 
     /* Load the key file, try for both IP filename and hostname filename */
-    if (!silc_pkcs_load_public_key(ipf, &public_key, 
+    if (!silc_pkcs_load_public_key(ipf, &public_key,
                                   SILC_PKCS_FILE_PEM) &&
-       !silc_pkcs_load_public_key(ipf, &public_key, 
+       !silc_pkcs_load_public_key(ipf, &public_key,
                                   SILC_PKCS_FILE_BIN) &&
-       (!hostf || (!silc_pkcs_load_public_key(hostf, &public_key, 
+       (!hostf || (!silc_pkcs_load_public_key(hostf, &public_key,
                                               SILC_PKCS_FILE_PEM) &&
-                   !silc_pkcs_load_public_key(hostf, &public_key, 
+                   !silc_pkcs_load_public_key(hostf, &public_key,
                                               SILC_PKCS_FILE_BIN)))) {
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
-                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ? 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ?
                         verify->entity_name : entity);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_BABBLEPRINT, babbleprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_COULD_NOT_LOAD, entity);
       format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                               SILCTXT_PUBKEY_ACCEPT_ANYWAY);
@@ -1973,14 +2548,14 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
     /* Encode the key data */
     encpk = silc_pkcs_public_key_encode(public_key, &encpk_len);
     if (!encpk) {
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
-                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ? 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ?
                         verify->entity_name : entity);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_BABBLEPRINT, babbleprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_MALFORMED, entity);
       format = format_get_text("fe-common/silc", NULL, NULL, NULL,
                               SILCTXT_PUBKEY_ACCEPT_ANYWAY);
@@ -1993,18 +2568,18 @@ silc_verify_public_key_internal(SilcClient client, SilcClientConnection conn,
 
     /* Compare the keys */
     if (memcmp(encpk, pk, encpk_len)) {
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
-                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ? 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
+                        SILCTXT_PUBKEY_RECEIVED,verify->entity_name ?
                         verify->entity_name : entity);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_FINGERPRINT, entity, fingerprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_BABBLEPRINT, babbleprint);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_NO_MATCH, entity);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_MAYBE_EXPIRED, entity);
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_PUBKEY_MITM_ATTACK, entity);
 
       /* Ask user to verify the key and save it */
@@ -2021,17 +2596,22 @@ 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);
   }
 }
 
 /* Verifies received public key. The `conn_type' indicates which entity
    (server, client etc.) has sent the public key. If user decides to trust
-   the key may be saved as trusted public key for later use. The 
+   the key may be saved as trusted public key for later use. The
    `completion' must be called after the public key has been verified. */
 
-void 
+void
 silc_verify_public_key(SilcClient client, SilcClientConnection conn,
-                      SilcSocketType conn_type, unsigned char *pk, 
+                      SilcSocketType conn_type, unsigned char *pk,
                       SilcUInt32 pk_len, SilcSKEPKType pk_type,
                       SilcVerifyPublicKey completion, void *context)
 {
@@ -2052,7 +2632,7 @@ 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, 
+  p->completion((unsigned char *)passphrase,
                passphrase ? strlen(passphrase) : 0, p->context);
   silc_free(p);
 }
@@ -2102,7 +2682,7 @@ static void silc_get_auth_method_callback(SilcClient client,
        (*internal->completion)(TRUE, auth_meth, NULL, 0, internal->context);
        break;
       }
-      
+
       (*internal->completion)(TRUE, auth_meth, setup->password,
                              strlen(setup->password), internal->context);
     }
@@ -2140,7 +2720,7 @@ void silc_get_auth_method(SilcClient client, SilcClientConnection conn,
   internal->completion = completion;
   internal->context = context;
 
-  silc_client_request_authentication_method(client, conn, 
+  silc_client_request_authentication_method(client, conn,
                                            silc_get_auth_method_callback,
                                            internal);
 }
@@ -2152,40 +2732,40 @@ void silc_get_auth_method(SilcClient client, SilcClientConnection conn,
    must explicitly cast it to correct type.  Usually `failure' is 32 bit
    failure type (see protocol specs for all protocol failure types). */
 
-void silc_failure(SilcClient client, SilcClientConnection conn, 
+void silc_failure(SilcClient client, SilcClientConnection conn,
                  SilcProtocol protocol, void *failure)
 {
   SILC_LOG_DEBUG(("Start"));
 
   if (protocol->protocol->type == SILC_PROTOCOL_CLIENT_KEY_EXCHANGE) {
     SilcSKEStatus status = (SilcSKEStatus)failure;
-    
+
     if (status == SILC_SKE_STATUS_BAD_VERSION)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_BAD_VERSION);
     if (status == SILC_SKE_STATUS_UNSUPPORTED_PUBLIC_KEY)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNSUPPORTED_PUBLIC_KEY);
     if (status == SILC_SKE_STATUS_UNKNOWN_GROUP)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNKNOWN_GROUP);
     if (status == SILC_SKE_STATUS_UNKNOWN_CIPHER)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNKNOWN_CIPHER);
     if (status == SILC_SKE_STATUS_UNKNOWN_PKCS)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNKNOWN_PKCS);
     if (status == SILC_SKE_STATUS_UNKNOWN_HASH_FUNCTION)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNKNOWN_HASH_FUNCTION);
     if (status == SILC_SKE_STATUS_UNKNOWN_HMAC)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_UNKNOWN_HMAC);
     if (status == SILC_SKE_STATUS_INCORRECT_SIGNATURE)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_INCORRECT_SIGNATURE);
     if (status == SILC_SKE_STATUS_INVALID_COOKIE)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_KE_INVALID_COOKIE);
   }
 
@@ -2193,7 +2773,7 @@ void silc_failure(SilcClient client, SilcClientConnection conn,
     SilcUInt32 err = (SilcUInt32)failure;
 
     if (err == SILC_AUTH_FAILED)
-      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP, 
+      printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
                         SILCTXT_AUTH_FAILED);
   }
 }
@@ -2217,7 +2797,7 @@ bool silc_key_agreement(SilcClient client, SilcClientConnection conn,
   /* We will just display the info on the screen and return FALSE and user
      will have to start the key agreement with a command. */
 
-  if (hostname) 
+  if (hostname)
     snprintf(portstr, sizeof(portstr) - 1, "%d", port);
 
   if (!hostname)
@@ -2225,7 +2805,7 @@ bool silc_key_agreement(SilcClient client, SilcClientConnection conn,
                       SILCTXT_KEY_AGREEMENT_REQUEST, client_entry->nickname);
   else
     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
-                      SILCTXT_KEY_AGREEMENT_REQUEST_HOST, 
+                      SILCTXT_KEY_AGREEMENT_REQUEST_HOST,
                       client_entry->nickname, hostname, portstr);
 
   *completion = NULL;
@@ -2271,7 +2851,7 @@ void silc_ftp(SilcClient client, SilcClientConnection conn,
     server->current_session = ftp;
   }
 
-  if (hostname) 
+  if (hostname)
     snprintf(portstr, sizeof(portstr) - 1, "%d", port);
 
   if (!hostname)
@@ -2279,7 +2859,7 @@ void silc_ftp(SilcClient client, SilcClientConnection conn,
                       SILCTXT_FILE_REQUEST, client_entry->nickname);
   else
     printformat_module("fe-common/silc", NULL, NULL, MSGLEVEL_CRAP,
-                      SILCTXT_FILE_REQUEST_HOST, 
+                      SILCTXT_FILE_REQUEST_HOST,
                       client_entry->nickname, hostname, portstr);
 }