forked from atheme/atheme-contrib-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs_badwords.c
447 lines (362 loc) · 10.9 KB
/
cs_badwords.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
* Rights to this code are as documented in doc/LICENSE.
*
*/
#include "atheme-compat.h"
DECLARE_MODULE_V1
(
"contrib/cs_badwords", false, _modinit, _moddeinit,
PACKAGE_STRING,
"Atheme Development Group <http://www.atheme.net>"
);
static void on_channel_message(hook_cmessage_data_t *data);
static void cs_cmd_badwords(sourceinfo_t *si, int parc, char *parv[]);
static void cs_set_cmd_blockbadwords(sourceinfo_t *si, int parc, char *parv[]);
static void write_badword_db(database_handle_t *db);
static void db_h_bw(database_handle_t *db, const char *type);
command_t cs_badwords = { "BADWORDS", N_("Manage the list of channel bad words."), AC_AUTHENTICATED, 4, cs_cmd_badwords, { .path = "contrib/badwords" } };
command_t cs_set_blockbadwords = { "BLOCKBADWORDS", N_("Set whether users can say badwords in channel or not."), AC_NONE, 2, cs_set_cmd_blockbadwords, { .path = "contrib/set_blockbadwords" } };
struct badword_ {
char *badword;
time_t add_ts;
char *creator;
char *channel;
char *action;
mowgli_node_t node;
};
typedef struct badword_ badword_t;
mowgli_patricia_t **cs_set_cmdtree;
void _modinit(module_t *m)
{
MODULE_TRY_REQUEST_SYMBOL(m, cs_set_cmdtree, "chanserv/set_core", "cs_set_cmdtree");
if (!module_find_published("backend/opensex"))
{
slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
m->mflags = MODTYPE_FAIL;
return;
}
hook_add_event("channel_message");
hook_add_channel_message(on_channel_message);
hook_add_db_write(write_badword_db);
db_register_type_handler("BW", db_h_bw);
service_named_bind_command("chanserv", &cs_badwords);
command_add(&cs_set_blockbadwords, *cs_set_cmdtree);
}
void _moddeinit(module_unload_intent_t intent)
{
hook_del_channel_message(on_channel_message);
hook_del_db_write(write_badword_db);
db_unregister_type_handler("BW");
service_named_unbind_command("chanserv", &cs_badwords);
command_delete(&cs_set_blockbadwords, *cs_set_cmdtree);
}
static inline mowgli_list_t *badwords_list_of(mychan_t *mc)
{
mowgli_list_t *l;
return_val_if_fail(mc != NULL, NULL);
l = privatedata_get(mc, "badword:list");
if (l != NULL)
return l;
l = mowgli_list_create();
privatedata_set(mc, "badword:list", l);
return l;
}
static void write_badword_db(database_handle_t *db)
{
mowgli_node_t *n;
mychan_t *mc;
mowgli_patricia_iteration_state_t state;
mowgli_list_t *l;
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
{
l = badwords_list_of(mc);
if (l == NULL)
return;
MOWGLI_ITER_FOREACH(n, l->head)
{
badword_t *bw = n->data;
db_start_row(db, "BW");
db_write_word(db, bw->badword);
db_write_time(db, bw->add_ts);
db_write_word(db, bw->creator);
db_write_word(db, bw->channel);
db_write_word(db, bw->action);
db_commit_row(db);
}
}
}
static void db_h_bw(database_handle_t *db, const char *type)
{
mychan_t *mc;
mowgli_patricia_iteration_state_t state;
mowgli_list_t *l;
const char *badword = db_sread_word(db);
time_t add_ts = db_sread_time(db);
const char *creator = db_sread_word(db);
const char *channel = db_sread_word(db);
const char *action = db_sread_word(db);
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
{
if (irccasecmp(mc->name, channel))
continue;
l = badwords_list_of(mc);
badword_t *bw = smalloc(sizeof(badword_t));
bw->badword = sstrdup(badword);
bw->add_ts = add_ts;
bw->creator = sstrdup(creator);
bw->channel = sstrdup(channel);
bw->action = sstrdup(action);
mowgli_node_add(bw, &bw->node, l);
}
}
static void on_channel_message(hook_cmessage_data_t *data)
{
badword_t *bw;
mowgli_node_t *n;
mowgli_list_t *l;
mychan_t *mc = mychan_from(data->c);
if (mc == NULL)
return;
if (metadata_find(mc, "blockbadwords") == NULL)
return;
l = badwords_list_of(mc);
if (MOWGLI_LIST_LENGTH(l) == 0)
return;
char *kickstring = "Foul language is prohibited here.";
if (data != NULL && data->msg != NULL)
{
MOWGLI_ITER_FOREACH(n, l->head)
{
bw = n->data;
if (!match(bw->badword, data->msg))
{
if (!strcasecmp("KICKBAN", bw->action))
{
char hostbuf[BUFSIZE];
hostbuf[0] = '\0';
mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'b', hostbuf);
chanban_add(data->c, hostbuf, 'b');
kick(chansvs.me->me, data->c, data->u, kickstring);
return;
}
else if (!strcasecmp("KICK", bw->action))
{
kick(chansvs.me->me, data->c, data->u, kickstring);
return;
}
else if (!strcasecmp("QUIET", bw->action))
{
char hostbuf[BUFSIZE];
hostbuf[0] = '\0';
mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'q', hostbuf);
chanban_add(data->c, hostbuf, 'q');
return;
}
else if (!strcasecmp("BAN", bw->action))
{
char hostbuf[BUFSIZE];
hostbuf[0] = '\0';
mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'b', hostbuf);
chanban_add(data->c, hostbuf, 'b');
return;
}
}
}
}
}
/* SET BADWORD */
static void cs_cmd_badwords(sourceinfo_t *si, int parc, char *parv[])
{
char *channel = parv[0];
char *command = parv[1];
char *word = parv[2];
char *action = parv[3];
mychan_t *mc;
mowgli_node_t *n, *tn;
badword_t *bw;
mowgli_list_t *l;
if (!channel || !command)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET BADWORDS");
command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD|DEL|LIST [badword] [action]"));
return;
}
if (!(mc = mychan_find(channel)))
{
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), channel);
return;
}
l = badwords_list_of(mc);
if (!strcasecmp("ADD", command))
{
if (!word || !action)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "BADWORDS");
command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD <badword> <action>"));
return;
}
if (!chanacs_source_has_flag(mc, si, CA_SET))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
return;
}
if(!strcasecmp("KICK", action) || !strcasecmp("KICKBAN", action) || !strcasecmp("BAN", action) || (!strcasecmp("QUIET", action) && ircd != NULL && strchr(ircd->ban_like_modes, 'q')))
{
if (l != NULL)
{
MOWGLI_ITER_FOREACH(n, l->head)
{
bw = n->data;
if (!irccasecmp(bw->badword, word))
{
command_success_nodata(si, _("\2%s\2 has already been entered into the bad word list."), word);
return;
}
}
}
bw = smalloc(sizeof(badword_t));
bw->add_ts = CURRTIME;;
bw->creator = sstrdup(get_source_name(si));
bw->channel = sstrdup(mc->name);
bw->badword = sstrdup(word);
bw->action = sstrdup(action);
mowgli_node_add(bw, &bw->node, l);
command_success_nodata(si, _("You have added \2%s\2 as a bad word."), word);
logcommand(si, CMDLOG_SET, "BADWORDS:ADD: \2%s\2 \2%s\2 \2%s\2", channel, word, action);
}
else
{
command_fail(si, fault_badparams, _("Invalid action given."));
return;
}
}
else if (!strcasecmp("DEL", command))
{
if (!word)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "BADWORDS");
command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> DEL <badword>"));
return;
}
if (!chanacs_source_has_flag(mc, si, CA_SET))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
return;
}
if (l == NULL)
{
command_fail(si, fault_nosuch_target, _("There are no badwords set in this channel."));
return;
}
MOWGLI_ITER_FOREACH_SAFE(n, tn, l->head)
{
bw = n->data;
if (!irccasecmp(bw->badword, word))
{
logcommand(si, CMDLOG_SET, "BADWORDS:DEL: \2%s\2 \2%s\2", mc->name, bw->badword);
command_success_nodata(si, _("Bad word \2%s\2 has been deleted."), bw->badword);
mowgli_node_delete(&bw->node, l);
free(bw->creator);
free(bw->channel);
free(bw->badword);
free(bw->action);
free(bw);
return;
}
}
command_success_nodata(si, _("Word \2%s\2 not found in bad word database."), word);
}
else if (!strcasecmp("LIST", command))
{
char buf[BUFSIZE];
struct tm tm;
if (!chanacs_source_has_flag(mc, si, CA_ACLVIEW))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
return;
}
if (l == NULL)
{
command_fail(si, fault_nosuch_target, _("There are no badwords set in this channel."));
return;
}
MOWGLI_ITER_FOREACH(n, l->head)
{
bw = n->data;
tm = *localtime(&bw->add_ts);
strftime(buf, BUFSIZE, TIME_FORMAT, &tm);
command_success_nodata(si, "Word: \2%s\2 Action: \2%s\2 (%s - %s)",
bw->badword, bw->action, bw->creator, buf);
}
command_success_nodata(si, "End of list.");
logcommand(si, CMDLOG_GET, "BADWORDS:LIST");
}
else
{
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BADWORDS");
command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD|DEL|LIST [badword] [action]"));
return;
}
}
static void cs_set_cmd_blockbadwords(sourceinfo_t *si, int parc, char *parv[])
{
mychan_t *mc;
if (!(mc = mychan_find(parv[0])))
{
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), parv[0]);
return;
}
if (!parv[1])
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET BLOCKBADWORDS");
return;
}
if (!chanacs_source_has_flag(mc, si, CA_SET))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this command."));
return;
}
if (!strcasecmp("ON", parv[1]))
{
metadata_t *md = metadata_find(mc, "blockbadwords");
if (md)
{
command_fail(si, fault_nochange, _("The \2%s\2 flag is already set for channel \2%s\2."), "BLOCKBADWORDS", mc->name);
return;
}
metadata_add(mc, "blockbadwords", "on");
logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDS:ON: \2%s\2", mc->name);
command_success_nodata(si, _("The \2%s\2 flag has been set for channel \2%s\2."), "BLOCKBADWORDS", mc->name);
return;
}
else if (!strcasecmp("OFF", parv[1]))
{
metadata_t *md = metadata_find(mc, "blockbadwords");
if (!md)
{
command_fail(si, fault_nochange, _("The \2%s\2 flag is not set for channel \2%s\2."), "BLOCKBADWORDS", mc->name);
return;
}
metadata_delete(mc, "blockbadwords");
logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDS:OFF: \2%s\2", mc->name);
command_success_nodata(si, _("The \2%s\2 flag has been removed for channel \2%s\2."), "BLOCKBADWORDS", mc->name);
return;
}
else
{
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "BLOCKBADWORDS");
return;
}
}
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
* vim:ts=8
* vim:sw=8
* vim:noexpandtab
*/