Mon Jun 18 22:21:37 CEST 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcclient / command_reply.c
index e99abb2e219463b824dd18b89b3ec9f63dcb0347..13fab10a168ad506500ec3996b8be031ef4c3ccd 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2003 Pekka Riikonen
+  Copyright (C) 1997 - 2005 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
@@ -48,11 +48,11 @@ do {                                                                \
   if (!silc_command_get_status(cmd->payload, NULL, NULL)) {    \
     if (SILC_STATUS_IS_ERROR(cmd->status)) {                   \
       /* Single error */                                       \
-      COMMAND_REPLY_ERROR;                                     \
+      COMMAND_REPLY_ERROR(cmd->status);                                \
       goto out;                                                        \
     }                                                          \
     /* List of errors */                                       \
-    COMMAND_REPLY_ERROR;                                       \
+    COMMAND_REPLY_ERROR(cmd->error);                           \
     if (cmd->status == SILC_STATUS_LIST_END)                   \
       goto out;                                                        \
     goto err;                                                  \
@@ -131,7 +131,7 @@ void silc_client_command_reply_process(SilcClient client,
       /* No specific identifier for command reply, call first one found */
       (*reply)(ctx, NULL);
     else
-      silc_free(ctx);
+      silc_client_command_reply_free(ctx);
   }
 }
 
@@ -155,12 +155,14 @@ void silc_client_command_reply_free(SilcClientCommandReplyContext cmd)
   SILC_LOG_DEBUG(("Command reply context %p refcnt %d->%d", cmd,
                  cmd->users + 1, cmd->users));
   if (cmd->users < 1) {
+    if (cmd->callbacks)
+      silc_free(cmd->callbacks);
     silc_command_payload_free(cmd->payload);
     silc_free(cmd);
   }
 }
 
-static void
+void
 silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd,
                                     SilcStatus status,
                                     bool notify)
@@ -181,14 +183,14 @@ silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd,
   id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!id_data) {
     if (notify)
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     return;
   }
 
   client_id = silc_id_payload_parse_id(id_data, len, NULL);
   if (!client_id) {
     if (notify)
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     return;
   }
 
@@ -197,7 +199,7 @@ silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd,
   realname = silc_argument_get_arg_type(cmd->args, 5, &len);
   if (!nickname || !username || !realname) {
     if (notify)
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     return;
   }
 
@@ -230,6 +232,11 @@ silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd,
     client_entry =
       silc_client_add_client(cmd->client, conn, nickname, username, realname,
                             client_id, mode);
+    if (!client_entry) {
+      if (notify)
+       COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      return;
+    }
   } else {
     silc_client_update_client(cmd->client, conn, client_entry,
                              nickname, username, realname, mode);
@@ -323,13 +330,13 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
 
   id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!id_data) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   client_id = silc_id_payload_parse_id(id_data, len, NULL);
   if (!client_id) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -341,7 +348,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
   username = silc_argument_get_arg_type(cmd->args, 4, &len);
   realname = silc_argument_get_arg_type(cmd->args, 5, &len);
   if (!nickname || !username) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -385,13 +392,13 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
   id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!id_data) {
     if (notify)
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     return;
   }
   idp = silc_id_payload_parse(id_data, len);
   if (!idp) {
     if (notify)
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     return;
   }
 
@@ -413,6 +420,11 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
       client_entry =
        silc_client_add_client(cmd->client, conn, name, info, NULL,
                               silc_id_dup(client_id, id_type), 0);
+      if (!client_entry) {
+       if (notify)
+         COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+       return;
+      }
     } else {
       silc_client_update_client(cmd->client, conn, client_entry,
                                name, info, NULL, 0);
@@ -438,7 +450,7 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
                                            silc_id_dup(server_id, id_type));
       if (!server_entry) {
        if (notify)
-         COMMAND_REPLY_ERROR;
+         COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
        return;
       }
     } else {
@@ -466,6 +478,11 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
       /* Add new channel entry */
       channel_entry = silc_client_add_channel(client, conn, name, 0,
                                              channel_id);
+      if (!channel_entry) {
+       if (notify)
+         COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+       return;
+      }
       channel_id = NULL;
     }
 
@@ -539,6 +556,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   SilcIDPayload idp;
   unsigned char *tmp;
   SilcUInt32 argc, len;
+  SilcClientID old_client_id;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -546,7 +564,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "Cannot set nickname: %s",
        silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -554,15 +572,18 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   if (argc < 2 || argc > 3) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "Cannot set nickname: bad reply to command");
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
+  /* Save old Client ID */
+  old_client_id = *conn->local_id;
+
   /* Take received Client ID */
   tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
   idp = silc_id_payload_parse(tmp, len);
   if (!idp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   silc_client_receive_new_id(cmd->client, cmd->sock, idp);
@@ -576,13 +597,24 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
       silc_free(conn->nickname);
     conn->nickname = strdup(tmp);
     conn->local_entry->nickname = conn->nickname;
+
+    /* Normalize nickname */
+    tmp = silc_identifier_check(conn->nickname, strlen(conn->nickname),
+                               SILC_STRING_UTF8, 128, NULL);
+    if (!tmp) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_BAD_NICKNAME);
+      goto out;
+    }
+
     silc_client_nickname_format(cmd->client, conn, conn->local_entry);
-    silc_idcache_add(conn->internal->client_cache, strdup(tmp),
-                     conn->local_entry->id, conn->local_entry, 0, NULL);
+
+    silc_idcache_add(conn->internal->client_cache, tmp,
+                    conn->local_entry->id, conn->local_entry, 0, NULL);
   }
 
   /* Notify application */
-  COMMAND_REPLY((SILC_ARGS, conn->local_entry, conn->local_entry->nickname));
+  COMMAND_REPLY((SILC_ARGS, conn->local_entry, conn->local_entry->nickname,
+                (const SilcClientID *)&old_client_id));
 
  out:
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_NICK);
@@ -604,19 +636,20 @@ SILC_CLIENT_CMD_REPLY_FUNC(list)
 
   tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    /* There were no channels in the network. */
+    COMMAND_REPLY((SILC_ARGS, NULL, NULL, 0));
     goto out;
   }
 
   channel_id = silc_id_payload_parse_id(tmp, len, NULL);
   if (!channel_id) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   name = silc_argument_get_arg_type(cmd->args, 3, NULL);
   if (!name) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -633,7 +666,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(list)
     channel_entry = silc_client_add_channel(cmd->client, conn, name, 0,
                                            channel_id);
     if (!channel_entry) {
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
     channel_id = NULL;
@@ -670,14 +703,14 @@ SILC_CLIENT_CMD_REPLY_FUNC(topic)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot set topic: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
   argc = silc_argument_get_arg_num(cmd->args);
   if (argc < 1 || argc > 3) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -699,10 +732,15 @@ SILC_CLIENT_CMD_REPLY_FUNC(topic)
   channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
   if (!channel) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
+  if (topic) {
+    silc_free(channel->topic);
+    channel->topic = silc_memdup(topic, strlen(topic));
+  }
+
   /* Notify application */
   COMMAND_REPLY((SILC_ARGS, channel, topic));
 
@@ -725,8 +763,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(invite)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot invite: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -743,7 +781,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(invite)
   channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
   if (!channel) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -766,16 +804,33 @@ SILC_CLIENT_CMD_REPLY_FUNC(kill)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
+  SilcClientID *client_id;
+  SilcClientEntry client_entry = NULL;
+  SilcUInt32 len;
+  unsigned char *id_data;
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot kill: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
+  id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
+  if (id_data) {
+    client_id = silc_id_payload_parse_id(id_data, len, NULL);
+    if (!client_id) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      goto out;
+    }
+
+    /* Get the client entry, if exists */
+    client_entry = silc_client_get_client_by_id(cmd->client, conn, client_id);
+    silc_free(client_id);
+  }
+
   /* Notify application */
-  COMMAND_REPLY((SILC_ARGS));
+  COMMAND_REPLY((SILC_ARGS, client_entry));
 
  out:
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KILL);
@@ -800,7 +855,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(info)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR, "%s",
        silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -855,7 +910,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(stats)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -881,14 +936,14 @@ SILC_CLIENT_CMD_REPLY_FUNC(ping)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
-  void *id;
+  void *id = NULL;
   int i;
   time_t diff, curtime;
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -896,7 +951,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(ping)
   id = silc_id_str2id(cmd->packet->src_id, cmd->packet->src_id_len,
                      cmd->packet->src_id_type);
   if (!id || !conn->internal->ping) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -919,12 +974,12 @@ SILC_CLIENT_CMD_REPLY_FUNC(ping)
     }
   }
 
-  silc_free(id);
-
   /* Notify application */
   COMMAND_REPLY((SILC_ARGS));
 
  out:
+  if (id)
+    silc_free(id);
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_PING);
   silc_client_command_reply_free(cmd);
 }
@@ -941,7 +996,6 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   SilcUInt32 argc, mode = 0, len, list_count;
   char *topic, *tmp, *channel_name = NULL, *hmac;
   SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
-  SilcPublicKey founder_key = NULL;
   SilcBufferStruct chpklist;
   int i;
 
@@ -950,8 +1004,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   if (cmd->error != SILC_STATUS_OK) {
     if (cmd->error != SILC_STATUS_ERR_USER_ON_CHANNEL)
       SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-         "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+         "Cannot join channel: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -959,7 +1013,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   if (argc < 7) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "Cannot join channel: Bad reply packet");
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -968,7 +1022,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   if (!tmp) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "Cannot join channel: Bad reply packet");
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   channel_name = tmp;
@@ -978,12 +1032,12 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   if (!tmp) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "Cannot join channel: Bad reply packet");
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   channel_id = silc_id_payload_parse_id(tmp, len, NULL);
   if (!channel_id) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1013,8 +1067,13 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
     channel = silc_client_add_channel(cmd->client, conn, channel_name,
                                      mode, channel_id);
   }
+  if (!channel) {
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_BAD_CHANNEL);
+    goto out;
+  }
 
   conn->current_channel = channel;
+  channel->mode = mode;
 
   /* Get hmac */
   hmac = silc_argument_get_arg_type(cmd->args, 11, NULL);
@@ -1022,7 +1081,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
     if (!silc_hmac_alloc(hmac, NULL, &channel->hmac)) {
       SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
          "Cannot join channel: Unsupported HMAC `%s'", hmac);
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
   }
@@ -1075,6 +1134,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
       client_entry =
        silc_client_add_client(cmd->client, conn, NULL, NULL, NULL,
                               silc_id_dup(client_id, SILC_ID_CLIENT), 0);
+      if (!client_entry)
+       goto out;
     }
 
     /* Join client to the channel */
@@ -1097,30 +1158,45 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
                   client_mode_list->head);
 
   /* Save channel key */
-  if (keyp && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY))
+  if (keyp)
     silc_client_save_channel_key(cmd->client, conn, keyp, channel);
 
   /* Get founder key */
   tmp = silc_argument_get_arg_type(cmd->args, 15, &len);
-  if (tmp)
-    silc_pkcs_public_key_payload_decode(tmp, len, &founder_key);
+  if (tmp) {
+    if (channel->founder_key)
+      silc_pkcs_public_key_free(channel->founder_key);
+    channel->founder_key = NULL;
+    silc_pkcs_public_key_payload_decode(tmp, len, &channel->founder_key);
+  }
+
+  /* Get user limit */
+  tmp = silc_argument_get_arg_type(cmd->args, 17, &len);
+  if (tmp && len == 4)
+    SILC_GET32_MSB(channel->user_limit, tmp);
+  if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
+    channel->user_limit = 0;
 
   /* Get channel public key list */
   tmp = silc_argument_get_arg_type(cmd->args, 16, &len);
   if (tmp)
     silc_buffer_set(&chpklist, tmp, len);
 
+  if (topic) {
+    silc_free(channel->topic);
+    channel->topic = silc_memdup(topic, strlen(topic));
+  }
+
   /* Notify application */
   COMMAND_REPLY((SILC_ARGS, channel_name, channel, mode, 0,
                 keyp ? keyp->head : NULL, NULL,
                 NULL, topic, hmac, list_count, client_id_list,
-                client_mode_list, founder_key, tmp ? &chpklist : NULL));
+                client_mode_list, channel->founder_key,
+                tmp ? &chpklist : NULL, channel->user_limit));
 
  out:
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
   silc_client_command_reply_free(cmd);
-  if (founder_key)
-    silc_pkcs_public_key_free(founder_key);
   silc_buffer_free(keyp);
   silc_buffer_free(client_id_list);
   silc_buffer_free(client_mode_list);
@@ -1138,20 +1214,20 @@ SILC_CLIENT_CMD_REPLY_FUNC(motd)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     return;
   }
 
   argc = silc_argument_get_arg_num(cmd->args);
   if (argc > 3) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   if (argc == 3) {
     motd = silc_argument_get_arg_type(cmd->args, 3, NULL);
     if (!motd) {
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
 
@@ -1194,14 +1270,14 @@ SILC_CLIENT_CMD_REPLY_FUNC(umode)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot change mode: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
   tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1232,8 +1308,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot change mode: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1249,7 +1325,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
   channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
   if (!channel) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1257,7 +1333,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
   tmp = silc_argument_get_arg_type(cmd->args, 3, NULL);
   if (!tmp) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1272,6 +1348,13 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
       public_key = NULL;
   }
 
+  /* Get user limit */
+  tmp = silc_argument_get_arg_type(cmd->args, 6, &len);
+  if (tmp && len == 4)
+    SILC_GET32_MSB(channel->user_limit, tmp);
+  if (!(channel->mode & SILC_CHANNEL_MODE_ULIMIT))
+    channel->user_limit = 0;
+
   /* Get channel public key(s) */
   tmp = silc_argument_get_arg_type(cmd->args, 5, &len);
   if (tmp)
@@ -1279,7 +1362,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
 
   /* Notify application */
   COMMAND_REPLY((SILC_ARGS, channel, mode, public_key,
-                tmp ? &channel_pubkeys : NULL));
+                tmp ? &channel_pubkeys : NULL, channel->user_limit));
 
   silc_free(channel_id);
 
@@ -1306,15 +1389,15 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot change mode: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
   /* Get channel mode */
   modev = silc_argument_get_arg_type(cmd->args, 2, NULL);
   if (!modev) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1330,7 +1413,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
   if (!channel) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1338,13 +1421,13 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   id = silc_argument_get_arg_type(cmd->args, 4, &len);
   if (!id) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   client_id = silc_id_payload_parse_id(id, len, NULL);
   if (!client_id) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1353,7 +1436,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   if (!client_entry) {
     silc_free(channel_id);
     silc_free(client_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1377,18 +1460,60 @@ SILC_CLIENT_CMD_REPLY_FUNC(kick)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
+  SilcClientID *client_id = NULL;
+  SilcChannelID *channel_id = NULL;
+  SilcClientEntry client_entry = NULL;
+  SilcChannelEntry channel = NULL;
+  unsigned char *tmp;
+  SilcUInt32 len;
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Cannot kick: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
+  /* Take Channel ID */
+  tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
+  if (tmp) {
+    channel_id = silc_id_payload_parse_id(tmp, len, NULL);
+    if (!channel_id) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      goto out;
+    }
+
+    /* Get the channel entry */
+    channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
+    if (!channel) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      goto out;
+    }
+  }
+
+  /* Get Client ID */
+  tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
+  if (tmp) {
+    client_id = silc_id_payload_parse_id(tmp, len, NULL);
+    if (!client_id) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      goto out;
+    }
+
+    /* Get client entry */
+    client_entry = silc_client_get_client_by_id(cmd->client, conn, client_id);
+    if (!client_entry) {
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+      goto out;
+    }
+  }
+
   /* Notify application */
-  COMMAND_REPLY((SILC_ARGS));
+  COMMAND_REPLY((SILC_ARGS, channel, client_entry));
 
  out:
+  silc_free(channel_id);
+  silc_free(client_id);
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KICK);
   silc_client_command_reply_free(cmd);
 }
@@ -1401,7 +1526,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(silcoper)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1421,7 +1546,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(oper)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1442,7 +1567,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(detach)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1471,7 +1596,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(watch)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1496,7 +1621,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(ban)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1513,7 +1638,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(ban)
   channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
   if (!channel) {
     silc_free(channel_id);
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1538,13 +1663,14 @@ SILC_CLIENT_CMD_REPLY_FUNC(leave)
   SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcChannelID *channel_id;
   SilcChannelEntry channel = NULL;
+  SilcChannelUser chu;
   unsigned char *tmp;
   SilcUInt32 len;
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1559,16 +1685,28 @@ SILC_CLIENT_CMD_REPLY_FUNC(leave)
     channel = silc_client_get_channel_by_id(cmd->client, conn, channel_id);
     if (!channel) {
       silc_free(channel_id);
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
 
+    /* Remove us from this channel. */
+    chu = silc_client_on_channel(channel, conn->local_entry);
+    if (chu) {
+      silc_hash_table_del(chu->client->channels, chu->channel);
+      silc_hash_table_del(chu->channel->user_list, chu->client);
+      silc_free(chu);
+    }
+
     silc_free(channel_id);
   }
 
   /* Notify application */
   COMMAND_REPLY((SILC_ARGS, channel));
 
+  /* Now delete the channel. */
+  if (channel)
+    silc_client_del_channel(cmd->client, conn, channel);
+
  out:
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_LEAVE);
   silc_client_command_reply_free(cmd);
@@ -1589,7 +1727,7 @@ static void silc_client_command_reply_users_cb(SilcClient client,
     cmd->status = cmd->error = SILC_STATUS_ERR_NO_SUCH_CHANNEL;
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
     silc_client_command_reply_free(cmd);
     return;
@@ -1624,19 +1762,19 @@ silc_client_command_reply_users_save(SilcClientCommandReplyContext cmd,
   /* Get channel ID */
   tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   channel_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
   if (!channel_id) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
   /* Get the list count */
   tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   SILC_GET32_MSB(list_count, tmp);
@@ -1644,7 +1782,7 @@ silc_client_command_reply_users_save(SilcClientCommandReplyContext cmd,
   /* Get Client ID list */
   tmp = silc_argument_get_arg_type(cmd->args, 4, &tmp_len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   silc_buffer_set(&client_id_list, tmp, tmp_len);
@@ -1652,7 +1790,7 @@ silc_client_command_reply_users_save(SilcClientCommandReplyContext cmd,
   /* Get client mode list */
   tmp = silc_argument_get_arg_type(cmd->args, 5, &tmp_len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   silc_buffer_set(&client_mode_list, tmp, tmp_len);
@@ -1780,8 +1918,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(users)
 
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-       "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+       "Query failed: %s", silc_get_status_message(cmd->error));
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -1796,8 +1934,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(users)
       goto out;
     } else {
       SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-         "%s", silc_get_status_message(cmd->error));
-      COMMAND_REPLY_ERROR;
+         "Query failed: %s", silc_get_status_message(cmd->error));
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
   }
@@ -1834,18 +1972,18 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
   tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!tmp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
   idp = silc_id_payload_parse(tmp, len);
   if (!idp) {
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
     goto out;
   }
 
@@ -1856,13 +1994,18 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
       public_key = NULL;
   }
 
+  if (!public_key) {
+    COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
+    goto out;
+  }
+
   id_type = silc_id_payload_get_type(idp);
   if (id_type == SILC_ID_CLIENT) {
     /* Received client's public key */
     client_id = silc_id_payload_get_id(idp);
     client_entry = silc_client_get_client_by_id(cmd->client, conn, client_id);
     if (!client_entry) {
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
 
@@ -1873,15 +2016,20 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
       silc_hash_make(cmd->client->sha1hash, tmp + 4, len - 4,
                     client_entry->fingerprint);
     }
+    if (!client_entry->public_key) {
+      client_entry->public_key = public_key;
+      public_key = NULL;
+    }
 
     /* Notify application */
-    COMMAND_REPLY((SILC_ARGS, id_type, client_entry, public_key));
+    COMMAND_REPLY((SILC_ARGS, id_type, client_entry,
+                  client_entry->public_key));
   } else if (id_type == SILC_ID_SERVER) {
     /* Received server's public key */
     server_id = silc_id_payload_get_id(idp);
     server_entry = silc_client_get_server_by_id(cmd->client, conn, server_id);
     if (!server_entry) {
-      COMMAND_REPLY_ERROR;
+      COMMAND_REPLY_ERROR(SILC_STATUS_ERR_NOT_ENOUGH_PARAMS);
       goto out;
     }
 
@@ -1900,6 +2048,32 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
   silc_client_command_reply_free(cmd);
 }
 
+/* Reply to SERVICE command. */
+/* XXX incomplete */
+
+SILC_CLIENT_CMD_REPLY_FUNC(service)
+{
+  SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
+  SilcUInt32 tmp_len;
+  unsigned char *service_list, *name;
+
+  COMMAND_CHECK_STATUS;
+
+  /* Get service list */
+  service_list = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
+
+  /* Get requested service name */
+  name = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
+
+  /* Notify application */
+  COMMAND_REPLY((SILC_ARGS, service_list, name));
+
+ out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_SERVICE);
+ err:
+  silc_client_command_reply_free(cmd);
+}
+
 SILC_CLIENT_CMD_REPLY_FUNC(quit)
 {
   silc_client_command_reply_free(context);
@@ -2069,7 +2243,7 @@ static void silc_client_command_reply_users_i_cb(SilcClient client,
     cmd->status = cmd->error = SILC_STATUS_ERR_NO_SUCH_CHANNEL;
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
     silc_client_command_reply_free(cmd);
     return;
@@ -2114,7 +2288,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(connect)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -2134,7 +2308,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(close)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }
 
@@ -2154,7 +2328,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(shutdown)
   if (cmd->error != SILC_STATUS_OK) {
     SAY(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
        "%s", silc_get_status_message(cmd->error));
-    COMMAND_REPLY_ERROR;
+    COMMAND_REPLY_ERROR(cmd->error);
     goto out;
   }