+ 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;
+ SilcStatus status, error;
+
+ COMMAND_CHECK_STATUS;
+
+ if (!silc_server_command_reply_whowas_save(cmd))
+ goto out;
+
+ /* 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_command_process_error(cmd, error);
+ SILC_SERVER_PENDING_EXEC(cmd, SILC_COMMAND_WHOWAS);
+ silc_server_command_reply_free(cmd);
+ return;
+
+ err:
+ silc_server_command_process_error(cmd, error);
+ silc_server_command_reply_free(cmd);
+}
+
+/* Caches the received IDENTIFY information. */
+
+static char
+silc_server_command_reply_identify_save(SilcServerCommandReplyContext cmd)
+{
+ SilcServer server = cmd->server;
+ SilcUInt32 len, id_len;
+ unsigned char *id_data;
+ char *name, *info;
+ SilcClientID *client_id = NULL;
+ SilcServerID *server_id = NULL;
+ SilcChannelID *channel_id = NULL;
+ SilcClientEntry client;
+ SilcServerEntry server_entry;
+ SilcChannelEntry channel;
+ char global = FALSE;
+ char *nick = NULL;
+ SilcIDPayload idp = NULL;
+ SilcIdType id_type;
+ int expire = 0;
+
+ id_data = silc_argument_get_arg_type(cmd->args, 2, &id_len);
+ if (!id_data)
+ return FALSE;
+ idp = silc_id_payload_parse(id_data, id_len);
+ if (!idp)
+ return FALSE;
+
+ name = silc_argument_get_arg_type(cmd->args, 3, &len);
+ info = silc_argument_get_arg_type(cmd->args, 4, &len);
+
+ id_type = silc_id_payload_get_type(idp);
+
+ switch (id_type) {
+ case SILC_ID_CLIENT:
+ client_id = silc_id_payload_get_id(idp);
+ if (!client_id)
+ goto error;
+
+ SILC_LOG_DEBUG(("Received client information"));
+
+ client = silc_idlist_find_client_by_id(server->local_list,
+ client_id, FALSE, NULL);
+ if (!client) {
+ client = silc_idlist_find_client_by_id(server->global_list, client_id,
+ FALSE, NULL);
+ 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_SERVER)
+ goto error;
+
+ /* Take nickname */
+ if (name)
+ silc_parse_userfqdn(name, &nick, NULL);
+
+ /* 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, info, NULL,
+ client_id, cmd->sock->user_data,
+ NULL, time(NULL) + 300);
+ if (!client) {
+ SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
+ silc_free(nick);
+ goto error;
+ }
+
+ client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
+ client->data.status |= SILC_IDLIST_STATUS_RESOLVED;
+ client->data.status &= ~SILC_IDLIST_STATUS_RESOLVING;
+ } else {
+ /* We have the client already, update the data */
+
+ SILC_LOG_DEBUG(("Updating client data"));
+
+ /* Take nickname */
+ if (name) {
+ silc_parse_userfqdn(name, &nick, NULL);
+
+ /* Check nickname */
+ name = silc_identifier_check(nick, strlen(nick), SILC_STRING_UTF8,
+ 128, NULL);
+ if (!name) {
+ SILC_LOG_ERROR(("Malformed nickname '%s' received in IDENTIFY reply "
+ "from %s",
+ cmd->sock->hostname ?
+ cmd->sock->hostname : "", nick));
+ return FALSE;
+ }
+
+ /* Remove the old cache entry */
+ silc_idcache_del_by_context(global ? server->global_list->clients :
+ server->local_list->clients, client);
+
+ silc_free(client->nickname);
+ client->nickname = nick;
+
+ /* Add new cache entry */
+ silc_idcache_add(global ? server->global_list->clients :
+ server->local_list->clients, name, client->id,
+ client, expire, NULL);
+ }
+
+ if (info) {
+ silc_free(client->username);
+ client->username = strdup(info);
+ }
+
+ 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 && server->server_type == SILC_SERVER) {
+ SilcIDCacheEntry cache = NULL;
+ silc_idlist_find_client_by_id(server->global_list, client->id,
+ FALSE, &cache);
+ if (!silc_hash_table_count(client->channels))
+ cache->expire = time(NULL) + 300;
+ else
+ cache->expire = 0;
+ }
+
+ silc_free(client_id);