+/* Caches the received WHOWAS information for a short period of time. */
+
+static char
+silc_server_command_reply_whowas_save(SilcServerCommandReplyContext cmd)
+{
+ SilcServer server = cmd->server;
+ int len, id_len;
+ unsigned char *id_data;
+ char *nickname, *username, *realname;
+ SilcClientID *client_id;
+ SilcClientEntry client;
+ SilcIDCacheEntry cache = NULL;
+ char *nick;
+ int global = FALSE;
+
+ id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
+ nickname = silc_argument_get_arg_type(cmd->args, 3, &len);
+ username = silc_argument_get_arg_type(cmd->args, 4, &len);
+ if (!id_data || !nickname || !username)
+ return FALSE;
+
+ realname = silc_argument_get_arg_type(cmd->args, 5, &len);
+
+ client_id = silc_id_payload_parse_id(id_data, id_len);
+ if (!client_id)
+ return FALSE;
+
+ /* Check if we have this client cached already. */
+
+ client = silc_idlist_find_client_by_id(server->local_list, client_id,
+ &cache);
+ if (!client) {
+ client = silc_idlist_find_client_by_id(server->global_list,
+ client_id, &cache);
+ global = TRUE;
+ }
+
+ 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)
+ return FALSE;
+
+ /* Take hostname out of nick string if it includes it. */
+ if (strchr(nickname, '@')) {
+ int len = strcspn(nickname, "@");
+ nick = silc_calloc(len + 1, sizeof(char));
+ memcpy(nick, nickname, len);
+ } else {
+ nick = strdup(nickname);
+ }
+
+ /* 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),
+ silc_id_dup(client_id, SILC_ID_CLIENT),
+ cmd->sock->user_data, NULL);
+ if (!client)
+ return FALSE;
+
+ client->data.registered = FALSE;
+ client = silc_idlist_find_client_by_id(server->global_list,
+ client_id, &cache);
+ cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
+ } else {
+ /* We have the client already, update the data */
+
+ /* Take hostname out of nick string if it includes it. */
+ if (strchr(nickname, '@')) {
+ int len = strcspn(nickname, "@");
+ nick = silc_calloc(len + 1, sizeof(char));
+ memcpy(nick, nickname, len);
+ } else {
+ nick = strdup(nickname);
+ }
+
+ if (client->nickname)
+ silc_free(client->nickname);
+ if (client->username)
+ silc_free(client->username);
+
+ client->nickname = nick;
+ client->username = strdup(username);
+
+ if (cache) {
+ cache->data = nick;
+ cache->data_len = strlen(nick);
+ silc_idcache_sort_by_data(global ? server->global_list->clients :
+ server->local_list->clients);
+ }
+ }
+
+ silc_free(client_id);
+
+ return TRUE;
+}
+
+/* Received reply for WHOWAS command. Cache the client information only for
+ a short period of time. */
+
+SILC_SERVER_CMD_REPLY_FUNC(whowas)
+{
+ SilcServerCommandReplyContext cmd = (SilcServerCommandReplyContext)context;
+ SilcCommandStatus status;
+
+ COMMAND_CHECK_STATUS_LIST;
+
+ if (!silc_server_command_reply_whowas_save(cmd))
+ goto out;
+
+ /* Execute any pending commands */
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
+
+ out:
+ SILC_SERVER_PENDING_DESTRUCTOR(cmd, SILC_COMMAND_WHOWAS);
+ silc_server_command_reply_free(cmd);
+}
+