updates.
[silc.git] / apps / irssi / src / silc / core / silc-nicklist.c
1 /*
2  silc-nicklist.c : irssi
3
4     Copyright (C) 2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "misc.h"
24 #include "servers.h"
25
26 #include "silc-channels.h"
27 #include "silc-nicklist.h"
28
29 SILC_NICK_REC *silc_nicklist_insert(SILC_CHANNEL_REC *channel,
30                                     SilcChannelUser user, int send_massjoin)
31 {
32   SILC_NICK_REC *rec;
33
34   g_return_val_if_fail(IS_SILC_CHANNEL(channel), NULL);
35   g_return_val_if_fail(user != NULL, NULL);
36
37   rec = g_new0(SILC_NICK_REC, 1);
38   rec->nick = g_strdup(user->client->nickname);
39   rec->host = g_strdup(user->client->username);
40   rec->silc_user = user;
41   rec->unique_id = user->client;
42
43   if (user->mode & SILC_CHANNEL_UMODE_CHANOP) 
44     rec->op = TRUE;
45   if (user->mode & SILC_CHANNEL_UMODE_CHANFO) 
46     rec->founder = TRUE;
47   rec->send_massjoin = send_massjoin;
48
49   nicklist_insert(CHANNEL(channel), (NICK_REC *) rec);
50   return rec;
51 }
52
53 SILC_NICK_REC *silc_nicklist_find(SILC_CHANNEL_REC *channel,
54                                   SilcClientEntry client)
55 {
56   return (SILC_NICK_REC *)nicklist_find_unique(CHANNEL(channel),
57                                                client->nickname, client);
58 }
59
60 #define isnickchar(a) \
61     (isalnum((int) (a)) || (a) == '`' || (a) == '-' || (a) == '_' || \
62     (a) == '[' || (a) == ']' || (a) == '{' || (a) == '}' || \
63     (a) == '|' || (a) == '\\' || (a) == '^')
64
65 /* Remove all "extra" characters from `nick'. Like _nick_ -> nick */
66 char *silc_nick_strip(const char *nick)
67 {
68   char *stripped, *spos;
69
70   g_return_val_if_fail(nick != NULL, NULL);
71   
72   spos = stripped = g_strdup(nick);
73   while (isnickchar(*nick)) {
74     if (isalnum((int) *nick))
75       *spos++ = *nick;
76     nick++;
77   }
78   if ((unsigned char) *nick >= 128)
79     *spos++ = *nick; /* just add it so that nicks won't match.. */
80   *spos = '\0';
81
82   return stripped;
83 }
84
85 /* Check is `msg' is meant for `nick'. */
86 int silc_nick_match(const char *nick, const char *msg)
87 {
88   char *stripnick, *stripmsg;
89   int ret, len;
90
91   g_return_val_if_fail(nick != NULL, FALSE);
92   g_return_val_if_fail(msg != NULL, FALSE);
93
94   len = strlen(nick);
95   if (g_strncasecmp(msg, nick, len) == 0 && !isalnum((int) msg[len]))
96     return TRUE;
97   
98   stripnick = silc_nick_strip(nick);
99   stripmsg = silc_nick_strip(msg);
100   
101   len = strlen(stripnick);
102   ret = len > 0 && g_strncasecmp(stripmsg, stripnick, len) == 0 &&
103     !isalnum((int) stripmsg[len]) &&
104     (unsigned char) stripmsg[len] < 128;
105   
106   g_free(stripnick);
107   g_free(stripmsg);
108
109   return ret;
110 }
111
112 static const char *get_nick_flags(void)
113 {
114   static char flags[3] = { '@', '+', '\0' };
115   return flags;
116 }
117
118 static void sig_connected(SILC_SERVER_REC *server)
119 {
120   if (IS_SILC_SERVER(server))
121     server->get_nick_flags = (void *) get_nick_flags;
122 }
123
124 void silc_nicklist_init(void)
125 {
126   signal_add("server connected", (SIGNAL_FUNC) sig_connected);
127 }
128
129 void silc_nicklist_deinit(void)
130 {
131   signal_remove("server connected", (SIGNAL_FUNC) sig_connected);
132 }