Send CMODE_CHANGE and CUMODE_CHANGE notifys to the original
[silc.git] / apps / silcd / command_reply.c
index ccb425f00d51b480495018e18a811caa1b1aecba..63b528fc707597b74692178965e269ab0149ff60 100644 (file)
 #include "command_reply.h"
 
 /* All functions that call the COMMAND_CHECK_STATUS macros must have
-   out: goto label. */
-
-#define COMMAND_CHECK_STATUS                                   \
-do {                                                           \
-  SILC_LOG_DEBUG(("Start"));                                   \
-  if (!silc_command_get_status(cmd->payload, &status, &error)) \
-    goto out;                                                  \
+   out: and err: goto labels. */
+
+#define COMMAND_CHECK_STATUS                                           \
+do {                                                                   \
+  SILC_LOG_DEBUG(("Start"));                                           \
+  if (!silc_command_get_status(cmd->payload, &status, &error)) {       \
+    if (SILC_STATUS_IS_ERROR(status))                                  \
+      goto out;                                                                \
+    if (status == SILC_STATUS_LIST_END)                                        \
+      goto out;                                                                \
+    goto err;                                                          \
+  }                                                                    \
 } while(0)
 
 /* Server command reply list. Not all commands have reply function as
@@ -115,21 +120,52 @@ void silc_server_command_reply_free(SilcServerCommandReplyContext cmd)
   }
 }
 
+static void 
+silc_server_command_process_error(SilcServerCommandReplyContext cmd,
+                                 SilcStatus error)
+{
+  SilcServer server = cmd->server;
+
+  /* If we received notify for invalid ID we'll remove the ID if we
+     have it cached. */
+  if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
+      cmd->sock->type == SILC_SOCKET_TYPE_ROUTER) {
+    SilcClientEntry client;
+    SilcUInt32 tmp_len;
+    unsigned char *tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
+    if (tmp) {
+      SilcClientID *client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
+      if (client_id) {
+       SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
+                       "the entry from cache"));
+       client = silc_idlist_find_client_by_id(server->global_list, 
+                                              client_id, FALSE, NULL);
+       if (client) {
+         silc_server_remove_from_channels(server, NULL, client, TRUE, 
+                                          NULL, TRUE);
+         silc_idlist_del_client(server->global_list, client);
+       }
+       silc_free(client_id);
+      }
+    }
+  }
+}
+
 /* Caches the received WHOIS information. */
 
 static char
 silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
 {
   SilcServer server = cmd->server;
-  unsigned char *tmp, *id_data;
+  unsigned char *tmp, *id_data, *umodes;
   char *nickname, *username, *realname, *servername = NULL;
   unsigned char *fingerprint;
   SilcClientID *client_id;
   SilcClientEntry client;
+  SilcIDCacheEntry cache = NULL;
   char global = FALSE;
   char *nick;
-  SilcUInt32 mode = 0, len, id_len, flen;
-  int expire = 0;
+  SilcUInt32 mode = 0, len, len2, 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);
@@ -210,19 +246,34 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
     client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
     client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
 
-    /* If client is global and is not on any channel then add that we'll
-       expire the entry after a while. */
-    if (global && !silc_hash_table_count(client->channels) &&
-       server->server_type == SILC_SERVER)
-      expire = time(NULL) + 300;
-
     /* Create new cache entry */
     silc_idcache_add(global ? server->global_list->clients :
                     server->local_list->clients, nick, client->id, 
-                    client, expire, NULL); 
+                    client, 0, &cache); 
     silc_free(client_id);
   }
 
+  /* Save channel list if it was sent to us */
+  if (server->server_type == SILC_SERVER) {
+    tmp = silc_argument_get_arg_type(cmd->args, 6, &len);
+    umodes = silc_argument_get_arg_type(cmd->args, 10, &len2);
+    if (tmp && umodes) {
+      SilcBufferStruct channels_buf, umodes_buf;
+      silc_buffer_set(&channels_buf, tmp, len);
+      silc_buffer_set(&umodes_buf, umodes, len2);
+      silc_server_save_user_channels(server, cmd->sock, client, &channels_buf,
+                                    &umodes_buf);
+    } else {
+      silc_server_save_user_channels(server, cmd->sock, client, NULL, NULL);
+    }
+
+    if (cache)
+      /* If client is global and is not on any channel then add that we'll
+        expire the entry after a while. */
+      if (global && !silc_hash_table_count(client->channels))
+       cache->expire = time(NULL) + 300;
+  }
+
   if (fingerprint && flen == sizeof(client->data.fingerprint))
     memcpy(client->data.fingerprint, fingerprint, flen);
 
@@ -238,8 +289,7 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
 SILC_SERVER_CMD_REPLY_FUNC(whois)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
-  SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
 
   COMMAND_CHECK_STATUS;
 
@@ -254,32 +304,14 @@ SILC_SERVER_CMD_REPLY_FUNC(whois)
   }
 
  out:
-  /* If we received notify for invalid ID we'll remove the ID if we
-     have it cached. */
-  if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
-      cmd->sock->type == SILC_SOCKET_TYPE_ROUTER) {
-    SilcClientEntry client;
-    SilcUInt32 tmp_len;
-    unsigned char *tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
-    if (tmp) {
-      SilcClientID *client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
-      if (client_id) {
-       SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
-                       "the entry from cache"));
-       client = silc_idlist_find_client_by_id(server->global_list, 
-                                              client_id, FALSE, NULL);
-       if (client) {
-         silc_server_remove_from_channels(server, NULL, client, TRUE, 
-                                          NULL, TRUE);
-         silc_idlist_del_client(server->global_list, client);
-       }
-       silc_free(client_id);
-      }
-    }
-  }
-
+  silc_server_command_process_error(cmd, error);
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOIS);
   silc_server_command_reply_free(cmd);
+  return;
+
+ err:
+  silc_server_command_process_error(cmd, error);
+  silc_server_command_reply_free(cmd);
 }
 
 /* Caches the received WHOWAS information for a short period of time. */
@@ -375,7 +407,7 @@ silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
 SILC_SERVER_CMD_REPLY_FUNC(whowas)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
 
   COMMAND_CHECK_STATUS;
 
@@ -391,6 +423,7 @@ SILC_SERVER_CMD_REPLY_FUNC(whowas)
 
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
+ err:
   silc_server_command_reply_free(cmd);
 }
 
@@ -568,7 +601,7 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
       if (server->server_type != SILC_SERVER)
        goto error;
       
-      /* We don't have that server anywhere, add it. */
+      /* We don't have that channel anywhere, add it. */
       channel = silc_idlist_add_channel(server->global_list, strdup(name),
                                        SILC_CHANNEL_MODE_NONE, channel_id, 
                                        server->router, NULL, NULL, 0);
@@ -599,8 +632,7 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
 SILC_SERVER_CMD_REPLY_FUNC(identify)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
-  SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
 
   COMMAND_CHECK_STATUS;
 
@@ -615,32 +647,14 @@ SILC_SERVER_CMD_REPLY_FUNC(identify)
   }
 
  out:
-  /* If we received notify for invalid ID we'll remove the ID if we
-     have it cached. */
-  if (error == SILC_STATUS_ERR_NO_SUCH_CLIENT_ID &&
-      cmd->sock->type == SILC_SOCKET_TYPE_ROUTER) {
-    SilcClientEntry client;
-    SilcUInt32 tmp_len;
-    unsigned char *tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
-    if (tmp) {
-      SilcClientID *client_id = silc_id_payload_parse_id(tmp, tmp_len, NULL);
-      if (client_id) {
-       SILC_LOG_DEBUG(("Received invalid client ID notification, deleting "
-                       "the entry from cache"));
-       client = silc_idlist_find_client_by_id(server->global_list, 
-                                              client_id, FALSE, NULL);
-       if (client) {
-         silc_server_remove_from_channels(server, NULL, client, TRUE, 
-                                          NULL, TRUE);
-         silc_idlist_del_client(server->global_list, client);
-       }
-       silc_free(client_id);
-      }
-    }
-  }
-
+  silc_server_command_process_error(cmd, error);
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_IDENTIFY);
   silc_server_command_reply_free(cmd);
+  return;
+
+ err:
+  silc_server_command_process_error(cmd, error);
+  silc_server_command_reply_free(cmd);
 }
 
 /* Received reply fro INFO command. Cache the server and its information */
@@ -649,7 +663,7 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcServerEntry entry;
   SilcServerID *server_id;
   SilcUInt32 tmp_len;
@@ -697,6 +711,7 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
 
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_INFO);
+ err:
   silc_server_command_reply_free(cmd);
 }
 
@@ -706,7 +721,7 @@ SILC_SERVER_CMD_REPLY_FUNC(motd)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcServerEntry entry = NULL;
   SilcServerID *server_id;
   SilcUInt32 tmp_len;
@@ -740,6 +755,7 @@ SILC_SERVER_CMD_REPLY_FUNC(motd)
 
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
+ err:
   silc_server_command_reply_free(cmd);
 
   if (entry)
@@ -755,7 +771,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
   SilcIDCacheEntry cache = NULL;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcChannelID *id;
   SilcClientID *client_id = NULL;
   SilcChannelEntry entry;
@@ -765,6 +781,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   char *channel_name, *tmp;
   SilcUInt32 mode, created;
   SilcBuffer keyp = NULL, client_id_list = NULL, client_mode_list = NULL;
+  SilcPublicKey founder_key = NULL;
 
   COMMAND_CHECK_STATUS;
 
@@ -844,6 +861,11 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   silc_buffer_pull_tail(client_mode_list, len);
   silc_buffer_put(client_mode_list, tmp, len);
 
+  /* Get founder key */
+  tmp = silc_argument_get_arg_type(cmd->args, 15, &len);
+  if (tmp)
+    silc_pkcs_public_key_decode(tmp, len, &founder_key);
+
   /* See whether we already have the channel. */
   entry = silc_idlist_find_channel_by_name(server->local_list, 
                                           channel_name, &cache);
@@ -870,6 +892,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
       goto out;
     }
     server->stat.my_channels++;
+    server->stat.channels++;
   } else {
     /* The entry exists. */
 
@@ -883,11 +906,17 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
        them in the entry */
     if (!(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && entry->founder_key) {
       silc_pkcs_public_key_free(entry->founder_key);
-      silc_free(entry->founder_passwd);
-      entry->founder_passwd = NULL;
+      entry->founder_key = NULL;
     }
   }
 
+  if (founder_key) {
+    if (entry->founder_key)
+      silc_pkcs_public_key_free(entry->founder_key);
+    entry->founder_key = founder_key;
+    founder_key = NULL;
+  }
+
   if (entry->hmac_name && hmac) {
     silc_free(entry->hmac_name);
     entry->hmac_name = strdup(silc_hmac_get_name(hmac));
@@ -939,12 +968,15 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   silc_server_save_users_on_channel(server, cmd->sock, entry, 
                                    client_id, client_id_list,
                                    client_mode_list, list_count);
+  entry->users_resolved = TRUE;
 
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_JOIN);
+ err:
   silc_free(client_id);
   silc_server_command_reply_free(cmd);
 
+  silc_pkcs_public_key_free(founder_key);
   if (client_id_list)
     silc_buffer_free(client_id_list);
   if (client_mode_list)
@@ -957,7 +989,7 @@ SILC_SERVER_CMD_REPLY_FUNC(stats)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   unsigned char *tmp;
   SilcUInt32 tmp_len;
   SilcBufferStruct buf;
@@ -988,14 +1020,16 @@ SILC_SERVER_CMD_REPLY_FUNC(stats)
   }
 
  out:
-  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_STATS);
+ err:
+  silc_server_command_reply_free(cmd);
 }
 
 SILC_SERVER_CMD_REPLY_FUNC(users)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcChannelEntry channel;
   SilcChannelID *channel_id = NULL;
   SilcBuffer client_id_list;
@@ -1027,7 +1061,7 @@ SILC_SERVER_CMD_REPLY_FUNC(users)
        goto out;
 
       idp = silc_id_payload_encode(channel_id, SILC_ID_CHANNEL);
-      silc_server_send_command(server, server->router->connection,
+      silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
                               SILC_COMMAND_IDENTIFY, ++server->cmd_ident,
                               1, 5, idp->data, idp->len);
       silc_buffer_free(idp);
@@ -1071,12 +1105,16 @@ SILC_SERVER_CMD_REPLY_FUNC(users)
                                    client_id_list, client_mode_list, 
                                    list_count);
 
+  channel->global_users = silc_server_channel_has_global(channel);
+  channel->users_resolved = TRUE;
+
   silc_buffer_free(client_id_list);
   silc_buffer_free(client_mode_list);
 
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_USERS);
   silc_free(channel_id);
+ err:
   silc_server_command_reply_free(cmd);
 }
 
@@ -1084,7 +1122,7 @@ SILC_SERVER_CMD_REPLY_FUNC(getkey)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcClientEntry client = NULL;
   SilcServerEntry server_entry = NULL;
   SilcClientID *client_id = NULL;
@@ -1164,6 +1202,7 @@ SILC_SERVER_CMD_REPLY_FUNC(getkey)
   silc_free(server_id);
   if (public_key)
     silc_pkcs_public_key_free(public_key);
+ err:
   silc_server_command_reply_free(cmd);
 }
 
@@ -1171,7 +1210,7 @@ SILC_SERVER_CMD_REPLY_FUNC(list)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
-  SilcCommandStatus status, error;
+  SilcStatus status, error;
   SilcChannelID *channel_id = NULL;
   SilcChannelEntry channel;
   SilcIDCacheEntry cache;
@@ -1241,5 +1280,19 @@ SILC_SERVER_CMD_REPLY_FUNC(list)
  out:
   SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_LIST);
   silc_free(channel_id);
+ err:
+  silc_server_command_reply_free(cmd);
+}
+
+SILC_SERVER_CMD_REPLY_FUNC(watch)
+{
+  SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+  SilcStatus status, error;
+
+  COMMAND_CHECK_STATUS;
+
+ out:
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WATCH);
+ err:
   silc_server_command_reply_free(cmd);
 }