Merged from silc_1_0_branch.
[silc.git] / lib / silcclient / command.c
index c0c058d00aa865054db8f0cd17ff918ed946c2ea..3131900efdc680fb6379fb6dafc5765c79639bef 100644 (file)
           "You are not connected to a server, use /SERVER to connect");
 
 /* Command operation that is called at the end of all commands. 
-   Usage: COMMAND; */
-#define COMMAND cmd->client->internal->ops->command(cmd->client, cmd->conn, \
-  cmd, TRUE, cmd->command->cmd)
+   Usage: COMMAND(status); */
+#define COMMAND(status) cmd->client->internal->ops->command(cmd->client, \
+  cmd->conn, cmd, TRUE, cmd->command->cmd, (status))
 
-/* Error to application. Usage: COMMAND_ERROR; */
-#define COMMAND_ERROR cmd->client->internal->ops->command(cmd->client, \
-  cmd->conn, cmd, FALSE, cmd->command->cmd)
+/* Error to application. Usage: COMMAND_ERROR(status); */
+#define COMMAND_ERROR(status)                          \
+  cmd->client->internal->ops->command(cmd->client,     \
+  cmd->conn, cmd, FALSE, cmd->command->cmd, (status))
 
 #define SAY cmd->client->internal->ops->say
 
@@ -47,6 +48,8 @@ void silc_client_command_send(SilcClient client, SilcClientConnection conn,
   SilcBuffer packet;
   va_list ap;
 
+  assert(client && conn);
+
   va_start(ap, argc);
 
   packet = silc_command_payload_encode_vap(command, ident, argc, ap);
@@ -64,32 +67,103 @@ SilcClientCommand silc_client_command_find(SilcClient client,
 {
   SilcClientCommand cmd;
 
+  assert(client);
+
   silc_list_start(client->internal->commands);
   while ((cmd = silc_list_get(client->internal->commands)) != SILC_LIST_END) {
-    if (cmd->name && !strcmp(cmd->name, name))
+    if (cmd->name && !strcasecmp(cmd->name, name))
       return cmd;
   }
 
   return NULL;
 }
 
-/* Calls the command (executes it).  Application can call this after
-   it has allocated the SilcClientCommandContext with the function
-   silc_client_command_alloc and found the command from the client
-   library by calling silc_client_command_find.  This will execute
-   the command. */
+/* Executes a command */
 
-void silc_client_command_call(SilcClientCommand command, 
-                             SilcClientCommandContext cmd)
+bool silc_client_command_call(SilcClient client,
+                             SilcClientConnection conn,
+                             const char *command_line, ...)
 {
-  (*command->command)((void *)cmd, NULL);
+  va_list va;
+  SilcUInt32 argc = 0;
+  unsigned char **argv = NULL;
+  SilcUInt32 *argv_lens = NULL, *argv_types = NULL;
+  SilcClientCommand cmd;
+  SilcClientCommandContext ctx;
+  char *arg;
+
+  assert(client);
+
+  /* Parse arguments */
+  va_start(va, command_line);
+  if (command_line) {
+    char *command_name;
+
+    /* Get command name */
+    command_name = silc_memdup(command_line, strcspn(command_line, " "));
+    if (!command_name)
+      return FALSE;
+
+    /* Find command by name */
+    cmd = silc_client_command_find(client, command_name);
+    if (!cmd) {
+      silc_free(command_name);
+      return FALSE;
+    }
+
+    /* Parse command line */
+    silc_parse_command_line((char *)command_line, &argv, &argv_lens,
+                           &argv_types, &argc, cmd->max_args);
+
+    silc_free(command_name);
+  } else {
+    arg = va_arg(va, char *);
+    if (!arg)
+      return FALSE;
+
+    /* Find command by name */
+    cmd = silc_client_command_find(client, arg);
+    if (!cmd)
+      return FALSE;
+
+    while (arg) {
+      argv = silc_realloc(argv, sizeof(*argv) * (argc + 1));
+      argv_lens = silc_realloc(argv_lens, sizeof(*argv_lens) * (argc + 1));
+      argv_types = silc_realloc(argv_types, sizeof(*argv_types) * (argc + 1));
+      argv[argc] = silc_memdup(arg, strlen(arg));
+      argv_lens[argc] = strlen(arg);
+      argv_types[argc] = argc;
+      argc++;
+      arg = va_arg(va, char *);
+    }
+  }
+
+  /* Allocate command context. */
+  ctx = silc_client_command_alloc();
+  ctx->client = client;
+  ctx->conn = conn;
+  ctx->command = cmd;
+  ctx->argc = argc;
+  ctx->argv = argv;
+  ctx->argv_lens = argv_lens;
+  ctx->argv_types = argv_types;
+  
+  /* Call the command */
+  cmd->command(ctx, NULL);
+
+  va_end(va);
+
+  return TRUE;
 }
 
 /* Add new pending command to be executed when reply to a command has been
-   received.  The `reply_cmd' is the command that will call the `callback'
-   with `context' when reply has been received.  If `ident is non-zero
-   the `callback' will be executed when received reply with command 
-   identifier `ident'. */
+   received. The `reply_cmd' is the command that will call the `callback'
+   with `context' when reply has been received.  It can be SILC_COMMAND_NONE
+   to match any command with the `ident'.  If `ident' is non-zero
+   the `callback' will be executed when received reply with command
+   identifier `ident'. If there already exists pending command for the
+   specified command, ident, callback and context this function has no
+   effect. */
 
 void silc_client_command_pending(SilcClientConnection conn,
                                 SilcCommand reply_cmd,
@@ -99,12 +173,13 @@ void silc_client_command_pending(SilcClientConnection conn,
 {
   SilcClientCommandPending *reply;
 
+  assert(conn);
   reply = silc_calloc(1, sizeof(*reply));
   reply->reply_cmd = reply_cmd;
   reply->ident = ident;
   reply->context = context;
   reply->callback = callback;
-  silc_dlist_add(conn->pending_commands, reply);
+  silc_dlist_add(conn->internal->pending_commands, reply);
 }
 
 /* Deletes pending command by reply command type. */
@@ -115,36 +190,51 @@ void silc_client_command_pending_del(SilcClientConnection conn,
 {
   SilcClientCommandPending *r;
 
-  silc_dlist_start(conn->pending_commands);
-  while ((r = silc_dlist_get(conn->pending_commands)) != SILC_LIST_END) {
-    if (r->reply_cmd == reply_cmd && r->ident == ident) {
-      silc_dlist_del(conn->pending_commands, r);
-      break;
+  if (!conn->internal->pending_commands)
+    return;
+
+  silc_dlist_start(conn->internal->pending_commands);
+  while ((r = silc_dlist_get(conn->internal->pending_commands))
+        != SILC_LIST_END) {
+    if ((r->reply_cmd == reply_cmd || (r->reply_cmd == SILC_COMMAND_NONE &&
+                                      r->reply_check))
+       && r->ident == ident) {
+      silc_dlist_del(conn->internal->pending_commands, r);
+      silc_free(r);
     }
   }
 }
 
 /* Checks for pending commands and marks callbacks to be called from
-   the command reply function. Returns TRUE if there were pending command. */
-
-int silc_client_command_pending_check(SilcClientConnection conn,
-                                     SilcClientCommandReplyContext ctx,
-                                     SilcCommand command, 
-                                     SilcUInt16 ident)
+   the command reply function. */
+
+SilcClientCommandPendingCallbacks
+silc_client_command_pending_check(SilcClientConnection conn,
+                                 SilcClientCommandReplyContext ctx,
+                                 SilcCommand command, 
+                                 SilcUInt16 ident,
+                                 SilcUInt32 *callbacks_count)
 {
   SilcClientCommandPending *r;
-
-  silc_dlist_start(conn->pending_commands);
-  while ((r = silc_dlist_get(conn->pending_commands)) != SILC_LIST_END) {
-    if (r->reply_cmd == command && r->ident == ident) {
-      ctx->context = r->context;
-      ctx->callback = r->callback;
+  SilcClientCommandPendingCallbacks callbacks = NULL;
+  int i = 0;
+
+  silc_dlist_start(conn->internal->pending_commands);
+  while ((r = silc_dlist_get(conn->internal->pending_commands))
+        != SILC_LIST_END) {
+    if ((r->reply_cmd == command || r->reply_cmd == SILC_COMMAND_NONE)
+       && r->ident == ident) {
+      callbacks = silc_realloc(callbacks, sizeof(*callbacks) * (i + 1));
+      callbacks[i].context = r->context;
+      callbacks[i].callback = r->callback;
+      r->reply_check = TRUE;
       ctx->ident = ident;
-      return TRUE;
+      i++;
     }
   }
 
-  return FALSE;
+  *callbacks_count = i;
+  return callbacks;
 }
 
 /* Allocate Command Context */
@@ -168,6 +258,7 @@ void silc_client_command_free(SilcClientCommandContext ctx)
 
     for (i = 0; i < ctx->argc; i++)
       silc_free(ctx->argv[i]);
+    silc_free(ctx->argv);
     silc_free(ctx->argv_lens);
     silc_free(ctx->argv_types);
     silc_free(ctx);
@@ -192,11 +283,12 @@ SILC_CLIENT_CMD_FUNC(whois)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
-  SilcBuffer buffer;
+  SilcBuffer buffer, attrs = NULL;
+  unsigned char count[4], *tmp = NULL;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -205,25 +297,40 @@ SILC_CLIENT_CMD_FUNC(whois)
     buffer = silc_id_payload_encode(cmd->conn->local_id, SILC_ID_CLIENT);
     silc_client_command_send(cmd->client, cmd->conn, SILC_COMMAND_WHOIS, 
                             ++conn->cmd_ident,
-                            1, 3, buffer->data, buffer->len);
+                            1, 4, buffer->data, buffer->len);
     silc_buffer_free(buffer);
     goto out;
   }
 
-  buffer = silc_command_payload_encode(SILC_COMMAND_WHOIS,
-                                      cmd->argc - 1, ++cmd->argv,
-                                      ++cmd->argv_lens, ++cmd->argv_types,
-                                      0);
+  if (cmd->argc == 2) {
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
+                                           ++conn->cmd_ident, 1,
+                                           1, cmd->argv[1], 
+                                           cmd->argv_lens[1]);
+  } else {
+    if (!strcasecmp(cmd->argv[2], "-details"))
+      attrs = silc_client_attributes_request(0);
+
+    if (!attrs || cmd->argc > 3) {
+      int c = atoi(cmd->argc > 3 ? cmd->argv[3] : cmd->argv[2]);
+      SILC_PUT32_MSB(c, count);
+      tmp = count;
+    }
+
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
+                                           ++conn->cmd_ident, 3,
+                                           1, cmd->argv[1], cmd->argv_lens[1],
+                                           2, tmp ? tmp : NULL, tmp ? 4 : 0,
+                                           3, attrs ? attrs->data : NULL,
+                                           attrs ? attrs->len : 0);
+  }
   silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
-  cmd->argv--;
-  cmd->argv_lens--;
-  cmd->argv_types--;
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -237,34 +344,43 @@ SILC_CLIENT_CMD_FUNC(whowas)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
+  unsigned char count[4];
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2 || cmd->argc > 3) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /WHOWAS <nickname>[@<server>] [<count>]");
-    COMMAND_ERROR;
+    COMMAND_ERROR((cmd->argc < 2 ? SILC_STATUS_ERR_NOT_ENOUGH_PARAMS :
+                  SILC_STATUS_ERR_TOO_MANY_PARAMS));
     goto out;
   }
 
-  buffer = silc_command_payload_encode(SILC_COMMAND_WHOWAS,
-                                      cmd->argc - 1, ++cmd->argv,
-                                      ++cmd->argv_lens, ++cmd->argv_types,
-                                      0);
+  if (cmd->argc == 2) {
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOWAS,
+                                           ++conn->cmd_ident, 1,
+                                           1, cmd->argv[1], 
+                                           cmd->argv_lens[1]);
+  } else {
+    int c = atoi(cmd->argv[2]);
+    memset(count, 0, sizeof(count));
+    SILC_PUT32_MSB(c, count);
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOWAS,
+                                           ++conn->cmd_ident, 2,
+                                           1, cmd->argv[1], cmd->argv_lens[1],
+                                           2, count, sizeof(count));
+  }
   silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
-  cmd->argv--;
-  cmd->argv_lens--;
-  cmd->argv_types--;
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -281,27 +397,32 @@ SILC_CLIENT_CMD_FUNC(identify)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
+  unsigned char count[4];
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2 || cmd->argc > 3)
     goto out;
 
-  if (cmd->argc == 2)
+  if (cmd->argc == 2) {
     buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY, 
                                            ++conn->cmd_ident, 1,
                                            1, cmd->argv[1],
                                            cmd->argv_lens[1]);
-  else
+  } else {
+    int c = atoi(cmd->argv[2]);
+    memset(count, 0, sizeof(count));
+    SILC_PUT32_MSB(c, count);
     buffer = silc_command_payload_encode_va(SILC_COMMAND_IDENTIFY, 
                                            ++conn->cmd_ident, 2,
                                            1, cmd->argv[1],
                                            cmd->argv_lens[1],
-                                           4, cmd->argv[2],
-                                           cmd->argv_lens[2]);
+                                           4, count, sizeof(count));
+  }
 
   silc_client_packet_send(cmd->client, cmd->conn->sock,
                          SILC_PACKET_COMMAND, NULL, 0, NULL, NULL,
@@ -312,36 +433,6 @@ SILC_CLIENT_CMD_FUNC(identify)
   silc_client_command_free(cmd);
 }
 
-/* Pending callbcak that will be called after the NICK command was
-   replied by the server.  This sets the nickname if there were no
-   errors. */
-
-SILC_CLIENT_CMD_FUNC(nick_change)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientConnection conn = cmd->conn;
-  SilcClientCommandReplyContext reply = 
-    (SilcClientCommandReplyContext)context2;
-  SilcCommandStatus status = silc_command_get_status(reply->payload);
-
-  if (status == SILC_STATUS_OK) {
-    /* Set the nickname */
-    silc_idcache_del_by_context(conn->client_cache, conn->local_entry);
-    if (conn->nickname)
-      silc_free(conn->nickname);
-    conn->nickname = strdup(cmd->argv[1]);
-    conn->local_entry->nickname = conn->nickname;
-    silc_client_nickname_format(cmd->client, conn, conn->local_entry);
-    silc_idcache_add(conn->client_cache, strdup(cmd->argv[1]), 
-                    conn->local_entry->id, conn->local_entry, 0, NULL);
-    COMMAND;
-  } else {
-    COMMAND_ERROR;
-  }
-
-  silc_client_command_free(cmd);
-}
-
 /* Command NICK. Shows current nickname/sets new nickname on current
    window. */
 
@@ -353,14 +444,14 @@ SILC_CLIENT_CMD_FUNC(nick)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /NICK <nickname>");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -378,7 +469,7 @@ SILC_CLIENT_CMD_FUNC(nick)
          "Your nickname is %s", conn->nickname);
     }
 
-    COMMAND;
+    COMMAND(SILC_STATUS_OK);
     goto out;
   }
 
@@ -396,14 +487,6 @@ SILC_CLIENT_CMD_FUNC(nick)
                          buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
-  /* Register pending callback that will actually set the new nickname
-     if there were no errors returned by the server. */
-  silc_client_command_pending(conn, SILC_COMMAND_NICK, 
-                             cmd->conn->cmd_ident,
-                             silc_client_command_nick_change,
-                             silc_client_command_dup(cmd));
-  cmd->pending = TRUE;
-
  out:
   silc_client_command_free(cmd);
 }
@@ -421,7 +504,7 @@ SILC_CLIENT_CMD_FUNC(list)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -429,7 +512,8 @@ SILC_CLIENT_CMD_FUNC(list)
     name = cmd->argv[1];
 
     /* Get the Channel ID of the channel */
-    if (silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) {
+    if (silc_idcache_find_by_name_one(conn->internal->channel_cache,
+                                     name, &id_cache)) {
       channel = (SilcChannelEntry)id_cache->context;
       idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL);
     }
@@ -451,7 +535,7 @@ SILC_CLIENT_CMD_FUNC(list)
     silc_buffer_free(idp);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -470,22 +554,21 @@ SILC_CLIENT_CMD_FUNC(topic)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2 || cmd->argc > 3) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO,
        "Usage: /TOPIC <channel> [<topic>]");
-    COMMAND_ERROR;
+    COMMAND_ERROR((cmd->argc < 2 ? SILC_STATUS_ERR_NOT_ENOUGH_PARAMS :
+                  SILC_STATUS_ERR_TOO_MANY_PARAMS));
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
     name = conn->current_channel->channel_name;
@@ -494,17 +577,14 @@ SILC_CLIENT_CMD_FUNC(topic)
   }
 
   if (!conn->current_channel) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "You are not on that channel");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
     goto out;
   }
 
   /* Get the Channel ID of the channel */
-  if (!silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "You are not on that channel");
-    COMMAND_ERROR;
+  if (!silc_idcache_find_by_name_one(conn->internal->channel_cache,
+                                    name, &id_cache)) {
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
     goto out;
   }
 
@@ -528,7 +608,7 @@ SILC_CLIENT_CMD_FUNC(topic)
   silc_buffer_free(idp);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -544,14 +624,15 @@ SILC_CLIENT_CMD_FUNC(invite)
   SilcClientConnection conn = cmd->conn;
   SilcClientEntry client_entry = NULL;
   SilcChannelEntry channel;
-  SilcBuffer buffer, clidp, chidp;
-  SilcUInt32 type = 0;
+  SilcBuffer buffer, clidp, chidp, args = NULL;
+  SilcPublicKey pubkey = NULL;
   char *nickname = NULL, *name;
   char *invite = NULL;
+  unsigned char action[1];
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -559,15 +640,13 @@ SILC_CLIENT_CMD_FUNC(invite)
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO,
        "Usage: /INVITE <channel> [<nickname>[@server>]"
        "[+|-[<nickname>[@<server>[!<username>[@hostname>]]]]]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
 
@@ -577,9 +656,7 @@ SILC_CLIENT_CMD_FUNC(invite)
 
     channel = silc_client_get_channel(cmd->client, conn, name);
     if (!channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are on that channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
   }
@@ -597,7 +674,7 @@ SILC_CLIENT_CMD_FUNC(invite)
                                            cmd->argv[2], TRUE);
       if (!client_entry) {
        if (cmd->pending) {
-         COMMAND_ERROR;
+         COMMAND_ERROR(SILC_STATUS_ERR_NO_SUCH_NICK);
          goto out;
        }
       
@@ -611,12 +688,35 @@ SILC_CLIENT_CMD_FUNC(invite)
        goto out;
       }
     } else {
-      invite = cmd->argv[2];
-      invite++;
       if (cmd->argv[2][0] == '+')
-       type = 3;
+       action[0] = 0x00;
       else
-       type = 4;
+       action[0] = 0x01;
+
+      /* Check if it is public key file to be added to invite list */
+      if (!silc_pkcs_load_public_key(cmd->argv[2] + 1, &pubkey,
+                                    SILC_PKCS_FILE_PEM))
+       silc_pkcs_load_public_key(cmd->argv[2] + 1, &pubkey,
+                                 SILC_PKCS_FILE_BIN);
+      invite = cmd->argv[2];
+      if (!pubkey)
+       invite++;
+    }
+  }
+
+  if (invite) {
+    args = silc_buffer_alloc_size(2);
+    silc_buffer_format(args,
+                      SILC_STR_UI_SHORT(1),
+                      SILC_STR_END);
+    if (pubkey) {
+      chidp = silc_pkcs_public_key_payload_encode(pubkey);
+      args = silc_argument_payload_encode_one(args, chidp->data,
+                                             chidp->len, 2);
+      silc_buffer_free(chidp);
+      silc_pkcs_public_key_free(pubkey);
+    } else {
+      args = silc_argument_payload_encode_one(args, invite, strlen(invite), 1);
     }
   }
 
@@ -625,27 +725,32 @@ SILC_CLIENT_CMD_FUNC(invite)
   if (client_entry) {
     clidp = silc_id_payload_encode(client_entry->id, SILC_ID_CLIENT);
     buffer = silc_command_payload_encode_va(SILC_COMMAND_INVITE, 
-                                           ++conn->cmd_ident, 3,
+                                           ++conn->cmd_ident, 4,
                                            1, chidp->data, chidp->len,
                                            2, clidp->data, clidp->len,
-                                           type, invite, invite ?
-                                           strlen(invite) : 0);
+                                           3, args ? action : NULL,
+                                           args ? 1 : 0,
+                                           4, args ? args->data : NULL,
+                                           args ? args->len : 0);
     silc_buffer_free(clidp);
   } else {
     buffer = silc_command_payload_encode_va(SILC_COMMAND_INVITE, 
-                                           ++conn->cmd_ident, 2,
+                                           ++conn->cmd_ident, 3,
                                            1, chidp->data, chidp->len,
-                                           type, invite, invite ?
-                                           strlen(invite) : 0);
+                                           3, args ? action : NULL,
+                                           args ? 1 : 0,
+                                           4, args ? args->data : NULL,
+                                           args ? args->len : 0);
   }
 
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   silc_buffer_free(chidp);
+  silc_buffer_free(args),
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_free(nickname);
@@ -662,8 +767,8 @@ SILC_TASK_CALLBACK(silc_client_command_quit_cb)
   QuitInternal q = (QuitInternal)context;
 
   /* Close connection */
-  q->client->internal->ops->disconnect(q->client, q->conn);
-  silc_client_close_connection(q->client, NULL, q->conn->sock->user_data);
+  q->client->internal->ops->disconnected(q->client, q->conn, 0, NULL);
+  silc_client_close_connection(q->client, q->conn->sock->user_data);
 
   silc_free(q);
 }
@@ -678,7 +783,7 @@ SILC_CLIENT_CMD_FUNC(quit)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -707,7 +812,7 @@ SILC_CLIENT_CMD_FUNC(quit)
                         1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -748,8 +853,9 @@ SILC_CLIENT_CMD_FUNC(kill_remove)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientCommandReplyContext reply = 
     (SilcClientCommandReplyContext)context2;
-  SilcCommandStatus status = silc_command_get_status(reply->payload);
+  SilcStatus status;
 
+  silc_command_get_status(reply->payload, &status, NULL);
   if (status == SILC_STATUS_OK) {
     /* Remove with timeout */
     silc_schedule_task_add(cmd->client->schedule, cmd->conn->sock->sock,
@@ -769,20 +875,20 @@ SILC_CLIENT_CMD_FUNC(kill)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClient client = cmd->client;
   SilcClientConnection conn = cmd->conn;
-  SilcBuffer buffer, idp;
+  SilcBuffer buffer, idp, auth = NULL;
   SilcClientEntry target;
-  char *nickname = NULL;
+  char *nickname = NULL, *comment = NULL;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "Usage: /KILL <nickname> [<comment>]");
-    COMMAND_ERROR;
+       "Usage: /KILL <nickname> [<comment>] [-pubkey]");
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -797,7 +903,7 @@ SILC_CLIENT_CMD_FUNC(kill)
                                  cmd->argv[1], TRUE);
   if (!target) {
     if (cmd->pending) {
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NO_SUCH_NICK);
       goto out;
     }
 
@@ -811,25 +917,38 @@ SILC_CLIENT_CMD_FUNC(kill)
     goto out;
   }
 
+  if (cmd->argc >= 3) {
+    if (strcasecmp(cmd->argv[2], "-pubkey"))
+      comment = cmd->argv[2];
+
+    if (!strcasecmp(cmd->argv[2], "-pubkey") ||
+       (cmd->argc >= 4 && !strcasecmp(cmd->argv[3], "-pubkey"))) {
+      /* Encode the public key authentication payload */
+      auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
+                                               cmd->client->private_key,
+                                               cmd->client->rng,
+                                               client->sha1hash,
+                                               target->id, SILC_ID_CLIENT);
+    }
+  }
+
   /* Send the KILL command to the server */
   idp = silc_id_payload_encode(target->id, SILC_ID_CLIENT);
-  if (cmd->argc == 2)
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_KILL, 
-                                           ++conn->cmd_ident, 1, 
-                                           1, idp->data, idp->len);
-  else
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_KILL, 
-                                           ++conn->cmd_ident, 2, 
-                                           1, idp->data, idp->len,
-                                           2, cmd->argv[2], 
-                                           strlen(cmd->argv[2]));
+  buffer =
+    silc_command_payload_encode_va(SILC_COMMAND_KILL, 
+                                  ++conn->cmd_ident, 3, 
+                                  1, idp->data, idp->len,
+                                  2, comment, comment ? strlen(comment) : 0,
+                                  3, auth ? auth->data : NULL,
+                                  auth ? auth->len : 0);
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   silc_buffer_free(idp);
+  silc_buffer_free(auth);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
   /* Register a pending callback that will actually remove the killed
      client from our cache. */
@@ -854,7 +973,7 @@ SILC_CLIENT_CMD_FUNC(info)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -875,7 +994,39 @@ SILC_CLIENT_CMD_FUNC(info)
     silc_free(name);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
+
+ out:
+  silc_client_command_free(cmd);
+}
+
+/* Command STATS. Shows server and network statistics. */
+
+SILC_CLIENT_CMD_FUNC(stats)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcBuffer buffer, idp = NULL;
+
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
+    goto out;
+  }
+
+  idp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER); 
+  
+  /* Send the command */
+  buffer = silc_command_payload_encode_va(SILC_COMMAND_STATS,
+                                         ++conn->cmd_ident, 1,
+                                         SILC_ID_SERVER, idp->data, idp->len);
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
+                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
+  silc_buffer_free(buffer);
+  silc_buffer_free(idp);
+
+  /* Notify application */
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -888,53 +1039,56 @@ SILC_CLIENT_CMD_FUNC(ping)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
-  SilcBuffer buffer;
+  SilcBuffer buffer, idp;
   void *id;
   int i;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
+  idp = silc_id_payload_encode(conn->remote_id, SILC_ID_SERVER); 
+
   /* Send the command */
   buffer = silc_command_payload_encode_va(SILC_COMMAND_PING, 0, 1, 
-                                         1, conn->remote_id_data, 
-                                         silc_id_get_len(conn->remote_id,
-                                                         SILC_ID_SERVER));
+                                         1, idp->data, idp->len);
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
+  silc_buffer_free(idp);
 
   id = silc_id_str2id(conn->remote_id_data, conn->remote_id_data_len,
                      SILC_ID_SERVER);
   if (!id) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   /* Start counting time */
-  for (i = 0; i < conn->ping_count; i++) {
-    if (conn->ping[i].dest_id == NULL) {
-      conn->ping[i].start_time = time(NULL);
-      conn->ping[i].dest_id = id;
-      conn->ping[i].dest_name = strdup(conn->remote_host);
+  for (i = 0; i < conn->internal->ping_count; i++) {
+    if (conn->internal->ping[i].dest_id == NULL) {
+      conn->internal->ping[i].start_time = time(NULL);
+      conn->internal->ping[i].dest_id = id;
+      conn->internal->ping[i].dest_name = strdup(conn->remote_host);
       break;
     }
   }
-  if (i >= conn->ping_count) {
-    i = conn->ping_count;
-    conn->ping = silc_realloc(conn->ping, sizeof(*conn->ping) * (i + 1));
-    conn->ping[i].start_time = time(NULL);
-    conn->ping[i].dest_id = id;
-    conn->ping[i].dest_name = strdup(conn->remote_host);
-    conn->ping_count++;
+  if (i >= conn->internal->ping_count) {
+    i = conn->internal->ping_count;
+    conn->internal->ping =
+      silc_realloc(conn->internal->ping,
+                  sizeof(*conn->internal->ping) * (i + 1));
+    conn->internal->ping[i].start_time = time(NULL);
+    conn->internal->ping[i].dest_id = id;
+    conn->internal->ping[i].dest_name = strdup(conn->remote_host);
+    conn->internal->ping_count++;
   }
   
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -948,17 +1102,17 @@ SILC_CLIENT_CMD_FUNC(join)
   SilcClientConnection conn = cmd->conn;
   SilcChannelEntry channel;
   SilcBuffer buffer, idp, auth = NULL;
-  char *name, *passphrase = NULL, *cipher = NULL, *hmac = NULL;
-  int i;
+  char *name, *passphrase = NULL, *pu8, *cipher = NULL, *hmac = NULL;
+  int i, passphrase_len = 0;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   
@@ -981,21 +1135,27 @@ SILC_CLIENT_CMD_FUNC(join)
     } else if (!strcasecmp(cmd->argv[i], "-hmac") && cmd->argc > i + 1) {
       hmac = cmd->argv[i + 1];
       i++;
-    } else if (!strcasecmp(cmd->argv[i], "-founder") && cmd->argc > i + 1) {
-      if (!strcasecmp(cmd->argv[i + 1], "-pubkey")) {
-       auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
-                                                 cmd->client->private_key,
-                                                 cmd->client->rng, conn->hash,
-                                                 conn->local_id,
-                                                 SILC_ID_CLIENT);
-      } else {
-       auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0,
-                                       cmd->argv[i + 1], 
-                                       cmd->argv_lens[i + 1]);
-      }
+    } else if (!strcasecmp(cmd->argv[i], "-founder")) {
+      auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
+                                               cmd->client->private_key,
+                                               cmd->client->rng, 
+                                               cmd->client->sha1hash,
+                                               conn->local_id,
+                                               SILC_ID_CLIENT);
       i++;
     } else {
-      passphrase = cmd->argv[i];
+      /* Passphrases must be UTF-8 encoded, so encode if it is not */
+      if (!silc_utf8_valid(cmd->argv[i], cmd->argv_lens[i])) {
+       passphrase_len = silc_utf8_encoded_len(cmd->argv[i], 
+                                              cmd->argv_lens[i], 0);
+       pu8 = silc_calloc(passphrase_len, sizeof(*pu8));
+       passphrase_len = silc_utf8_encode(cmd->argv[i], cmd->argv_lens[i],
+                                         0, pu8, passphrase_len);
+       passphrase = pu8;
+      } else {
+       passphrase = strdup(cmd->argv[i]);
+       passphrase_len = cmd->argv_lens[i];
+      }
     }
   }
 
@@ -1004,8 +1164,7 @@ SILC_CLIENT_CMD_FUNC(join)
     silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 6,
                                   1, name, strlen(name),
                                   2, idp->data, idp->len,
-                                  3, passphrase, 
-                                  passphrase ? strlen(passphrase) : 0,
+                                  3, passphrase, passphrase_len,
                                   4, cipher, cipher ? strlen(cipher) : 0,
                                   5, hmac, hmac ? strlen(hmac) : 0,
                                   6, auth ? auth->data : NULL,
@@ -1016,9 +1175,10 @@ SILC_CLIENT_CMD_FUNC(join)
   silc_buffer_free(idp);
   if (auth)
     silc_buffer_free(auth);
+  silc_free(passphrase);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -1034,14 +1194,15 @@ SILC_CLIENT_CMD_FUNC(motd)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 1 || cmd->argc > 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO,
        "Usage: /MOTD [<server>]");
-    COMMAND_ERROR;
+    COMMAND_ERROR((cmd->argc < 1 ? SILC_STATUS_ERR_NOT_ENOUGH_PARAMS :
+                  SILC_STATUS_ERR_TOO_MANY_PARAMS));
     goto out;
   }
 
@@ -1059,7 +1220,7 @@ SILC_CLIENT_CMD_FUNC(motd)
   silc_buffer_free(buffer);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -1079,14 +1240,14 @@ SILC_CLIENT_CMD_FUNC(umode)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /UMODE +|-<modes>");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1108,6 +1269,14 @@ SILC_CLIENT_CMD_FUNC(umode)
        mode = 0;
        mode |= SILC_UMODE_SERVER_OPERATOR;
        mode |= SILC_UMODE_ROUTER_OPERATOR;
+       mode |= SILC_UMODE_GONE;
+       mode |= SILC_UMODE_INDISPOSED;
+       mode |= SILC_UMODE_BUSY;
+       mode |= SILC_UMODE_PAGE;
+       mode |= SILC_UMODE_HYPER;
+       mode |= SILC_UMODE_ROBOT;
+       mode |= SILC_UMODE_BLOCK_PRIVMSG;
+       mode |= SILC_UMODE_REJECT_WATCHING;
       } else {
        mode = SILC_UMODE_NONE;
       }
@@ -1130,8 +1299,56 @@ SILC_CLIENT_CMD_FUNC(umode)
       else
        mode &= ~SILC_UMODE_GONE;
       break;
+    case 'i':
+      if (add)
+       mode |= SILC_UMODE_INDISPOSED;
+      else
+       mode &= ~SILC_UMODE_INDISPOSED;
+      break;
+    case 'b':
+      if (add)
+       mode |= SILC_UMODE_BUSY;
+      else
+       mode &= ~SILC_UMODE_BUSY;
+      break;
+    case 'p':
+      if (add)
+       mode |= SILC_UMODE_PAGE;
+      else
+       mode &= ~SILC_UMODE_PAGE;
+      break;
+    case 'h':
+      if (add)
+       mode |= SILC_UMODE_HYPER;
+      else
+       mode &= ~SILC_UMODE_HYPER;
+      break;
+    case 't':
+      if (add)
+       mode |= SILC_UMODE_ROBOT;
+      else
+       mode &= ~SILC_UMODE_ROBOT;
+      break;
+    case 'P':
+      if (add)
+       mode |= SILC_UMODE_BLOCK_PRIVMSG;
+      else
+       mode &= ~SILC_UMODE_BLOCK_PRIVMSG;
+      break;
+    case 'w':
+      if (add)
+       mode |= SILC_UMODE_REJECT_WATCHING;
+      else
+       mode &= ~SILC_UMODE_REJECT_WATCHING;
+      break;
+    case 'I':
+      if (add)
+       mode |= SILC_UMODE_BLOCK_INVITE;
+      else
+       mode &= ~SILC_UMODE_BLOCK_INVITE;
+      break;
     default:
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_UNKNOWN_MODE);
       goto out;
       break;
     }
@@ -1152,7 +1369,7 @@ SILC_CLIENT_CMD_FUNC(umode)
   silc_buffer_free(idp);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -1167,29 +1384,27 @@ SILC_CLIENT_CMD_FUNC(cmode)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcChannelEntry channel;
-  SilcBuffer buffer, chidp, auth = NULL;
+  SilcBuffer buffer, chidp, auth = NULL, pk = NULL;
   unsigned char *name, *cp, modebuf[4], tmp[4], *arg = NULL;
   SilcUInt32 mode, add, type, len, arg_len = 0;
   int i;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 3) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
 
@@ -1199,9 +1414,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
 
     channel = silc_client_get_channel(cmd->client, conn, name);
     if (!channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are on that channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
   }
@@ -1252,6 +1465,18 @@ SILC_CLIENT_CMD_FUNC(cmode)
       else
        mode &= ~SILC_CHANNEL_MODE_TOPIC;
       break;
+    case 'm':
+      if (add)
+       mode |= SILC_CHANNEL_MODE_SILENCE_USERS;
+      else
+       mode &= ~SILC_CHANNEL_MODE_SILENCE_USERS;
+      break;
+    case 'M':
+      if (add)
+       mode |= SILC_CHANNEL_MODE_SILENCE_OPERS;
+      else
+       mode &= ~SILC_CHANNEL_MODE_SILENCE_OPERS;
+      break;
     case 'l':
       if (add) {
        int ll;
@@ -1260,7 +1485,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
        if (cmd->argc < 4) {
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
              "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-         COMMAND_ERROR;
+         COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
          goto out;
        }
        ll = atoi(cmd->argv[3]);
@@ -1278,7 +1503,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
        if (cmd->argc < 4) {
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
              "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-         COMMAND_ERROR;
+         COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
          goto out;
        }
        arg = cmd->argv[3];
@@ -1294,7 +1519,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
        if (cmd->argc < 4) {
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
              "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-         COMMAND_ERROR;
+         COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
          goto out;
        }
        arg = cmd->argv[3];
@@ -1310,7 +1535,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
        if (cmd->argc < 4) {
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
              "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-         COMMAND_ERROR;
+         COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
          goto out;
        }
        arg = cmd->argv[3];
@@ -1321,28 +1546,31 @@ SILC_CLIENT_CMD_FUNC(cmode)
       break;
     case 'f':
       if (add) {
+       SilcPublicKey pubkey = cmd->client->public_key;
+       SilcPrivateKey privkey = cmd->client->private_key;
+
        mode |= SILC_CHANNEL_MODE_FOUNDER_AUTH;
        type = 7;
 
-       if (cmd->argc < 4) {
-         SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-             "Usage: /CMODE <channel> +|-<modes> [{ <arguments>}]");
-         COMMAND_ERROR;
-         goto out;
-       }
-
-       if (!strcasecmp(cmd->argv[3], "-pubkey")) {
-         auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
-                                                   cmd->client->private_key,
-                                                   cmd->client->rng, 
-                                                   conn->hash,
-                                                   conn->local_id,
-                                                   SILC_ID_CLIENT);
-       } else {
-         auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0,
-                                         cmd->argv[3], cmd->argv_lens[3]);
+       if (cmd->argc >= 5) {
+         char *pass = "";
+         if (cmd->argc >= 6)
+           pass = cmd->argv[5];
+         if (!silc_load_key_pair(cmd->argv[3], cmd->argv[4], pass,
+                                 NULL, &pubkey, &privkey)) {
+           SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
+               "Could not load key pair, check your arguments");
+           COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+           goto out;
+         }
        }
 
+       pk = silc_pkcs_public_key_payload_encode(pubkey);
+       auth = silc_auth_public_key_auth_generate(pubkey, privkey,
+                                                 cmd->client->rng, 
+                                                 cmd->client->sha1hash,
+                                                 conn->local_id,
+                                                 SILC_ID_CLIENT);
        arg = auth->data;
        arg_len = auth->len;
       } else {
@@ -1350,7 +1578,7 @@ SILC_CLIENT_CMD_FUNC(cmode)
       }
       break;
     default:
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_UNKNOWN_MODE);
       goto out;
       break;
     }
@@ -1363,13 +1591,15 @@ SILC_CLIENT_CMD_FUNC(cmode)
      that requires an argument. */
   if (type && arg) {
     buffer = 
-      silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 3, 
+      silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 4,
                                     1, chidp->data, chidp->len, 
                                     2, modebuf, sizeof(modebuf),
-                                    type, arg, arg_len);
+                                    type, arg, arg_len,
+                                    8, pk ? pk->data : NULL,
+                                    pk ? pk->len : 0);
   } else {
     buffer = 
-      silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 2, 
+      silc_command_payload_encode_va(SILC_COMMAND_CMODE, 0, 2,
                                     1, chidp->data, chidp->len, 
                                     2, modebuf, sizeof(modebuf));
   }
@@ -1378,11 +1608,11 @@ SILC_CLIENT_CMD_FUNC(cmode)
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   silc_buffer_free(chidp);
-  if (auth)
-    silc_buffer_free(auth);
+  silc_buffer_free(auth);
+  silc_buffer_free(pk);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -1406,22 +1636,20 @@ SILC_CLIENT_CMD_FUNC(cumode)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 4) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /CUMODE <channel> +|-<modes> <nickname>[@<server>]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
 
@@ -1431,9 +1659,7 @@ SILC_CLIENT_CMD_FUNC(cumode)
 
     channel = silc_client_get_channel(cmd->client, conn, name);
     if (!channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are on that channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
   }
@@ -1449,7 +1675,7 @@ SILC_CLIENT_CMD_FUNC(cumode)
                                        cmd->argv[3], TRUE);
   if (!client_entry) {
     if (cmd->pending) {
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NO_SUCH_NICK);
       goto out;
     }
 
@@ -1483,25 +1709,36 @@ SILC_CLIENT_CMD_FUNC(cumode)
       if (add) {
        mode |= SILC_CHANNEL_UMODE_CHANFO;
        mode |= SILC_CHANNEL_UMODE_CHANOP;
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
       } else {
        mode = SILC_CHANNEL_UMODE_NONE;
       }
       break;
     case 'f':
       if (add) {
-       if (cmd->argc == 5) {
-         if (!strcasecmp(cmd->argv[4], "-pubkey")) {
-           auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
-                                                     cmd->client->private_key,
-                                                     cmd->client->rng,
-                                                     conn->hash,
-                                                     conn->local_id,
-                                                     SILC_ID_CLIENT);
-         } else {
-           auth = silc_auth_payload_encode(SILC_AUTH_PASSWORD, NULL, 0,
-                                           cmd->argv[4], cmd->argv_lens[4]);
+       SilcPublicKey pubkey = cmd->client->public_key;
+       SilcPrivateKey privkey = cmd->client->private_key;
+
+       if (cmd->argc >= 6) {
+         char *pass = "";
+         if (cmd->argc >= 7)
+           pass = cmd->argv[6];
+         if (!silc_load_key_pair(cmd->argv[4], cmd->argv[5], pass,
+                                 NULL, &pubkey, &privkey)) {
+           SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
+               "Could not load key pair, check your arguments");
+           COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+           goto out;
          }
        }
+
+       auth = silc_auth_public_key_auth_generate(pubkey, privkey,
+                                                 cmd->client->rng,
+                                                 cmd->client->sha1hash,
+                                                 conn->local_id,
+                                                 SILC_ID_CLIENT);
        mode |= SILC_CHANNEL_UMODE_CHANFO;
       } else {
        mode &= ~SILC_CHANNEL_UMODE_CHANFO;
@@ -1513,8 +1750,32 @@ SILC_CLIENT_CMD_FUNC(cumode)
       else
        mode &= ~SILC_CHANNEL_UMODE_CHANOP;
       break;
+    case 'b':
+      if (add)
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
+      else
+       mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES;
+      break;
+    case 'u':
+      if (add)
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
+      else
+       mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS;
+      break;
+    case 'r':
+      if (add)
+       mode |= SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
+      else
+       mode &= ~SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS;
+      break;
+    case 'q':
+      if (add)
+       mode |= SILC_CHANNEL_UMODE_QUIET;
+      else
+       mode &= ~SILC_CHANNEL_UMODE_QUIET;
+      break;
     default:
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_UNKNOWN_MODE);
       goto out;
       break;
     }
@@ -1543,7 +1804,7 @@ SILC_CLIENT_CMD_FUNC(cumode)
     silc_buffer_free(auth);
   
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_free(nickname);
@@ -1566,22 +1827,20 @@ SILC_CLIENT_CMD_FUNC(kick)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 3) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /KICK <channel> <nickname> [<comment>]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
     name = conn->current_channel->channel_name;
@@ -1590,17 +1849,14 @@ SILC_CLIENT_CMD_FUNC(kick)
   }
 
   if (!conn->current_channel) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "You are not on that channel");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
     goto out;
   }
 
   /* Get the Channel ID of the channel */
-  if (!silc_idcache_find_by_name_one(conn->channel_cache, name, &id_cache)) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "You are not on that channel");
-    COMMAND_ERROR;
+  if (!silc_idcache_find_by_name_one(conn->internal->channel_cache,
+                                    name, &id_cache)) {
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
     goto out;
   }
 
@@ -1618,7 +1874,7 @@ SILC_CLIENT_CMD_FUNC(kick)
   if (!target) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "No such client: %s", cmd->argv[2]);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NO_SUCH_NICK);
     goto out;
   }
 
@@ -1642,7 +1898,7 @@ SILC_CLIENT_CMD_FUNC(kick)
   silc_buffer_free(idp2);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_free(nickname);
@@ -1660,7 +1916,8 @@ static void silc_client_command_oper_send(unsigned char *data,
     /* Encode the public key authentication payload */
     auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
                                              cmd->client->private_key,
-                                             cmd->client->rng, conn->hash,
+                                             cmd->client->rng,
+                                             conn->internal->hash,
                                              conn->local_id,
                                              SILC_ID_CLIENT);
   } else {
@@ -1672,7 +1929,8 @@ static void silc_client_command_oper_send(unsigned char *data,
   buffer = silc_command_payload_encode_va(SILC_COMMAND_OPER, 0, 2, 
                                          1, cmd->argv[1], 
                                          strlen(cmd->argv[1]),
-                                         2, auth->data, auth->len);
+                                         2, auth ? auth->data : NULL,
+                                         auth ? auth->len : 0);
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
 
@@ -1680,7 +1938,7 @@ static void silc_client_command_oper_send(unsigned char *data,
   silc_buffer_free(auth);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 }
 
 /* OPER command. Used to obtain server operator privileges. */
@@ -1692,14 +1950,14 @@ SILC_CLIENT_CMD_FUNC(oper)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /OPER <username> [-pubkey]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1718,7 +1976,8 @@ SILC_CLIENT_CMD_FUNC(oper)
 }
 
 static void silc_client_command_silcoper_send(unsigned char *data,
-                                             SilcUInt32 data_len, void *context)
+                                             SilcUInt32 data_len, 
+                                             void *context)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
@@ -1728,7 +1987,8 @@ static void silc_client_command_silcoper_send(unsigned char *data,
     /* Encode the public key authentication payload */
     auth = silc_auth_public_key_auth_generate(cmd->client->public_key,
                                              cmd->client->private_key,
-                                             cmd->client->rng, conn->hash,
+                                             cmd->client->rng,
+                                             conn->internal->hash,
                                              conn->local_id,
                                              SILC_ID_CLIENT);
   } else {
@@ -1740,7 +2000,8 @@ static void silc_client_command_silcoper_send(unsigned char *data,
   buffer = silc_command_payload_encode_va(SILC_COMMAND_SILCOPER, 0, 2, 
                                          1, cmd->argv[1], 
                                          strlen(cmd->argv[1]),
-                                         2, auth->data, auth->len);
+                                         2, auth ? auth->data : NULL,
+                                         auth ? auth->len : 0);
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
 
@@ -1748,7 +2009,7 @@ static void silc_client_command_silcoper_send(unsigned char *data,
   silc_buffer_free(auth);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 }
 
 /* SILCOPER command. Used to obtain router operator privileges. */
@@ -1760,14 +2021,14 @@ SILC_CLIENT_CMD_FUNC(silcoper)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /SILCOPER <username> [-pubkey]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1785,54 +2046,6 @@ SILC_CLIENT_CMD_FUNC(silcoper)
   silc_client_command_free(cmd);
 }
 
-/* CONNECT command. Connects the server to another server. */
-
-SILC_CLIENT_CMD_FUNC(connect)
-{
-  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
-  SilcClientConnection conn = cmd->conn;
-  SilcBuffer buffer;
-  unsigned char port[4];
-  SilcUInt32 tmp;
-
-  if (!cmd->conn) {
-    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
-    goto out;
-  }
-
-  if (cmd->argc < 2) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "Usage: /CONNECT <server> [<port>]");
-    COMMAND_ERROR;
-    goto out;
-  }
-
-  if (cmd->argc == 3) {
-    tmp = atoi(cmd->argv[2]);
-    SILC_PUT32_MSB(tmp, port);
-  }
-
-  if (cmd->argc == 3)
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_CONNECT, 0, 2, 
-                                           1, cmd->argv[1], 
-                                           strlen(cmd->argv[1]),
-                                           2, port, 4);
-  else
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_CONNECT, 0, 1,
-                                           1, cmd->argv[1], 
-                                           strlen(cmd->argv[1]));
-  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
-                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
-  silc_buffer_free(buffer);
-
-  /* Notify application */
-  COMMAND;
-
- out:
-  silc_client_command_free(cmd);
-}
-
 /* Command BAN. This is used to manage the ban list of the channel. */
 
 SILC_CLIENT_CMD_FUNC(ban)
@@ -1840,13 +2053,14 @@ SILC_CLIENT_CMD_FUNC(ban)
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcChannelEntry channel;
-  SilcBuffer buffer, chidp;
-  int type = 0;
+  SilcBuffer buffer, chidp, args = NULL;
   char *name, *ban = NULL;
-
+  unsigned char action[1];
+  SilcPublicKey pubkey = NULL;
+  
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
@@ -1854,15 +2068,13 @@ SILC_CLIENT_CMD_FUNC(ban)
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /BAN <channel> "
        "[+|-[<nickname>[@<server>[!<username>[@hostname>]]]]]");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
 
@@ -1872,114 +2084,139 @@ SILC_CLIENT_CMD_FUNC(ban)
 
     channel = silc_client_get_channel(cmd->client, conn, name);
     if (!channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are on that channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
   }
 
   if (cmd->argc == 3) {
     if (cmd->argv[2][0] == '+')
-      type = 2;
+      action[0] = 0x00;
     else
-      type = 3;
+      action[0] = 0x01;
 
+    /* Check if it is public key file to be added to invite list */
+    if (!silc_pkcs_load_public_key(cmd->argv[2] + 1, &pubkey,
+                                  SILC_PKCS_FILE_PEM))
+      silc_pkcs_load_public_key(cmd->argv[2] + 1, &pubkey,
+                               SILC_PKCS_FILE_BIN);
     ban = cmd->argv[2];
-    ban++;
+    if (!pubkey)
+      ban++;
+  }
+
+  if (ban) {
+    args = silc_buffer_alloc_size(2);
+    silc_buffer_format(args,
+                      SILC_STR_UI_SHORT(1),
+                      SILC_STR_END);
+    if (pubkey) {
+      chidp = silc_pkcs_public_key_payload_encode(pubkey);
+      args = silc_argument_payload_encode_one(args, chidp->data,
+                                             chidp->len, 2);
+      silc_buffer_free(chidp);
+      silc_pkcs_public_key_free(pubkey);
+    } else {
+      args = silc_argument_payload_encode_one(args, ban, strlen(ban), 1);
+    }
   }
 
   chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
 
   /* Send the command */
-  if (ban)
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_BAN, 0, 2, 
-                                           1, chidp->data, chidp->len,
-                                           type, ban, strlen(ban));
-  else
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_BAN, 0, 1, 
-                                           1, chidp->data, chidp->len);
-
+  buffer = silc_command_payload_encode_va(SILC_COMMAND_BAN, 
+                                         ++conn->cmd_ident, 3,
+                                         1, chidp->data, chidp->len,
+                                         2, args ? action : NULL,
+                                         args ? 1 : 0,
+                                         3, args ? args->data : NULL,
+                                         args ? args->len : 0);
   silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
   silc_buffer_free(chidp);
+  silc_buffer_free(args);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
 }
 
-/* CLOSE command. Close server connection to the remote server */
-SILC_CLIENT_CMD_FUNC(close)
+/* Command DETACH. This is used to detach from the server */
+
+SILC_CLIENT_CMD_FUNC(detach)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcBuffer buffer;
-  unsigned char port[4];
-  SilcUInt32 tmp;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
-  if (cmd->argc < 2) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "Usage: /CLOSE <server> [<port>]");
-    COMMAND_ERROR;
-    goto out;
-  }
-
-  if (cmd->argc == 3) {
-    tmp = atoi(cmd->argv[2]);
-    SILC_PUT32_MSB(tmp, port);
-  }
-
-  if (cmd->argc == 3)
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_CLOSE, 0, 2, 
-                                           1, cmd->argv[1], 
-                                           strlen(cmd->argv[1]),
-                                           2, port, 4);
-  else
-    buffer = silc_command_payload_encode_va(SILC_COMMAND_CLOSE, 0, 1,
-                                           1, cmd->argv[1], 
-                                           strlen(cmd->argv[1]));
-  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
+  buffer = silc_command_payload_encode_va(SILC_COMMAND_DETACH,
+                                         ++conn->cmd_ident, 0);
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
 }
-/* SHUTDOWN command. Shutdowns the server. */
 
-SILC_CLIENT_CMD_FUNC(shutdown)
+/* Command WATCH. */
+
+SILC_CLIENT_CMD_FUNC(watch)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcBuffer buffer, idp = NULL;
+  int type = 0;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
-  /* Send the command */
-  silc_client_command_send(cmd->client, cmd->conn, 
-                          SILC_COMMAND_SHUTDOWN, 0, 0);
+  if (cmd->argc < 3) {
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+    goto out;
+  }
+
+  idp = silc_id_payload_encode(conn->local_id, SILC_ID_CLIENT);
+
+  if (!strcasecmp(cmd->argv[1], "-add")) {
+    type = 2;
+  } else if (!strcasecmp(cmd->argv[1], "-del")) {
+    type = 3;
+  } else {
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+    goto out;
+  }
+
+  buffer = silc_command_payload_encode_va(SILC_COMMAND_WATCH, 
+                                         ++conn->cmd_ident, 2,
+                                         1, idp->data, idp->len,
+                                         type, cmd->argv[2],
+                                         cmd->argv_lens[2]);
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL, 
+                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
+  silc_buffer_free(buffer);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
+  if (idp)
+    silc_buffer_free(idp);
   silc_client_command_free(cmd);
 }
 
@@ -1996,22 +2233,20 @@ SILC_CLIENT_CMD_FUNC(leave)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc != 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /LEAVE <channel>");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
     name = conn->current_channel->channel_name;
@@ -2022,9 +2257,7 @@ SILC_CLIENT_CMD_FUNC(leave)
   /* Get the channel entry */
   channel = silc_client_get_channel(cmd->client, conn, name);
   if (!channel) {
-    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-       "You are not on that channel");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
     goto out;
   }
 
@@ -2046,7 +2279,7 @@ SILC_CLIENT_CMD_FUNC(leave)
   silc_buffer_free(idp);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
   if (conn->current_channel == channel)
     conn->current_channel = NULL;
@@ -2069,22 +2302,20 @@ SILC_CLIENT_CMD_FUNC(users)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc != 2) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
        "Usage: /USERS <channel>");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (cmd->argv[1][0] == '*') {
     if (!conn->current_channel) {
-      SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
-         "You are not on any channel");
-      COMMAND_ERROR;
+      COMMAND_ERROR(SILC_STATUS_ERR_NOT_ON_CHANNEL);
       goto out;
     }
     name = conn->current_channel->channel_name;
@@ -2102,7 +2333,7 @@ SILC_CLIENT_CMD_FUNC(users)
   silc_buffer_free(buffer);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_client_command_free(cmd);
@@ -2124,14 +2355,14 @@ SILC_CLIENT_CMD_FUNC(getkey)
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
     goto out;
   }
 
   if (cmd->argc < 2) {
     client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_INFO, 
                     "Usage: /GETKEY <nickname or server name>");
-    COMMAND_ERROR;
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -2165,10 +2396,11 @@ SILC_CLIENT_CMD_FUNC(getkey)
       } else {
        SilcClientCommandReplyContext reply = 
          (SilcClientCommandReplyContext)context2;
-       SilcCommandStatus status = silc_command_get_status(reply->payload);
-       
+       SilcStatus error;
+
        /* If nickname was not found, then resolve the server. */
-       if (status == SILC_STATUS_ERR_NO_SUCH_NICK) {
+       silc_command_get_status(reply->payload, NULL, &error);
+       if (error == SILC_STATUS_ERR_NO_SUCH_NICK) {
          /* This sends the IDENTIFY command to resolve the server. */
          silc_client_command_register(client, SILC_COMMAND_IDENTIFY, 
                                       NULL, NULL,
@@ -2186,16 +2418,16 @@ SILC_CLIENT_CMD_FUNC(getkey)
 
        /* If server was not found, then we've resolved both nickname and
           server and did not find anybody. */
-       if (status == SILC_STATUS_ERR_NO_SUCH_SERVER) {
+       if (error == SILC_STATUS_ERR_NO_SUCH_SERVER) {
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, "%s", 
-            silc_client_command_status_message(SILC_STATUS_ERR_NO_SUCH_NICK));
+            silc_get_status_message(SILC_STATUS_ERR_NO_SUCH_NICK));
          SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, "%s", 
-           silc_client_command_status_message(status));
-         COMMAND_ERROR;
+           silc_get_status_message(error));
+         COMMAND_ERROR(SILC_STATUS_ERR_NO_SUCH_NICK);
          goto out;
        }
 
-       COMMAND_ERROR;
+       COMMAND_ERROR(error);
        goto out;
       }
     }
@@ -2213,7 +2445,7 @@ SILC_CLIENT_CMD_FUNC(getkey)
   silc_buffer_free(idp);
 
   /* Notify application */
-  COMMAND;
+  COMMAND(SILC_STATUS_OK);
 
  out:
   silc_free(nickname);
@@ -2282,6 +2514,129 @@ bool silc_client_command_unregister(SilcClient client,
   return FALSE;
 }
 
+/* Private range commands, specific to this implementation (and compatible
+   with SILC Server). */
+
+/* CONNECT command. Connects the server to another server. */
+
+SILC_CLIENT_CMD_FUNC(connect)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcBuffer buffer;
+  unsigned char port[4];
+  SilcUInt32 tmp;
+
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
+    goto out;
+  }
+
+  if (cmd->argc < 2) {
+    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
+       "Usage: /CONNECT <server> [<port>]");
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+    goto out;
+  }
+
+  if (cmd->argc == 3) {
+    tmp = atoi(cmd->argv[2]);
+    SILC_PUT32_MSB(tmp, port);
+  }
+
+  if (cmd->argc == 3)
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CONNECT, 0, 2, 
+                                           1, cmd->argv[1], 
+                                           strlen(cmd->argv[1]),
+                                           2, port, 4);
+  else
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CONNECT, 0, 1,
+                                           1, cmd->argv[1], 
+                                           strlen(cmd->argv[1]));
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
+                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
+  silc_buffer_free(buffer);
+
+  /* Notify application */
+  COMMAND(SILC_STATUS_OK);
+
+ out:
+  silc_client_command_free(cmd);
+}
+
+
+/* CLOSE command. Close server connection to the remote server */
+SILC_CLIENT_CMD_FUNC(close)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+  SilcClientConnection conn = cmd->conn;
+  SilcBuffer buffer;
+  unsigned char port[4];
+  SilcUInt32 tmp;
+
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
+    goto out;
+  }
+
+  if (cmd->argc < 2) {
+    SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_INFO, 
+       "Usage: /CLOSE <server> [<port>]");
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+    goto out;
+  }
+
+  if (cmd->argc == 3) {
+    tmp = atoi(cmd->argv[2]);
+    SILC_PUT32_MSB(tmp, port);
+  }
+
+  if (cmd->argc == 3)
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CLOSE, 0, 2, 
+                                           1, cmd->argv[1], 
+                                           strlen(cmd->argv[1]),
+                                           2, port, 4);
+  else
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_PRIV_CLOSE, 0, 1,
+                                           1, cmd->argv[1], 
+                                           strlen(cmd->argv[1]));
+  silc_client_packet_send(cmd->client, conn->sock, SILC_PACKET_COMMAND, NULL,
+                         0, NULL, NULL, buffer->data, buffer->len, TRUE);
+  silc_buffer_free(buffer);
+
+  /* Notify application */
+  COMMAND(SILC_STATUS_OK);
+
+ out:
+  silc_client_command_free(cmd);
+}
+/* SHUTDOWN command. Shutdowns the server. */
+
+SILC_CLIENT_CMD_FUNC(shutdown)
+{
+  SilcClientCommandContext cmd = (SilcClientCommandContext)context;
+
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR(SILC_STATUS_ERR_NOT_REGISTERED);
+    goto out;
+  }
+
+  /* Send the command */
+  silc_client_command_send(cmd->client, cmd->conn, 
+                          SILC_COMMAND_PRIV_SHUTDOWN, 0, 0);
+
+  /* Notify application */
+  COMMAND(SILC_STATUS_OK);
+
+ out:
+  silc_client_command_free(cmd);
+}
+
 /* Register all default commands provided by the client library for the
    application. */
 
@@ -2298,24 +2653,28 @@ void silc_client_commands_register(SilcClient client)
   SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", 3);
   SILC_CLIENT_CMD(invite, INVITE, "INVITE", 3);
   SILC_CLIENT_CMD(quit, QUIT, "QUIT", 2);
-  SILC_CLIENT_CMD(kill, KILL, "KILL", 3);
+  SILC_CLIENT_CMD(kill, KILL, "KILL", 4);
   SILC_CLIENT_CMD(info, INFO, "INFO", 2);
-  SILC_CLIENT_CMD(connect, CONNECT, "CONNECT", 3);
+  SILC_CLIENT_CMD(stats, STATS, "STATS", 0);
   SILC_CLIENT_CMD(ping, PING, "PING", 2);
   SILC_CLIENT_CMD(oper, OPER, "OPER", 3);
   SILC_CLIENT_CMD(join, JOIN, "JOIN", 9);
   SILC_CLIENT_CMD(motd, MOTD, "MOTD", 2);
   SILC_CLIENT_CMD(umode, UMODE, "UMODE", 2);
-  SILC_CLIENT_CMD(cmode, CMODE, "CMODE", 4);
-  SILC_CLIENT_CMD(cumode, CUMODE, "CUMODE", 5);
+  SILC_CLIENT_CMD(cmode, CMODE, "CMODE", 6);
+  SILC_CLIENT_CMD(cumode, CUMODE, "CUMODE", 9);
   SILC_CLIENT_CMD(kick, KICK, "KICK", 4);
   SILC_CLIENT_CMD(ban, BAN, "BAN", 3);
-  SILC_CLIENT_CMD(close, CLOSE, "CLOSE", 3);
-  SILC_CLIENT_CMD(shutdown, SHUTDOWN, "SHUTDOWN", 1);
+  SILC_CLIENT_CMD(detach, DETACH, "DETACH", 0);
+  SILC_CLIENT_CMD(watch, WATCH, "WATCH", 3);
   SILC_CLIENT_CMD(silcoper, SILCOPER, "SILCOPER", 3);
   SILC_CLIENT_CMD(leave, LEAVE, "LEAVE", 2);
   SILC_CLIENT_CMD(users, USERS, "USERS", 2);
   SILC_CLIENT_CMD(getkey, GETKEY, "GETKEY", 2);
+
+  SILC_CLIENT_CMD(connect, PRIV_CONNECT, "CONNECT", 3);
+  SILC_CLIENT_CMD(close, PRIV_CLOSE, "CLOSE", 3);
+  SILC_CLIENT_CMD(shutdown, PRIV_SHUTDOWN, "SHUTDOWN", 1);
 }
 
 /* Unregister all commands. */
@@ -2332,7 +2691,7 @@ void silc_client_commands_unregister(SilcClient client)
   SILC_CLIENT_CMDU(quit, QUIT, "QUIT");
   SILC_CLIENT_CMDU(kill, KILL, "KILL");
   SILC_CLIENT_CMDU(info, INFO, "INFO");
-  SILC_CLIENT_CMDU(connect, CONNECT, "CONNECT");
+  SILC_CLIENT_CMDU(stats, STATS, "STATS");
   SILC_CLIENT_CMDU(ping, PING, "PING");
   SILC_CLIENT_CMDU(oper, OPER, "OPER");
   SILC_CLIENT_CMDU(join, JOIN, "JOIN");
@@ -2342,10 +2701,108 @@ void silc_client_commands_unregister(SilcClient client)
   SILC_CLIENT_CMDU(cumode, CUMODE, "CUMODE");
   SILC_CLIENT_CMDU(kick, KICK, "KICK");
   SILC_CLIENT_CMDU(ban, BAN, "BAN");
-  SILC_CLIENT_CMDU(close, CLOSE, "CLOSE");
-  SILC_CLIENT_CMDU(shutdown, SHUTDOWN, "SHUTDOWN");
+  SILC_CLIENT_CMDU(detach, DETACH, "DETACH");
+  SILC_CLIENT_CMDU(watch, WATCH, "WATCH");
   SILC_CLIENT_CMDU(silcoper, SILCOPER, "SILCOPER");
   SILC_CLIENT_CMDU(leave, LEAVE, "LEAVE");
   SILC_CLIENT_CMDU(users, USERS, "USERS");
   SILC_CLIENT_CMDU(getkey, GETKEY, "GETKEY");
+
+  SILC_CLIENT_CMDU(connect, PRIV_CONNECT, "CONNECT");
+  SILC_CLIENT_CMDU(close, PRIV_CLOSE, "CLOSE");
+  SILC_CLIENT_CMDU(shutdown, PRIV_SHUTDOWN, "SHUTDOWN");
+}
+
+/**** Client side incoming command handling **********************************/
+
+void silc_client_command_process_whois(SilcClient client,
+                                      SilcSocketConnection sock,
+                                      SilcCommandPayload payload,
+                                      SilcArgumentPayload args);
+
+/* Client is able to receive some command packets even though they are
+   special case.  Server may send WHOIS command to the client to retrieve
+   Requested Attributes information for WHOIS query the server is
+   processing. This function currently handles only the WHOIS command,
+   but if in the future for commands may arrive then this can be made
+   to support other commands too. */
+
+void silc_client_command_process(SilcClient client,
+                                SilcSocketConnection sock,
+                                SilcPacketContext *packet)
+{
+  SilcCommandPayload payload;
+  SilcCommand command;
+  SilcArgumentPayload args;
+
+  /* Get command payload from packet */
+  payload = silc_command_payload_parse(packet->buffer->data,
+                                      packet->buffer->len);
+  if (!payload) {
+    /* Silently ignore bad reply packet */
+    SILC_LOG_DEBUG(("Bad command packet"));
+    return;
+  }
+
+  /* Get arguments */
+  args = silc_command_get_args(payload);
+  
+  /* Get the command */
+  command = silc_command_get(payload);
+  switch (command) {
+
+  case SILC_COMMAND_WHOIS:
+    /* Ignore everything if requested by application */
+    if (client->internal->params->ignore_requested_attributes)
+      break;
+
+    silc_client_command_process_whois(client, sock, payload, args);
+    break;
+
+  default:
+    break;
+  }
+
+  silc_command_payload_free(payload);
+}
+
+void silc_client_command_process_whois(SilcClient client,
+                                      SilcSocketConnection sock,
+                                      SilcCommandPayload payload,
+                                      SilcArgumentPayload args)
+{
+  SilcDList attrs;
+  unsigned char *tmp;
+  SilcUInt32 tmp_len;
+  SilcBuffer buffer, packet;
+
+  SILC_LOG_DEBUG(("Received WHOIS command"));
+
+  /* Try to take the Requested Attributes */
+  tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
+  if (!tmp)
+    return;
+
+  attrs = silc_attribute_payload_parse(tmp, tmp_len);
+  if (!attrs)
+    return;
+
+  /* Process requested attributes */
+  buffer = silc_client_attributes_process(client, sock, attrs);
+  if (!buffer) {
+    silc_attribute_payload_list_free(attrs);
+    return;
+  }
+
+  /* Send the attributes back */
+  packet =
+    silc_command_reply_payload_encode_va(SILC_COMMAND_WHOIS,
+                                        SILC_STATUS_OK, 0,
+                                        silc_command_get_ident(payload),
+                                        1, 11, buffer->data, buffer->len);
+  silc_client_packet_send(client, sock, SILC_PACKET_COMMAND_REPLY,
+                         NULL, 0, NULL, NULL, packet->data, 
+                         packet->len, TRUE);
+  silc_buffer_free(packet);
+  silc_buffer_free(buffer);
 }