updates.
[silc.git] / apps / silcd / command_reply.c
index 6dac16d1e42fcd9631905543a695b16748883915..79eb2bbeabebbc518b25719608d2e336f2e17d5f 100644 (file)
@@ -54,6 +54,7 @@ SilcServerCommandReply silc_command_reply_list[] =
   SILC_SERVER_CMD_REPLY(whowas, WHOWAS),
   SILC_SERVER_CMD_REPLY(identify, IDENTIFY),
   SILC_SERVER_CMD_REPLY(info, INFO),
+  SILC_SERVER_CMD_REPLY(motd, MOTD),
   SILC_SERVER_CMD_REPLY(join, JOIN),
   SILC_SERVER_CMD_REPLY(users, USERS),
 
@@ -121,9 +122,7 @@ void silc_server_command_reply_free(SilcServerCommandReplyContext cmd)
   }
 }
 
-/* Caches the received WHOIS information. If we are normal server currently
-   we cache global information only for short period of time.  */
-/* XXX cache expirying not implemented yet! */
+/* Caches the received WHOIS information. */
 
 static char
 silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
@@ -187,7 +186,7 @@ silc_server_command_reply_whois_save(SilcServerCommandReplyContext cmd)
     /* We don't have that client anywhere, add it. The client is added
        to global list since server didn't have it in the lists so it must be 
        global. */
-    client = silc_idlist_add_client(server->global_list, nick,
+    client = silc_idlist_add_client(server->global_list, nick, strlen(nick),
                                    strdup(username), 
                                    strdup(realname), client_id, 
                                    cmd->sock->user_data, NULL);
@@ -314,9 +313,8 @@ silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
     /* We don't have that client anywhere, add it. The client is added
        to global list since server didn't have it in the lists so it must be 
        global. */
-    client = silc_idlist_add_client(server->global_list, nick,
-                                   strdup(username), 
-                                   strdup(realname), 
+    client = silc_idlist_add_client(server->global_list, nick, strlen(nick),
+                                   strdup(username), strdup(realname), 
                                    silc_id_dup(client_id, SILC_ID_CLIENT), 
                                    cmd->sock->user_data, NULL);
     if (!client)
@@ -435,7 +433,7 @@ silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
     /* We don't have that client anywhere, add it. The client is added
        to global list since server didn't have it in the lists so it must be 
        global. */
-    client = silc_idlist_add_client(server->global_list, nick,
+    client = silc_idlist_add_client(server->global_list, nick, strlen(nick),
                                    username ? strdup(username) : NULL, NULL,
                                    client_id, cmd->sock->user_data, NULL);
     client->data.registered = TRUE;
@@ -524,7 +522,7 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
   if (!server_id)
     goto out;
 
-  /* Get the info string */
+  /* Get the name */
   name = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
   if (tmp_len > 256)
     goto out;
@@ -560,6 +558,53 @@ SILC_SERVER_CMD_REPLY_FUNC(info)
   silc_server_command_reply_free(cmd);
 }
 
+/* Received reply fro MOTD command. */
+
+SILC_SERVER_CMD_REPLY_FUNC(motd)
+{
+  SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+  SilcServer server = cmd->server;
+  SilcCommandStatus status;
+  SilcServerEntry entry;
+  SilcServerID *server_id;
+  unsigned int tmp_len;
+  unsigned char *tmp;
+
+  COMMAND_CHECK_STATUS;
+
+  /* Get Server ID */
+  tmp = silc_argument_get_arg_type(cmd->args, 2, &tmp_len);
+  if (!tmp)
+    goto out;
+  server_id = silc_id_payload_parse_id(tmp, tmp_len);
+  if (!server_id)
+    goto out;
+
+  entry = silc_idlist_find_server_by_id(server->local_list, server_id, NULL);
+  if (!entry) {
+    entry = silc_idlist_find_server_by_id(server->global_list, server_id, 
+                                         NULL);
+    if (!entry)
+      goto out;
+  }
+
+  /* Get the motd */
+  tmp = silc_argument_get_arg_type(cmd->args, 3, &tmp_len);
+  if (tmp_len > 256)
+    tmp = NULL;
+
+  entry->motd = tmp;
+
+  /* Execute any pending commands */
+  SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_MOTD);
+
+  entry->motd = NULL;
+
+ out:
+  SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_MOTD);
+  silc_server_command_reply_free(cmd);
+}
+
 /* Received reply for forwarded JOIN command. Router has created or joined
    the client to the channel. We save some channel information locally
    for future use. */
@@ -568,6 +613,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
 {
   SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
   SilcServer server = cmd->server;
+  SilcIDCacheEntry cache = NULL;
   SilcCommandStatus status;
   SilcChannelID *id;
   SilcClientID *client_id = NULL;
@@ -577,7 +623,7 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   unsigned char *id_string;
   char *channel_name, *tmp;
   unsigned int mode, created;
-  SilcBuffer keyp, client_id_list, client_mode_list;
+  SilcBuffer keyp = NULL, client_id_list, client_mode_list;
 
   COMMAND_CHECK_STATUS;
 
@@ -615,11 +661,11 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
 
   /* Get channel key */
   tmp = silc_argument_get_arg_type(cmd->args, 7, &len);
-  if (!tmp)
-    goto out;
-  keyp = silc_buffer_alloc(len);
-  silc_buffer_pull_tail(keyp, SILC_BUFFER_END(keyp));
-  silc_buffer_put(keyp, tmp, len);
+  if (tmp) {
+    keyp = silc_buffer_alloc(len);
+    silc_buffer_pull_tail(keyp, SILC_BUFFER_END(keyp));
+    silc_buffer_put(keyp, tmp, len);
+  }
 
   id = silc_id_payload_parse_id(id_string, id_len);
   if (!id)
@@ -657,7 +703,8 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   silc_buffer_put(client_mode_list, tmp, len);
 
   /* See whether we already have the channel. */
-  entry = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
+  entry = silc_idlist_find_channel_by_name(server->local_list, 
+                                          channel_name, &cache);
   if (!entry) {
     /* Add new channel */
 
@@ -674,7 +721,52 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
       goto out;
     }
   } else {
-    silc_free(id);
+    /* The entry exists. */
+    if (entry->id)
+      silc_free(entry->id);
+    entry->id = id;
+    cache->id = entry->id;
+
+    /* Remove the founder auth data if the mode is not set but we have
+       them in the entry */
+    if (!(mode & SILC_CHANNEL_MODE_FOUNDER_AUTH) && entry->founder_key) {
+      silc_pkcs_public_key_free(entry->founder_key);
+      if (entry->founder_passwd) {
+       silc_free(entry->founder_passwd);
+       entry->founder_passwd = NULL;
+      }
+    }
+  }
+
+  if (entry->hmac_name && hmac) {
+    silc_free(entry->hmac_name);
+    entry->hmac_name = strdup(hmac->hmac->name);
+  }
+
+  /* Get the ban list */
+  tmp = silc_argument_get_arg_type(cmd->args, 8, &len);
+  if (tmp) {
+    if (entry->ban_list)
+      silc_free(entry->ban_list);
+    entry->ban_list = silc_calloc(len, sizeof(*entry->ban_list));
+    memcpy(entry->ban_list, tmp, len);
+  }
+
+  /* Get the invite list */
+  tmp = silc_argument_get_arg_type(cmd->args, 9, &len);
+  if (tmp) {
+    if (entry->invite_list)
+      silc_free(entry->invite_list);
+    entry->invite_list = silc_calloc(len, sizeof(*entry->invite_list));
+    memcpy(entry->invite_list, tmp, len);
+  }
+
+  /* Get the topic */
+  tmp = silc_argument_get_arg_type(cmd->args, 10, &len);
+  if (tmp) {
+    if (entry->topic)
+      silc_free(entry->topic);
+    entry->topic = strdup(tmp);
   }
 
   /* If channel was not created we know there is global users on the 
@@ -692,8 +784,11 @@ SILC_SERVER_CMD_REPLY_FUNC(join)
   entry->mode = mode;
 
   /* Save channel key */
-  silc_server_save_channel_key(server, keyp, entry);
-  silc_buffer_free(keyp);
+  if (!(entry->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
+    silc_server_save_channel_key(server, keyp, entry);
+  }
+  if (keyp)
+    silc_buffer_free(keyp);
 
   /* Save the users to the channel */
   silc_server_save_users_on_channel(server, cmd->sock, entry,