Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / core / ignore.c
1 /*
2  ignore.c : irssi
3
4     Copyright (C) 1999-2000 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 #include "misc.h"
24 #include "levels.h"
25 #include "lib-config/iconfig.h"
26 #include "settings.h"
27
28 #include "masks.h"
29 #include "servers.h"
30 #include "channels.h"
31 #include "nicklist.h"
32 #include "nickmatch-cache.h"
33
34 #include "ignore.h"
35
36 GSList *ignores;
37
38 static NICKMATCH_REC *nickmatch;
39 static int time_tag;
40
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,
43                                     const char *text)
44 {
45         GSList *nicks, *tmp;
46
47         nicks = nicklist_find_multiple(channel, rec->mask);
48         if (nicks == NULL) return FALSE;
49
50         for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
51                 NICK_REC *nick = tmp->data;
52
53                 if (nick_match_msg(channel, text, nick->nick))
54                         return TRUE;
55         }
56         g_slist_free(nicks);
57
58         return FALSE;
59 }
60
61 static int ignore_check_replies(CHANNEL_REC *chanrec, const char *text)
62 {
63         GSList *tmp;
64
65         if (text == NULL || chanrec == NULL)
66                 return FALSE;
67
68         /* check reply ignores */
69         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
70                 IGNORE_REC *rec = tmp->data;
71
72                 if (rec->mask != NULL && rec->replies &&
73                     ignore_check_replies_rec(rec, chanrec, text))
74                         return TRUE;
75         }
76
77         return FALSE;
78 }
79
80 static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
81 {
82         if (rec->pattern == NULL)
83                 return TRUE;
84
85         if (text == NULL)
86                 return FALSE;
87
88         if (rec->regexp) {
89 #ifdef HAVE_REGEX_H
90                 return rec->regexp_compiled &&
91                         regexec(&rec->preg, text, 0, NULL, 0) == 0;
92 #else
93                 return FALSE;
94 #endif
95         }
96
97         return rec->fullword ?
98                 stristr_full(text, rec->pattern) != NULL :
99                 stristr(text, rec->pattern) != NULL;
100 }
101
102 #define ignore_match_level(rec, level) \
103         ((level & (rec)->level) != 0)
104
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)))
110
111 #define ignore_match_server(rec, server) \
112         ((rec)->servertag == NULL || \
113         g_strcasecmp((server)->tag, (rec)->servertag) == 0)
114
115 #define ignore_match_channel(rec, channel) \
116         ((rec)->channels == NULL || ((channel) != NULL && \
117                 strarray_find((rec)->channels, (channel)) != -1))
118
119 static int ignore_check_without_mask(GSList *list, CHANNEL_REC *channel,
120                                      int level, const char *text)
121 {
122         GSList *tmp;
123         int len, best_mask, best_match, best_patt;
124
125         best_mask = best_patt = -1; best_match = FALSE;
126         for (tmp = list; tmp != NULL; tmp = tmp->next) {
127                 IGNORE_REC *rec = tmp->data;
128
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) {
133                                 best_mask = len;
134                                 best_match = !rec->exception;
135                         } else if (len == best_mask && rec->pattern != NULL) {
136                                 len = strlen(rec->pattern);
137                                 if (len > best_patt) {
138                                         best_patt = len;
139                                         best_match = !rec->exception;
140                                 }
141                         }
142                 }
143         }
144
145         if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
146                 return best_match;
147
148         return ignore_check_replies(channel, text);
149 }
150
151 int ignore_check(SERVER_REC *server, const char *nick, const char *host,
152                  const char *channel, const char *text, int level)
153 {
154         CHANNEL_REC *chanrec;
155         NICK_REC *nickrec;
156         IGNORE_REC *rec;
157         GSList *tmp, *list;
158         char *nickmask;
159         int len, best_mask, best_match, best_patt;
160
161         g_return_val_if_fail(server != NULL, 0);
162         if (nick == NULL) nick = "";
163
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);
171
172                 list = nickmatch_find(nickmatch, nickrec);
173                 return ignore_check_without_mask(list, chanrec, level, text);
174         }
175
176         nickmask = g_strconcat(nick, "!", host, NULL);
177
178         best_mask = best_patt = -1; best_match = FALSE;
179         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
180                 rec = tmp->data;
181
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) {
189                                 best_mask = len;
190                                 best_match = !rec->exception;
191                         } else if (len == best_mask && rec->pattern != NULL) {
192                                 len = strlen(rec->pattern);
193                                 if (len > best_patt) {
194                                         best_patt = len;
195                                         best_match = !rec->exception;
196                                 }
197                         }
198                 }
199         }
200         g_free(nickmask);
201
202         if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
203                 return best_match;
204
205         return ignore_check_replies(chanrec, text);
206 }
207
208 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
209                         char **channels)
210 {
211         GSList *tmp;
212         char **chan;
213         int ignore_servertag;
214
215         if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
216                 mask = NULL;
217
218         ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
219         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
220                 IGNORE_REC *rec = tmp->data;
221
222                 if (!ignore_servertag) {
223                         if ((servertag == NULL && rec->servertag != NULL) ||
224                             (servertag != NULL && rec->servertag == NULL))
225                                 continue;
226
227                         if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
228                                 continue;
229                 }
230
231                 if ((rec->mask == NULL && mask != NULL) ||
232                     (rec->mask != NULL && mask == NULL)) continue;
233
234                 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
235                         continue;
236
237                 if ((channels == NULL && rec->channels == NULL))
238                         return rec; /* no channels - ok */
239
240                 if (channels != NULL && strcmp(*channels, "*") == 0)
241                         return rec; /* ignore channels */
242
243                 if (channels == NULL || rec->channels == NULL)
244                         continue; /* other doesn't have channels */
245
246                 if (strarray_length(channels) != strarray_length(rec->channels))
247                         continue; /* different amount of channels */
248
249                 /* check that channels match */
250                 for (chan = channels; *chan != NULL; chan++) {
251                         if (strarray_find(rec->channels, *chan) == -1)
252                                 break;
253                 }
254
255                 if (*chan == NULL)
256                         return rec; /* channels ok */
257         }
258
259         return NULL;
260 }
261
262 static void ignore_set_config(IGNORE_REC *rec)
263 {
264         CONFIG_NODE *node;
265         char *levelstr;
266
267         if (rec->level == 0)
268                 return;
269
270         node = iconfig_node_traverse("(ignores", TRUE);
271         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
272
273         if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
274         if (rec->level) {
275                 levelstr = bits2level(rec->level);
276                 iconfig_node_set_str(node, "level", levelstr);
277                 g_free(levelstr);
278         }
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);
287
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);
291         }
292 }
293
294 static int ignore_index(IGNORE_REC *find)
295 {
296         GSList *tmp;
297         int index;
298
299         index = 0;
300         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
301                 IGNORE_REC *rec = tmp->data;
302
303                 if (rec == find)
304                         return index;
305                 index++;
306         }
307
308         return -1;
309 }
310
311 static void ignore_remove_config(IGNORE_REC *rec)
312 {
313         CONFIG_NODE *node;
314
315         node = iconfig_node_traverse("ignores", FALSE);
316         if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
317 }
318
319 static void ignore_init_rec(IGNORE_REC *rec)
320 {
321 #ifdef HAVE_REGEX_H
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;
326 #endif
327 }
328
329 void ignore_add_rec(IGNORE_REC *rec)
330 {
331         ignore_init_rec(rec);
332
333         ignores = g_slist_append(ignores, rec);
334         ignore_set_config(rec);
335
336         signal_emit("ignore created", 1, rec);
337         nickmatch_rebuild(nickmatch);
338 }
339
340 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
341 {
342         ignores = g_slist_remove(ignores, rec);
343         if (send_signal)
344                 signal_emit("ignore destroyed", 1, rec);
345
346 #ifdef HAVE_REGEX_H
347         if (rec->regexp_compiled) regfree(&rec->preg);
348 #endif
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);
353         g_free(rec);
354
355         nickmatch_rebuild(nickmatch);
356 }
357
358 void ignore_update_rec(IGNORE_REC *rec)
359 {
360         if (rec->level == 0) {
361                 /* unignored everything */
362                 ignore_remove_config(rec);
363                 ignore_destroy(rec, TRUE);
364         } else {
365                 /* unignore just some levels.. */
366                 ignore_remove_config(rec);
367                 ignores = g_slist_remove(ignores, rec);
368
369                 ignores = g_slist_append(ignores, rec);
370                 ignore_set_config(rec);
371
372                 ignore_init_rec(rec);
373                 signal_emit("ignore changed", 1, rec);
374                 nickmatch_rebuild(nickmatch);
375         }
376 }
377
378 static int unignore_timeout(void)
379 {
380         GSList *tmp, *next;
381         time_t now;
382
383         now = time(NULL);
384         for (tmp = ignores; tmp != NULL; tmp = next) {
385                 IGNORE_REC *rec = tmp->data;
386
387                 next = tmp->next;
388                 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
389                         rec->level = 0;
390                         ignore_update_rec(rec);
391                 }
392         }
393
394         return TRUE;
395 }
396
397 static void read_ignores(void)
398 {
399         IGNORE_REC *rec;
400         CONFIG_NODE *node;
401         GSList *tmp;
402
403         while (ignores != NULL)
404                 ignore_destroy(ignores->data, FALSE);
405
406         node = iconfig_node_traverse("ignores", FALSE);
407         if (node == NULL) {
408                 nickmatch_rebuild(nickmatch);
409                 return;
410         }
411
412         tmp = config_node_first(node->value);
413         for (; tmp != NULL; tmp = config_node_next(tmp)) {
414                 node = tmp->data;
415
416                 if (node->type != NODE_TYPE_BLOCK)
417                         continue;
418
419                 rec = g_new0(IGNORE_REC, 1);
420                 ignores = g_slist_append(ignores, rec);
421
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));
431
432                 node = config_node_section(node, "channels", -1);
433                 if (node != NULL) rec->channels = config_node_get_list(node);
434
435                 ignore_init_rec(rec);
436         }
437
438         nickmatch_rebuild(nickmatch);
439 }
440
441 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
442                               NICK_REC *nick)
443 {
444         GSList *tmp, *matches;
445         char *nickmask;
446
447         if (nick->host == NULL)
448                 return; /* don't check until host is known */
449
450         matches = NULL;
451         nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
452         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
453                 IGNORE_REC *rec = tmp->data;
454
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);
459         }
460         g_free_not_null(nickmask);
461
462         if (matches == NULL)
463                 g_hash_table_remove(list, nick);
464         else
465                 g_hash_table_insert(list, nick, matches);
466 }
467
468 void ignore_init(void)
469 {
470         ignores = NULL;
471         nickmatch = nickmatch_init(ignore_nick_cache);
472         time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
473
474         read_ignores();
475         signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
476 }
477
478 void ignore_deinit(void)
479 {
480         g_source_remove(time_tag);
481         while (ignores != NULL)
482                 ignore_destroy(ignores->data, TRUE);
483         nickmatch_deinit(nickmatch);
484
485         signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);
486 }