1 /* mod-watchdog.c - Watchdog module for srvx
2 * Copyright 2003-2004 Martijn Smit and srvx Development Team
4 * This file is part of srvx.
6 * srvx 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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 /* Adds new section to srvx.conf:
26 "ban_duration" "2h"; //only if the channel is registered with chanserv
27 "gline_duration" "1h";
28 "punishment_reason" "Your message contained a forbidden word.";
41 #define KEY_BADWORDS "badwords"
42 #define KEY_BADWORD_MASK "mask"
43 #define KEY_BADWORD_TRIGGERED "count"
44 #define KEY_BADWORD_ACTION "action"
45 #define KEY_CHANNELS "channel"
46 #define KEY_BADWORDID "badwordid"
48 static const struct message_entry msgtab[] = {
49 { "WDMSG_REGISTER_SUCCESS", "$b%s$b is now registered with %s." },
50 { "WDMSG_NOT_REGISTERED", "$b%s$b is not registered with %s." },
51 { "WDMSG_ALREADY_ADDED", "$b%s$b is already added. (ID: %s)" },
52 { "WDMSG_BADWORD_ADDED", "added '$b%s$b' to the badword list with ID %s." },
53 { "WDMSG_BADWORD_NOT_FOUND", "badword with ID %s does not exist." },
54 { "WDMSG_BADWORD_REMOVED", "badword ID $b%s$b has been removed (mask: '%s')" },
55 { "WDMSG_BADWORD_SET_DONE", "Done." },
56 { "WDMSG_BADWORD_SET_INVALID", "Invalid Option for setting %s" },
57 { "OSMSG_BADWORD_SETTING_INVALID", "unknown setting $b%s$b." },
58 { "WDMSG_BADWORD_SET", "Settings for BadWord entry $b%s$b" },
59 { "WDMSG_BADWORD_SET_MASK", "$bMASK$b: %s" },
60 { "WDMSG_BADWORD_SET_ACTION", "$bACTION$b: %s" },
67 unsigned int triggered : 29;
68 unsigned int action : 3;
71 struct watchdog_channel {
72 struct chanNode *channel;
73 //struct shitList *shitlist;
76 /* badword.action fields */
77 #define BADACTION_KICK 0
78 #define BADACTION_BAN 1
79 #define BADACTION_KILL 2
80 #define BADACTION_GLINE 3
85 const char *punishment_reason;
86 unsigned long ban_duration;
87 unsigned long gline_duration;
90 const char *watchdog_module_deps[] = { NULL };
91 struct userNode *watchdog;
92 static struct module *watchdog_module;
93 static struct service *watchdog_service;
94 static dict_t shitlist;
95 static dict_t chanlist;
96 static struct log_type *MS_LOG;
97 static unsigned int last_badword_id = 0;
99 static struct watchdog_channel *add_channel(const char *name);
100 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id);
101 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
103 static MODCMD_FUNC(cmd_addbad)
106 char *mask = unsplit_string(argv + 1, argc - 1, NULL);
107 for (it = dict_first(shitlist); it; it = iter_next(it)) {
108 struct badword *badword = iter_data(it);
109 if(match_ircglob(mask,badword->badword_mask)) {
110 reply("WDMSG_ALREADY_ADDED", mask, badword->id);
115 struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, NULL);
116 for (it = dict_first(shitlist); it; it = iter_next(it)) {
117 struct badword *badword = iter_data(it);
118 if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
119 dict_remove(shitlist, badword->id);
123 reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
127 static MODCMD_FUNC(cmd_delbad)
131 for (n=1; n<argc; n++) {
132 struct badword *badword = dict_find(shitlist, argv[n], NULL);
134 reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
137 reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
138 dict_remove(shitlist, argv[n]);
143 static MODCMD_FUNC(cmd_setbad)
145 struct badword *badword;
146 if ((badword = dict_find(shitlist, argv[1], NULL))) {
149 char *setting = argv[2];
150 char *value = argv[3];
151 for( ii = 0; setting[ ii ]; ii++)
152 setting[ ii ] = toupper( setting[ ii ] );
153 for( ii = 0; value[ ii ]; ii++)
154 value[ ii ] = toupper( value[ ii ] );
155 if(!strcmp("MASK",setting)) {
156 free(badword->badword_mask);
157 badword->badword_mask = strdup(argv[3]);
158 badword->triggered = 0;
159 reply("WDMSG_BADWORD_SET_DONE");
161 else if(!strcmp("ACTION",setting)) {
162 if (!strcmp("1",value) || !strcmp("KICK",value)) {
163 badword->action = BADACTION_KICK;
164 reply("WDMSG_BADWORD_SET_DONE");
165 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
166 badword->action = BADACTION_BAN;
167 reply("WDMSG_BADWORD_SET_DONE");
168 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
169 badword->action = BADACTION_KILL;
170 reply("WDMSG_BADWORD_SET_DONE");
171 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
172 badword->action = BADACTION_GLINE;
173 reply("WDMSG_BADWORD_SET_DONE");
175 reply("WDMSG_BADWORD_SET_INVALID", setting);
178 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
182 reply("WDMSG_BADWORD_SET", badword->id);
183 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
184 switch(badword->action) {
186 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
189 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
192 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
194 case BADACTION_GLINE:
195 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
198 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
202 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
209 badwords_sort(const void *pa, const void *pb)
211 struct badword *a = *(struct badword**)pa;
212 struct badword *b = *(struct badword**)pb;
214 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
217 static MODCMD_FUNC(cmd_listbad)
219 struct helpfile_table tbl;
220 unsigned int count = 0, ii = 0;
221 struct badword **badwords;
224 for (it = dict_first(shitlist); it; it = iter_next(it)) {
227 tbl.length = count+1;
230 tbl.flags = TABLE_NO_FREE;
231 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
232 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
233 tbl.contents[0][0] = "#";
234 tbl.contents[0][1] = "Badword";
235 tbl.contents[0][2] = "Action";
236 tbl.contents[0][3] = "(Triggered)";
239 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
241 free(tbl.contents[0]);
245 badwords = alloca(count * sizeof(badwords[0]));
246 for (it = dict_first(shitlist); it; it = iter_next(it)) {
247 struct badword *bw = iter_data(it);
250 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
251 for (ii = 1; ii <= count; ii++) {
252 struct badword *bw = badwords[ii-1];
253 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
254 tbl.contents[ii][0] = strdup(bw->id);
255 tbl.contents[ii][1] = strdup(bw->badword_mask);
258 tbl.contents[ii][2] = "KICK";
261 tbl.contents[ii][2] = "BAN";
264 tbl.contents[ii][2] = "KILL";
266 case BADACTION_GLINE:
267 tbl.contents[ii][2] = "GLINE";
270 tbl.contents[ii][2] = "*undef*";
272 tbl.contents[ii][3] = strtab(bw->triggered);
274 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
275 for(ii = 1; ii < tbl.length; ++ii)
277 free(tbl.contents[ii]);
279 free(tbl.contents[0]);
284 static MODCMD_FUNC(cmd_register)
288 if((argc < 2) || !IsChannelName(argv[1]))
290 reply("MSG_NOT_CHANNEL_NAME");
294 if(opserv_bad_channel(argv[1]))
296 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
300 channel = AddChannel(argv[1], now, NULL, NULL);
302 for (it = dict_first(chanlist); it; it = iter_next(it)) {
303 struct watchdog_channel *chan = iter_data(it);
304 if(chan->channel == channel) {
305 reply("CSMSG_ALREADY_REGGED", channel->name);
310 add_channel(channel->name);
311 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
315 static MODCMD_FUNC(cmd_unregister)
317 struct watchdog_channel *chan = NULL;
320 for (it = dict_first(chanlist); it; it = iter_next(it)) {
321 chan = iter_data(it);
322 if(chan->channel == channel)
326 if(chan && chan->channel == channel) {
327 //found, unregister it!
328 DelChannelUser(watchdog, channel, "unregistered.", 0);
329 dict_remove(chanlist, channel->name);
330 reply("CSMSG_UNREG_SUCCESS", channel->name);
333 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
340 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword)
343 char *reason = watchdog_conf.punishment_reason;
344 switch(badword->action) {
346 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
347 sanitize_ircmask(hostmask);
348 if(chan->channel_info) {
350 add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
352 mod_chanmode_init(&change);
354 change.args[0].mode = MODE_BAN;
355 change.args[0].u.hostmask = hostmask;
356 mod_chanmode_announce(watchdog, chan, &change);
360 KickChannelUser(user, chan, watchdog, reason);
363 DelUser(user, watchdog, 1, reason);
365 case BADACTION_GLINE:
366 int size = strlen(user->hostname) + 3;
367 hostmask = alloca(size);
368 snprintf(hostmask, size, "*@%s", user->hostname);
369 gline_add(watchdog->nick, hostmask, watchdog_conf.gline_duration, reason, now, now, 0, 1);
377 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
384 for (it = dict_first(shitlist); it; it = iter_next(it)) {
385 struct badword *badword = iter_data(it);
386 if(match_ircglob(text, badword->badword_mask)) {
387 watchdog_detected_badword(user, chan, badword);
392 static struct badword*
393 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
395 struct badword *badword;
397 badword = calloc(1, sizeof(*badword));
403 badword->id = strtab(last_badword_id);
405 badword->id = strdup(id);
406 badword->badword_mask = strdup(badword_mask);
407 badword->triggered = triggered;
408 badword->action = action;
409 dict_insert(shitlist, badword->id, badword);
414 free_shitlist_entry(void *data)
416 struct badword *badword = data;
418 free(badword->badword_mask);
422 static struct watchdog_channel*
423 add_channel(const char *name)
425 struct watchdog_channel *wc;
426 struct mod_chanmode *change;
428 if(!watchdog) //module disabled
431 wc = calloc(1, sizeof(*wc));
435 wc->channel = AddChannel(name, now, NULL, NULL);
436 change = mod_chanmode_alloc(1);
438 change->args[0].mode = MODE_CHANOP;
439 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
440 mod_chanmode_announce(watchdog, wc->channel, change);
441 mod_chanmode_free(change);
442 dict_insert(chanlist, wc->channel->name, wc);
447 free_chanlist_entry(void *data)
449 struct watchdog_channel *wc = data;
455 watchdog_conf_read(void)
460 str = "modules/watchdog";
461 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
462 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
466 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
467 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
470 watchdog_conf.nick = str;
472 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
473 watchdog_conf.modes = (str ? str : NULL);
475 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
476 watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
478 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
479 watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
481 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
482 watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
487 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
489 struct record_data *rd = data;
491 char *triggered, *action;
493 if (rd->type == RECDB_OBJECT) {
494 dict_t obj = GET_RECORD_OBJECT(rd);
495 /* new style structure */
496 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
497 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
498 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
500 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
506 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
508 struct record_data *rd = data;
510 if (rd->type == RECDB_OBJECT) {
511 dict_t obj = GET_RECORD_OBJECT(rd);
512 /* nothing in here, yet */
520 watchdog_saxdb_read(struct dict *db)
524 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
525 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
527 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
528 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
530 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
531 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
537 watchdog_saxdb_write(struct saxdb_context *ctx)
542 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
544 if (dict_size(shitlist)) {
545 saxdb_start_record(ctx, KEY_BADWORDS, 1);
546 for (it = dict_first(shitlist); it; it = iter_next(it)) {
547 struct badword *badword = iter_data(it);
548 saxdb_start_record(ctx, iter_key(it), 0);
550 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
551 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
552 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
554 saxdb_end_record(ctx);
556 saxdb_end_record(ctx);
559 if (dict_size(chanlist)) {
560 saxdb_start_record(ctx, KEY_CHANNELS, 1);
561 for (it = dict_first(chanlist); it; it = iter_next(it)) {
562 struct watchdog_channel *wc = iter_data(it);
563 saxdb_start_record(ctx, wc->channel->name, 0);
565 saxdb_end_record(ctx);
567 saxdb_end_record(ctx);
574 watchdog_cleanup(void)
576 dict_delete(shitlist);
577 dict_delete(chanlist);
583 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
585 /* set up shitlist dict */
586 dict_delete(shitlist);
587 shitlist = dict_new();
588 dict_set_free_data(shitlist, free_shitlist_entry);
589 /* set up chanlist dict */
590 dict_delete(chanlist);
591 chanlist = dict_new();
592 dict_set_free_data(chanlist, free_chanlist_entry);
594 const char *nick, *modes;
595 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
596 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
597 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
598 watchdog_service = service_register(watchdog);
599 watchdog_service->trigger = ',';
600 reg_allchanmsg_func(watchdog, watchdog_channel_message);
603 conf_register_reload(watchdog_conf_read);
604 reg_exit_func(watchdog_cleanup);
605 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
607 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
608 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
609 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
610 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
611 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
612 modcmd_register(watchdog_module, "register", cmd_register, 2, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
613 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
614 message_register_table(msgtab);
620 watchdog_finalize(void) {
624 str = "modules/watchdog";
625 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
626 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
630 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
631 if (str) watchdog_conf.nick = str;
633 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
634 if (str) watchdog_conf.modes = str;
636 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
637 if (str) watchdog_conf.ban_duration = ParseInterval(str);
639 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
640 if (str) watchdog_conf.gline_duration = ParseInterval(str);
642 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
643 if (str) watchdog_conf.punishment_reason = str;