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
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 static int ignore_check_replies(CHANNEL_REC *chanrec, const char *text)
65 if (text == NULL || chanrec == NULL)
68 /* check reply ignores */
69 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
70 IGNORE_REC *rec = tmp->data;
72 if (rec->mask != NULL && rec->replies &&
73 ignore_check_replies_rec(rec, chanrec, text))
80 static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
82 if (rec->pattern == NULL)
90 return rec->regexp_compiled &&
91 regexec(&rec->preg, text, 0, NULL, 0) == 0;
97 return rec->fullword ?
98 stristr_full(text, rec->pattern) != NULL :
99 stristr(text, rec->pattern) != NULL;
102 #define ignore_match_level(rec, level) \
103 ((level & (rec)->level) != 0)
105 #define ignore_match_nickmask(rec, nick, nickmask) \
106 ((rec)->mask == NULL || \
107 (strchr((rec)->mask, '!') != NULL ? \
108 match_wildcards((rec)->mask, nickmask) : \
109 match_wildcards((rec)->mask, nick)))
111 #define ignore_match_server(rec, server) \
112 ((rec)->servertag == NULL || \
113 g_strcasecmp((server)->tag, (rec)->servertag) == 0)
115 #define ignore_match_channel(rec, channel) \
116 ((rec)->channels == NULL || ((channel) != NULL && \
117 strarray_find((rec)->channels, (channel)) != -1))
119 static int ignore_check_without_mask(GSList *list, CHANNEL_REC *channel,
120 int level, const char *text)
123 int len, best_mask, best_match, best_patt;
125 best_mask = best_patt = -1; best_match = FALSE;
126 for (tmp = list; tmp != NULL; tmp = tmp->next) {
127 IGNORE_REC *rec = tmp->data;
129 if (ignore_match_level(rec, level) &&
130 ignore_match_pattern(rec, text)) {
131 len = rec->mask == NULL ? 0 : strlen(rec->mask);
132 if (len > best_mask) {
134 best_match = !rec->exception;
135 } else if (len == best_mask && rec->pattern != NULL) {
136 len = strlen(rec->pattern);
137 if (len > best_patt) {
139 best_match = !rec->exception;
145 if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
148 return ignore_check_replies(channel, text);
151 int ignore_check(SERVER_REC *server, const char *nick, const char *host,
152 const char *channel, const char *text, int level)
154 CHANNEL_REC *chanrec;
159 int len, best_mask, best_match, best_patt;
161 g_return_val_if_fail(server != NULL, 0);
162 if (nick == NULL) nick = "";
164 chanrec = (channel != NULL && server != NULL &&
165 server->ischannel(channel)) ?
166 channel_find(server, channel) : NULL;
167 if (chanrec != NULL && nick != NULL &&
168 (nickrec = nicklist_find(chanrec, nick)) != NULL) {
169 /* nick found - check only ignores in nickmatch cache */
170 if (nickrec->host == NULL)
171 nicklist_set_host(chanrec, nickrec, host);
173 list = nickmatch_find(nickmatch, nickrec);
174 return ignore_check_without_mask(list, chanrec, level, text);
177 nickmask = g_strconcat(nick, "!", host, NULL);
179 best_mask = best_patt = -1; best_match = FALSE;
180 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
183 if (ignore_match_level(rec, level) &&
184 ignore_match_server(rec, server) &&
185 ignore_match_channel(rec, channel) &&
186 ignore_match_nickmask(rec, nick, nickmask) &&
187 ignore_match_pattern(rec, text)) {
188 len = rec->mask == NULL ? 0 : strlen(rec->mask);
189 if (len > best_mask) {
191 best_match = !rec->exception;
192 } else if (len == best_mask && rec->pattern != NULL) {
193 len = strlen(rec->pattern);
194 if (len > best_patt) {
196 best_match = !rec->exception;
203 if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
206 return ignore_check_replies(chanrec, text);
209 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
214 int ignore_servertag;
216 if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
219 ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
220 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
221 IGNORE_REC *rec = tmp->data;
223 if (!ignore_servertag) {
224 if ((servertag == NULL && rec->servertag != NULL) ||
225 (servertag != NULL && rec->servertag == NULL))
228 if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
232 if ((rec->mask == NULL && mask != NULL) ||
233 (rec->mask != NULL && mask == NULL)) continue;
235 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
238 if ((channels == NULL && rec->channels == NULL))
239 return rec; /* no channels - ok */
241 if (channels != NULL && strcmp(*channels, "*") == 0)
242 return rec; /* ignore channels */
244 if (channels == NULL || rec->channels == NULL)
245 continue; /* other doesn't have channels */
247 if (strarray_length(channels) != strarray_length(rec->channels))
248 continue; /* different amount of channels */
250 /* check that channels match */
251 for (chan = channels; *chan != NULL; chan++) {
252 if (strarray_find(rec->channels, *chan) == -1)
257 return rec; /* channels ok */
263 static void ignore_set_config(IGNORE_REC *rec)
268 if (rec->level == 0 || rec->unignore_time > 0)
271 node = iconfig_node_traverse("(ignores", TRUE);
272 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
274 if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
276 levelstr = bits2level(rec->level);
277 iconfig_node_set_str(node, "level", levelstr);
280 iconfig_node_set_str(node, "pattern", rec->pattern);
281 if (rec->exception) iconfig_node_set_bool(node, "exception", TRUE);
282 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
283 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
284 if (rec->replies) iconfig_node_set_bool(node, "replies", TRUE);
286 if (rec->channels != NULL && *rec->channels != NULL) {
287 node = config_node_section(node, "channels", NODE_TYPE_LIST);
288 iconfig_node_add_list(node, rec->channels);
292 static int ignore_index(IGNORE_REC *find)
298 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
299 IGNORE_REC *rec = tmp->data;
301 if (rec->servertag != NULL)
312 static void ignore_remove_config(IGNORE_REC *rec)
316 node = iconfig_node_traverse("ignores", FALSE);
317 if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
320 void ignore_add_rec(IGNORE_REC *rec)
323 rec->regexp_compiled = !rec->regexp || rec->pattern == NULL ? FALSE :
324 regcomp(&rec->preg, rec->pattern,
325 REG_EXTENDED|REG_ICASE|REG_NOSUB) == 0;
328 ignores = g_slist_append(ignores, rec);
329 ignore_set_config(rec);
331 signal_emit("ignore created", 1, rec);
334 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
336 ignores = g_slist_remove(ignores, rec);
338 signal_emit("ignore destroyed", 1, rec);
341 if (rec->regexp_compiled) regfree(&rec->preg);
343 if (rec->channels != NULL) g_strfreev(rec->channels);
344 g_free_not_null(rec->mask);
345 g_free_not_null(rec->servertag);
346 g_free_not_null(rec->pattern);
349 nickmatch_rebuild(nickmatch);
352 void ignore_update_rec(IGNORE_REC *rec)
354 if (rec->level == 0) {
355 /* unignored everything */
356 ignore_remove_config(rec);
357 ignore_destroy(rec, TRUE);
359 /* unignore just some levels.. */
360 ignore_remove_config(rec);
361 ignores = g_slist_remove(ignores, rec);
363 ignores = g_slist_append(ignores, rec);
364 ignore_set_config(rec);
366 signal_emit("ignore changed", 1, rec);
367 nickmatch_rebuild(nickmatch);
371 static int unignore_timeout(void)
377 for (tmp = ignores; tmp != NULL; tmp = next) {
378 IGNORE_REC *rec = tmp->data;
381 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
383 ignore_update_rec(rec);
390 static void read_ignores(void)
396 while (ignores != NULL)
397 ignore_destroy(ignores->data, FALSE);
399 node = iconfig_node_traverse("ignores", FALSE);
401 nickmatch_rebuild(nickmatch);
405 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
408 if (node->type != NODE_TYPE_BLOCK)
411 rec = g_new0(IGNORE_REC, 1);
412 ignores = g_slist_append(ignores, rec);
414 rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
415 if (rec->mask != NULL && strcmp(rec->mask, "*") == 0) {
416 /* FIXME: remove after .98 */
420 rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
421 rec->level = level2bits(config_node_get_str(node, "level", ""));
422 rec->exception = config_node_get_bool(node, "exception", FALSE);
423 if (*config_node_get_str(node, "except_level", "") != '\0') {
424 /* FIXME: remove after .98 */
425 rec->level = level2bits(config_node_get_str(node, "except_level", ""));
426 rec->exception = TRUE;
428 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
429 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
430 rec->replies = config_node_get_bool(node, "replies", FALSE);
432 node = config_node_section(node, "channels", -1);
433 if (node != NULL) rec->channels = config_node_get_list(node);
436 nickmatch_rebuild(nickmatch);
439 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
442 GSList *tmp, *matches;
445 if (nick->host == NULL)
446 return; /* don't check until host is known */
449 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
450 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
451 IGNORE_REC *rec = tmp->data;
453 if (ignore_match_nickmask(rec, nick->nick, nickmask) &&
454 ignore_match_server(rec, channel->server) &&
455 ignore_match_channel(rec, channel->name))
456 matches = g_slist_append(matches, rec);
458 g_free_not_null(nickmask);
461 g_hash_table_remove(list, nick);
463 g_hash_table_insert(list, nick, matches);
466 void ignore_init(void)
469 nickmatch = nickmatch_init(ignore_nick_cache);
470 time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
473 signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
476 void ignore_deinit(void)
478 g_source_remove(time_tag);
479 while (ignores != NULL)
480 ignore_destroy(ignores->data, TRUE);
481 nickmatch_deinit(nickmatch);
483 signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);