Merge Irssi 0.8.16-rc1
[silc.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 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.
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 #define ignore_match_channel(rec, channel) \
62         ((rec)->channels == NULL || ((channel) != NULL && \
63                 strarray_find((rec)->channels, (channel)) != -1))
64
65 static int ignore_check_replies(CHANNEL_REC *chanrec, const char *text)
66 {
67         GSList *tmp;
68
69         if (text == NULL || chanrec == NULL)
70                 return FALSE;
71
72         /* check reply ignores */
73         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
74                 IGNORE_REC *rec = tmp->data;
75
76                 if (rec->mask != NULL && rec->replies &&
77                     ignore_match_channel(rec, chanrec->name) &&
78                     ignore_check_replies_rec(rec, chanrec, text))
79                         return TRUE;
80         }
81
82         return FALSE;
83 }
84
85 static int ignore_match_pattern(IGNORE_REC *rec, const char *text)
86 {
87         if (rec->pattern == NULL)
88                 return TRUE;
89
90         if (text == NULL)
91                 return FALSE;
92
93         if (rec->regexp) {
94 #ifdef HAVE_REGEX_H
95                 return rec->regexp_compiled &&
96                         regexec(&rec->preg, text, 0, NULL, 0) == 0;
97 #else
98                 return FALSE;
99 #endif
100         }
101
102         return rec->fullword ?
103                 stristr_full(text, rec->pattern) != NULL :
104                 stristr(text, rec->pattern) != NULL;
105 }
106
107 #define ignore_match_level(rec, level) \
108         ((level & (rec)->level) != 0)
109
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)))
115
116 #define ignore_match_server(rec, server) \
117         ((rec)->servertag == NULL || \
118         g_strcasecmp((server)->tag, (rec)->servertag) == 0)
119
120 int ignore_check(SERVER_REC *server, const char *nick, const char *host,
121                  const char *channel, const char *text, int level)
122 {
123         CHANNEL_REC *chanrec;
124         NICK_REC *nickrec;
125         IGNORE_REC *rec;
126         GSList *tmp;
127         char *nickmask;
128         int len, best_mask, best_match, best_patt;
129
130         g_return_val_if_fail(server != NULL, 0);
131         if (nick == NULL) nick = "";
132
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);
140
141                 tmp = nickmatch_find(nickmatch, nickrec);
142                 nickmask = NULL;
143         } else {
144                 tmp = ignores;
145                 nickmask = g_strconcat(nick, "!", host, NULL);
146         }
147
148         best_mask = best_patt = -1; best_match = FALSE;
149         for (; tmp != NULL; tmp = tmp->next) {
150                 int match = 1;
151                 rec = tmp->data;
152
153                 if (nickmask != NULL)
154                         match = ignore_match_server(rec, server) &&
155                                 ignore_match_channel(rec, channel) &&
156                                 ignore_match_nickmask(rec, nick, nickmask);
157                 if (match &&
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) {
162                                 best_mask = len;
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) {
167                                         best_patt = len;
168                                         best_match = !rec->exception;
169                                 } else if (len == best_patt && rec->exception)
170                                         best_match = 0;
171                         }
172                 }
173         }
174         g_free(nickmask);
175
176         if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
177                 return best_match;
178
179         return ignore_check_replies(chanrec, text);
180 }
181
182 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
183                         char **channels)
184 {
185         GSList *tmp;
186         char **chan;
187         int ignore_servertag;
188
189         if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
190                 mask = NULL;
191
192         ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
193         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
194                 IGNORE_REC *rec = tmp->data;
195
196                 if (!ignore_servertag) {
197                         if ((servertag == NULL && rec->servertag != NULL) ||
198                             (servertag != NULL && rec->servertag == NULL))
199                                 continue;
200
201                         if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
202                                 continue;
203                 }
204
205                 if ((rec->mask == NULL && mask != NULL) ||
206                     (rec->mask != NULL && mask == NULL)) continue;
207
208                 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
209                         continue;
210
211                 if ((channels == NULL && rec->channels == NULL))
212                         return rec; /* no channels - ok */
213
214                 if (channels != NULL && strcmp(*channels, "*") == 0)
215                         return rec; /* ignore channels */
216
217                 if (channels == NULL || rec->channels == NULL)
218                         continue; /* other doesn't have channels */
219
220                 if (strarray_length(channels) != strarray_length(rec->channels))
221                         continue; /* different amount of channels */
222
223                 /* check that channels match */
224                 for (chan = channels; *chan != NULL; chan++) {
225                         if (strarray_find(rec->channels, *chan) == -1)
226                                 break;
227                 }
228
229                 if (*chan == NULL)
230                         return rec; /* channels ok */
231         }
232
233         return NULL;
234 }
235
236 static void ignore_set_config(IGNORE_REC *rec)
237 {
238         CONFIG_NODE *node;
239         char *levelstr;
240
241         if (rec->level == 0)
242                 return;
243
244         node = iconfig_node_traverse("(ignores", TRUE);
245         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
246
247         if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
248         if (rec->level) {
249                 levelstr = bits2level(rec->level);
250                 iconfig_node_set_str(node, "level", levelstr);
251                 g_free(levelstr);
252         }
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);
261
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);
265         }
266 }
267
268 static int ignore_index(IGNORE_REC *find)
269 {
270         GSList *tmp;
271         int index;
272
273         index = 0;
274         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
275                 IGNORE_REC *rec = tmp->data;
276
277                 if (rec == find)
278                         return index;
279                 index++;
280         }
281
282         return -1;
283 }
284
285 static void ignore_remove_config(IGNORE_REC *rec)
286 {
287         CONFIG_NODE *node;
288
289         node = iconfig_node_traverse("ignores", FALSE);
290         if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
291 }
292
293 static void ignore_init_rec(IGNORE_REC *rec)
294 {
295 #ifdef HAVE_REGEX_H
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;
300 #endif
301 }
302
303 void ignore_add_rec(IGNORE_REC *rec)
304 {
305         ignore_init_rec(rec);
306
307         ignores = g_slist_append(ignores, rec);
308         ignore_set_config(rec);
309
310         signal_emit("ignore created", 1, rec);
311         nickmatch_rebuild(nickmatch);
312 }
313
314 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
315 {
316         ignores = g_slist_remove(ignores, rec);
317         if (send_signal)
318                 signal_emit("ignore destroyed", 1, rec);
319
320 #ifdef HAVE_REGEX_H
321         if (rec->regexp_compiled) regfree(&rec->preg);
322 #endif
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);
327         g_free(rec);
328
329         nickmatch_rebuild(nickmatch);
330 }
331
332 void ignore_update_rec(IGNORE_REC *rec)
333 {
334         if (rec->level == 0) {
335                 /* unignored everything */
336                 ignore_remove_config(rec);
337                 ignore_destroy(rec, TRUE);
338         } else {
339                 /* unignore just some levels.. */
340                 ignore_remove_config(rec);
341                 ignores = g_slist_remove(ignores, rec);
342
343                 ignores = g_slist_append(ignores, rec);
344                 ignore_set_config(rec);
345
346                 ignore_init_rec(rec);
347                 signal_emit("ignore changed", 1, rec);
348                 nickmatch_rebuild(nickmatch);
349         }
350 }
351
352 static int unignore_timeout(void)
353 {
354         GSList *tmp, *next;
355         time_t now;
356
357         now = time(NULL);
358         for (tmp = ignores; tmp != NULL; tmp = next) {
359                 IGNORE_REC *rec = tmp->data;
360
361                 next = tmp->next;
362                 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
363                         rec->level = 0;
364                         ignore_update_rec(rec);
365                 }
366         }
367
368         return TRUE;
369 }
370
371 static void read_ignores(void)
372 {
373         IGNORE_REC *rec;
374         CONFIG_NODE *node;
375         GSList *tmp;
376
377         while (ignores != NULL)
378                 ignore_destroy(ignores->data, FALSE);
379
380         node = iconfig_node_traverse("ignores", FALSE);
381         if (node == NULL) {
382                 nickmatch_rebuild(nickmatch);
383                 return;
384         }
385
386         tmp = config_node_first(node->value);
387         for (; tmp != NULL; tmp = config_node_next(tmp)) {
388                 node = tmp->data;
389
390                 if (node->type != NODE_TYPE_BLOCK)
391                         continue;
392
393                 rec = g_new0(IGNORE_REC, 1);
394                 ignores = g_slist_append(ignores, rec);
395
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));
405
406                 node = config_node_section(node, "channels", -1);
407                 if (node != NULL) rec->channels = config_node_get_list(node);
408
409                 ignore_init_rec(rec);
410         }
411
412         nickmatch_rebuild(nickmatch);
413 }
414
415 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
416                               NICK_REC *nick)
417 {
418         GSList *tmp, *matches;
419         char *nickmask;
420
421         if (nick->host == NULL)
422                 return; /* don't check until host is known */
423
424         matches = NULL;
425         nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
426         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
427                 IGNORE_REC *rec = tmp->data;
428
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);
433         }
434         g_free_not_null(nickmask);
435
436         if (matches == NULL)
437                 g_hash_table_remove(list, nick);
438         else
439                 g_hash_table_insert(list, nick, matches);
440 }
441
442 void ignore_init(void)
443 {
444         ignores = NULL;
445         nickmatch = nickmatch_init(ignore_nick_cache);
446         time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
447
448         read_ignores();
449         signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
450 }
451
452 void ignore_deinit(void)
453 {
454         g_source_remove(time_tag);
455         while (ignores != NULL)
456                 ignore_destroy(ignores->data, TRUE);
457         nickmatch_deinit(nickmatch);
458
459         signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);
460 }