implemented KICK command
[silc.git] / lib / silcclient / idlist.c
index 7f632e965ed30ab389f56df3622bebdd5ce66cc3..0236c812f28ddc836b2f91d62e149cf44bb0c70b 100644 (file)
@@ -31,7 +31,8 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
                                       SilcClientConnection conn,
                                       char *nickname,
                                       char *server,
-                                      unsigned int num)
+                                      unsigned int num,
+                                      int query)
 {
   SilcIDCacheEntry id_cache;
   SilcIDCacheList list = NULL;
@@ -39,28 +40,31 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
 
   /* Find ID from cache */
   if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list)) {
-    SilcClientCommandContext ctx;
-    char ident[512];
-    
   identify:
 
-    SILC_LOG_DEBUG(("Requesting Client ID from server"));
-
-    /* No ID found. Do query from the server. The query is done by 
-       sending simple IDENTIFY command to the server. */
-    ctx = silc_calloc(1, sizeof(*ctx));
-    ctx->client = client;
-    ctx->conn = conn;
-    ctx->command = silc_client_command_find("IDENTIFY");
-    memset(ident, 0, sizeof(ident));
-    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);
-
-    if (list)
-      silc_idcache_list_free(list);
-
+    if (query) {
+      SilcClientCommandContext ctx;
+      char ident[512];
+      
+      SILC_LOG_DEBUG(("Requesting Client ID from server"));
+      
+      /* No ID found. Do query from the server. The query is done by 
+        sending simple IDENTIFY command to the server. */
+      ctx = silc_client_command_alloc();
+      ctx->client = client;
+      ctx->conn = conn;
+      ctx->command = silc_client_command_find("IDENTIFY");
+      memset(ident, 0, sizeof(ident));
+      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);
+      
+      if (list)
+       silc_idcache_list_free(list);
+      
+      return NULL;
+    }
     return NULL;
   }
 
@@ -72,21 +76,23 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
     entry = (SilcClientEntry)id_cache->context;
   } else {
     /* Check multiple cache entries for match */
-    while (silc_idcache_list_next(list, &id_cache)) {
-      entry = (SilcClientEntry)id_cache->context;
-
+    silc_idcache_list_first(list, &id_cache);
+    entry = (SilcClientEntry)id_cache->context;
+    
+    while (entry) {
       if (server && entry->server && 
-         strncasecmp(server, entry->server, strlen(server))) {
-       entry = NULL;
-       continue;
-      }
+         !strncasecmp(server, entry->server, strlen(server)))
+       break;
       
-      if (num && entry->num != num) {
+      if (num && entry->num == num)
+       break;
+
+      if (!silc_idcache_list_next(list, &id_cache)) {
        entry = NULL;
-       continue;
+       break;
       }
 
-      break;
+      entry = (SilcClientEntry)id_cache->context;
     }
 
     /* If match weren't found, request it */
@@ -100,6 +106,27 @@ SilcClientEntry silc_idlist_get_client(SilcClient client,
   return entry;
 }
 
+/* Finds client entry from cache by Client ID. */
+
+SilcClientEntry silc_idlist_get_client_by_id(SilcClient client,
+                                            SilcClientConnection conn,
+                                            SilcClientID *client_id)
+{
+  SilcIDCacheEntry id_cache;
+
+  SILC_LOG_DEBUG(("Finding client by ID (%s)", 
+                 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))
+    return NULL;
+
+  SILC_LOG_DEBUG(("Found"));
+
+  return (SilcClientEntry)id_cache->context;
+}
+
 /* Finds channel entry from ID cache by channel name. */
 
 SilcChannelEntry silc_idlist_get_channel(SilcClient client,