4 Copyright (C) 1999-2000 Timo Sirainen
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.
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.
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
30 #define isalnumhigh(a) \
31 (i_isalnum(a) || (unsigned char) (a) >= 128)
33 static void nick_hash_add(CHANNEL_REC *channel, NICK_REC *nick)
39 list = g_hash_table_lookup(channel->nicks, nick->nick);
41 g_hash_table_insert(channel->nicks, nick->nick, nick);
43 /* multiple nicks with same name */
44 while (list->next != NULL)
49 if (nick == channel->ownnick) {
50 /* move our own nick to beginning of the nick list.. */
51 nicklist_set_own(channel, nick);
55 static void nick_hash_remove(CHANNEL_REC *channel, NICK_REC *nick)
59 list = g_hash_table_lookup(channel->nicks, nick->nick);
63 if (list == nick || list->next == NULL) {
64 g_hash_table_remove(channel->nicks, nick->nick);
65 if (list->next != NULL) {
66 g_hash_table_insert(channel->nicks, nick->next->nick,
70 while (list->next != nick)
72 list->next = nick->next;
76 /* Add new nick to list */
77 void nicklist_insert(CHANNEL_REC *channel, NICK_REC *nick)
79 /*MODULE_DATA_INIT(nick);*/
81 nick->type = module_get_uniq_id("NICK", 0);
82 nick->chat_type = channel->chat_type;
84 nick_hash_add(channel, nick);
85 signal_emit("nicklist new", 2, channel, nick);
88 /* Set host address for nick */
89 void nicklist_set_host(CHANNEL_REC *channel, NICK_REC *nick, const char *host)
91 g_return_if_fail(channel != NULL);
92 g_return_if_fail(nick != NULL);
93 g_return_if_fail(host != NULL);
95 g_free_not_null(nick->host);
96 nick->host = g_strdup(host);
98 signal_emit("nicklist host changed", 2, channel, nick);
101 static void nicklist_destroy(CHANNEL_REC *channel, NICK_REC *nick)
103 signal_emit("nicklist remove", 2, channel, nick);
105 /*MODULE_DATA_DEINIT(nick);*/
107 g_free_not_null(nick->realname);
108 g_free_not_null(nick->host);
112 /* Remove nick from list */
113 void nicklist_remove(CHANNEL_REC *channel, NICK_REC *nick)
115 g_return_if_fail(IS_CHANNEL(channel));
116 g_return_if_fail(nick != NULL);
118 nick_hash_remove(channel, nick);
119 nicklist_destroy(channel, nick);
122 static void nicklist_rename_list(SERVER_REC *server, void *new_nick_id,
123 const char *old_nick, const char *new_nick,
126 CHANNEL_REC *channel;
130 for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
132 nickrec = tmp->next->data;
134 /* remove old nick from hash table */
135 nick_hash_remove(channel, nickrec);
137 if (new_nick_id != NULL)
138 nickrec->unique_id = new_nick_id;
140 g_free(nickrec->nick);
141 nickrec->nick = g_strdup(new_nick);
143 /* add new nick to hash table */
144 nick_hash_add(channel, nickrec);
146 signal_emit("nicklist changed", 3, channel, nickrec, old_nick);
151 void nicklist_rename(SERVER_REC *server, const char *old_nick,
152 const char *new_nick)
154 nicklist_rename_list(server, NULL, old_nick, new_nick,
155 nicklist_get_same(server, old_nick));
158 void nicklist_rename_unique(SERVER_REC *server,
159 void *old_nick_id, const char *old_nick,
160 void *new_nick_id, const char *new_nick)
162 nicklist_rename_list(server, new_nick_id, old_nick, new_nick,
163 nicklist_get_same_unique(server, old_nick_id));
166 static NICK_REC *nicklist_find_wildcards(CHANNEL_REC *channel,
172 nicks = nicklist_getnicks(channel);
174 for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
177 if (mask_match_address(channel->server, mask,
178 nick->nick, nick->host))
182 return tmp == NULL ? NULL : nick;
185 GSList *nicklist_find_multiple(CHANNEL_REC *channel, const char *mask)
187 GSList *nicks, *tmp, *next;
189 g_return_val_if_fail(IS_CHANNEL(channel), NULL);
190 g_return_val_if_fail(mask != NULL, NULL);
192 nicks = nicklist_getnicks(channel);
193 for (tmp = nicks; tmp != NULL; tmp = next) {
194 NICK_REC *nick = tmp->data;
197 if (!mask_match_address(channel->server, mask,
198 nick->nick, nick->host))
199 nicks = g_slist_remove(nicks, tmp->data);
206 NICK_REC *nicklist_find(CHANNEL_REC *channel, const char *nick)
208 g_return_val_if_fail(IS_CHANNEL(channel), NULL);
209 g_return_val_if_fail(nick != NULL, NULL);
211 return g_hash_table_lookup(channel->nicks, nick);
214 NICK_REC *nicklist_find_unique(CHANNEL_REC *channel, const char *nick,
219 g_return_val_if_fail(IS_CHANNEL(channel), NULL);
220 g_return_val_if_fail(nick != NULL, NULL);
222 rec = g_hash_table_lookup(channel->nicks, nick);
223 while (rec != NULL && rec->unique_id != id)
229 /* Find nick mask, wildcards allowed */
230 NICK_REC *nicklist_find_mask(CHANNEL_REC *channel, const char *mask)
235 g_return_val_if_fail(IS_CHANNEL(channel), NULL);
236 g_return_val_if_fail(mask != NULL, NULL);
238 nick = g_strdup(mask);
239 host = strchr(nick, '!');
240 if (host != NULL) *host++ = '\0';
242 if (strchr(nick, '*') || strchr(nick, '?')) {
244 return nicklist_find_wildcards(channel, mask);
247 nickrec = g_hash_table_lookup(channel->nicks, nick);
250 while (nickrec != NULL) {
251 if (nickrec->host != NULL &&
252 match_wildcards(host, nickrec->host))
254 nickrec = nickrec->next;
261 static void get_nicks_hash(gpointer key, NICK_REC *rec, GSList **list)
263 while (rec != NULL) {
264 *list = g_slist_append(*list, rec);
269 /* Get list of nicks */
270 GSList *nicklist_getnicks(CHANNEL_REC *channel)
274 g_return_val_if_fail(IS_CHANNEL(channel), NULL);
277 g_hash_table_foreach(channel->nicks, (GHFunc) get_nicks_hash, &list);
282 CHANNEL_REC *channel;
285 } NICKLIST_GET_SAME_REC;
287 static void get_nicks_same_hash(gpointer key, NICK_REC *nick,
288 NICKLIST_GET_SAME_REC *rec)
290 while (nick != NULL) {
291 if (g_strcasecmp(nick->nick, rec->nick) == 0) {
292 rec->list = g_slist_append(rec->list, rec->channel);
293 rec->list = g_slist_append(rec->list, nick);
300 GSList *nicklist_get_same(SERVER_REC *server, const char *nick)
302 NICKLIST_GET_SAME_REC rec;
305 g_return_val_if_fail(IS_SERVER(server), NULL);
309 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
310 rec.channel = tmp->data;
311 g_hash_table_foreach(rec.channel->nicks,
312 (GHFunc) get_nicks_same_hash, &rec);
318 CHANNEL_REC *channel;
321 } NICKLIST_GET_SAME_UNIQUE_REC;
323 static void get_nicks_same_hash_unique(gpointer key, NICK_REC *nick,
324 NICKLIST_GET_SAME_UNIQUE_REC *rec)
326 while (nick != NULL) {
327 if (nick->unique_id == rec->id) {
328 rec->list = g_slist_append(rec->list, rec->channel);
329 rec->list = g_slist_append(rec->list, nick);
337 GSList *nicklist_get_same_unique(SERVER_REC *server, void *id)
339 NICKLIST_GET_SAME_UNIQUE_REC rec;
342 g_return_val_if_fail(IS_SERVER(server), NULL);
343 g_return_val_if_fail(id != NULL, NULL);
347 for (tmp = server->channels; tmp != NULL; tmp = tmp->next) {
348 rec.channel = tmp->data;
349 g_hash_table_foreach(rec.channel->nicks,
350 (GHFunc) get_nicks_same_hash_unique,
356 /* nick record comparision for sort functions */
357 int nicklist_compare(NICK_REC *p1, NICK_REC *p2)
359 int status1, status2;
361 if (p1 == NULL) return -1;
362 if (p2 == NULL) return 1;
364 /* we assign each status (op, halfop, voice, normal) a number
365 * and compare them. this is easier than 100,000 if's and
387 if (status1 < status2)
389 else if (status1 > status2)
392 return g_strcasecmp(p1->nick, p2->nick);
395 static void nicklist_update_flags_list(SERVER_REC *server, int gone,
396 int serverop, GSList *nicks)
399 CHANNEL_REC *channel;
402 g_return_if_fail(IS_SERVER(server));
404 for (tmp = nicks; tmp != NULL; tmp = tmp->next->next) {
406 rec = tmp->next->data;
408 rec->last_check = time(NULL);
410 if (gone != -1 && (int)rec->gone != gone) {
412 signal_emit("nicklist gone changed", 2, channel, rec);
415 if (serverop != -1 && (int)rec->serverop != serverop) {
416 rec->serverop = serverop;
417 signal_emit("nicklist serverop changed", 2, channel, rec);
423 void nicklist_update_flags(SERVER_REC *server, const char *nick,
424 int gone, int serverop)
426 nicklist_update_flags_list(server, gone, serverop,
427 nicklist_get_same(server, nick));
430 void nicklist_update_flags_unique(SERVER_REC *server, void *id,
431 int gone, int serverop)
433 nicklist_update_flags_list(server, gone, serverop,
434 nicklist_get_same_unique(server, id));
437 /* Specify which nick in channel is ours */
438 void nicklist_set_own(CHANNEL_REC *channel, NICK_REC *nick)
440 NICK_REC *first, *next;
442 channel->ownnick = nick;
444 /* move our nick in the list to first, makes some things easier
445 (like handling multiple identical nicks in fe-messages.c) */
446 first = g_hash_table_lookup(channel->nicks, nick->nick);
447 if (first->next == NULL)
453 while (first->next != nick)
457 g_hash_table_insert(channel->nicks, nick->nick, nick);
460 static void sig_channel_created(CHANNEL_REC *channel)
462 g_return_if_fail(IS_CHANNEL(channel));
464 channel->nicks = g_hash_table_new((GHashFunc) g_istr_hash,
465 (GCompareFunc) g_istr_equal);
468 static void nicklist_remove_hash(gpointer key, NICK_REC *nick,
469 CHANNEL_REC *channel)
473 while (nick != NULL) {
475 nicklist_destroy(channel, nick);
480 static void sig_channel_destroyed(CHANNEL_REC *channel)
482 g_return_if_fail(IS_CHANNEL(channel));
484 g_hash_table_foreach(channel->nicks,
485 (GHFunc) nicklist_remove_hash, channel);
486 g_hash_table_destroy(channel->nicks);
489 static NICK_REC *nick_nfind(CHANNEL_REC *channel, const char *nick, int len)
494 tmpnick = g_strndup(nick, len);
495 rec = g_hash_table_lookup(channel->nicks, tmpnick);
498 /* if there's multiple, get the one with identical case */
499 while (rec->next != NULL) {
500 if (strcmp(rec->nick, tmpnick) == 0)
510 /* Check is `msg' is meant for `nick'. */
511 int nick_match_msg(CHANNEL_REC *channel, const char *msg, const char *nick)
513 const char *msgstart, *orignick;
516 g_return_val_if_fail(nick != NULL, FALSE);
517 g_return_val_if_fail(msg != NULL, FALSE);
519 if (channel != NULL && channel->server->nick_match_msg != NULL)
520 return channel->server->nick_match_msg(msg, nick);
522 /* first check for identical match */
524 if (g_strncasecmp(msg, nick, len) == 0 && !isalnumhigh((int) msg[len]))
533 /* check if it matches for alphanumeric parts of nick */
534 while (*nick != '\0' && *msg != '\0') {
535 if (i_toupper(*nick) == i_toupper(*msg)) {
538 } else if (i_isalnum(*msg) && !i_isalnum(*nick)) {
539 /* some strange char in your nick, pass it */
547 if (msg != msgstart && !isalnumhigh(*msg)) {
548 /* at least some of the chars in line matched the
549 nick, and msg continue with non-alphanum character,
550 this might be for us.. */
552 /* remove the rest of the non-alphanum chars
553 from nick and check if it then matches. */
555 while (*nick != '\0' && !i_isalnum(*nick))
565 /* no match. check if this is a message to multiple people
566 (like nick1,nick2: text) */
567 while (*msg != '\0' && *msg != ' ' && *msg != ',') msg++;
578 return FALSE; /* didn't match */
581 return TRUE; /* matched without fuzzyness */
583 /* matched with some fuzzyness .. check if there's an exact match
584 for some other nick in the same channel. */
585 return nick_nfind(channel, msgstart, (int) (msg-msgstart)) == NULL;
588 void nicklist_init(void)
590 signal_add_first("channel created", (SIGNAL_FUNC) sig_channel_created);
591 signal_add("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
594 void nicklist_deinit(void)
596 signal_remove("channel created", (SIGNAL_FUNC) sig_channel_created);
597 signal_remove("channel destroyed", (SIGNAL_FUNC) sig_channel_destroyed);
599 module_uniq_destroy("NICK");