CONNECT, CLOSE and SHUTDOWN commands.
[silc.git] / lib / silcclient / command.c
index 16990b38cf0c592e3e1beec45ad1b9a70a795f0c..288a138af4d461306a88901fd94a3659ee7c9edb 100644 (file)
@@ -32,7 +32,7 @@ SilcClientCommand silc_command_list[] =
   SILC_CLIENT_CMD(list, LIST, "LIST", SILC_CF_LAG | SILC_CF_REG, 2),
   SILC_CLIENT_CMD(topic, TOPIC, "TOPIC", SILC_CF_LAG | SILC_CF_REG, 3),
   SILC_CLIENT_CMD(invite, INVITE, "INVITE", SILC_CF_LAG | SILC_CF_REG, 3),
-  SILC_CLIENT_CMD(quit, QUIT, "QUIT", SILC_CF_LAG | SILC_CF_REG, 1),
+  SILC_CLIENT_CMD(quit, QUIT, "QUIT", SILC_CF_LAG | SILC_CF_REG, 2),
   SILC_CLIENT_CMD(kill, KILL, "KILL", 
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
   SILC_CLIENT_CMD(info, INFO, "INFO", SILC_CF_LAG | SILC_CF_REG, 2),
@@ -46,12 +46,12 @@ SilcClientCommand silc_command_list[] =
   SILC_CLIENT_CMD(umode, UMODE, "UMODE", SILC_CF_LAG | SILC_CF_REG, 2),
   SILC_CLIENT_CMD(cmode, CMODE, "CMODE", SILC_CF_LAG | SILC_CF_REG, 4),
   SILC_CLIENT_CMD(cumode, CUMODE, "CUMODE", SILC_CF_LAG | SILC_CF_REG, 5),
-  SILC_CLIENT_CMD(kick, KICK, "KICK", SILC_CF_LAG | SILC_CF_REG, 2),
+  SILC_CLIENT_CMD(kick, KICK, "KICK", SILC_CF_LAG | SILC_CF_REG, 4),
   SILC_CLIENT_CMD(restart, RESTART, "RESTART",
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
   SILC_CLIENT_CMD(close, CLOSE, "CLOSE",
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
-  SILC_CLIENT_CMD(die, DIE, "DIE",
+  SILC_CLIENT_CMD(shutdown, SHUTDOWN, "SHUTDOWN",
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_OPER, 2),
   SILC_CLIENT_CMD(silcoper, SILCOPER, "SILOPER",
                  SILC_CF_LAG | SILC_CF_REG | SILC_CF_SILC_OPER, 2),
@@ -117,6 +117,7 @@ SilcClientCommand *silc_client_command_find(const char *name)
 void silc_client_command_pending(SilcClientConnection conn,
                                 SilcCommand reply_cmd,
                                 unsigned short ident,
+                                SilcClientPendingDestructor destructor,
                                 SilcCommandCb callback,
                                 void *context)
 {
@@ -127,6 +128,7 @@ void silc_client_command_pending(SilcClientConnection conn,
   reply->ident = ident;
   reply->context = context;
   reply->callback = callback;
+  reply->destructor = destructor;
   silc_dlist_add(conn->pending_commands, reply);
 }
 
@@ -162,6 +164,7 @@ int silc_client_command_pending_check(SilcClientConnection conn,
     if (r->reply_cmd == command && r->ident == ident) {
       ctx->context = r->context;
       ctx->callback = r->callback;
+      ctx->destructor = r->destructor;
       ctx->ident = ident;
       return TRUE;
     }
@@ -170,19 +173,50 @@ int silc_client_command_pending_check(SilcClientConnection conn,
   return FALSE;
 }
 
+/* Allocate Command Context */
+
+SilcClientCommandContext silc_client_command_alloc()
+{
+  SilcClientCommandContext ctx = silc_calloc(1, sizeof(*ctx));
+  ctx->users++;
+  return ctx;
+}
+
 /* Free command context and its internals */
 
-void silc_client_command_free(SilcClientCommandContext cmd)
+void silc_client_command_free(SilcClientCommandContext ctx)
 {
-  int i;
+  ctx->users--;
+  SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users + 1,
+                 ctx->users));
+  if (ctx->users < 1) {
+    int i;
 
-  if (cmd) {
-    for (i = 0; i < cmd->argc; i++)
-      silc_free(cmd->argv[i]);
-    silc_free(cmd);
+    for (i = 0; i < ctx->argc; i++)
+      silc_free(ctx->argv[i]);
+    silc_free(ctx);
   }
 }
 
+/* Duplicate Command Context by adding reference counter. The context won't
+   be free'd untill it hits zero. */
+
+SilcClientCommandContext 
+silc_client_command_dup(SilcClientCommandContext ctx)
+{
+  ctx->users++;
+  SILC_LOG_DEBUG(("Command context %p refcnt %d->%d", ctx, ctx->users - 1,
+                 ctx->users));
+  return ctx;
+}
+
+/* Pending command destructor. */
+
+static void silc_client_command_destructor(void *context)
+{
+  silc_client_command_free((SilcClientCommandContext)context);
+}
+
 /* Command WHOIS. This command is used to query information about 
    specific user. */
 
@@ -437,7 +471,8 @@ SILC_CLIENT_CMD_FUNC(invite)
   }
 
   /* Find client entry */
-  client_entry = silc_idlist_get_client(client, conn, nickname, server, num);
+  client_entry = silc_idlist_get_client(client, conn, nickname, server, num,
+                                       TRUE);
   if (!client_entry) {
     if (nickname)
       silc_free(nickname);
@@ -447,7 +482,10 @@ SILC_CLIENT_CMD_FUNC(invite)
     /* Client entry not found, it was requested thus mark this to be
        pending command. */
     silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, 0,
-                               silc_client_command_invite, context);
+                               silc_client_command_destructor,
+                               silc_client_command_invite, 
+                               silc_client_command_dup(cmd));
+    cmd->pending = 1;
     return;
   }
 
@@ -482,12 +520,29 @@ SILC_CLIENT_CMD_FUNC(invite)
   silc_client_command_free(cmd);
 }
 
+typedef struct {
+  SilcClient client;
+  SilcClientConnection conn;
+} *QuitInternal;
+
+SILC_TASK_CALLBACK(silc_client_command_quit_cb)
+{
+  QuitInternal q = (QuitInternal)context;
+
+  /* Close connection */
+  q->client->ops->disconnect(q->client, q->conn);
+  silc_client_close_connection(q->client, q->conn->sock);
+
+  silc_free(q);
+}
+
 /* Command QUIT. Closes connection with current server. */
  
 SILC_CLIENT_CMD_FUNC(quit)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcBuffer buffer;
+  QuitInternal q;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
@@ -495,20 +550,26 @@ SILC_CLIENT_CMD_FUNC(quit)
     goto out;
   }
 
-  buffer = silc_command_payload_encode(SILC_COMMAND_QUIT, cmd->argc - 1, 
-                                      ++cmd->argv, ++cmd->argv_lens,
-                                      ++cmd->argv_types, 0);
+  if (cmd->argc > 1)
+    buffer = silc_command_payload_encode(SILC_COMMAND_QUIT, cmd->argc - 1, 
+                                        &cmd->argv[1], &cmd->argv_lens[1],
+                                        &cmd->argv_types[1], 0);
+  else
+    buffer = silc_command_payload_encode(SILC_COMMAND_QUIT, 0,
+                                        NULL, NULL, NULL, 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--;
 
-  /* Close connection */
-  cmd->client->ops->disconnect(cmd->client, cmd->conn);
-  silc_client_close_connection(cmd->client, cmd->conn->sock);
+  q = silc_calloc(1, sizeof(*q));
+  q->client = cmd->client;
+  q->conn = cmd->conn;
+
+  /* We quit the connection with little timeout */
+  silc_task_register(cmd->client->timeout_queue, cmd->conn->sock->sock,
+                    silc_client_command_quit_cb, (void *)q,
+                    1, 0, SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
 
   /* Notify application */
   COMMAND;
@@ -991,12 +1052,15 @@ SILC_CLIENT_CMD_FUNC(cumode)
 
   /* Find client entry */
   client_entry = silc_idlist_get_client(cmd->client, conn, 
-                                       nickname, server, num);
+                                       nickname, server, num, TRUE);
   if (!client_entry) {
     /* Client entry not found, it was requested thus mark this to be
        pending command. */
     silc_client_command_pending(conn, SILC_COMMAND_CUMODE, 0,  
-                               silc_client_command_cumode, context);
+                               silc_client_command_destructor,
+                               silc_client_command_cumode, 
+                               silc_client_command_dup(cmd));
+    cmd->pending = 1;
     return;
   }
   
@@ -1075,7 +1139,94 @@ SILC_CLIENT_CMD_FUNC(kick)
 {
   SilcClientCommandContext cmd = (SilcClientCommandContext)context;
   SilcClientConnection conn = cmd->conn;
+  SilcIDCacheEntry id_cache = NULL;
+  SilcChannelEntry channel;
+  SilcBuffer buffer, idp, idp2;
+  SilcClientEntry target;
+  char *name;
+  unsigned int num = 0;
+  char *nickname = NULL, *server = NULL;
+
+  if (!cmd->conn) {
+    SILC_NOT_CONNECTED(cmd->client, cmd->conn);
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  if (cmd->argc < 3) {
+    cmd->client->ops->say(cmd->client, conn, 
+                         "Usage: /KICK <channel> <client> [<comment>]");
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  if (cmd->argv[1][0] == '*') {
+    if (!conn->current_channel) {
+      cmd->client->ops->say(cmd->client, conn, "You are not on any channel");
+      COMMAND_ERROR;
+      goto out;
+    }
+    name = conn->current_channel->channel_name;
+  } else {
+    name = cmd->argv[1];
+  }
 
+  if (!conn->current_channel) {
+    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  /* Get the Channel ID of the channel */
+  if (!silc_idcache_find_by_data_one(conn->channel_cache, name, &id_cache)) {
+    cmd->client->ops->say(cmd->client, conn, "You are not on that channel");
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  channel = (SilcChannelEntry)id_cache->context;
+
+  /* Parse the typed nickname. */
+  if (!silc_parse_nickname(cmd->argv[2], &nickname, &server, &num)) {
+    cmd->client->ops->say(cmd->client, conn, "Bad nickname");
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  /* Get the target client */
+  target = silc_idlist_get_client(cmd->client, conn, nickname, 
+                                 server, num, FALSE);
+  if (!target) {
+    cmd->client->ops->say(cmd->client, conn, "No such client: %s",
+                         cmd->argv[2]);
+    COMMAND_ERROR;
+    goto out;
+  }
+
+  /* Send KICK command to the server */
+  idp = silc_id_payload_encode(id_cache->id, SILC_ID_CHANNEL);
+  idp2 = silc_id_payload_encode(target->id, SILC_ID_CLIENT);
+  if (cmd->argc == 3)
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_KICK, 0, 2, 
+                                           1, idp->data, idp->len,
+                                           2, idp2->data, idp2->len);
+  else
+    buffer = silc_command_payload_encode_va(SILC_COMMAND_KICK, 0, 3, 
+                                           1, idp->data, idp->len,
+                                           2, idp2->data, idp2->len,
+                                           3, cmd->argv[3], 
+                                           strlen(cmd->argv[3]));
+  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(idp2);
+
+  /* Notify application */
+  COMMAND;
+
+ out:
+  silc_client_command_free(cmd);
 }
 
 SILC_CLIENT_CMD_FUNC(restart)
@@ -1086,7 +1237,7 @@ SILC_CLIENT_CMD_FUNC(close)
 {
 }
  
-SILC_CLIENT_CMD_FUNC(die)
+SILC_CLIENT_CMD_FUNC(shutdown)
 {
 }
  
@@ -1181,7 +1332,8 @@ SILC_CLIENT_CMD_FUNC(users)
   SilcIDCacheEntry id_cache = NULL;
   SilcChannelEntry channel;
   SilcBuffer buffer, idp;
-  char *name;
+  char *name, *line = NULL;
+  unsigned int line_len = 0;
 
   if (!cmd->conn) {
     SILC_NOT_CONNECTED(cmd->client, cmd->conn);
@@ -1238,7 +1390,9 @@ SILC_CLIENT_CMD_FUNC(users)
        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);
+                               silc_client_command_destructor,
+                               silc_client_command_users, 
+                               silc_client_command_dup(cmd));
     cmd->pending = TRUE;
     return;
   }
@@ -1252,18 +1406,27 @@ SILC_CLIENT_CMD_FUNC(users)
     cmd->client->ops->say(cmd->client, conn, "Users on %s", 
                          channel->channel_name);
 
+    line = silc_calloc(4096, sizeof(*line));
+    line_len = 4096;
     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;
+      char *m, tmp[80], len1;
+
+      memset(line, 0, sizeof(line_len));
+
+      if (strlen(e->nickname) + strlen(e->server) + 100 > line_len) {
+       silc_free(line);
+       line_len += strlen(e->nickname) + strlen(e->server) + 100;
+       line = silc_calloc(line_len, sizeof(*line));
+      }
 
-      memset(line, 0, sizeof(line));
       memset(tmp, 0, sizeof(tmp));
       m = silc_client_chumode_char(chu->mode);
 
-      strcat(line, " ");
-      strcat(line, e->nickname);
-      strcat(line, e->server ? "@" : "");
+      strncat(line, " ", 1);
+      strncat(line, e->nickname, strlen(e->nickname));
+      strncat(line, e->server ? "@" : "", 1);
 
       len1 = 0;
       if (e->server)
@@ -1278,9 +1441,9 @@ SILC_CLIENT_CMD_FUNC(users)
          strcat(line, " ");
       }
 
-      strcat(line, "  H");
+      strncat(line, "  H", 3);
       strcat(tmp, m ? m : "");
-      strcat(line, tmp);
+      strncat(line, tmp, strlen(tmp));
 
       if (strlen(tmp) < 5)
        for (i = 0; i < 5 - strlen(tmp); i++)
@@ -1295,6 +1458,9 @@ SILC_CLIENT_CMD_FUNC(users)
     }
   }
 
+  if (line)
+    silc_free(line);
+
   /* Notify application */
   COMMAND;