Splitted SILC core library. Core library includes now only
[silc.git] / apps / silc / 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 /*
21  * $Id$
22  * $Log$
23  * Revision 1.1  2000/07/12 05:55:05  priikone
24  *      Created idlist.c
25  *
26  */
27
28 #include "clientincludes.h"
29
30 /* Finds client entry from cache by nickname. If the entry is not found
31    from the cache this function queries it from the server. If `server'
32    and `num' are defined as well thisk checks the match from multiple
33    cache entries thus providing support for multiple same nickname
34    handling. This also ignores case-sensitivity. */
35
36 SilcClientEntry silc_idlist_get_client(SilcClient client,
37                                        SilcClientWindow win,
38                                        char *nickname,
39                                        char *server,
40                                        unsigned int num)
41 {
42   SilcIDCacheEntry id_cache;
43   SilcIDCacheList list = NULL;
44   SilcClientEntry entry = NULL;
45
46   /* Find ID from cache */
47   if (!silc_idcache_find_by_data_loose(win->client_cache, nickname, &list)) {
48     SilcClientCommandContext ctx;
49     char ident[512];
50     
51   identify:
52
53     SILC_LOG_DEBUG(("Requesting Client ID from server"));
54
55     /* No ID found. Do query from the server. The query is done by 
56        sending simple IDENTIFY command to the server. */
57     ctx = silc_calloc(1, sizeof(*ctx));
58     ctx->client = client;
59     ctx->sock = win->sock;
60     memset(ident, 0, sizeof(ident));
61     snprintf(ident, sizeof(ident), "/IDENTIFY %s", nickname);
62     silc_client_parse_command_line(ident, &ctx->argv, &ctx->argv_lens, 
63                                    &ctx->argv_types, &ctx->argc, 2);
64     silc_client_command_identify(ctx);
65
66     if (list)
67       silc_idcache_list_free(list);
68
69     return NULL;
70   }
71
72   if (!server && !num) {
73     /* Take first found cache entry */
74     if (!silc_idcache_list_first(list, &id_cache))
75       goto identify;
76
77     entry = (SilcClientEntry)id_cache->context;
78   } else {
79     /* Check multiple cache entries for match */
80     while (silc_idcache_list_next(list, &id_cache)) {
81       entry = (SilcClientEntry)id_cache->context;
82
83       if (server && entry->server && 
84           strncasecmp(server, entry->server, strlen(server))) {
85         entry = NULL;
86         continue;
87       }
88       
89       if (num && entry->num != num) {
90         entry = NULL;
91         continue;
92       }
93
94       break;
95     }
96
97     /* If match weren't found, request it */
98     if (!entry)
99       goto identify;
100   }
101
102   if (list)
103     silc_idcache_list_free(list);
104
105   return entry;
106 }
107
108 /* Finds channel entry from ID cache by channel name. */
109
110 SilcChannelEntry silc_idlist_get_channel(SilcClient client,
111                                          SilcClientWindow win,
112                                          char *channel)
113 {
114   SilcIDCacheEntry id_cache;
115   SilcChannelEntry entry;
116
117   if (!silc_idcache_find_by_data_one(win->channel_cache, channel, &id_cache))
118     return NULL;
119
120   entry = (SilcChannelEntry)id_cache->context;
121
122   return entry;
123 }