Debian Codegraph @

modules/contrib/src/cs_badwords.c

raw
1 /*
2 * Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
3 * Rights to this code are as documented in doc/LICENSE.
4 */
5
6 #include "atheme-compat.h"
7
8 struct badword_ {
9 char *badword;
10 time_t add_ts;
11 char *creator;
12 char *channel;
13 char *action;
14 mowgli_node_t node;
15 };
16
17 typedef struct badword_ badword_t;
18
19 static mowgli_patricia_t **cs_set_cmdtree = NULL;
20
21 static inline mowgli_list_t *
22 badwords_list_of(mychan_t *mc)
23 {
24 mowgli_list_t *l;
25
26 return_val_if_fail(mc != NULL, NULL);
27
28 l = privatedata_get(mc, "badword:list");
29 if (l != NULL)
30 return l;
31
32 l = mowgli_list_create();
33 privatedata_set(mc, "badword:list", l);
34
35 return l;
36 }
37
38 static void
39 write_badword_db(database_handle_t *db)
40 {
41 mowgli_node_t *n;
42 mychan_t *mc;
43 mowgli_patricia_iteration_state_t state;
44 mowgli_list_t *l;
45
46 MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
47 {
48 l = badwords_list_of(mc);
49
50 if (l == NULL)
51 return;
52
53 MOWGLI_ITER_FOREACH(n, l->head)
54 {
55 badword_t *bw = n->data;
56
57 db_start_row(db, "BW");
58 db_write_word(db, bw->badword);
59 db_write_time(db, bw->add_ts);
60 db_write_word(db, bw->creator);
61 db_write_word(db, bw->channel);
62 db_write_word(db, bw->action);
63 db_commit_row(db);
64 }
65 }
66 }
67
68 static void
69 db_h_bw(database_handle_t *db, const char *type)
70 {
71 mychan_t *mc;
72 mowgli_patricia_iteration_state_t state;
73 mowgli_list_t *l;
74
75 const char *badword = db_sread_word(db);
76 time_t add_ts = db_sread_time(db);
77 const char *creator = db_sread_word(db);
78 const char *channel = db_sread_word(db);
79 const char *action = db_sread_word(db);
80
81 MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
82 {
83 if (irccasecmp(mc->name, channel))
84 continue;
85
86 l = badwords_list_of(mc);
87
88 badword_t *bw = smalloc(sizeof(badword_t));
89
90 bw->badword = sstrdup(badword);
91 bw->add_ts = add_ts;
92 bw->creator = sstrdup(creator);
93 bw->channel = sstrdup(channel);
94 bw->action = sstrdup(action);
95
96 mowgli_node_add(bw, &bw->node, l);
97 }
98 }
99
100 static void
101 on_channel_message(hook_cmessage_data_t *data)
102 {
103 badword_t *bw;
104 mowgli_node_t *n;
105 mowgli_list_t *l;
106
107 mychan_t *mc = mychan_from(data->c);
108
109 if (mc == NULL)
110 return;
111
112 if (metadata_find(mc, "blockbadwords") == NULL)
113 return;
114
115 l = badwords_list_of(mc);
116 if (MOWGLI_LIST_LENGTH(l) == 0)
117 return;
118
119 char *kickstring = "Foul language is prohibited here.";
120
121 if (data != NULL && data->msg != NULL)
122 {
123 MOWGLI_ITER_FOREACH(n, l->head)
124 {
125 bw = n->data;
126 chanuser_t *cu;
127 cu = chanuser_find(data->c, data->u);
128 if (cu == NULL)
129 return;
130 if ((metadata_find(mc, "blockbadwordsops") != NULL) && ((CSTATUS_OP | CSTATUS_PROTECT | CSTATUS_OWNER) & cu->modes))
131 return;
132
133 if (!match(bw->badword, data->msg))
134 {
135 if (!strcasecmp("KICKBAN", bw->action))
136 {
137 char hostbuf[BUFSIZE];
138
139 hostbuf[0] = '\0';
140
141 mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
142 mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
143
144 modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'b', hostbuf);
145 chanban_add(data->c, hostbuf, 'b');
146 kick(chansvs.me->me, data->c, data->u, kickstring);
147 return;
148 }
149 else if (!strcasecmp("KICK", bw->action))
150 {
151 kick(chansvs.me->me, data->c, data->u, kickstring);
152 return;
153 }
154 else if (!strcasecmp("WARN", bw->action))
155 {
156 notice(chansvs.nick, data->u->nick, "Foul language is prohibited on %s.", data->c->name);
157 return;
158 }
159 else if (!strcasecmp("QUIET", bw->action))
160 {
161 char hostbuf[BUFSIZE];
162
163 hostbuf[0] = '\0';
164
165 mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
166 mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
167
168 modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'q', hostbuf);
169 chanban_add(data->c, hostbuf, 'q');
170 return;
171 }
172 else if (!strcasecmp("BAN", bw->action))
173 {
174 char hostbuf[BUFSIZE];
175
176 hostbuf[0] = '\0';
177
178 mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
179 mowgli_strlcat(hostbuf, data->u->vhost, BUFSIZE);
180
181 modestack_mode_param(chansvs.nick, data->c, MTYPE_ADD, 'b', hostbuf);
182 chanban_add(data->c, hostbuf, 'b');
183 return;
184 }
185 }
186 }
187 }
188 }
189
190 static void
191 cs_cmd_badwords(sourceinfo_t *si, int parc, char *parv[])
192 {
193 char *channel = parv[0];
194 char *command = parv[1];
195 char *word = parv[2];
196 char *action = parv[3];
197 mychan_t *mc;
198 mowgli_node_t *n, *tn;
199 badword_t *bw;
200 mowgli_list_t *l;
201
202 if (!channel || !command)
203 {
204 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET BADWORDS");
205 command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD|DEL|LIST [badword] [action]"));
206 return;
207 }
208
209 if (!(mc = mychan_find(channel)))
210 {
211 command_fail(si, fault_nosuch_target, STR_IS_NOT_REGISTERED, channel);
212 return;
213 }
214
215 l = badwords_list_of(mc);
216
217 if (!strcasecmp("ADD", command))
218 {
219
220 if (!word || !action)
221 {
222 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "BADWORDS");
223 command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD <badword> <action>"));
224 return;
225 }
226
227 if (!chanacs_source_has_flag(mc, si, CA_SET))
228 {
229 command_fail(si, fault_noprivs, STR_NOT_AUTHORIZED);
230 return;
231 }
232
233 if (!strcasecmp("KICK", action) || !strcasecmp("KICKBAN", action) || !strcasecmp("WARN", action) ||
234 !strcasecmp("BAN", action) || (!strcasecmp("QUIET", action) && ircd != NULL &&
235 strchr(ircd->ban_like_modes, 'q')))
236 {
237 if (l != NULL)
238 {
239 MOWGLI_ITER_FOREACH(n, l->head)
240 {
241 bw = n->data;
242
243 if (!irccasecmp(bw->badword, word))
244 {
245 command_success_nodata(si, _("\2%s\2 has already been entered "
246 "into the bad word list."), word);
247 return;
248 }
249 }
250 }
251
252 bw = smalloc(sizeof(badword_t));
253 bw->add_ts = CURRTIME;;
254 bw->creator = sstrdup(get_source_name(si));
255 bw->channel = sstrdup(mc->name);
256 bw->badword = sstrdup(word);
257 bw->action = sstrdup(action);
258 mowgli_node_add(bw, &bw->node, l);
259
260 command_success_nodata(si, _("You have added \2%s\2 as a bad word."), word);
261 logcommand(si, CMDLOG_SET, "BADWORDS:ADD: \2%s\2 \2%s\2 \2%s\2", channel, word, action);
262 }
263 else
264 {
265 command_fail(si, fault_badparams, _("Invalid action given."));
266 return;
267 }
268 }
269 else if (!strcasecmp("DEL", command))
270 {
271
272 if (!word)
273 {
274 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "BADWORDS");
275 command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> DEL <badword>"));
276 return;
277 }
278
279 if (!chanacs_source_has_flag(mc, si, CA_SET))
280 {
281 command_fail(si, fault_noprivs, STR_NOT_AUTHORIZED);
282 return;
283 }
284
285 if (l == NULL)
286 {
287 command_fail(si, fault_nosuch_target, _("There are no badwords set in this channel."));
288 return;
289 }
290
291 MOWGLI_ITER_FOREACH_SAFE(n, tn, l->head)
292 {
293 bw = n->data;
294
295 if (!irccasecmp(bw->badword, word))
296 {
297 logcommand(si, CMDLOG_SET, "BADWORDS:DEL: \2%s\2 \2%s\2", mc->name, bw->badword);
298 command_success_nodata(si, _("Bad word \2%s\2 has been deleted."), bw->badword);
299
300 mowgli_node_delete(&bw->node, l);
301
302 sfree(bw->creator);
303 sfree(bw->channel);
304 sfree(bw->badword);
305 sfree(bw->action);
306 sfree(bw);
307
308 return;
309 }
310 }
311
312 command_success_nodata(si, _("Word \2%s\2 not found in bad word database."), word);
313 }
314 else if (!strcasecmp("LIST", command))
315 {
316 char buf[BUFSIZE];
317 struct tm tm;
318
319 if (!chanacs_source_has_flag(mc, si, CA_ACLVIEW))
320 {
321 command_fail(si, fault_noprivs, STR_NOT_AUTHORIZED);
322 return;
323 }
324
325 if (l == NULL)
326 {
327 command_fail(si, fault_nosuch_target, _("There are no badwords set in this channel."));
328 return;
329 }
330
331 MOWGLI_ITER_FOREACH(n, l->head)
332 {
333 bw = n->data;
334
335 tm = *localtime(&bw->add_ts);
336 strftime(buf, BUFSIZE, TIME_FORMAT, &tm);
337 command_success_nodata(si, _("Word: \2%s\2, Action: \2%s\2 (%s - %s)"),
338 bw->badword, bw->action, bw->creator, buf);
339 }
340
341 command_success_nodata(si, "End of list.");
342 logcommand(si, CMDLOG_GET, "BADWORDS:LIST");
343 }
344 else
345 {
346 command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BADWORDS");
347 command_fail(si, fault_needmoreparams, _("Syntax: BADWORDS <#channel> ADD|DEL|LIST [badword] [action]"));
348 return;
349 }
350 }
351
352 static void
353 cs_set_cmd_blockbadwords(sourceinfo_t *si, int parc, char *parv[])
354 {
355 mychan_t *mc;
356
357 if (!(mc = mychan_find(parv[0])))
358 {
359 command_fail(si, fault_nosuch_target, STR_IS_NOT_REGISTERED, parv[0]);
360 return;
361 }
362
363 if (!parv[1])
364 {
365 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET BLOCKBADWORDS");
366 return;
367 }
368
369 if (!chanacs_source_has_flag(mc, si, CA_SET))
370 {
371 command_fail(si, fault_noprivs, STR_NOT_AUTHORIZED);
372 return;
373 }
374
375 if (!strcasecmp("ON", parv[1]))
376 {
377 metadata_t *md = metadata_find(mc, "blockbadwords");
378
379 if (md)
380 {
381 command_fail(si, fault_nochange, _("The \2%s\2 flag is already set for channel \2%s\2."),
382 "BLOCKBADWORDS", mc->name);
383 return;
384 }
385
386 metadata_add(mc, "blockbadwords", "on");
387 logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDS:ON: \2%s\2", mc->name);
388 command_success_nodata(si, _("The \2%s\2 flag has been set for channel \2%s\2."),
389 "BLOCKBADWORDS", mc->name);
390 }
391 else if (!strcasecmp("OFF", parv[1]))
392 {
393 metadata_t *md = metadata_find(mc, "blockbadwords");
394
395 if (!md)
396 {
397 command_fail(si, fault_nochange, _("The \2%s\2 flag is not set for channel \2%s\2."),
398 "BLOCKBADWORDS", mc->name);
399 return;
400 }
401
402 metadata_delete(mc, "blockbadwords");
403 logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDS:OFF: \2%s\2", mc->name);
404 command_success_nodata(si, _("The \2%s\2 flag has been removed for channel \2%s\2."),
405 "BLOCKBADWORDS", mc->name);
406 }
407 else
408 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "BLOCKBADWORDS");
409 }
410
411 static void
412 cs_set_cmd_blockbadwordsops(sourceinfo_t *si, int parc, char *parv[])
413 {
414 mychan_t *mc;
415
416 if (!(mc = mychan_find(parv[0])))
417 {
418 command_fail(si, fault_nosuch_target, STR_IS_NOT_REGISTERED, parv[0]);
419 return;
420 }
421
422 if (!parv[1])
423 {
424 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SET BLOCKBADWORDSOPS");
425 return;
426 }
427
428 if (!chanacs_source_has_flag(mc, si, CA_SET))
429 {
430 command_fail(si, fault_noprivs, STR_NOT_AUTHORIZED);
431 return;
432 }
433
434 if (!strcasecmp("ON", parv[1]))
435 {
436 metadata_t *md = metadata_find(mc, "blockbadwordsops");
437
438 if (md)
439 {
440 command_fail(si, fault_nochange, _("The \2%s\2 flag is already set for channel \2%s\2."),
441 "BLOCKBADWORDSOPS", mc->name);
442 return;
443 }
444
445 metadata_add(mc, "blockbadwordsops", "on");
446 logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDSOPS:ON: \2%s\2", mc->name);
447 command_success_nodata(si, _("The \2%s\2 flag has been set for channel \2%s\2."),
448 "BLOCKBADWORDSOPS", mc->name);
449 }
450 else if (!strcasecmp("OFF", parv[1]))
451 {
452 metadata_t *md = metadata_find(mc, "blockbadwordsops");
453
454 if (!md)
455 {
456 command_fail(si, fault_nochange, _("The \2%s\2 flag is not set for channel \2%s\2."),
457 "BLOCKBADWORDSOPS", mc->name);
458 return;
459 }
460
461 metadata_delete(mc, "blockbadwordsops");
462 logcommand(si, CMDLOG_SET, "SET:BLOCKBADWORDSOPS:OFF: \2%s\2", mc->name);
463 command_success_nodata(si, _("The \2%s\2 flag has been removed for channel \2%s\2."),
464 "BLOCKBADWORDSOPS", mc->name);
465 }
466 else
467 command_fail(si, fault_badparams, STR_INVALID_PARAMS, "BLOCKBADWORDSOPS");
468 }
469
470 static command_t cs_badwords = {
471 .name = "BADWORDS",
472 .desc = N_("Manage the list of channel bad words."),
473 .access = AC_AUTHENTICATED,
474 .maxparc = 4,
475 .cmd = &cs_cmd_badwords,
476 .help = { .path = "contrib/badwords" },
477 };
478
479 static command_t cs_set_blockbadwords = {
480 .name = "BLOCKBADWORDS",
481 .desc = N_("Set whether users can say badwords in channel or not."),
482 .access = AC_NONE,
483 .maxparc = 2,
484 .cmd = &cs_set_cmd_blockbadwords,
485 .help = { .path = "contrib/set_blockbadwords" },
486 };
487
488 static command_t cs_set_blockbadwordsops = {
489 .name = "BLOCKBADWORDSOPS",
490 .desc = N_("Set whether ops can say badwords in channel or not."),
491 .access = AC_NONE,
492 .maxparc = 2,
493 .cmd = &cs_set_cmd_blockbadwordsops,
494 .help = { .path = "contrib/set_blockbadwordsops" },
495 };
496
497 static void
498 mod_init(module_t *const restrict m)
499 {
500 MODULE_TRY_REQUEST_SYMBOL(m, cs_set_cmdtree, "chanserv/set_core", "cs_set_cmdtree");
501
502 if (!module_find_published("backend/opensex"))
503 {
504 slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name);
505 m->mflags |= MODFLAG_FAIL;
506 return;
507 }
508
509 hook_add_event("channel_message");
510 hook_add_channel_message(on_channel_message);
511
512 hook_add_db_write(write_badword_db);
513
514 db_register_type_handler("BW", db_h_bw);
515
516 service_named_bind_command("chanserv", &cs_badwords);
517 command_add(&cs_set_blockbadwords, *cs_set_cmdtree);
518 command_add(&cs_set_blockbadwordsops, *cs_set_cmdtree);
519 }
520
521 static void
522 mod_deinit(const module_unload_intent_t intent)
523 {
524 hook_del_channel_message(on_channel_message);
525 hook_del_db_write(write_badword_db);
526
527 db_unregister_type_handler("BW");
528
529 service_named_unbind_command("chanserv", &cs_badwords);
530 command_delete(&cs_set_blockbadwords, *cs_set_cmdtree);
531 command_delete(&cs_set_blockbadwordsops, *cs_set_cmdtree);
532 }
533
534 SIMPLE_DECLARE_MODULE_V1("contrib/cs_badwords", MODULE_UNLOAD_CAPABILITY_OK)