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)
267 if (rec->level == 0 || rec->unignore_time > 0)
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);
285 if (rec->channels != NULL && *rec->channels != NULL) {
286 node = config_node_section(node, "channels", NODE_TYPE_LIST);
287 iconfig_node_add_list(node, rec->channels);
291 static int ignore_index(IGNORE_REC *find)
297 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
298 IGNORE_REC *rec = tmp->data;
300 if (rec->servertag != NULL)
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 rec->regexp_compiled = !rec->regexp || rec->pattern == NULL ? FALSE :
323 regcomp(&rec->preg, rec->pattern,
324 REG_EXTENDED|REG_ICASE|REG_NOSUB) == 0;
328 void ignore_add_rec(IGNORE_REC *rec)
330 ignore_init_rec(rec);
332 ignores = g_slist_append(ignores, rec);
333 ignore_set_config(rec);
335 signal_emit("ignore created", 1, rec);
336 nickmatch_rebuild(nickmatch);
339 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
341 ignores = g_slist_remove(ignores, rec);
343 signal_emit("ignore destroyed", 1, rec);
346 if (rec->regexp_compiled) regfree(&rec->preg);
348 if (rec->channels != NULL) g_strfreev(rec->channels);
349 g_free_not_null(rec->mask);
350 g_free_not_null(rec->servertag);
351 g_free_not_null(rec->pattern);
354 nickmatch_rebuild(nickmatch);
357 void ignore_update_rec(IGNORE_REC *rec)
359 if (rec->level == 0) {
360 /* unignored everything */
361 ignore_remove_config(rec);
362 ignore_destroy(rec, TRUE);
364 /* unignore just some levels.. */
365 ignore_remove_config(rec);
366 ignores = g_slist_remove(ignores, rec);
368 ignores = g_slist_append(ignores, rec);
369 ignore_set_config(rec);
371 signal_emit("ignore changed", 1, rec);
372 nickmatch_rebuild(nickmatch);
376 static int unignore_timeout(void)
382 for (tmp = ignores; tmp != NULL; tmp = next) {
383 IGNORE_REC *rec = tmp->data;
386 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
388 ignore_update_rec(rec);
395 static void read_ignores(void)
401 while (ignores != NULL)
402 ignore_destroy(ignores->data, FALSE);
404 node = iconfig_node_traverse("ignores", FALSE);
406 nickmatch_rebuild(nickmatch);
410 tmp = config_node_first(node->value);
411 for (; tmp != NULL; tmp = config_node_next(tmp)) {
414 if (node->type != NODE_TYPE_BLOCK)
417 rec = g_new0(IGNORE_REC, 1);
418 ignores = g_slist_append(ignores, rec);
420 rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
421 rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
422 rec->level = level2bits(config_node_get_str(node, "level", ""));
423 rec->exception = config_node_get_bool(node, "exception", FALSE);
424 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
425 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
426 rec->replies = config_node_get_bool(node, "replies", FALSE);
428 node = config_node_section(node, "channels", -1);
429 if (node != NULL) rec->channels = config_node_get_list(node);
431 ignore_init_rec(rec);
434 nickmatch_rebuild(nickmatch);
437 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
440 GSList *tmp, *matches;
443 if (nick->host == NULL)
444 return; /* don't check until host is known */
447 nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
448 for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
449 IGNORE_REC *rec = tmp->data;
451 if (ignore_match_nickmask(rec, nick->nick, nickmask) &&
452 ignore_match_server(rec, channel->server) &&
453 ignore_match_channel(rec, channel->name))
454 matches = g_slist_append(matches, rec);
456 g_free_not_null(nickmask);
459 g_hash_table_remove(list, nick);
461 g_hash_table_insert(list, nick, matches);
464 void ignore_init(void)
467 nickmatch = nickmatch_init(ignore_nick_cache);
468 time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
471 signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
474 void ignore_deinit(void)
476 g_source_remove(time_tag);
477 while (ignores != NULL)
478 ignore_destroy(ignores->data, TRUE);
479 nickmatch_deinit(nickmatch);
481 signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);