updates.
[silc.git] / lib / silcclient / idlist.c
index 29752426126ab5a8f3ef65d5968108bda46b8e2f..6d865882b390042967e7504fb0684d4e807323b5 100644 (file)
@@ -34,7 +34,7 @@ SILC_CLIENT_CMD_FUNC(get_client_callback)
 {
   GetClientInternal i = (GetClientInternal)context;
   SilcClientEntry *clients;
-  unsigned int clients_count;
+  uint32 clients_count;
 
   /* Get the clients */
   clients = silc_client_get_clients_local(i->cmd->client, i->cmd->conn,
@@ -93,17 +93,19 @@ void silc_client_get_clients(SilcClient client,
   snprintf(ident, sizeof(ident), "IDENTIFY %s", nickname);
   silc_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
                          &ctx->argv_types, &ctx->argc, 2);
-  ctx->command->cb(ctx);
-      
-  i->cmd = ctx;
+
+  i->cmd = silc_client_command_dup(ctx);
   i->nickname = nickname ? strdup(nickname) : NULL;
   i->server = server ? strdup(server) : NULL;
   i->completion = completion;
   i->context = context;
 
+  /* Call the command */
+  ctx->command->cb(ctx, NULL);
+
   /* Add pending callback */
   silc_client_command_pending(conn, SILC_COMMAND_IDENTIFY, 
-                             ++conn->cmd_ident, 
+                             conn->cmd_ident, 
                              silc_client_get_client_destructor,
                              silc_client_command_get_client_callback, 
                              (void *)i);
@@ -116,7 +118,7 @@ SilcClientEntry *silc_client_get_clients_local(SilcClient client,
                                               SilcClientConnection conn,
                                               char *nickname,
                                               char *server,
-                                              unsigned int *clients_count)
+                                              uint32 *clients_count)
 {
   SilcIDCacheEntry id_cache;
   SilcIDCacheList list = NULL;
@@ -124,7 +126,7 @@ SilcClientEntry *silc_client_get_clients_local(SilcClient client,
   int i = 0;
 
   /* Find ID from cache */
-  if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list))
+  if (!silc_idcache_find_by_name(conn->client_cache, nickname, &list))
     return NULL;
 
   if (!silc_idcache_list_count(list)) {
@@ -173,7 +175,7 @@ SilcClientEntry *silc_client_get_clients_local(SilcClient client,
 typedef struct {
   SilcClient client;
   SilcClientConnection conn;
-  unsigned int list_count;
+  uint32 list_count;
   SilcBuffer client_id_list;
   SilcGetClientCallback completion;
   void *context;
@@ -186,11 +188,11 @@ SILC_CLIENT_CMD_FUNC(get_clients_list_callback)
   SilcIDCacheEntry id_cache = NULL;
   SilcBuffer client_id_list = i->client_id_list;
   SilcClientEntry *clients = NULL;
-  unsigned int clients_count = 0;
+  uint32 clients_count = 0;
   int c;
 
   for (c = 0; c < i->list_count; c++) {
-    unsigned short idp_len;
+    uint16 idp_len;
     SilcClientID *client_id;
 
     /* Get Client ID */
@@ -201,8 +203,11 @@ SILC_CLIENT_CMD_FUNC(get_clients_list_callback)
       continue;
 
     /* Get the client entry */
-    if (silc_idcache_find_by_id_one(i->conn->client_cache, (void *)client_id,
-                                   SILC_ID_CLIENT, &id_cache)) {
+    if (silc_idcache_find_by_id_one_ext(i->conn->client_cache, 
+                                       (void *)client_id, 
+                                       NULL, NULL, 
+                                       silc_hash_client_id_compare, NULL,
+                                       &id_cache)) {
       clients = silc_realloc(clients, sizeof(*clients) * 
                             (clients_count + 1));
       clients[clients_count] = (SilcClientEntry)id_cache->context;
@@ -241,7 +246,7 @@ static void silc_client_get_clients_list_destructor(void *context)
 
 void silc_client_get_clients_by_list(SilcClient client,
                                     SilcClientConnection conn,
-                                    unsigned int list_count,
+                                    uint32 list_count,
                                     SilcBuffer client_id_list,
                                     SilcGetClientCallback completion,
                                     void *context)
@@ -249,7 +254,7 @@ void silc_client_get_clients_by_list(SilcClient client,
   SilcIDCacheEntry id_cache = NULL;
   int i;
   unsigned char **res_argv = NULL;
-  unsigned int *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
+  uint32 *res_argv_lens = NULL, *res_argv_types = NULL, res_argc = 0;
   GetClientsByListInternal in;
 
   in = silc_calloc(1, sizeof(*in));
@@ -261,7 +266,7 @@ void silc_client_get_clients_by_list(SilcClient client,
   in->context = context;
 
   for (i = 0; i < list_count; i++) {
-    unsigned short idp_len;
+    uint16 idp_len;
     SilcClientID *client_id;
     SilcClientEntry entry;
 
@@ -274,8 +279,10 @@ void silc_client_get_clients_by_list(SilcClient client,
 
     /* Check if we have this client cached already. */
     id_cache = NULL;
-    silc_idcache_find_by_id_one(conn->client_cache, (void *)client_id,
-                               SILC_ID_CLIENT, &id_cache);
+    silc_idcache_find_by_id_one_ext(conn->client_cache, (void *)client_id, 
+                                   NULL, NULL, 
+                                   silc_hash_client_id_compare, NULL,
+                                   &id_cache);
 
     /* If we don't have the entry or it has incomplete info, then resolve
        it from the server. */
@@ -298,8 +305,6 @@ void silc_client_get_clients_by_list(SilcClient client,
     silc_free(client_id);
     silc_buffer_pull(client_id_list, idp_len);
   }
-  silc_buffer_push(client_id_list, client_id_list->data - 
-                  client_id_list->head);
 
   /* Query the client information from server if the list included clients
      that we don't know about. */
@@ -320,6 +325,8 @@ void silc_client_get_clients_by_list(SilcClient client,
                                silc_client_command_get_clients_list_callback, 
                                (void *)in);
 
+    silc_buffer_push(client_id_list, client_id_list->data - 
+                    client_id_list->head);
     silc_buffer_free(res_cmd);
     silc_free(res_argv);
     silc_free(res_argv_lens);
@@ -327,8 +334,11 @@ void silc_client_get_clients_by_list(SilcClient client,
     return;
   }
 
+  silc_buffer_push(client_id_list, client_id_list->data - 
+                  client_id_list->head);
+
   /* We have the clients in cache, get them and call the completion */
-  silc_client_command_get_clients_list_callback((void *)in);
+  silc_client_command_get_clients_list_callback((void *)in, NULL);
 }
 
 /* The old style function to find client entry. This is used by the
@@ -340,7 +350,7 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
                                       SilcClientConnection conn,
                                       char *nickname,
                                       char *server,
-                                      unsigned int num,
+                                      uint32 num,
                                       int query)
 {
   SilcIDCacheEntry id_cache;
@@ -348,7 +358,7 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
   SilcClientEntry entry = NULL;
 
   /* Find ID from cache */
-  if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list)) {
+  if (!silc_idcache_find_by_name(conn->client_cache, nickname, &list)) {
   identify:
 
     if (query) {
@@ -367,7 +377,7 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
       snprintf(ident, sizeof(ident), "IDENTIFY %s", nickname);
       silc_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
                              &ctx->argv_types, &ctx->argc, 2);
-      ctx->command->cb(ctx);
+      ctx->command->cb(ctx, NULL);
       
       if (list)
        silc_idcache_list_free(list);
@@ -428,8 +438,10 @@ SilcClientEntry silc_client_get_client_by_id(SilcClient client,
                  silc_id_render(client_id, SILC_ID_CLIENT)));
 
   /* Find ID from cache */
-  if (!silc_idcache_find_by_id_one(conn->client_cache, client_id, 
-                                  SILC_ID_CLIENT, &id_cache))
+  if (!silc_idcache_find_by_id_one_ext(conn->client_cache, (void *)client_id, 
+                                      NULL, NULL, 
+                                      silc_hash_client_id_compare, NULL,
+                                      &id_cache))
     return NULL;
 
   SILC_LOG_DEBUG(("Found"));
@@ -499,12 +511,31 @@ void silc_client_get_client_by_id_resolve(SilcClient client,
       
   /* Add pending callback */
   silc_client_command_pending(conn, SILC_COMMAND_WHOIS, 
-                             ++conn->cmd_ident, 
+                             conn->cmd_ident, 
                              silc_client_get_client_by_id_destructor,
                              silc_client_command_get_client_by_id_callback, 
                              (void *)i);
 }
 
+/* Removes client from the cache by the client entry. */
+
+bool silc_client_del_client(SilcClient client, SilcClientConnection conn,
+                           SilcClientEntry client_entry)
+{
+  return silc_idcache_del_by_context(conn->client_cache, client_entry);
+}
+
+/* Removes client from the cache by the client ID. */
+
+bool silc_client_del_client_by_id(SilcClient client, 
+                                 SilcClientConnection conn,
+                                 SilcClientID *client_id)
+{
+  return silc_idcache_del_by_id_ext(conn->client_cache, (void *)client_id, 
+                                   NULL, NULL, 
+                                   silc_hash_client_id_compare, NULL);
+}
+
 /* Finds entry for channel by the channel name. Returns the entry or NULL
    if the entry was not found. It is found only if the client is joined
    to the channel. */
@@ -516,10 +547,29 @@ SilcChannelEntry silc_client_get_channel(SilcClient client,
   SilcIDCacheEntry id_cache;
   SilcChannelEntry entry;
 
-  if (!silc_idcache_find_by_data_one(conn->channel_cache, channel, &id_cache))
+  if (!silc_idcache_find_by_name_one(conn->channel_cache, channel, 
+                                    &id_cache))
     return NULL;
 
   entry = (SilcChannelEntry)id_cache->context;
 
   return entry;
 }
+
+/* Finds entry for server by the server ID. */
+
+SilcServerEntry silc_client_get_server_by_id(SilcClient client,
+                                            SilcClientConnection conn,
+                                            SilcServerID *server_id)
+{
+  SilcIDCacheEntry id_cache;
+  SilcServerEntry entry;
+
+  if (!silc_idcache_find_by_id_one(conn->server_cache, (void *)server_id, 
+                                  &id_cache))
+    return NULL;
+
+  entry = (SilcServerEntry)id_cache->context;
+
+  return entry;
+}