imported.
[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 = (channel != NULL && server != NULL &&
165                    server->ischannel(channel)) ?
166                 channel_find(server, channel) : NULL;
167         if (chanrec != NULL && nick != NULL &&
168             (nickrec = nicklist_find(chanrec, nick)) != NULL) {
169                 /* nick found - check only ignores in nickmatch cache */
170                 if (nickrec->host == NULL)
171                         nicklist_set_host(chanrec, nickrec, host);
172
173                 list = nickmatch_find(nickmatch, nickrec);
174                 return ignore_check_without_mask(list, chanrec, level, text);
175         }
176
177         nickmask = g_strconcat(nick, "!", host, NULL);
178
179         best_mask = best_patt = -1; best_match = FALSE;
180         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
181                 rec = tmp->data;
182
183                 if (ignore_match_level(rec, level) &&
184                     ignore_match_server(rec, server) &&
185                     ignore_match_channel(rec, channel) &&
186                     ignore_match_nickmask(rec, nick, nickmask) &&
187                     ignore_match_pattern(rec, text)) {
188                         len = rec->mask == NULL ? 0 : strlen(rec->mask);
189                         if (len > best_mask) {
190                                 best_mask = len;
191                                 best_match = !rec->exception;
192                         } else if (len == best_mask && rec->pattern != NULL) {
193                                 len = strlen(rec->pattern);
194                                 if (len > best_patt) {
195                                         best_patt = len;
196                                         best_match = !rec->exception;
197                                 }
198                         }
199                 }
200         }
201         g_free(nickmask);
202
203         if (best_match || (level & MSGLEVEL_PUBLIC) == 0)
204                 return best_match;
205
206         return ignore_check_replies(chanrec, text);
207 }
208
209 IGNORE_REC *ignore_find(const char *servertag, const char *mask,
210                         char **channels)
211 {
212         GSList *tmp;
213         char **chan;
214         int ignore_servertag;
215
216         if (mask != NULL && (*mask == '\0' || strcmp(mask, "*") == 0))
217                 mask = NULL;
218
219         ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
220         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
221                 IGNORE_REC *rec = tmp->data;
222
223                 if (!ignore_servertag) {
224                         if ((servertag == NULL && rec->servertag != NULL) ||
225                             (servertag != NULL && rec->servertag == NULL))
226                                 continue;
227
228                         if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
229                                 continue;
230                 }
231
232                 if ((rec->mask == NULL && mask != NULL) ||
233                     (rec->mask != NULL && mask == NULL)) continue;
234
235                 if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
236                         continue;
237
238                 if ((channels == NULL && rec->channels == NULL))
239                         return rec; /* no channels - ok */
240
241                 if (channels != NULL && strcmp(*channels, "*") == 0)
242                         return rec; /* ignore channels */
243
244                 if (channels == NULL || rec->channels == NULL)
245                         continue; /* other doesn't have channels */
246
247                 if (strarray_length(channels) != strarray_length(rec->channels))
248                         continue; /* different amount of channels */
249
250                 /* check that channels match */
251                 for (chan = channels; *chan != NULL; chan++) {
252                         if (strarray_find(rec->channels, *chan) == -1)
253                                 break;
254                 }
255
256                 if (*chan == NULL)
257                         return rec; /* channels ok */
258         }
259
260         return NULL;
261 }
262
263 static void ignore_set_config(IGNORE_REC *rec)
264 {
265         CONFIG_NODE *node;
266         char *levelstr;
267
268         if (rec->level == 0 || rec->unignore_time > 0)
269                 return;
270
271         node = iconfig_node_traverse("(ignores", TRUE);
272         node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
273
274         if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
275         if (rec->level) {
276                 levelstr = bits2level(rec->level);
277                 iconfig_node_set_str(node, "level", levelstr);
278                 g_free(levelstr);
279         }
280         iconfig_node_set_str(node, "pattern", rec->pattern);
281         if (rec->exception) iconfig_node_set_bool(node, "exception", TRUE);
282         if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE);
283         if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
284         if (rec->replies) iconfig_node_set_bool(node, "replies", TRUE);
285
286         if (rec->channels != NULL && *rec->channels != NULL) {
287                 node = config_node_section(node, "channels", NODE_TYPE_LIST);
288                 iconfig_node_add_list(node, rec->channels);
289         }
290 }
291
292 static int ignore_index(IGNORE_REC *find)
293 {
294         GSList *tmp;
295         int index;
296
297         index = 0;
298         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
299                 IGNORE_REC *rec = tmp->data;
300
301                 if (rec->servertag != NULL)
302                         continue;
303
304                 if (rec == find)
305                         return index;
306                 index++;
307         }
308
309         return -1;
310 }
311
312 static void ignore_remove_config(IGNORE_REC *rec)
313 {
314         CONFIG_NODE *node;
315
316         node = iconfig_node_traverse("ignores", FALSE);
317         if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
318 }
319
320 void ignore_add_rec(IGNORE_REC *rec)
321 {
322 #ifdef HAVE_REGEX_H
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         ignores = g_slist_append(ignores, rec);
329         ignore_set_config(rec);
330
331         signal_emit("ignore created", 1, rec);
332 }
333
334 static void ignore_destroy(IGNORE_REC *rec, int send_signal)
335 {
336         ignores = g_slist_remove(ignores, rec);
337         if (send_signal)
338                 signal_emit("ignore destroyed", 1, rec);
339
340 #ifdef HAVE_REGEX_H
341         if (rec->regexp_compiled) regfree(&rec->preg);
342 #endif
343         if (rec->channels != NULL) g_strfreev(rec->channels);
344         g_free_not_null(rec->mask);
345         g_free_not_null(rec->servertag);
346         g_free_not_null(rec->pattern);
347         g_free(rec);
348
349         nickmatch_rebuild(nickmatch);
350 }
351
352 void ignore_update_rec(IGNORE_REC *rec)
353 {
354         if (rec->level == 0) {
355                 /* unignored everything */
356                 ignore_remove_config(rec);
357                 ignore_destroy(rec, TRUE);
358         } else {
359                 /* unignore just some levels.. */
360                 ignore_remove_config(rec);
361                 ignores = g_slist_remove(ignores, rec);
362
363                 ignores = g_slist_append(ignores, rec);
364                 ignore_set_config(rec);
365
366                 signal_emit("ignore changed", 1, rec);
367                 nickmatch_rebuild(nickmatch);
368         }
369 }
370
371 static int unignore_timeout(void)
372 {
373         GSList *tmp, *next;
374         time_t now;
375
376         now = time(NULL);
377         for (tmp = ignores; tmp != NULL; tmp = next) {
378                 IGNORE_REC *rec = tmp->data;
379
380                 next = tmp->next;
381                 if (rec->unignore_time > 0 && now >= rec->unignore_time) {
382                         rec->level = 0;
383                         ignore_update_rec(rec);
384                 }
385         }
386
387         return TRUE;
388 }
389
390 static void read_ignores(void)
391 {
392         IGNORE_REC *rec;
393         CONFIG_NODE *node;
394         GSList *tmp;
395
396         while (ignores != NULL)
397                 ignore_destroy(ignores->data, FALSE);
398
399         node = iconfig_node_traverse("ignores", FALSE);
400         if (node == NULL) {
401                 nickmatch_rebuild(nickmatch);
402                 return;
403         }
404
405         for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
406                 node = tmp->data;
407
408                 if (node->type != NODE_TYPE_BLOCK)
409                         continue;
410
411                 rec = g_new0(IGNORE_REC, 1);
412                 ignores = g_slist_append(ignores, rec);
413
414                 rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
415                 if (rec->mask != NULL && strcmp(rec->mask, "*") == 0) {
416                         /* FIXME: remove after .98 */
417                         g_free(rec->mask);
418                         rec->mask = NULL;
419                 }
420                 rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
421                 rec->level = level2bits(config_node_get_str(node, "level", ""));
422                 rec->exception = config_node_get_bool(node, "exception", FALSE);
423                 if (*config_node_get_str(node, "except_level", "") != '\0') {
424                         /* FIXME: remove after .98 */
425                         rec->level = level2bits(config_node_get_str(node, "except_level", ""));
426                         rec->exception = TRUE;
427                 }
428                 rec->regexp = config_node_get_bool(node, "regexp", FALSE);
429                 rec->fullword = config_node_get_bool(node, "fullword", FALSE);
430                 rec->replies = config_node_get_bool(node, "replies", FALSE);
431
432                 node = config_node_section(node, "channels", -1);
433                 if (node != NULL) rec->channels = config_node_get_list(node);
434         }
435
436         nickmatch_rebuild(nickmatch);
437 }
438
439 static void ignore_nick_cache(GHashTable *list, CHANNEL_REC *channel,
440                               NICK_REC *nick)
441 {
442         GSList *tmp, *matches;
443         char *nickmask;
444
445         if (nick->host == NULL)
446                 return; /* don't check until host is known */
447
448         matches = NULL;
449         nickmask = g_strconcat(nick->nick, "!", nick->host, NULL);
450         for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
451                 IGNORE_REC *rec = tmp->data;
452
453                 if (ignore_match_nickmask(rec, nick->nick, nickmask) &&
454                     ignore_match_server(rec, channel->server) &&
455                     ignore_match_channel(rec, channel->name))
456                         matches = g_slist_append(matches, rec);
457         }
458         g_free_not_null(nickmask);
459
460         if (matches == NULL)
461                 g_hash_table_remove(list, nick);
462         else
463                 g_hash_table_insert(list, nick, matches);
464 }
465
466 void ignore_init(void)
467 {
468         ignores = NULL;
469         nickmatch = nickmatch_init(ignore_nick_cache);
470         time_tag = g_timeout_add(1000, (GSourceFunc) unignore_timeout, NULL);
471
472         read_ignores();
473         signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
474 }
475
476 void ignore_deinit(void)
477 {
478         g_source_remove(time_tag);
479         while (ignores != NULL)
480                 ignore_destroy(ignores->data, TRUE);
481         nickmatch_deinit(nickmatch);
482
483         signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);
484 }