updates
[silc.git] / apps / silcd / command_reply.c
index 8945bb4191bfba1d34f506380a964354b95e6ffc..f83b9f9563da2c05c74e27fe7a316f784f0a3e74 100644 (file)
@@ -57,6 +57,7 @@ SilcServerCommandReply silc_command_reply_list[] =
   SILC_SERVER_CMD_REPLY(join, JOIN),
   SILC_SERVER_CMD_REPLY(users, USERS),
   SILC_SERVER_CMD_REPLY(getkey, GETKEY),
+  SILC_SERVER_CMD_REPLY(list, LIST),
 
   { NULL, 0 },
 };
@@ -133,11 +134,12 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
   SilcServer server = cmd->server;
   unsigned char *tmp, *id_data;
   char *nickname, *username, *realname, *servername = NULL;
+  unsigned char *fingerprint;
   SilcClientID *client_id;
   SilcClientEntry client;
   char global = FALSE;
   char *nick;
-  uint32 mode = 0, len, id_len;
+  uint32 mode = 0, len, id_len, flen;
 
   id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
   nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
@@ -159,6 +161,8 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
   if (!client_id)
     return FALSE;
 
+  fingerprint = silc_argument_get_arg_type(cmd->args, 9, &flen);
+
   /* Check if we have this client cached already. */
 
   client = silc_idlist_find_client_by_id(server->local_list, client_id, 
@@ -172,7 +176,7 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
   if (!client) {
     /* If router did not find such Client ID in its lists then this must
        be bogus client or some router in the net is buggy. */
-    if (server->server_type == SILC_ROUTER)
+    if (server->server_type != SILC_SERVER)
       return FALSE;
 
     /* Take hostname out of nick string if it includes it. */
@@ -227,6 +231,9 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
     silc_free(client_id);
   }
 
+  if (fingerprint && flen == sizeof(client->data.fingerprint))
+    memcpy(client->data.fingerprint, fingerprint, flen);
+
   return TRUE;
 }
 
@@ -299,7 +306,7 @@ silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
   if (!client) {
     /* If router did not find such Client ID in its lists then this must
        be bogus client or some router in the net is buggy. */
-    if (server->server_type == SILC_ROUTER)
+    if (server->server_type != SILC_SERVER)
       return FALSE;
 
     /* Take hostname out of nick string if it includes it. */
@@ -424,7 +431,7 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
     if (!client) {
       /* If router did not find such Client ID in its lists then this must
         be bogus client or some router in the net is buggy. */
-      if (server->server_type == SILC_ROUTER)
+      if (server->server_type != SILC_SERVER)
        goto error;
 
       /* Take nickname */
@@ -495,8 +502,8 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
                                                   server_id, FALSE, NULL);
     if (!server_entry) {
       /* If router did not find such Server ID in its lists then this must
-        be bogus client or some router in the net is buggy. */
-      if (server->server_type == SILC_ROUTER)
+        be bogus server or some router in the net is buggy. */
+      if (server->server_type != SILC_SERVER)
        goto error;
       
       /* We don't have that server anywhere, add it. */
@@ -529,15 +536,15 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
       channel = silc_idlist_find_channel_by_id(server->global_list, channel_id,
                                               NULL);
     if (!channel) {
-      /* If router did not find such Server ID in its lists then this must
-        be bogus client or some router in the net is buggy. */
-      if (server->server_type == SILC_ROUTER)
+      /* If router did not find such Channel ID in its lists then this must
+        be bogus channel or some router in the net is buggy. */
+      if (server->server_type != SILC_SERVER)
        goto error;
       
       /* We don't have that server anywhere, add it. */
       channel = silc_idlist_add_channel(server->global_list, strdup(name),
                                        SILC_CHANNEL_MODE_NONE, channel_id, 
-                                       server->router->connection
+                                       server->router, 
                                        NULL, NULL);
       if (!channel) {
        silc_free(channel_id);
@@ -798,6 +805,13 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
                    (created == 0 ? "existing" : "created"), channel_name,
                    silc_id_render(id, SILC_ID_CHANNEL)));
 
+    /* If the channel is found from global list we must move it to the
+       local list. */
+    entry = silc_idlist_find_channel_by_name(server->global_list, 
+                                            channel_name, &cache);
+    if (entry)
+      silc_idlist_del_channel(server->global_list, entry);
+
     /* Add the channel to our local list. */
     entry = silc_idlist_add_channel(server->local_list, strdup(channel_name), 
                                    SILC_CHANNEL_MODE_NONE, id, 
@@ -806,12 +820,13 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
       silc_free(id);
       goto out;
     }
+    server->stat.my_channels++;
   } else {
     /* The entry exists. */
-    if (cache->id)
-      silc_free(cache->id);
+    silc_free(cache->id);
     entry->id = id;
     cache->id = entry->id;
+    entry->disabled = FALSE;
 
     /* Remove the founder auth data if the mode is not set but we have
        them in the entry */
@@ -826,7 +841,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
 
   if (entry->hmac_name && hmac) {
     silc_free(entry->hmac_name);
-    entry->hmac_name = strdup(hmac->hmac->name);
+    entry->hmac_name = strdup(silc_hmac_get_name(hmac));
   }
 
   /* Get the ban list */
@@ -924,7 +939,7 @@ SILC_SERVER_CMD_REPLY_FUNC(users)
     if (!channel) {
       SilcBuffer idp;
 
-      if (server->server_type == SILC_ROUTER)
+      if (server->server_type != SILC_SERVER)
        goto out;
 
       idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
@@ -1057,8 +1072,8 @@ SILC_SERVER_CMD_REPLY_FUNC(getkey)
   }
 
  out:
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
-  SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_USERS);
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_GETKEY);
+  SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_GETKEY);
   if (idp)
     silc_id_payload_free(idp);
   silc_free(client_id);
@@ -1067,3 +1082,67 @@ SILC_SERVER_CMD_REPLY_FUNC(getkey)
     silc_pkcs_public_key_free(public_key);
   silc_server_command_reply_free(cmd);
 }
+
+SILC_SERVER_CMD_REPLY_FUNC(list)
+{
+  SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+  SilcServer server = cmd->server;
+  SilcCommandStatus status;
+  SilcChannelID *channel_id = NULL;
+  SilcChannelEntry channel;
+  uint32 len;
+  unsigned char *tmp, *name, *topic;
+  uint32 usercount = 0;
+
+  COMMAND_CHECK_STATUS_LIST;
+
+  tmp = silc_argument_get_arg_type(cmd->args, 2, &len);
+  channel_id = silc_id_payload_parse_id(tmp, len);
+  if (!channel_id)
+    goto out;
+
+  name = silc_argument_get_arg_type(cmd->args, 3, NULL);
+  topic = silc_argument_get_arg_type(cmd->args, 4, NULL);
+  tmp = silc_argument_get_arg_type(cmd->args, 5, NULL);
+  if (tmp)
+    SILC_GET32_MSB(usercount, tmp);
+
+  /* Add the channel entry if we do not have it already */
+  channel = silc_idlist_find_channel_by_id(server->local_list, 
+                                          channel_id, NULL);
+  if (!channel)
+    channel = silc_idlist_find_channel_by_id(server->global_list, 
+                                            channel_id, NULL);
+  if (!channel) {
+    /* If router did not find such Channel ID in its lists then this must
+       be bogus channel or some router in the net is buggy. */
+    if (server->server_type != SILC_SERVER)
+      goto out;
+    
+    channel = silc_idlist_add_channel(server->global_list, strdup(name),
+                                     SILC_CHANNEL_MODE_NONE, channel_id, 
+                                     server->router, 
+                                     NULL, NULL);
+    if (!channel)
+      goto out;
+    channel_id = NULL;
+  }
+
+  if (topic) {
+    silc_free(channel->topic);
+    channel->topic = strdup(topic);
+  }
+
+  /* Pending callbacks are not executed if this was an list entry */
+  if (status != SILC_STATUS_OK &&
+      status != SILC_STATUS_LIST_END) {
+    silc_server_command_reply_free(cmd);
+    return;
+  }
+
+ out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
+  SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_LIST);
+  silc_free(channel_id);
+  silc_server_command_reply_free(cmd);
+}