Code auditing weekend results and fixes committing.
[silc.git] / lib / silcclient / command.c
index 29eceb036b15b17c6ff628f4dcfa5c362c5f8a27..97d074822e6621e51f14714b116e7c734836ca2f 100644 (file)
@@ -56,7 +56,7 @@ SilcClientCommand silc_command_list[] =
   SILC_CLIENT_CMD(silcoper, SILCOPER, "SILOPER",
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER, 2),
   SILC_CLIENT_CMD(leave, LEAVE, "LEAVE", SILC_CF_LAG | SILC_CF_REG, 2),
-  SILC_CLIENT_CMD(names, NAMES, "NAMES", SILC_CF_LAG | SILC_CF_REG, 2),
+  SILC_CLIENT_CMD(users, USERS, "USERS", SILC_CF_LAG | SILC_CF_REG, 2),
 
   { NULL, 0, NULL, 0, 0 },
 };
@@ -74,21 +74,19 @@ SilcClientCommand silc_command_list[] =
 #define COMMAND_ERROR cmd->client->ops->command(cmd->client, cmd->conn, \
   cmd, FALSE, cmd->command->cmd)
 
-/* List of pending commands. */
-SilcClientCommandPending *silc_command_pending = NULL;
-
 /* Generic function to send any command. The arguments must be sent already
    encoded into correct form in correct order. */
 
 void silc_client_send_command(SilcClient client, SilcClientConnection conn,
-                             SilcCommand command, unsigned int argc, ...)
+                             SilcCommand command, unsigned short ident,
+                             unsigned int argc, ...)
 {
   SilcBuffer packet;
   va_list ap;
 
   va_start(ap, argc);
 
-  packet = silc_command_payload_encode_vap(command, 0, argc, ap);
+  packet = silc_command_payload_encode_vap(command, ident, argc, ap);
   silc_client_packet_send(client, conn->sock, SILC_PACKET_COMMAND, 
                          NULL, 0, NULL, NULL, packet->data, 
                          packet->len, TRUE);
@@ -110,63 +108,66 @@ SilcClientCommand *silc_client_command_find(const char *name)
   return NULL;
 }
 
-/* Add new pending command to the list of pending commands. Currently
-   pending commands are executed from command replies, thus we can
-   execute any command after receiving some specific command reply.
-
-   The argument `reply_cmd' is the command reply from where the callback
-   function is to be called, thus, it IS NOT the command to be executed.
-
-   XXX: If needed in the future this support may be extended for
-   commands as well, when any command could be executed after executing
-   some specific command. */
+/* 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'. */
 
-void silc_client_command_pending(SilcCommand reply_cmd,
-                                SilcClientCommandCallback callback,
+void silc_client_command_pending(SilcClientConnection conn,
+                                SilcCommand reply_cmd,
+                                unsigned short ident,
+                                SilcCommandCb callback,
                                 void *context)
 {
-  SilcClientCommandPending *reply, *r;
+  SilcClientCommandPending *reply;
 
   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);
+}
 
-  if (silc_command_pending == NULL) {
-    silc_command_pending = reply;
-    return;
-  }
+/* Deletes pending command by reply command type. */
+
+void silc_client_command_pending_del(SilcClientConnection conn,
+                                    SilcCommand reply_cmd,
+                                    unsigned short ident)
+{
+  SilcClientCommandPending *r;
 
-  for (r = silc_command_pending; r; r = r->next) {
-    if (r->next == NULL) {
-      r->next = reply;
+  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;
     }
   }
 }
 
-/* Deletes pending command by reply command type. */
+/* Checks for pending commands and marks callbacks to be called from
+   the command reply function. Returns TRUE if there were pending command. */
 
-void silc_client_command_pending_del(SilcCommand reply_cmd)
+int silc_client_command_pending_check(SilcClientConnection conn,
+                                     SilcClientCommandReplyContext ctx,
+                                     SilcCommand command, 
+                                     unsigned short ident)
 {
-  SilcClientCommandPending *r, *tmp;
-  
-  if (silc_command_pending) {
-    if (silc_command_pending->reply_cmd == reply_cmd) {
-      silc_free(silc_command_pending);
-      silc_command_pending = NULL;
-      return;
-    }
-
-    for (r = silc_command_pending; r; r = r->next) {
-      if (r->next && r->next->reply_cmd == reply_cmd) {
-       tmp = r->next;
-       r->next = r->next->next;
-       silc_free(tmp);
-       break;
-      }
+  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;
+      ctx->ident = ident;
+      return TRUE;
     }
   }
+
+  return FALSE;
 }
 
 /* Free command context and its internals */
@@ -445,7 +446,7 @@ SILC_CLIENT_CMD_FUNC(invite)
 
     /* Client entry not found, it was requested thus mark this to be
        pending command. */
-    silc_client_command_pending(SILC_COMMAND_IDENTIFY, 
+    silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, 0,
                                silc_client_command_invite, context);
     return;
   }
@@ -588,7 +589,13 @@ SILC_CLIENT_CMD_FUNC(ping)
                          0, NULL, NULL, buffer->data, buffer->len, TRUE);
   silc_buffer_free(buffer);
 
-  id = silc_id_str2id(conn->remote_id_data, SILC_ID_SERVER);
+  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;
+    goto out;
+  }
 
   /* Start counting time */
   for (i = 0; i < conn->ping_count; i++) {
@@ -665,12 +672,12 @@ SILC_CLIENT_CMD_FUNC(join)
   idp = silc_id_payload_encode(conn->local_id, SILC_ID_CLIENT);
 
   /* Send JOIN command to the server */
-  if (cmd->argc == 3)
+  if (cmd->argc == 2)
     buffer = 
       silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 2,
                                     1, cmd->argv[1], cmd->argv_lens[1],
                                     2, idp->data, idp->len);
-  else if (cmd->argc == 4)
+  else if (cmd->argc == 3)
     /* XXX Buggy */
     buffer = 
       silc_command_payload_encode_va(SILC_COMMAND_JOIN, 0, 3,
@@ -988,7 +995,7 @@ SILC_CLIENT_CMD_FUNC(cumode)
   if (!client_entry) {
     /* Client entry not found, it was requested thus mark this to be
        pending command. */
-    silc_client_command_pending(SILC_COMMAND_CUMODE, 
+    silc_client_command_pending(conn, SILC_COMMAND_CUMODE, 0,  
                                silc_client_command_cumode, context);
     return;
   }
@@ -1164,14 +1171,15 @@ SILC_CLIENT_CMD_FUNC(leave)
   silc_client_command_free(cmd);
 }
 
-/* Command NAMES. Requests the names of the clients joined on requested
+/* Command USERS. Requests the USERS of the clients joined on requested
    channel. */
 
-SILC_CLIENT_CMD_FUNC(names)
+SILC_CLIENT_CMD_FUNC(users)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
   SilcIDCacheEntry id_cache = NULL;
+  SilcChannelEntry channel;
   SilcBuffer buffer, idp;
   char *name;
 
@@ -1182,7 +1190,7 @@ SILC_CLIENT_CMD_FUNC(names)
   }
 
   if (cmd->argc != 2) {
-    cmd->client->ops->say(cmd->client, conn, "Usage: /NAMES <channel>");
+    cmd->client->ops->say(cmd->client, conn, "Usage: /USERS <channel>");
     COMMAND_ERROR;
     goto out;
   }
@@ -1213,25 +1221,79 @@ SILC_CLIENT_CMD_FUNC(names)
     goto out;
   }
 
-  /* Send NAMES command to the server */
-  idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL);
-  buffer = silc_command_payload_encode_va(SILC_COMMAND_NAMES, 0, 1, 
-                                         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);
+  channel = (SilcChannelEntry)id_cache->context;
+
+  if (!cmd->pending) {
+    /* Send USERS command to the server */
+    idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL);
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_USERS, 0, 1, 
+                                           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);
+
+    /* Register pending callback which will recall this command callback with
+       same context and reprocesses the command. When reprocessing we actually
+       display the information on the screen. */
+    silc_client_command_pending(conn, SILC_COMMAND_USERS, 0, 
+                               silc_client_command_users, context);
+    cmd->pending = TRUE;
+    return;
+  }
+
+  if (cmd->pending) {
+    /* Pending command. Now we've resolved the information from server and
+       we are ready to display the information on screen. */
+    int i;
+    SilcChannelUser chu;
+
+    cmd->client->ops->say(cmd->client, conn, "Users on %s", 
+                         channel->channel_name);
+
+    silc_list_start(channel->clients);
+    while ((chu = silc_list_get(channel->clients)) != SILC_LIST_END) {
+      SilcClientEntry e = chu->client;
+      char *m, tmp[80], line[80], len1;
+
+      memset(line, 0, sizeof(line));
+      memset(tmp, 0, sizeof(tmp));
+      m = silc_client_chumode_char(chu->mode);
 
-  /* Register dummy pending command that will tell the reply command
-     that user called this command. Server may send reply to this command
-     even if user did not send this command thus we want to handle things
-     differently when user sent the command. This is dummy and won't be
-     executed. */
-  /* XXX this is kludge and should be removed after pending command reply 
-     support is added. Currently only commands may be pending not command
-     replies. */
-  silc_client_command_pending(SILC_COMMAND_NAMES, 
-                             silc_client_command_names, NULL);
+      strcat(line, " ");
+      strcat(line, e->nickname);
+      strcat(line, e->server ? "@" : "");
+
+      len1 = 0;
+      if (e->server)
+       len1 = strlen(e->server);
+      strncat(line, e->server ? e->server : "", len1 > 30 ? 30 : len1);
+
+      len1 = strlen(line);
+      if (len1 >= 30) {
+       memset(&line[29], 0, len1 - 29);
+      } else {
+       for (i = 0; i < 30 - len1 - 1; i++)
+         strcat(line, " ");
+      }
+
+      strcat(line, "  H");
+      strcat(tmp, m ? m : "");
+      strcat(line, tmp);
+
+      if (strlen(tmp) < 5)
+       for (i = 0; i < 5 - strlen(tmp); i++)
+         strcat(line, " ");
+
+      strcat(line, e->username ? e->username : "");
+
+      cmd->client->ops->say(cmd->client, conn, "%s", line);
+
+      if (m)
+       silc_free(m);
+    }
+  }
 
   /* Notify application */
   COMMAND;