New silcconfig library and server parser. Merged silc-newconfig-final.patch.
[crypto.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 || rec->unignore_time > 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
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);
288         }
289 }
290
291 static int ignore_index(IGNORE_REC *find)
292 {
293         GSList *tmp;
294         int index;
295
296         index = 0;
297         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
298                 IGNORE_REC *rec = tmp->data;
299
300                 if (rec->servertag != NULL)
301                         continue;
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         rec->regexp_compiled = !rec->regexp || rec->pattern == NULL ? FALSE :
323                 regcomp(&rec->preg, rec->pattern,
324                         REG_EXTENDED|REG_ICASE|REG_NOSUB) == 0;
325 #endif
326 }
327
328 void ignore_add_rec(IGNORE_REC *rec)
329 {
330         ignore_init_rec(rec);
331
332         ignores = g_slist_append(ignores, rec);
333         ignore_set_config(rec);
334
335         signal_emit("ignore created", 1, rec);
336         nickmatch_rebuild(nickmatch);
337 }
338
339 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
340 {
341         ignores = g_slist_remove(ignores, rec);
342         if (send_signal)
343                 signal_emit("ignore destroyed", 1, rec);
344
345 #ifdef HAVE_REGEX_H
346         if (rec->regexp_compiled) regfree(&rec->preg);
347 #endif
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);
352         g_free(rec);
353
354         nickmatch_rebuild(nickmatch);
355 }
356
357 void ignore_update_rec(IGNORE_REC *rec)
358 {
359         if (rec->level == 0) {
360                 /* unignored everything */
361                 ignore_remove_config(rec);
362                 ignore_destroy(rec, TRUE);
363         } else {
364                 /* unignore just some levels.. */
365                 ignore_remove_config(rec);
366                 ignores = g_slist_remove(ignores, rec);
367
368                 ignores = g_slist_append(ignores, rec);
369                 ignore_set_config(rec);
370
371                 signal_emit("ignore changed", 1, rec);
372                 nickmatch_rebuild(nickmatch);
373         }
374 }
375
376 static int unignore_timeout(void)
377 {
378         GSList *tmp, *next;
379         time_t now;
380
381         now = time(NULL);
382         for (tmp = ignores; tmp != NULL; tmp = next) {
383                 IGNORE_REC *rec = tmp->data;
384
385                 next = tmp->next;
386                 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
387                         rec->level = 0;
388                         ignore_update_rec(rec);
389                 }
390         }
391
392         return TRUE;
393 }
394
395 static void read_ignores(void)
396 {
397         IGNORE_REC *rec;
398         CONFIG_NODE *node;
399         GSList *tmp;
400
401         while (ignores != NULL)
402                 ignore_destroy(ignores->data, FALSE);
403
404         node = iconfig_node_traverse("ignores", FALSE);
405         if (node == NULL) {
406                 nickmatch_rebuild(nickmatch);
407                 return;
408         }
409
410         tmp = config_node_first(node->value);
411         for (; tmp != NULL; tmp = config_node_next(tmp)) {
412                 node = tmp->data;
413
414                 if (node->type != NODE_TYPE_BLOCK)
415                         continue;
416
417                 rec = g_new0(IGNORE_REC, 1);
418                 ignores = g_slist_append(ignores, rec);
419
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);
427
428                 node = config_node_section(node, "channels", -1);
429                 if (node != NULL) rec->channels = config_node_get_list(node);
430
431                 ignore_init_rec(rec);
432         }
433
434         nickmatch_rebuild(nickmatch);
435 }
436
437 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
438                               NICK_REC *nick)
439 {
440         GSList *tmp, *matches;
441         char *nickmask;
442
443         if (nick->host == NULL)
444                 return; /* don't check until host is known */
445
446         matches = NULL;
447         nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
448         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
449                 IGNORE_REC *rec = tmp->data;
450
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);
455         }
456         g_free_not_null(nickmask);
457
458         if (matches == NULL)
459                 g_hash_table_remove(list, nick);
460         else
461                 g_hash_table_insert(list, nick, matches);
462 }
463
464 void ignore_init(void)
465 {
466         ignores = NULL;
467         nickmatch = nickmatch_init(ignore_nick_cache);
468         time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
469
470         read_ignores();
471         signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
472 }
473
474 void ignore_deinit(void)
475 {
476         g_source_remove(time_tag);
477         while (ignores != NULL)
478                 ignore_destroy(ignores->data, TRUE);
479         nickmatch_deinit(nickmatch);
480
481         signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);
482 }