Added SILC Thread Queue API
[crypto.git] / apps / irssi / src / core / nickmatch-cache.c
1 /*
2  nickmatch-cache.c : irssi
3
4     Copyright (C) 2001 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
24 #include "channels.h"
25 #include "nicklist.h"
26
27 #include "nickmatch-cache.h"
28
29 static GSList *lists;
30
31 NICKMATCH_REC *nickmatch_init(NICKMATCH_REBUILD_FUNC func)
32 {
33         NICKMATCH_REC *rec;
34
35         rec = g_new0(NICKMATCH_REC, 1);
36         rec->func = func;
37
38         lists = g_slist_append(lists, rec);
39         return rec;
40 }
41
42 void nickmatch_deinit(NICKMATCH_REC *rec)
43 {
44         lists = g_slist_remove(lists, rec);
45
46         g_hash_table_destroy(rec->nicks);
47         g_free(rec);
48 }
49
50 static void nickmatch_check_channel(CHANNEL_REC *channel, NICKMATCH_REC *rec)
51 {
52         GSList *nicks, *tmp;
53
54         nicks = nicklist_getnicks(channel);
55         for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
56                 NICK_REC *nick = tmp->data;
57
58                 rec->func(rec->nicks, channel, nick);
59         }
60         g_slist_free(nicks);
61 }
62
63 void nickmatch_rebuild(NICKMATCH_REC *rec)
64 {
65         if (rec->nicks != NULL)
66                 g_hash_table_destroy(rec->nicks);
67
68         rec->nicks = g_hash_table_new((GHashFunc) g_direct_hash,
69                                       (GCompareFunc) g_direct_equal);
70
71         g_slist_foreach(channels, (GFunc) nickmatch_check_channel, rec);
72 }
73
74 static void sig_nick_new(CHANNEL_REC *channel, NICK_REC *nick)
75 {
76         GSList *tmp;
77
78         g_return_if_fail(channel != NULL);
79         g_return_if_fail(nick != NULL);
80
81         for (tmp = lists; tmp != NULL; tmp = tmp->next) {
82                 NICKMATCH_REC *rec = tmp->data;
83
84                 rec->func(rec->nicks, channel, nick);
85         }
86 }
87
88 static void sig_nick_remove(CHANNEL_REC *channel, NICK_REC *nick)
89 {
90         GSList *tmp;
91
92         g_return_if_fail(channel != NULL);
93         g_return_if_fail(nick != NULL);
94
95         for (tmp = lists; tmp != NULL; tmp = tmp->next) {
96                 NICKMATCH_REC *rec = tmp->data;
97
98                 g_hash_table_remove(rec->nicks, nick);
99         }
100 }
101
102 void nickmatch_cache_init(void)
103 {
104         lists = NULL;
105         signal_add("nicklist new", (SIGNAL_FUNC) sig_nick_new);
106         signal_add("nicklist changed", (SIGNAL_FUNC) sig_nick_new);
107         signal_add("nicklist host changed", (SIGNAL_FUNC) sig_nick_new);
108         signal_add("nicklist remove", (SIGNAL_FUNC) sig_nick_remove);
109 }
110
111 void nickmatch_cache_deinit(void)
112 {
113         g_slist_foreach(lists, (GFunc) nickmatch_deinit, NULL);
114         g_slist_free(lists);
115
116         signal_remove("nicklist new", (SIGNAL_FUNC) sig_nick_new);
117         signal_remove("nicklist changed", (SIGNAL_FUNC) sig_nick_new);
118         signal_remove("nicklist host changed", (SIGNAL_FUNC) sig_nick_new);
119         signal_remove("nicklist remove", (SIGNAL_FUNC) sig_nick_remove);
120 }