updates.
[silc.git] / apps / silcd / idlist.c
index abcfb9f12c34e14b5f1594019df606783ce6516d..f762787fe80af56f9e921192b0bb14947bafccce 100644 (file)
@@ -43,7 +43,9 @@ void silc_idlist_add_data(void *entry, SilcIDListData idata)
   data->public_key = idata->public_key;
   data->last_receive = idata->last_receive;
   data->last_sent = idata->last_sent;
-  data->registered = idata->registered;
+  data->status = idata->status;
+
+  data->created = time(0);     /* Update creation time */
 }
 
 /* Free's all data in the common ID entry data structure. */
@@ -77,10 +79,10 @@ SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge)
   SILC_LOG_DEBUG(("Start"));
 
   silc_idcache_purge(i->cache);
-  silc_task_register(i->timeout_queue, 0, 
-                    silc_idlist_purge,
-                    (void *)i, 600, 0,
-                    SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
+  silc_schedule_task_add(i->schedule, 0, 
+                        silc_idlist_purge,
+                        (void *)i, 600, 0,
+                        SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
 }
 
 /******************************************************************************
@@ -124,7 +126,7 @@ silc_idlist_add_server(SilcIDList id_list,
 
 SilcServerEntry
 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
-                             SilcIDCacheEntry *ret_entry)
+                             bool registered, SilcIDCacheEntry *ret_entry)
 {
   SilcIDCacheEntry id_cache = NULL;
   SilcServerEntry server;
@@ -144,6 +146,12 @@ silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
   if (ret_entry)
     *ret_entry = id_cache;
 
+  if (server && registered && 
+      !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
+    return NULL;
+
+  SILC_LOG_DEBUG(("Found"));
+
   return server;
 }
 
@@ -151,7 +159,7 @@ silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
 
 SilcServerEntry
 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
-                               SilcIDCacheEntry *ret_entry)
+                               bool registered, SilcIDCacheEntry *ret_entry)
 {
   SilcIDCacheEntry id_cache = NULL;
   SilcServerEntry server;
@@ -166,6 +174,10 @@ silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
   if (ret_entry)
     *ret_entry = id_cache;
 
+  if (server && registered &&
+      !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
+    return NULL;
+
   SILC_LOG_DEBUG(("Found"));
 
   return server;
@@ -175,7 +187,8 @@ silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
 
 SilcServerEntry
 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
-                               int port, SilcIDCacheEntry *ret_entry)
+                               int port, bool registered,
+                               SilcIDCacheEntry *ret_entry)
 {
   SilcIDCacheList list = NULL;
   SilcIDCacheEntry id_cache = NULL;
@@ -213,6 +226,10 @@ silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
   if (ret_entry)
     *ret_entry = id_cache;
 
+  if (server && registered &&
+      !(server->data.status & SILC_IDLIST_STATUS_REGISTERED))
+    return NULL;
+
   SILC_LOG_DEBUG(("Found"));
 
   return server;
@@ -266,10 +283,8 @@ int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry)
        return FALSE;
 
     /* Free data */
-    if (entry->server_name)
-      silc_free(entry->server_name);
-    if (entry->id)
-      silc_free(entry->id);
+    silc_free(entry->server_name);
+    silc_free(entry->id);
 
     memset(entry, 'F', sizeof(*entry));
     silc_free(entry);
@@ -337,14 +352,10 @@ int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
        return FALSE;
 
     /* Free data */
-    if (entry->nickname)
-      silc_free(entry->nickname);
-    if (entry->username)
-      silc_free(entry->username);
-    if (entry->userinfo)
-      silc_free(entry->userinfo);
-    if (entry->id)
-      silc_free(entry->id);
+    silc_free(entry->nickname);
+    silc_free(entry->username);
+    silc_free(entry->userinfo);
+    silc_free(entry->id);
 
     memset(entry, 'F', sizeof(*entry));
     silc_free(entry);
@@ -365,7 +376,6 @@ int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
 {
   SilcIDCacheList list = NULL;
   SilcIDCacheEntry id_cache = NULL;
-  int i;
 
   SILC_LOG_DEBUG(("Start"));
 
@@ -376,18 +386,15 @@ int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
                          (silc_idcache_list_count(list) + *clients_count) * 
                          sizeof(**clients));
 
-  i = 0;
   silc_idcache_list_first(list, &id_cache);
-  (*clients)[i++] = (SilcClientEntry)id_cache->context;
+  (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
 
   while (silc_idcache_list_next(list, &id_cache))
-    (*clients)[i++] = (SilcClientEntry)id_cache->context;
+    (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
   
   silc_idcache_list_free(list);
   
-  *clients_count += i;
-
-  SILC_LOG_DEBUG(("Found %d clients", *clients_count));
+  SILC_LOG_DEBUG(("Found total %d clients", *clients_count));
 
   return TRUE;
 }
@@ -403,7 +410,6 @@ int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
   SilcIDCacheList list = NULL;
   SilcIDCacheEntry id_cache = NULL;
   unsigned char hash[32];
-  int i;
   SilcClientID client_id;
 
   SILC_LOG_DEBUG(("Start"));
@@ -423,18 +429,15 @@ int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
                          (silc_idcache_list_count(list) + *clients_count) * 
                          sizeof(**clients));
 
-  i = 0;
   silc_idcache_list_first(list, &id_cache);
-  (*clients)[i++] = (SilcClientEntry)id_cache->context;
+  (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
 
   while (silc_idcache_list_next(list, &id_cache))
-    (*clients)[i++] = (SilcClientEntry)id_cache->context;
+    (*clients)[(*clients_count)++] = (SilcClientEntry)id_cache->context;
   
   silc_idcache_list_free(list);
   
-  *clients_count += i;
-
-  SILC_LOG_DEBUG(("Found %d clients", *clients_count));
+  SILC_LOG_DEBUG(("Found total %d clients", *clients_count));
 
   return TRUE;
 }
@@ -443,7 +446,7 @@ int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
 
 SilcClientEntry
 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
-                             SilcIDCacheEntry *ret_entry)
+                             bool registered, SilcIDCacheEntry *ret_entry)
 {
   SilcIDCacheEntry id_cache = NULL;
   SilcClientEntry client;
@@ -469,6 +472,10 @@ silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
   if (ret_entry)
     *ret_entry = id_cache;
 
+  if (client && registered &&
+      !(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
+    return NULL;
+
   SILC_LOG_DEBUG(("Found"));
 
   return client;
@@ -507,8 +514,7 @@ silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
   silc_free(client->id);
   client->id = new_id;
 
-  silc_idcache_add(id_list->clients, client->nickname, client->id, 
-                  client, FALSE);
+  silc_idcache_add(id_list->clients, NULL, client->id, client, FALSE);
 
   SILC_LOG_DEBUG(("Replaced"));
 
@@ -556,6 +562,8 @@ silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
 {
   SilcChannelEntry channel;
 
+  SILC_LOG_DEBUG(("Adding new channel entry"));
+
   channel = silc_calloc(1, sizeof(*channel));
   channel->channel_name = channel_name;
   channel->mode = mode;
@@ -563,8 +571,9 @@ silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
   channel->router = router;
   channel->channel_key = channel_key;
   channel->hmac = hmac;
+  channel->created = time(0);
   if (!channel->hmac)
-    if (!silc_hmac_alloc("hmac-sha1-96", NULL, &channel->hmac)) {
+    if (!silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac)) {
       silc_free(channel);
       return NULL;
     }
@@ -610,24 +619,18 @@ int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
        return FALSE;
 
     /* Free data */
-    if (entry->channel_name)
-      silc_free(entry->channel_name);
-    if (entry->id)
-      silc_free(entry->id);
-    if (entry->topic)
-      silc_free(entry->topic);
+    silc_free(entry->channel_name);
+    silc_free(entry->id);
+    silc_free(entry->topic);
     if (entry->channel_key)
       silc_cipher_free(entry->channel_key);
     if (entry->key) {
       memset(entry->key, 0, entry->key_len / 8);
       silc_free(entry->key);
     }
-    if (entry->cipher)
-      silc_free(entry->cipher);
-    if (entry->hmac_name)
-      silc_free(entry->hmac_name);
-    if (entry->rekey)
-      silc_free(entry->rekey);
+    silc_free(entry->cipher);
+    silc_free(entry->hmac_name);
+    silc_free(entry->rekey);
 
     /* Free all client entrys from the users list. The silc_hash_table_free
        will free all the entries so they are not freed at the foreach