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 = server == NULL || channel == NULL ? NULL :
165 channel_find(server, channel);
166 if (chanrec != NULL && nick != NULL &&
167 (nickrec = nicklist_find(chanrec, nick)) != NULL) {
168 /* nick found - check only ignores in nickmatch cache */
169 if (nickrec->host == NULL)
170 nicklist_set_host(chanrec, nickrec, host);
172 list = nickmatch_find(nickmatch, nickrec);
173 return ignore_check_without_mask(list, chanrec, level, text);
176 nickmask = g_strconcat(nick, "!", host, NULL);
178 best_mask = best_patt = -1; best_match = FALSE;
179 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
182 if (ignore_match_level(rec, level) &&
183 ignore_match_server(rec, server) &&
184 ignore_match_channel(rec, channel) &&
185 ignore_match_nickmask(rec, nick, nickmask) &&
186 ignore_match_pattern(rec, text)) {
187 len = rec->mask == NULL ? 0 : strlen(rec->mask);
188 if (len > best_mask) {
190 best_match = !rec->exception;
191 } else if (len == best_mask && rec->pattern != NULL) {
192 len = strlen(rec->pattern);
193 if (len > best_patt) {
195 best_match = !rec->exception;
202 if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
205 return ignore_check_replies(chanrec, text);
208 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
213 int ignore_servertag;
215 if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
218 ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
219 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
220 IGNORE_REC *rec = tmp->data;
222 if (!ignore_servertag) {
223 if ((servertag == NULL && rec->servertag != NULL) ||
224 (servertag != NULL && rec->servertag == NULL))
227 if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
231 if ((rec->mask == NULL && mask != NULL) ||
232 (rec->mask != NULL && mask == NULL)) continue;
234 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
237 if ((channels == NULL && rec->channels == NULL))
238 return rec; /* no channels - ok */
240 if (channels != NULL && strcmp(*channels, "*") == 0)
241 return rec; /* ignore channels */
243 if (channels == NULL || rec->channels == NULL)
244 continue; /* other doesn't have channels */
246 if (strarray_length(channels) != strarray_length(rec->channels))
247 continue; /* different amount of channels */
249 /* check that channels match */
250 for (chan = channels; *chan != NULL; chan++) {
251 if (strarray_find(rec->channels, *chan) == -1)
256 return rec; /* channels ok */
262 static void ignore_set_config(IGNORE_REC *rec)
270 node = iconfig_node_traverse("(ignores", TRUE);
271 node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
273 if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
275 levelstr = bits2level(rec->level);
276 iconfig_node_set_str(node, "level", levelstr);
279 iconfig_node_set_str(node, "pattern", rec->pattern);
280 if (rec->exception) iconfig_node_set_bool(node, "exception", TRUE);
281 if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
282 if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
283 if (rec->replies) iconfig_node_set_bool(node, "replies", TRUE);
284 if (rec->unignore_time != 0)
285 iconfig_node_set_int(node, "unignore_time", rec->unignore_time);
286 iconfig_node_set_str(node, "servertag", rec->servertag);
288 if (rec->channels != NULL && *rec->channels != NULL) {
289 node = config_node_section(node, "channels", NODE_TYPE_LIST);
290 iconfig_node_add_list(node, rec->channels);
294 static int ignore_index(IGNORE_REC *find)
300 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
301 IGNORE_REC *rec = tmp->data;
311 static void ignore_remove_config(IGNORE_REC *rec)
315 node = iconfig_node_traverse("ignores", FALSE);
316 if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
319 static void ignore_init_rec(IGNORE_REC *rec)
322 if (rec->regexp_compiled) regfree(&rec->preg);
323 rec->regexp_compiled = !rec->regexp || rec->pattern == NULL ? FALSE :
324 regcomp(&rec->preg, rec->pattern,
325 REG_EXTENDED|REG_ICASE|REG_NOSUB) == 0;
329 void ignore_add_rec(IGNORE_REC *rec)
331 ignore_init_rec(rec);
333 ignores = g_slist_append(ignores, rec);
334 ignore_set_config(rec);
336 signal_emit("ignore created", 1, rec);
337 nickmatch_rebuild(nickmatch);
340 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
342 ignores = g_slist_remove(ignores, rec);
344 signal_emit("ignore destroyed", 1, rec);
347 if (rec->regexp_compiled) regfree(&rec->preg);
349 if (rec->channels != NULL) g_strfreev(rec->channels);
350 g_free_not_null(rec->mask);
351 g_free_not_null(rec->servertag);
352 g_free_not_null(rec->pattern);
355 nickmatch_rebuild(nickmatch);
358 void ignore_update_rec(IGNORE_REC *rec)
360 if (rec->level == 0) {
361 /* unignored everything */
362 ignore_remove_config(rec);
363 ignore_destroy(rec, TRUE);
365 /* unignore just some levels.. */
366 ignore_remove_config(rec);
367 ignores = g_slist_remove(ignores, rec);
369 ignores = g_slist_append(ignores, rec);
370 ignore_set_config(rec);
372 ignore_init_rec(rec);
373 signal_emit("ignore changed", 1, rec);
374 nickmatch_rebuild(nickmatch);
378 static int unignore_timeout(void)
384 for (tmp = ignores; tmp != NULL; tmp = next) {
385 IGNORE_REC *rec = tmp->data;
388 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
390 ignore_update_rec(rec);
397 static void read_ignores(void)
403 while (ignores != NULL)
404 ignore_destroy(ignores->data, FALSE);
406 node = iconfig_node_traverse("ignores", FALSE);
408 nickmatch_rebuild(nickmatch);
412 tmp = config_node_first(node->value);
413 for (; tmp != NULL; tmp = config_node_next(tmp)) {
416 if (node->type != NODE_TYPE_BLOCK)
419 rec = g_new0(IGNORE_REC, 1);
420 ignores = g_slist_append(ignores, rec);
422 rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
423 rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
424 rec->level = level2bits(config_node_get_str(node, "level", ""));
425 rec->exception = config_node_get_bool(node, "exception", FALSE);
426 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
427 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
428 rec->replies = config_node_get_bool(node, "replies", FALSE);
429 rec->unignore_time = config_node_get_int(node, "unignore_time", 0);
430 rec->servertag = g_strdup(config_node_get_str(node, "servertag", 0));
432 node = config_node_section(node, "channels", -1);
433 if (node != NULL) rec->channels = config_node_get_list(node);
435 ignore_init_rec(rec);
438 nickmatch_rebuild(nickmatch);
441 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
444 GSList *tmp, *matches;
447 if (nick->host == NULL)
448 return; /* don't check until host is known */
451 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
452 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
453 IGNORE_REC *rec = tmp->data;
455 if (ignore_match_nickmask(rec, nick->nick, nickmask) &&
456 ignore_match_server(rec, channel->server) &&
457 ignore_match_channel(rec, channel->name))
458 matches = g_slist_append(matches, rec);
460 g_free_not_null(nickmask);
463 g_hash_table_remove(list, nick);
465 g_hash_table_insert(list, nick, matches);
468 void ignore_init(void)
471 nickmatch = nickmatch_init(ignore_nick_cache);
472 time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
475 signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
478 void ignore_deinit(void)
480 g_source_remove(time_tag);
481 while (ignores != NULL)
482 ignore_destroy(ignores->data, TRUE);
483 nickmatch_deinit(nickmatch);
485 signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);