1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/*
ignore.c : irssi
Copyright (C) 1999-2000 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "module.h"
#include "signals.h"
#include "misc.h"
#include "levels.h"
#include "lib-config/iconfig.h"
#include "settings.h"
#include "irc.h"
#include "irc-masks.h"
#include "irc-servers.h"
#include "irc-channels.h"
#include "irc-nicklist.h"
#include "ignore.h"
GSList *ignores;
/* check if `text' contains ignored nick at the start of the line. */
static int ignore_check_replies(IGNORE_REC *rec, IRC_SERVER_REC *server,
const char *channel, const char *text)
{
IRC_CHANNEL_REC *chanrec;
GSList *nicks, *tmp;
g_return_val_if_fail(rec != NULL, FALSE);
g_return_val_if_fail(server != NULL, FALSE);
g_return_val_if_fail(channel != NULL, FALSE);
g_return_val_if_fail(text != NULL, FALSE);
chanrec = irc_channel_find(server, channel);
if (chanrec == NULL) return FALSE;
nicks = nicklist_find_multiple(CHANNEL(chanrec), rec->mask);
if (nicks == NULL) return FALSE;
for (tmp = nicks; tmp != NULL; tmp = tmp->next) {
NICK_REC *nick = tmp->data;
if (irc_nick_match(nick->nick, text))
return TRUE;
}
g_slist_free(nicks);
return FALSE;
}
int ignore_check(IRC_SERVER_REC *server, const char *nick, const char *host,
const char *channel, const char *text, int level)
{
GSList *tmp;
int ok, mask_len, patt_len;
int best_mask, best_patt, best_ignore;
g_return_val_if_fail(server != NULL, 0);
best_mask = 0; best_patt = 0; best_ignore = FALSE;
for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
IGNORE_REC *rec = tmp->data;
if ((level & (rec->level|rec->except_level)) == 0)
continue;
/* server */
if (rec->servertag != NULL && g_strcasecmp(server->tag, rec->servertag) != 0)
continue;
/* channel list */
if (rec->channels != NULL) {
if (channel == NULL || !ischannel(*channel))
continue;
if (strarray_find(rec->channels, channel) == -1)
continue;
}
/* nick mask */
mask_len = 0;
if (rec->mask != NULL) {
if (nick == NULL)
continue;
mask_len = strlen(rec->mask);
if (mask_len <= best_mask) continue;
ok = ((host == NULL || *host == '\0')) ?
match_wildcards(rec->mask, nick) :
mask_match_address(SERVER(server), rec->mask, nick, host);
if (!ok) {
/* nick didn't match, but maybe this is a reply to nick? */
if (!rec->replies || channel == NULL || text == NULL ||
!ignore_check_replies(rec, server, channel, text))
continue;
}
}
/* pattern */
patt_len = 0;
if (rec->pattern != NULL) {
if (!mask_len && !best_mask) {
patt_len = strlen(rec->pattern);
if (patt_len <= best_patt) continue;
}
ok = rec->regexp ? regexp_match(text, rec->pattern) :
rec->fullword ? stristr_full(text, rec->pattern) != NULL :
stristr(text, rec->pattern) != NULL;
if (!ok) continue;
}
if (mask_len || best_mask)
best_mask = mask_len;
else if (patt_len)
best_patt = patt_len;
best_ignore = (rec->level & level) != 0;
}
return best_ignore;
}
IGNORE_REC *ignore_find(const char *servertag, const char *mask, char **channels)
{
GSList *tmp;
char **chan;
int ignore_servertag;
if (mask != NULL && *mask == '\0') mask = NULL;
ignore_servertag = servertag != NULL && strcmp(servertag, "*") == 0;
for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
IGNORE_REC *rec = tmp->data;
if (!ignore_servertag) {
if ((servertag == NULL && rec->servertag != NULL) ||
(servertag != NULL && rec->servertag == NULL))
continue;
if (servertag != NULL && g_strcasecmp(servertag, rec->servertag) != 0)
continue;
}
if ((rec->mask == NULL && mask != NULL) ||
(rec->mask != NULL && mask == NULL)) continue;
if (rec->mask != NULL && g_strcasecmp(rec->mask, mask) != 0)
continue;
if ((channels == NULL && rec->channels == NULL))
return rec; /* no channels - ok */
if (channels != NULL && strcmp(*channels, "*") == 0)
return rec; /* ignore channels */
if (channels == NULL || rec->channels == NULL)
continue; /* other doesn't have channels */
if (strarray_length(channels) != strarray_length(rec->channels))
continue; /* different amount of channels */
/* check that channels match */
for (chan = channels; *chan != NULL; chan++) {
if (strarray_find(rec->channels, *chan) == -1)
break;
}
if (*chan == NULL)
return rec; /* channels ok */
}
return NULL;
}
static void ignore_set_config(IGNORE_REC *rec)
{
CONFIG_NODE *node;
char *levelstr;
if (rec->level == 0 && rec->except_level == 0)
return;
if (rec->time > 0)
return;
node = iconfig_node_traverse("(ignores", TRUE);
node = config_node_section(node, NULL, NODE_TYPE_BLOCK);
if (rec->mask != NULL) iconfig_node_set_str(node, "mask", rec->mask);
if (rec->level) {
levelstr = bits2level(rec->level);
iconfig_node_set_str(node, "level", levelstr);
g_free(levelstr);
}
if (rec->except_level) {
levelstr = bits2level(rec->except_level);
iconfig_node_set_str(node, "except_level", levelstr);
g_free(levelstr);
}
iconfig_node_set_str(node, "pattern", rec->pattern);
if (rec->regexp) config_node_set_bool(node, "regexp", TRUE);
if (rec->fullword) config_node_set_bool(node, "fullword", TRUE);
if (rec->replies) config_node_set_bool(node, "replies", TRUE);
if (rec->channels != NULL && *rec->channels != NULL) {
node = config_node_section(node, "channels", NODE_TYPE_LIST);
config_node_add_list(node, rec->channels);
}
}
static int ignore_index(IGNORE_REC *find)
{
GSList *tmp;
int index;
index = 0;
for (tmp = ignores; tmp != NULL; tmp = tmp->next) {
IGNORE_REC *rec = tmp->data;
if (rec->servertag != NULL)
continue;
if (rec == find)
return index;
index++;
}
return -1;
}
static void ignore_remove_config(IGNORE_REC *rec)
{
CONFIG_NODE *node;
node = iconfig_node_traverse("ignores", FALSE);
if (node != NULL) iconfig_node_list_remove(node, ignore_index(rec));
}
void ignore_add_rec(IGNORE_REC *rec)
{
ignores = g_slist_append(ignores, rec);
ignore_set_config(rec);
signal_emit("ignore created", 1, rec);
}
static void ignore_destroy(IGNORE_REC *rec)
{
ignores = g_slist_remove(ignores, rec);
signal_emit("ignore destroyed", 1, rec);
if (rec->time_tag > 0) g_source_remove(rec->time_tag);
if (rec->channels != NULL) g_strfreev(rec->channels);
g_free_not_null(rec->mask);
g_free_not_null(rec->servertag);
g_free_not_null(rec->pattern);
g_free(rec);
}
void ignore_update_rec(IGNORE_REC *rec)
{
if (rec->level == 0 && rec->except_level == 0) {
/* unignored everything */
ignore_remove_config(rec);
ignore_destroy(rec);
} else {
/* unignore just some levels.. */
ignore_remove_config(rec);
ignores = g_slist_remove(ignores, rec);
ignores = g_slist_append(ignores, rec);
ignore_set_config(rec);
signal_emit("ignore changed", 1, rec);
}
}
static void read_ignores(void)
{
IGNORE_REC *rec;
CONFIG_NODE *node;
GSList *tmp;
while (ignores != NULL)
ignore_destroy(ignores->data);
node = iconfig_node_traverse("ignores", FALSE);
if (node == NULL) return;
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
node = tmp->data;
if (node->type != NODE_TYPE_BLOCK)
continue;
rec = g_new0(IGNORE_REC, 1);
ignores = g_slist_append(ignores, rec);
rec->mask = g_strdup(config_node_get_str(node, "mask", NULL));
rec->pattern = g_strdup(config_node_get_str(node, "pattern", NULL));
rec->level = level2bits(config_node_get_str(node, "level", ""));
rec->except_level = level2bits(config_node_get_str(node, "except_level", ""));
rec->regexp = config_node_get_bool(node, "regexp", FALSE);
rec->fullword = config_node_get_bool(node, "fullword", FALSE);
rec->replies = config_node_get_bool(node, "replies", FALSE);
node = config_node_section(node, "channels", -1);
if (node != NULL) rec->channels = config_node_get_list(node);
}
}
void ignore_init(void)
{
ignores = NULL;
read_ignores();
signal_add("setup reread", (SIGNAL_FUNC) read_ignores);
}
void ignore_deinit(void)
{
while (ignores != NULL)
ignore_destroy(ignores->data);
signal_remove("setup reread", (SIGNAL_FUNC) read_ignores);
}
|