implemented KICK command
[silc.git] / lib / silcclient / idlist.c
1 /*
2
3   idlist.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2000 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "clientlibincludes.h"
23
24 /* Finds client entry from cache by nickname. If the entry is not found
25    from the cache this function queries it from the server. If `server'
26    and `num' are defined as well thisk checks the match from multiple
27    cache entries thus providing support for multiple same nickname
28    handling. This also ignores case-sensitivity. */
29
30 SilcClientEntry silc_idlist_get_client(SilcClient client,
31                                        SilcClientConnection conn,
32                                        char *nickname,
33                                        char *server,
34                                        unsigned int num,
35                                        int query)
36 {
37   SilcIDCacheEntry id_cache;
38   SilcIDCacheList list = NULL;
39   SilcClientEntry entry = NULL;
40
41   /* Find ID from cache */
42   if (!silc_idcache_find_by_data_loose(conn->client_cache, nickname, &list)) {
43   identify:
44
45     if (query) {
46       SilcClientCommandContext ctx;
47       char ident[512];
48       
49       SILC_LOG_DEBUG(("Requesting Client ID from server"));
50       
51       /* No ID found. Do query from the server. The query is done by 
52          sending simple IDENTIFY command to the server. */
53       ctx = silc_client_command_alloc();
54       ctx->client = client;
55       ctx->conn = conn;
56       ctx->command = silc_client_command_find("IDENTIFY");
57       memset(ident, 0, sizeof(ident));
58       snprintf(ident, sizeof(ident), "IDENTIFY %s", nickname);
59       silc_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
60                               &ctx->argv_types, &ctx->argc, 2);
61       ctx->command->cb(ctx);
62       
63       if (list)
64         silc_idcache_list_free(list);
65       
66       return NULL;
67     }
68     return NULL;
69   }
70
71   if (!server && !num) {
72     /* Take first found cache entry */
73     if (!silc_idcache_list_first(list, &id_cache))
74       goto identify;
75
76     entry = (SilcClientEntry)id_cache->context;
77   } else {
78     /* Check multiple cache entries for match */
79     silc_idcache_list_first(list, &id_cache);
80     entry = (SilcClientEntry)id_cache->context;
81     
82     while (entry) {
83       if (server && entry->server && 
84           !strncasecmp(server, entry->server, strlen(server)))
85         break;
86       
87       if (num && entry->num == num)
88         break;
89
90       if (!silc_idcache_list_next(list, &id_cache)) {
91         entry = NULL;
92         break;
93       }
94
95       entry = (SilcClientEntry)id_cache->context;
96     }
97
98     /* If match weren't found, request it */
99     if (!entry)
100       goto identify;
101   }
102
103   if (list)
104     silc_idcache_list_free(list);
105
106   return entry;
107 }
108
109 /* Finds client entry from cache by Client ID. */
110
111 SilcClientEntry silc_idlist_get_client_by_id(SilcClient client,
112                                              SilcClientConnection conn,
113                                              SilcClientID *client_id)
114 {
115   SilcIDCacheEntry id_cache;
116
117   SILC_LOG_DEBUG(("Finding client by ID (%s)", 
118                   silc_id_render(client_id, SILC_ID_CLIENT)));
119
120   /* Find ID from cache */
121   if (!silc_idcache_find_by_id_one(conn->client_cache, client_id, 
122                                    SILC_ID_CLIENT, &id_cache))
123     return NULL;
124
125   SILC_LOG_DEBUG(("Found"));
126
127   return (SilcClientEntry)id_cache->context;
128 }
129
130 /* Finds channel entry from ID cache by channel name. */
131
132 SilcChannelEntry silc_idlist_get_channel(SilcClient client,
133                                          SilcClientConnection conn,
134                                          char *channel)
135 {
136   SilcIDCacheEntry id_cache;
137   SilcChannelEntry entry;
138
139   if (!silc_idcache_find_by_data_one(conn->channel_cache, channel, &id_cache))
140     return NULL;
141
142   entry = (SilcChannelEntry)id_cache->context;
143
144   return entry;
145 }