updates.
[silc.git] / lib / silcclient / command_reply.c
index ae68cb7981a036af02cb77c03a359749b4e8a284..9a8b9fe6b1b82837e610accd5193b05a7737af4c 100644 (file)
@@ -122,6 +122,32 @@ const SilcCommandStatusMessage silc_command_status_messages[] = {
   cmd->sock->user_data, cmd->payload, FALSE, \
   silc_command_get(cmd->payload), status)
 
+/* All functions that call the COMMAND_CHECK_STATUS or the
+   COMMAND_CHECK_STATUS_LIST macros must have out: goto label. */
+
+#define COMMAND_CHECK_STATUS                                             \
+do {                                                                     \
+  SILC_LOG_DEBUG(("Start"));                                             \
+  SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL)); \
+  if (status != SILC_STATUS_OK)  {                                       \
+    COMMAND_REPLY_ERROR;                                                 \
+    goto out;                                                            \
+  }                                                                      \
+} while(0)
+
+#define COMMAND_CHECK_STATUS_LIST                                        \
+do {                                                                     \
+  SILC_LOG_DEBUG(("Start"));                                             \
+  SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL)); \
+  if (status != SILC_STATUS_OK &&                                        \
+      status != SILC_STATUS_LIST_START &&                                \
+      status != SILC_STATUS_LIST_ITEM &&                                 \
+      status != SILC_STATUS_LIST_END) {                                          \
+    COMMAND_REPLY_ERROR;                                                 \
+    goto out;                                                            \
+  }                                                                      \
+} while(0)
+
 /* Process received command reply. */
 
 void silc_client_command_reply_process(SilcClient client,
@@ -257,43 +283,13 @@ silc_client_command_reply_whois_save(SilcClientCommandReplyContext cmd,
                                       silc_hash_client_id_compare, NULL,
                                       &id_cache)) {
     SILC_LOG_DEBUG(("Adding new client entry"));
-
-    client_entry = silc_calloc(1, sizeof(*client_entry));
-    client_entry->id = client_id;
-    silc_parse_nickname(nickname, &client_entry->nickname, 
-                       &client_entry->server, &client_entry->num);
-    client_entry->username = strdup(username);
-    if (realname)
-      client_entry->realname = strdup(realname);
-    client_entry->mode = mode;
-    
-    /* Add client to cache */
-    silc_idcache_add(conn->client_cache, client_entry->nickname,
-                    client_id, (void *)client_entry, FALSE);
+    client_entry = 
+      silc_client_add_client(cmd->client, conn, nickname, username, realname,
+                            client_id, mode);
   } else {
     client_entry = (SilcClientEntry)id_cache->context;
-    if (client_entry->nickname)
-      silc_free(client_entry->nickname);
-    if (client_entry->server)
-      silc_free(client_entry->server);
-    if (client_entry->username)
-      silc_free(client_entry->username);
-    if (client_entry->realname)
-      silc_free(client_entry->realname);
-    client_entry->mode = mode;
-
-    SILC_LOG_DEBUG(("Updating client entry"));
-
-    silc_parse_nickname(nickname, &client_entry->nickname, 
-                       &client_entry->server, &client_entry->num);
-    client_entry->username = strdup(username);
-    if (realname)
-      client_entry->realname = strdup(realname);
-
-    /* Remove the old cache entry and create a new one */
-    silc_idcache_del_by_context(conn->client_cache, client_entry);
-    silc_idcache_add(conn->client_cache, client_entry->nickname, 
-                    client_entry->id, client_entry, FALSE);
+    silc_client_update_client(cmd->client, conn, client_entry, 
+                             nickname, username, realname, mode);
     silc_free(client_id);
   }
 
@@ -313,29 +309,11 @@ SILC_CLIENT_CMD_REPLY_FUNC(whois)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcCommandStatus status;
-  unsigned char *tmp;
-
-  SILC_LOG_DEBUG(("Start"));
-
-  tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
-  SILC_GET16_MSB(status, tmp);
-  if (status != SILC_STATUS_OK && 
-      status != SILC_STATUS_LIST_START &&
-      status != SILC_STATUS_LIST_ITEM &&
-      status != SILC_STATUS_LIST_END) {
-    COMMAND_REPLY_ERROR;
-    goto out;
-  }
 
-  /* Display one whois reply */
-  if (status == SILC_STATUS_OK)
-    silc_client_command_reply_whois_save(cmd, status);
+  COMMAND_CHECK_STATUS_LIST;
 
-  /* List */
-  if (status == SILC_STATUS_LIST_START ||
-      status == SILC_STATUS_LIST_ITEM ||
-      status == SILC_STATUS_LIST_END)
-    silc_client_command_reply_whois_save(cmd, status);
+  /* Save WHOIS info */
+  silc_client_command_reply_whois_save(cmd, status);
 
   /* Pending callbacks are not executed if this was an list entry */
   if (status != SILC_STATUS_OK &&
@@ -344,10 +322,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(whois)
     return;
   }
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_WHOIS);
   silc_client_command_reply_free(cmd);
 }
@@ -363,32 +339,22 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
   SilcIDCacheEntry id_cache = NULL;
   SilcClientEntry client_entry = NULL;
   uint32 len;
-  unsigned char *id_data, *tmp;
+  unsigned char *id_data;
   char *nickname, *username;
   char *realname = NULL;
 
-  SILC_LOG_DEBUG(("Start"));
+  COMMAND_CHECK_STATUS_LIST;
 
-  tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
-  SILC_GET16_MSB(status, tmp);
-  if (status != SILC_STATUS_OK && 
-      status != SILC_STATUS_LIST_START &&
-      status != SILC_STATUS_LIST_ITEM &&
-      status != SILC_STATUS_LIST_END) {
-    COMMAND_REPLY_ERROR;
-    goto out;
-  }
-  
   id_data = silc_argument_get_arg_type(cmd->args, 2, &len);
   if (!id_data) {
     COMMAND_REPLY_ERROR;
-    return;
+    goto out;
   }
   
   client_id = silc_id_payload_parse_id(id_data, len);
   if (!client_id) {
     COMMAND_REPLY_ERROR;
-    return;
+    goto out;
   }
 
   /* Get the client entry, if exists */
@@ -404,8 +370,9 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
   realname = silc_argument_get_arg_type(cmd->args, 5, &len);
   if (!nickname || !username) {
     COMMAND_REPLY_ERROR;
-    return;
+    goto out;
   }
+
   /* Notify application. We don't save any history information to any
      cache. Just pass the data to the application for displaying on 
      the screen. */
@@ -418,10 +385,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(whowas)
     return;
   }
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_WHOIS);
   silc_client_command_reply_free(cmd);
 }
@@ -477,38 +442,13 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
                                         silc_hash_client_id_compare, NULL,
                                         &id_cache)) {
       SILC_LOG_DEBUG(("Adding new client entry"));
-      
-      client_entry = silc_calloc(1, sizeof(*client_entry));
-      client_entry->id = silc_id_dup(client_id, id_type);
-      silc_parse_nickname(name, &client_entry->nickname, 
-                         &client_entry->server, &client_entry->num);
-      if (info)
-       client_entry->username = strdup(info);
-      
-      /* Add client to cache */
-      silc_idcache_add(conn->client_cache, client_entry->nickname,
-                      client_entry->id, (void *)client_entry, FALSE);
+      client_entry = 
+       silc_client_add_client(cmd->client, conn, name, info, NULL,
+                              silc_id_dup(client_id, id_type), 0);
     } else {
       client_entry = (SilcClientEntry)id_cache->context;
-      if (client_entry->nickname)
-       silc_free(client_entry->nickname);
-      if (client_entry->server)
-       silc_free(client_entry->server);
-      if (info && client_entry->username)
-       silc_free(client_entry->username);
-      
-      SILC_LOG_DEBUG(("Updating client entry"));
-      
-      silc_parse_nickname(name, &client_entry->nickname, 
-                         &client_entry->server, &client_entry->num);
-      
-      if (info)
-       client_entry->username = strdup(info);
-      
-      /* Remove the old cache entry and create a new one */
-      silc_idcache_del_by_context(conn->client_cache, client_entry);
-      silc_idcache_add(conn->client_cache, client_entry->nickname, 
-                      client_entry->id, client_entry, FALSE);
+      silc_client_update_client(cmd->client, conn, client_entry, 
+                               name, info, NULL, 0);
     }
 
     /* Notify application */
@@ -527,7 +467,8 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
       
       server_entry = silc_calloc(1, sizeof(*server_entry));
       server_entry->server_id = silc_id_dup(server_id, id_type);
-      server_entry->server_name = strdup(name);
+      if (name)
+       server_entry->server_name = strdup(name);
       if (info)
        server_entry->server_info = strdup(info);
       
@@ -550,9 +491,12 @@ silc_client_command_reply_identify_save(SilcClientCommandReplyContext cmd,
     /* Check if we have this channel cached already. */
     if (!silc_idcache_find_by_id_one(conn->channel_cache, 
                                     (void *)channel_id, &id_cache)) {
+      if (!name)
+       break;
+
       SILC_LOG_DEBUG(("Adding new channel entry"));
       channel_entry = silc_client_new_channel_id(client, conn->sock, 
-                                                name, 0, idp);
+                                                strdup(name), 0, idp);
     } else {
       channel_entry = (SilcChannelEntry)id_cache->context;
     }
@@ -576,29 +520,11 @@ SILC_CLIENT_CMD_REPLY_FUNC(identify)
 {
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcCommandStatus status;
-  unsigned char *tmp;
 
-  SILC_LOG_DEBUG(("Start"));
+  COMMAND_CHECK_STATUS_LIST;
 
-  tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
-  SILC_GET16_MSB(status, tmp);
-  if (status != SILC_STATUS_OK && 
-      status != SILC_STATUS_LIST_START &&
-      status != SILC_STATUS_LIST_ITEM &&
-      status != SILC_STATUS_LIST_END) {
-    COMMAND_REPLY_ERROR;
-    goto out;
-  }
-
-  /* Save one IDENTIFY entry */
-  if (status == SILC_STATUS_OK)
-    silc_client_command_reply_identify_save(cmd, status);
-
-  /* List */
-  if (status == SILC_STATUS_LIST_START ||
-      status == SILC_STATUS_LIST_ITEM ||
-      status == SILC_STATUS_LIST_END)
-    silc_client_command_reply_identify_save(cmd, status);
+  /* Save IDENTIFY info */
+  silc_client_command_reply_identify_save(cmd, status);
 
   /* Pending callbacks are not executed if this was an list entry */
   if (status != SILC_STATUS_OK &&
@@ -607,10 +533,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(identify)
     return;
   }
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_IDENTIFY);
   silc_client_command_reply_free(cmd);
 }
@@ -633,7 +557,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   if (status != SILC_STATUS_OK) {
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
                          "Cannot set nickname: %s", 
-            silc_client_command_status_message(status));
+                         silc_client_command_status_message(status));
     COMMAND_REPLY_ERROR;
     goto out;
   }
@@ -658,10 +582,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(nick)
   /* Notify application */
   COMMAND_REPLY((ARGS, conn->local_entry));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_NICK);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_NICK);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_NICK);
   silc_client_command_reply_free(cmd);
 }
@@ -675,15 +597,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(list)
   unsigned char *tmp, *name, *topic;
   uint32 usercount = 0;
 
-  tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
-  SILC_GET16_MSB(status, tmp);
-  if (status != SILC_STATUS_OK && 
-      status != SILC_STATUS_LIST_START &&
-      status != SILC_STATUS_LIST_ITEM &&
-      status != SILC_STATUS_LIST_END) {
-    COMMAND_REPLY_ERROR;
-    goto out;
-  }
+  COMMAND_CHECK_STATUS_LIST;
 
   name = silc_argument_get_arg_type(cmd->args, 3, NULL);
   topic = silc_argument_get_arg_type(cmd->args, 4, NULL);
@@ -701,10 +615,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(list)
     return;
   }
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_LIST);
   silc_client_command_reply_free(cmd);
 }
@@ -728,9 +640,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(topic)
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
             "%s", silc_client_command_status_message(status));
     COMMAND_REPLY_ERROR;
-    SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_TOPIC);
-    silc_client_command_reply_free(cmd);
-    return;
+    goto out;
   }
 
   argc = silc_argument_get_arg_num(cmd->args);
@@ -766,10 +676,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(topic)
   /* Notify application */
   COMMAND_REPLY((ARGS, channel, topic));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_TOPIC);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_TOPIC);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_TOPIC);
   silc_client_command_reply_free(cmd);
 }
@@ -793,9 +701,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(invite)
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
             "%s", silc_client_command_status_message(status));
     COMMAND_REPLY_ERROR;
-    SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_INVITE);
-    silc_client_command_reply_free(cmd);
-    return;
+    goto out;
   }
 
   /* Take Channel ID */
@@ -823,10 +729,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(invite)
   /* Notify application */
   COMMAND_REPLY((ARGS, channel, tmp));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_INVITE);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_INVITE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_INVITE);
   silc_client_command_reply_free(cmd);
 }
@@ -838,10 +742,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(kill)
   SilcClientCommandReplyContext cmd = (SilcClientCommandReplyContext)context;
   SilcClientConnection conn = (SilcClientConnection)cmd->sock->user_data;
   SilcCommandStatus status;
-  unsigned char *tmp;
 
-  tmp = silc_argument_get_arg_type(cmd->args, 1, NULL);
-  SILC_GET16_MSB(status, tmp);
+  SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
   if (status != SILC_STATUS_OK) {
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
             "%s", silc_client_command_status_message(status));
@@ -852,10 +754,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(kill)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KILL);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KILL);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_KILL);
   silc_client_command_reply_free(cmd);
 }
@@ -881,9 +781,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(info)
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
             "%s", silc_client_command_status_message(status));
     COMMAND_REPLY_ERROR;
-    SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_INFO);
-    silc_client_command_reply_free(cmd);
-    return;
+    goto out;
   }
 
   /* Get server ID */
@@ -925,13 +823,10 @@ SILC_CLIENT_CMD_REPLY_FUNC(info)
   /* Notify application */
   COMMAND_REPLY((ARGS, server, server->server_name, server->server_info));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
-
  out:
-  if (server_id)
-    silc_free(server_id);
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_INFO);
+  silc_free(server_id);
   silc_client_command_reply_free(cmd);
 }
 
@@ -984,10 +879,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(ping)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_PING);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_PING);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_PING);
   silc_client_command_reply_free(cmd);
 }
@@ -1005,7 +898,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   SilcChannelUser chu;
   uint32 argc, mode, len, list_count;
   char *topic, *tmp, *channel_name = NULL, *hmac;
-  SilcBuffer keyp = NULL, client_id_list, client_mode_list;
+  SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
   int i;
 
   SILC_LOG_DEBUG(("Start"));
@@ -1070,6 +963,11 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
   /* Get topic */
   topic = silc_argument_get_arg_type(cmd->args, 10, NULL);
 
+  /* If we have the channel entry, remove it and create a new one */
+  channel = silc_client_get_channel(cmd->client, conn, channel_name);
+  if (channel)
+    silc_client_del_channel(cmd->client, conn, channel);
+
   /* Save received Channel ID. This actually creates the channel */
   channel = silc_client_new_channel_id(cmd->client, cmd->sock, channel_name, 
                                       mode, idp);
@@ -1138,10 +1036,9 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
                                         silc_hash_client_id_compare, NULL,
                                         &id_cache)) {
       /* No, we don't have it, add entry for it. */
-      client_entry = silc_calloc(1, sizeof(*client_entry));
-      client_entry->id = silc_id_dup(client_id, SILC_ID_CLIENT);
-      silc_idcache_add(conn->client_cache, NULL, client_entry->id, 
-                      (void *)client_entry, FALSE);
+      client_entry = 
+       silc_client_add_client(cmd->client, conn, NULL, NULL, NULL,
+                              silc_id_dup(client_id, SILC_ID_CLIENT), 0);
     } else {
       /* Yes, we have it already */
       client_entry = (SilcClientEntry)id_cache->context;
@@ -1175,17 +1072,17 @@ SILC_CLIENT_CMD_REPLY_FUNC(join)
                 NULL, topic, hmac, list_count, client_id_list, 
                 client_mode_list));
 
-  /* Execute any pending command callbacks */
+ out:
   SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
+  SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_JOIN);
+  silc_client_command_reply_free(cmd);
 
   if (keyp)
     silc_buffer_free(keyp);
-  silc_buffer_free(client_id_list);
-  silc_buffer_free(client_mode_list);
-
- out:
-  SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_JOIN);
-  silc_client_command_reply_free(cmd);
+  if (client_id_list)
+    silc_buffer_free(client_id_list);
+  if (client_mode_list)
+    silc_buffer_free(client_mode_list);
 }
 
 /* Received reply for MOTD command */
@@ -1245,10 +1142,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(motd)
   /* Notify application */
   COMMAND_REPLY((ARGS, motd));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_MOTD);
   silc_client_command_reply_free(cmd);
 }
@@ -1284,10 +1179,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(umode)
   /* Notify application */
   COMMAND_REPLY((ARGS, mode));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_UMODE);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_UMODE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_UMODE);
   silc_client_command_reply_free(cmd);
 }
@@ -1346,12 +1239,11 @@ SILC_CLIENT_CMD_REPLY_FUNC(cmode)
 
   /* Notify application */
   COMMAND_REPLY((ARGS, channel, mode));
-  silc_free(channel_id);
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CMODE);
+  silc_free(channel_id);
 
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CMODE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_CMODE);
   silc_client_command_reply_free(cmd);
 }
@@ -1369,7 +1261,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   SilcClientEntry client_entry;
   SilcChannelEntry channel;
   SilcChannelUser chu;
-  unsigned char *tmp, *id;
+  unsigned char *modev, *tmp, *id;
   uint32 len, mode;
   
   SILC_GET16_MSB(status, silc_argument_get_arg_type(cmd->args, 1, NULL));
@@ -1381,8 +1273,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   }
   
   /* Get channel mode */
-  tmp = silc_argument_get_arg_type(cmd->args, 2, NULL);
-  if (!tmp) {
+  modev = silc_argument_get_arg_type(cmd->args, 2, NULL);
+  if (!modev) {
     COMMAND_REPLY_ERROR;
     goto out;
   }
@@ -1433,7 +1325,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   client_entry = (SilcClientEntry)id_cache->context;
 
   /* Save the mode */
-  SILC_GET32_MSB(mode, tmp);
+  SILC_GET32_MSB(mode, modev);
+  silc_list_start(channel->clients);
   while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
     if (chu->client == client_entry) {
       chu->mode = mode;
@@ -1446,10 +1339,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(cumode)
   silc_free(client_id);
   silc_free(channel_id);
   
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CUMODE);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CUMODE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_CUMODE);
   silc_client_command_reply_free(cmd);
 }
@@ -1473,10 +1364,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(kick)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KICK);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_KICK);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_KICK);
   silc_client_command_reply_free(cmd);
 }
@@ -1500,10 +1389,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(silcoper)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_SILCOPER);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_SILCOPER);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_SILCOPER);
   silc_client_command_reply_free(cmd);
 }
@@ -1527,10 +1414,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(oper)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_OPER);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_OPER);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_OPER);
   silc_client_command_reply_free(cmd);
 }
@@ -1554,10 +1439,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(connect)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CONNECT);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CONNECT);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_CONNECT);
   silc_client_command_reply_free(cmd);
 }
@@ -1607,10 +1490,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(ban)
   /* Notify application */
   COMMAND_REPLY((ARGS, channel, tmp));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_BAN);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_BAN);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_BAN);
   silc_client_command_reply_free(cmd);
 }
@@ -1634,10 +1515,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(close)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CLOSE);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_CLOSE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_CLOSE);
   silc_client_command_reply_free(cmd);
 }
@@ -1661,10 +1540,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(shutdown)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_SHUTDOWN);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_SHUTDOWN);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_SHUTDOWN);
   silc_client_command_reply_free(cmd);
 }
@@ -1690,10 +1567,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(leave)
   /* Notify application */
   COMMAND_REPLY((ARGS));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_LEAVE);
-
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_LEAVE);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_LEAVE);
   silc_client_command_reply_free(cmd);
 }
@@ -1710,8 +1585,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(users)
   SilcChannelEntry channel;
   SilcChannelUser chu;
   SilcChannelID *channel_id = NULL;
-  SilcBuffer client_id_list;
-  SilcBuffer client_mode_list;
+  SilcBuffer client_id_list = NULL;
+  SilcBuffer client_mode_list = NULL;
   unsigned char *tmp;
   uint32 tmp_len, list_count;
   int i;
@@ -1819,7 +1694,8 @@ SILC_CLIENT_CMD_REPLY_FUNC(users)
                                    silc_hash_client_id_compare, NULL,
                                    &id_cache);
 
-    if (!id_cache || !((SilcClientEntry)id_cache->context)->username) {
+    if (!id_cache || !((SilcClientEntry)id_cache->context)->username ||
+       !((SilcClientEntry)id_cache->context)->realname) {
       /* No we don't have it (or it is incomplete in information), query
         it from the server. Assemble argument table that will be sent
         for the WHOIS command later. */
@@ -1881,17 +1757,15 @@ SILC_CLIENT_CMD_REPLY_FUNC(users)
   /* Notify application */
   COMMAND_REPLY((ARGS, channel, list_count, client_id_list, client_mode_list));
 
-  /* Execute any pending command callbacks */
-  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
-
-  silc_buffer_free(client_id_list);
-  silc_buffer_free(client_mode_list);
-
  out:
-  if (channel_id)
-    silc_free(channel_id);
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_USERS);
   silc_client_command_reply_free(cmd);
+  silc_free(channel_id);
+  if (client_id_list)
+    silc_buffer_free(client_id_list);
+  if (client_mode_list)
+    silc_buffer_free(client_mode_list);
 }
 
 /* Received command reply to GETKEY command. WE've received the remote
@@ -1921,35 +1795,41 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
   SILC_GET16_MSB(status, tmp);
   if (status != SILC_STATUS_OK) {
     cmd->client->ops->say(cmd->client, conn, SILC_CLIENT_MESSAGE_ERROR,
-            "%s", silc_client_command_status_message(status));
+                         "%s", silc_client_command_status_message(status));
     COMMAND_REPLY_ERROR;
     goto out;
   }
 
   tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
-  if (!tmp)
+  if (!tmp) {
+    COMMAND_REPLY_ERROR;
     goto out;
+  }
   idp = silc_id_payload_parse_data(tmp, len);
-  if (!idp)
+  if (!idp) {
+    COMMAND_REPLY_ERROR;
     goto out;
+  }
 
   /* Get the public key payload */
   tmp = silc_argument_get_arg_type(cmd->args, 3, &len);
-  if (!tmp)
-    goto out;
-
-  /* Decode the public key */
-
-  SILC_GET16_MSB(pk_len, tmp);
-  SILC_GET16_MSB(type, tmp + 2);
-  pk = tmp + 4;
-
-  if (type != SILC_SKE_PK_TYPE_SILC)
-    goto out;
-
-  if (!silc_pkcs_public_key_decode(pk, pk_len, &public_key))
-    goto out;
-
+  if (tmp) {
+    /* Decode the public key */
+    SILC_GET16_MSB(pk_len, tmp);
+    SILC_GET16_MSB(type, tmp + 2);
+    pk = tmp + 4;
+    
+    if (type != SILC_SKE_PK_TYPE_SILC) {
+      COMMAND_REPLY_ERROR;
+      goto out;
+    }
+    
+    if (!silc_pkcs_public_key_decode(pk, pk_len, &public_key)) {
+      COMMAND_REPLY_ERROR;
+      goto out;
+    }
+  } 
+   
   id_type = silc_id_payload_get_type(idp);
   if (id_type == SILC_ID_CLIENT) {
     /* Received client's public key */
@@ -1958,8 +1838,10 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
                                         (void *)client_id, 
                                         NULL, NULL, 
                                         silc_hash_client_id_compare, NULL,
-                                        &id_cache))
+                                        &id_cache)) {
+      COMMAND_REPLY_ERROR;
       goto out;
+    }
 
     client_entry = (SilcClientEntry)id_cache->context;
 
@@ -1969,8 +1851,10 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
     /* Received server's public key */
     server_id = silc_id_payload_get_id(idp);
     if (!silc_idcache_find_by_id_one(conn->server_cache, (void *)server_id,
-                                    &id_cache))
+                                    &id_cache)) {
+      COMMAND_REPLY_ERROR;
       goto out;
+    }
 
     server_entry = (SilcServerEntry)id_cache->context;
 
@@ -1979,6 +1863,7 @@ SILC_CLIENT_CMD_REPLY_FUNC(getkey)
   }
 
  out:
+  SILC_CLIENT_PENDING_EXEC(cmd, SILC_COMMAND_GETKEY);
   SILC_CLIENT_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_GETKEY);
   if (idp)
     silc_id_payload_free(idp);