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
|
/*
* Copyright (c) 2003-2009 by FlashCode <flashcode@flashtux.org>
* See README for License detail, AUTHORS for developers list.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/* irc-ignore.c: manages ignore list (nicks/hosts) on IRC servers/channels */
#include <stdlib.h>
#include <string.h>
#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-ignore.h"
#include "irc-channel.h"
#include "irc-server.h"
struct t_irc_ignore *irc_ignore_list = NULL; /* list of ignore */
struct t_irc_ignore *last_irc_ignore = NULL; /* last ignore in list */
/*
* irc_ignore_valid: check if a ignore pointer exists
* return 1 if ignore exists
* 0 if ignore is not found
*/
int
irc_ignore_valid (struct t_irc_ignore *ignore)
{
struct t_irc_ignore *ptr_ignore;
if (!ignore)
return 0;
for (ptr_ignore = irc_ignore_list; ptr_ignore;
ptr_ignore = ptr_ignore->next_ignore)
{
if (ptr_ignore == ignore)
return 1;
}
/* ignore not found */
return 0;
}
/*
* irc_ignore_search: search a ignore
*/
struct t_irc_ignore *
irc_ignore_search (const char *mask, const char *server, const char *channel)
{
struct t_irc_ignore *ptr_ignore;
char any[2] = "*";
if (!server)
server = any;
if (!channel)
channel = any;
for (ptr_ignore = irc_ignore_list; ptr_ignore;
ptr_ignore = ptr_ignore->next_ignore)
{
if ((strcmp (ptr_ignore->mask, mask) == 0)
&& (weechat_strcasecmp (ptr_ignore->server, server) == 0)
&& (weechat_strcasecmp (ptr_ignore->channel, channel) == 0))
{
return ptr_ignore;
}
}
/* ignore not found */
return NULL;
}
/*
* irc_ignore_search_by_number: search a ignore by number (first is #1)
*/
struct t_irc_ignore *
irc_ignore_search_by_number (int number)
{
struct t_irc_ignore *ptr_ignore;
for (ptr_ignore = irc_ignore_list; ptr_ignore;
ptr_ignore = ptr_ignore->next_ignore)
{
if (ptr_ignore->number == number)
return ptr_ignore;
}
/* ignore not found */
return NULL;
}
/*
* irc_ignore_new: add new ignore
*/
struct t_irc_ignore *
irc_ignore_new (const char *mask, const char *server, const char *channel)
{
struct t_irc_ignore *new_ignore;
regex_t *regex;
char *complete_mask;
if (!mask || !mask[0])
return NULL;
complete_mask = malloc (1 + strlen (mask) + 1 + 1);
if (!complete_mask)
return NULL;
if (mask[0] == '^')
strcpy (complete_mask, mask);
else
{
strcpy (complete_mask, "^");
strcat (complete_mask, mask);
}
if (complete_mask[strlen (complete_mask) - 1] != '$')
strcat (complete_mask, "$");
regex = malloc (sizeof (*regex));
if (!regex)
{
free (complete_mask);
return NULL;
}
if (regcomp (regex, complete_mask, REG_NOSUB | REG_ICASE) != 0)
{
free (regex);
free (complete_mask);
return NULL;
}
new_ignore = malloc (sizeof (*new_ignore));
if (new_ignore)
{
new_ignore->number = (last_irc_ignore) ? last_irc_ignore->number + 1 : 1;
new_ignore->mask = strdup (complete_mask);
new_ignore->regex_mask = regex;
new_ignore->server = (server) ? strdup (server) : strdup ("*");
new_ignore->channel = (channel) ? strdup (channel) : strdup ("*");
/* add ignore to ignore list */
new_ignore->prev_ignore = last_irc_ignore;
if (irc_ignore_list)
last_irc_ignore->next_ignore = new_ignore;
else
irc_ignore_list = new_ignore;
last_irc_ignore = new_ignore;
new_ignore->next_ignore = NULL;
}
free (complete_mask);
return new_ignore;
}
/*
* irc_ignore_check: check if a message (from an IRC server) should be ignored
* or not
* return: 1 if message will be ignored
* 0 if message will be displayed (NOT ignored)
*/
int
irc_ignore_check (struct t_irc_server *server, struct t_irc_channel *channel,
const char *nick, const char *host)
{
struct t_irc_ignore *ptr_ignore;
int server_match, channel_match, regex_match;
if (!server)
return 0;
for (ptr_ignore = irc_ignore_list; ptr_ignore;
ptr_ignore = ptr_ignore->next_ignore)
{
server_match = 0;
channel_match = 0;
regex_match = 0;
if (!server || (strcmp (ptr_ignore->server, "*") == 0))
server_match = 1;
else
server_match = (weechat_strcasecmp (ptr_ignore->server,
server->name) == 0);
if (!channel || (strcmp (ptr_ignore->channel, "*") == 0))
channel_match = 1;
else
{
channel_match = (weechat_strcasecmp (ptr_ignore->channel,
channel->name) == 0);
}
if (server_match && channel_match)
{
if (nick && (regexec (ptr_ignore->regex_mask, nick, 0, NULL, 0) == 0))
return 1;
if (host && (regexec (ptr_ignore->regex_mask, host, 0, NULL, 0) == 0))
return 1;
}
}
return 0;
}
/*
* irc_ignore_free: remove a ignore
*/
void
irc_ignore_free (struct t_irc_ignore *ignore)
{
struct t_irc_ignore *ptr_ignore;
weechat_hook_signal_send ("irc_ignore_removing",
WEECHAT_HOOK_SIGNAL_POINTER, ignore);
/* decrement number for all ignore after this one */
for (ptr_ignore = ignore->next_ignore; ptr_ignore;
ptr_ignore = ptr_ignore->next_ignore)
{
ptr_ignore->number--;
}
/* free data */
if (ignore->mask)
free (ignore->mask);
if (ignore->regex_mask)
{
regfree (ignore->regex_mask);
free (ignore->regex_mask);
}
if (ignore->server)
free (ignore->server);
if (ignore->channel)
free (ignore->channel);
/* remove filter from filters list */
if (ignore->prev_ignore)
(ignore->prev_ignore)->next_ignore = ignore->next_ignore;
if (ignore->next_ignore)
(ignore->next_ignore)->prev_ignore = ignore->prev_ignore;
if (irc_ignore_list == ignore)
irc_ignore_list = ignore->next_ignore;
if (last_irc_ignore == ignore)
last_irc_ignore = ignore->prev_ignore;
free (ignore);
weechat_hook_signal_send ("irc_ignore_removed",
WEECHAT_HOOK_SIGNAL_STRING, NULL);
}
/*
* irc_ignore_free_all: remove all ignore
*/
void
irc_ignore_free_all ()
{
while (irc_ignore_list)
{
irc_ignore_free (irc_ignore_list);
}
}
/*
* irc_ignore_add_to_infolist: add a ignore in an infolist
* return 1 if ok, 0 if error
*/
int
irc_ignore_add_to_infolist (struct t_infolist *infolist,
struct t_irc_ignore *ignore)
{
struct t_infolist_item *ptr_item;
if (!infolist || !ignore)
return 0;
ptr_item = weechat_infolist_new_item (infolist);
if (!ptr_item)
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "mask", ignore->mask))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "server", ignore->server))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "channel", ignore->channel))
return 0;
return 1;
}
|