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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "lib-config/iconfig.h"
32 #include "nickmatch-cache.h"
38 static NICKMATCH_REC *nickmatch;
41 /* check if `text' contains ignored nick at the start of the line. */
42 static int ignore_check_replies_rec(IGNORE_REC *rec, CHANNEL_REC *channel,
47 nicks = nicklist_find_multiple(channel, rec->mask);
48 if (nicks == NULL) return FALSE;
50 for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
51 NICK_REC *nick = tmp->data;
53 if (nick_match_msg(channel, text, nick->nick))
61 #define ignore_match_channel(rec, channel) \
62 ((rec)->channels == NULL || ((channel) != NULL && \
63 strarray_find((rec)->channels, (channel)) != -1))
65 static int ignore_check_replies(CHANNEL_REC *chanrec, const char *text)
69 if (text == NULL || chanrec == NULL)
72 /* check reply ignores */
73 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
74 IGNORE_REC *rec = tmp->data;
76 if (rec->mask != NULL && rec->replies &&
77 ignore_match_channel(rec, chanrec->name) &&
78 ignore_check_replies_rec(rec, chanrec, text))
85 static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
87 if (rec->pattern == NULL)
95 return rec->regexp_compiled &&
96 regexec(&rec->preg, text, 0, NULL, 0) == 0;
102 return rec->fullword ?
103 stristr_full(text, rec->pattern) != NULL :
104 stristr(text, rec->pattern) != NULL;
107 #define ignore_match_level(rec, level) \
108 ((level & (rec)->level) != 0)
110 #define ignore_match_nickmask(rec, nick, nickmask) \
111 ((rec)->mask == NULL || \
112 (strchr((rec)->mask, '!') != NULL ? \
113 match_wildcards((rec)->mask, nickmask) : \
114 match_wildcards((rec)->mask, nick)))
116 #define ignore_match_server(rec, server) \
117 ((rec)->servertag == NULL || \
118 g_strcasecmp((server)->tag, (rec)->servertag) == 0)
120 int ignore_check(SERVER_REC *server, const char *nick, const char *host,
121 const char *channel, const char *text, int level)
123 CHANNEL_REC *chanrec;
128 int len, best_mask, best_match, best_patt;
130 g_return_val_if_fail(server != NULL, 0);
131 if (nick == NULL) nick = "";
133 chanrec = server == NULL || channel == NULL ? NULL :
134 channel_find(server, channel);
135 if (chanrec != NULL && nick != NULL &&
136 (nickrec = nicklist_find(chanrec, nick)) != NULL) {
137 /* nick found - check only ignores in nickmatch cache */
138 if (nickrec->host == NULL)
139 nicklist_set_host(chanrec, nickrec, host);
141 tmp = nickmatch_find(nickmatch, nickrec);
145 nickmask = g_strconcat(nick, "!", host, NULL);
148 best_mask = best_patt = -1; best_match = FALSE;
149 for (; tmp != NULL; tmp = tmp->next) {
153 if (nickmask != NULL)
154 match = ignore_match_server(rec, server) &&
155 ignore_match_channel(rec, channel) &&
156 ignore_match_nickmask(rec, nick, nickmask);
158 ignore_match_level(rec, level) &&
159 ignore_match_pattern(rec, text)) {
160 len = rec->mask == NULL ? 0 : strlen(rec->mask);
161 if (len > best_mask) {
163 best_match = !rec->exception;
164 } else if (len == best_mask) {
165 len = rec->pattern == NULL ? 0 : strlen(rec->pattern);
166 if (len > best_patt) {
168 best_match = !rec->exception;
169 } else if (len == best_patt && rec->exception)
176 if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
179 return ignore_check_replies(chanrec, text);
182 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
187 int ignore_servertag;
189 if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
192 ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
193 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
194 IGNORE_REC *rec = tmp->data;
196 if (!ignore_servertag) {
197 if ((servertag == NULL && rec->servertag != NULL) ||
198 (servertag != NULL && rec->servertag == NULL))
201 if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
205 if ((rec->mask == NULL && mask != NULL) ||
206 (rec->mask != NULL && mask == NULL)) continue;
208 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
211 if ((channels == NULL && rec->channels == NULL))
212 return rec; /* no channels - ok */
214 if (channels != NULL && strcmp(*channels, "*") == 0)
215 return rec; /* ignore channels */
217 if (channels == NULL || rec->channels == NULL)
218 continue; /* other doesn't have channels */
220 if (strarray_length(channels) != strarray_length(rec->channels))
221 continue; /* different amount of channels */
223 /* check that channels match */
224 for (chan = channels; *chan != NULL; chan++) {
225 if (strarray_find(rec->channels, *chan) == -1)
230 return rec; /* channels ok */
236 static void ignore_set_config(IGNORE_REC *rec)
244 node = iconfig_node_traverse("(ignores", TRUE);
245 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
247 if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
249 levelstr = bits2level(rec->level);
250 iconfig_node_set_str(node, "level", levelstr);
253 iconfig_node_set_str(node, "pattern", rec->pattern);
254 if (rec->exception) iconfig_node_set_bool(node, "exception", TRUE);
255 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
256 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
257 if (rec->replies) iconfig_node_set_bool(node, "replies", TRUE);
258 if (rec->unignore_time != 0)
259 iconfig_node_set_int(node, "unignore_time", rec->unignore_time);
260 iconfig_node_set_str(node, "servertag", rec->servertag);
262 if (rec->channels != NULL && *rec->channels != NULL) {
263 node = config_node_section(node, "channels", NODE_TYPE_LIST);
264 iconfig_node_add_list(node, rec->channels);
268 static int ignore_index(IGNORE_REC *find)
274 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
275 IGNORE_REC *rec = tmp->data;
285 static void ignore_remove_config(IGNORE_REC *rec)
289 node = iconfig_node_traverse("ignores", FALSE);
290 if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
293 static void ignore_init_rec(IGNORE_REC *rec)
296 if (rec->regexp_compiled) regfree(&rec->preg);
297 rec->regexp_compiled = !rec->regexp || rec->pattern == NULL ? FALSE :
298 regcomp(&rec->preg, rec->pattern,
299 REG_EXTENDED|REG_ICASE|REG_NOSUB) == 0;
303 void ignore_add_rec(IGNORE_REC *rec)
305 ignore_init_rec(rec);
307 ignores = g_slist_append(ignores, rec);
308 ignore_set_config(rec);
310 signal_emit("ignore created", 1, rec);
311 nickmatch_rebuild(nickmatch);
314 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
316 ignores = g_slist_remove(ignores, rec);
318 signal_emit("ignore destroyed", 1, rec);
321 if (rec->regexp_compiled) regfree(&rec->preg);
323 if (rec->channels != NULL) g_strfreev(rec->channels);
324 g_free_not_null(rec->mask);
325 g_free_not_null(rec->servertag);
326 g_free_not_null(rec->pattern);
329 nickmatch_rebuild(nickmatch);
332 void ignore_update_rec(IGNORE_REC *rec)
334 if (rec->level == 0) {
335 /* unignored everything */
336 ignore_remove_config(rec);
337 ignore_destroy(rec, TRUE);
339 /* unignore just some levels.. */
340 ignore_remove_config(rec);
341 ignores = g_slist_remove(ignores, rec);
343 ignores = g_slist_append(ignores, rec);
344 ignore_set_config(rec);
346 ignore_init_rec(rec);
347 signal_emit("ignore changed", 1, rec);
348 nickmatch_rebuild(nickmatch);
352 static int unignore_timeout(void)
358 for (tmp = ignores; tmp != NULL; tmp = next) {
359 IGNORE_REC *rec = tmp->data;
362 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
364 ignore_update_rec(rec);
371 static void read_ignores(void)
377 while (ignores != NULL)
378 ignore_destroy(ignores->data, FALSE);
380 node = iconfig_node_traverse("ignores", FALSE);
382 nickmatch_rebuild(nickmatch);
386 tmp = config_node_first(node->value);
387 for (; tmp != NULL; tmp = config_node_next(tmp)) {
390 if (node->type != NODE_TYPE_BLOCK)
393 rec = g_new0(IGNORE_REC, 1);
394 ignores = g_slist_append(ignores, rec);
396 rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
397 rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
398 rec->level = level2bits(config_node_get_str(node, "level", ""), NULL);
399 rec->exception = config_node_get_bool(node, "exception", FALSE);
400 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
401 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
402 rec->replies = config_node_get_bool(node, "replies", FALSE);
403 rec->unignore_time = config_node_get_int(node, "unignore_time", 0);
404 rec->servertag = g_strdup(config_node_get_str(node, "servertag", 0));
406 node = config_node_section(node, "channels", -1);
407 if (node != NULL) rec->channels = config_node_get_list(node);
409 ignore_init_rec(rec);
412 nickmatch_rebuild(nickmatch);
415 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
418 GSList *tmp, *matches;
421 if (nick->host == NULL)
422 return; /* don't check until host is known */
425 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
426 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
427 IGNORE_REC *rec = tmp->data;
429 if (ignore_match_nickmask(rec, nick->nick, nickmask) &&
430 ignore_match_server(rec, channel->server) &&
431 ignore_match_channel(rec, channel->name))
432 matches = g_slist_append(matches, rec);
434 g_free_not_null(nickmask);
437 g_hash_table_remove(list, nick);
439 g_hash_table_insert(list, nick, matches);
442 void ignore_init(void)
445 nickmatch = nickmatch_init(ignore_nick_cache);
446 time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
449 signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
452 void ignore_deinit(void)
454 g_source_remove(time_tag);
455 while (ignores != NULL)
456 ignore_destroy(ignores->data, TRUE);
457 nickmatch_deinit(nickmatch);
459 signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);