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.";
42 #define KEY_BADWORDS "badwords"
43 #define KEY_BADWORD_MASK "mask"
44 #define KEY_BADWORD_TRIGGERED "count"
45 #define KEY_BADWORD_ACTION "action"
46 #define KEY_BADWOR_ALERT "alert"
47 #define KEY_CHANNELS "channel"
48 #define KEY_BADWORDID "badwordid"
50 static const struct message_entry msgtab[] = {
51 { "WDMSG_REGISTER_SUCCESS", "$b%s$b is now registered with %s." },
52 { "WDMSG_UNREG_SUCCESS", "$b%s$b is now unregistered." },
53 { "WDMSG_NOT_REGISTERED", "$b%s$b is not registered with %s." },
54 { "WDMSG_ALREADY_ADDED", "$b%s$b is already added. (ID: %s)" },
55 { "WDMSG_BADWORD_ADDED", "added '$b%s$b' to the badword list with ID %s." },
56 { "WDMSG_BADWORD_NOT_FOUND", "badword with ID %s does not exist." },
57 { "WDMSG_BADWORD_REMOVED", "badword ID $b%s$b has been removed (mask: '%s')" },
58 { "WDMSG_BADWORD_SET_DONE", "Done." },
59 { "WDMSG_BADWORD_SET_INVALID", "Invalid Option for setting %s" },
60 { "OSMSG_BADWORD_SETTING_INVALID", "unknown setting $b%s$b." },
61 { "WDMSG_BADWORD_SET", "Settings for BadWord entry $b%s$b" },
62 { "WDMSG_BADWORD_SET_MASK", "$bMASK$b: %s" },
63 { "WDMSG_BADWORD_SET_ACTION", "$bACTION$b: %s" },
64 { "WDMSG_BADWORD_SET_ALERT", "$bALERT$b: %d" },
71 unsigned int triggered : 29;
72 unsigned int action : 3;
76 struct watchdog_channel {
77 struct chanNode *channel;
78 //struct shitList *shitlist;
81 /* badword.action fields */
82 #define BADACTION_KICK 0
83 #define BADACTION_BAN 1
84 #define BADACTION_KILL 2
85 #define BADACTION_GLINE 3
87 #define WDMSG_BADWORD_ALERT "%s used badword '%s' in channel: %s."
88 #define WDMSG_REGISTER_SUCCESS "$b%s$b is now registered with %s by %s."
89 #define WDMSG_UNREG_SUCCESS "$b%s$b is now unregistered by %s."
94 const char *punishment_reason;
95 unsigned long ban_duration;
96 unsigned long gline_duration;
97 struct chanNode *alert_channel;
98 struct chanNode *oper_channel;
101 const char *watchdog_module_deps[] = { NULL };
102 struct userNode *watchdog;
103 static struct module *watchdog_module;
104 static struct service *watchdog_service;
105 static dict_t shitlist;
106 static dict_t chanlist;
107 static struct log_type *MS_LOG;
108 static unsigned int last_badword_id = 0;
110 static struct watchdog_channel *add_channel(const char *name);
111 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id);
112 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
113 #define watchdog_debug(format...) do { if(watchdog_conf.alert_channel) send_channel_message(watchdog_conf.alert_channel , watchdog , ## format); } while(0)
114 #define watchdog_oper_message(format...) do { if(watchdog_conf.oper_channel) send_channel_message(watchdog_conf.oper_channel , watchdog , ## format); } while(0)
116 static MODCMD_FUNC(cmd_addbad)
119 char *mask = unsplit_string(argv + 1, argc - 1, NULL);
120 for (it = dict_first(shitlist); it; it = iter_next(it)) {
121 struct badword *badword = iter_data(it);
122 if(match_ircglob(mask,badword->badword_mask)) {
123 reply("WDMSG_ALREADY_ADDED", mask, badword->id);
128 struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, 0, NULL);
129 for (it = dict_first(shitlist); it; it = iter_next(it)) {
130 struct badword *badword = iter_data(it);
131 if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
132 dict_remove(shitlist, badword->id);
136 reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
140 static MODCMD_FUNC(cmd_delbad)
144 for (n=1; n<argc; n++) {
145 struct badword *badword = dict_find(shitlist, argv[n], NULL);
147 reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
150 reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
151 dict_remove(shitlist, argv[n]);
156 static MODCMD_FUNC(cmd_setbad)
158 struct badword *badword;
159 if ((badword = dict_find(shitlist, argv[1], NULL))) {
162 char *setting = argv[2];
163 char *value = argv[3];
164 for( ii = 0; setting[ ii ]; ii++)
165 setting[ ii ] = toupper( setting[ ii ] );
166 for( ii = 0; value[ ii ]; ii++)
167 value[ ii ] = toupper( value[ ii ] );
168 if(!strcmp("MASK",setting)) {
169 free(badword->badword_mask);
170 badword->badword_mask = strdup(argv[3]);
171 badword->triggered = 0;
172 reply("WDMSG_BADWORD_SET_DONE");
174 else if(!strcmp("ACTION",setting)) {
175 if (!strcmp("1",value) || !strcmp("KICK",value)) {
176 badword->action = BADACTION_KICK;
177 reply("WDMSG_BADWORD_SET_DONE");
178 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
179 badword->action = BADACTION_BAN;
180 reply("WDMSG_BADWORD_SET_DONE");
181 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
182 badword->action = BADACTION_KILL;
183 reply("WDMSG_BADWORD_SET_DONE");
184 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
185 badword->action = BADACTION_GLINE;
186 reply("WDMSG_BADWORD_SET_DONE");
188 reply("WDMSG_BADWORD_SET_INVALID", setting);
191 else if(!strcmp("ALERT",setting)) {
192 if (!strcmp("0",value)) {
194 reply("WDMSG_BADWORD_SET_DONE");
195 } else if (!strcmp("1",value)) {
197 reply("WDMSG_BADWORD_SET_DONE");
199 reply("WDMSG_BADWORD_SET_INVALID", setting);
202 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
206 reply("WDMSG_BADWORD_SET", badword->id);
207 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
208 switch(badword->action) {
210 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
213 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
216 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
218 case BADACTION_GLINE:
219 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
222 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
224 reply("WDMSG_BADWORD_SET_ALERT", badword->alert);
227 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
234 badwords_sort(const void *pa, const void *pb)
236 struct badword *a = *(struct badword**)pa;
237 struct badword *b = *(struct badword**)pb;
239 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
242 static MODCMD_FUNC(cmd_listbad)
244 struct helpfile_table tbl;
245 unsigned int count = 0, ii = 0;
246 struct badword **badwords;
249 for (it = dict_first(shitlist); it; it = iter_next(it)) {
252 tbl.length = count+1;
255 tbl.flags = TABLE_NO_FREE;
256 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
257 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
258 tbl.contents[0][0] = "#";
259 tbl.contents[0][1] = "Badword";
260 tbl.contents[0][2] = "Action";
261 tbl.contents[0][3] = "(Triggered)";
262 tbl.contents[0][4] = "Alert";
265 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
267 free(tbl.contents[0]);
271 badwords = alloca(count * sizeof(badwords[0]));
272 for (it = dict_first(shitlist); it; it = iter_next(it)) {
273 struct badword *bw = iter_data(it);
276 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
277 for (ii = 1; ii <= count; ii++) {
278 struct badword *bw = badwords[ii-1];
279 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
280 tbl.contents[ii][0] = strdup(bw->id);
281 tbl.contents[ii][1] = strdup(bw->badword_mask);
284 tbl.contents[ii][2] = "KICK";
287 tbl.contents[ii][2] = "BAN";
290 tbl.contents[ii][2] = "KILL";
292 case BADACTION_GLINE:
293 tbl.contents[ii][2] = "GLINE";
296 tbl.contents[ii][2] = "*undef*";
298 tbl.contents[ii][3] = strtab(bw->triggered);
299 tbl.contents[ii][4] = strtab(bw->alert);
301 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
302 for(ii = 1; ii < tbl.length; ++ii)
304 free(tbl.contents[ii]);
306 free(tbl.contents[0]);
311 static MODCMD_FUNC(cmd_register)
319 if(channel->bad_channel)
321 reply("CSMSG_ILLEGAL_CHANNEL", channel->name);
326 && (!(mn = GetUserMode(channel, user)) || !(mn->modes & MODE_CHANOP)))
328 reply("CSMSG_MUST_BE_OPPED", channel->name);
336 if((argc < 2) || !IsChannelName(argv[1]))
338 reply("MSG_NOT_CHANNEL_NAME");
342 if(opserv_bad_channel(argv[1]))
344 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
348 channel = AddChannel(argv[1], now, NULL, NULL);
351 for (it = dict_first(chanlist); it; it = iter_next(it)) {
352 struct watchdog_channel *chan = iter_data(it);
353 if(chan->channel == channel) {
354 reply("CSMSG_ALREADY_REGGED", channel->name);
359 add_channel(channel->name);
360 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
361 watchdog_oper_message(WDMSG_REGISTER_SUCCESS, channel->name, watchdog->nick, user->handle_info->handle);
365 static MODCMD_FUNC(cmd_unregister)
367 struct watchdog_channel *chan = NULL;
370 for (it = dict_first(chanlist); it; it = iter_next(it)) {
371 chan = iter_data(it);
372 if(chan->channel == channel)
376 if(chan && chan->channel == channel) {
377 //found, unregister it!
379 sprintf(reason, "Unregistered by %s.", user->handle_info->handle);
380 DelChannelUser(watchdog, channel, reason, 0);
381 dict_remove(chanlist, channel->name);
382 reply("CSMSG_UNREG_SUCCESS", channel->name);
383 watchdog_oper_message(WDMSG_UNREG_SUCCESS, channel->name, user->handle_info->handle);
386 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
393 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword)
396 char *reason = watchdog_conf.punishment_reason;
397 char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
399 if(badword->alert == 1) {
400 log_module(MS_LOG, LOG_WARNING, "%s used badword '%s' in channel: %s", user->nick, badword->badword_mask, chan->name);
401 watchdog_debug(WDMSG_BADWORD_ALERT, user->nick, badword->badword_mask, chan->name);
403 switch(badword->action) {
405 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
406 sanitize_ircmask(hostmask);
407 if(chan->channel_info) {
409 add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
411 struct mod_chanmode change;
412 mod_chanmode_init(&change);
414 change.args[0].mode = MODE_BAN;
415 change.args[0].u.hostmask = hostmask;
416 mod_chanmode_announce(watchdog, chan, &change);
420 if(GetUserMode(chan, user))
421 KickChannelUser(user, chan, watchdog, reason);
424 DelUser(user, watchdog, 1, reason);
426 case BADACTION_GLINE:
427 irc_ntop(mask + 2, sizeof(mask) - 2, &user->ip);
428 gline_add(watchdog->nick, mask, watchdog_conf.gline_duration, reason, now, now, 0, 1);
438 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
442 if(!watchdog || !dict_find(chanlist, chan->name, NULL))
445 for (it = dict_first(shitlist); it; it = iter_next(it)) {
446 struct badword *badword = iter_data(it);
447 if(match_ircglob(text, badword->badword_mask)) {
448 watchdog_detected_badword(user, chan, badword);
453 static struct badword*
454 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id)
456 struct badword *badword;
458 badword = calloc(1, sizeof(*badword));
464 badword->id = strtab(last_badword_id);
466 badword->id = strdup(id);
467 badword->badword_mask = strdup(badword_mask);
468 badword->triggered = triggered;
469 badword->action = action;
470 badword->alert = alert;
471 dict_insert(shitlist, badword->id, badword);
476 free_shitlist_entry(void *data)
478 struct badword *badword = data;
480 free(badword->badword_mask);
484 static struct watchdog_channel*
485 add_channel(const char *name)
487 struct watchdog_channel *wc;
488 struct mod_chanmode *change;
490 if(!watchdog) //module disabled
493 wc = calloc(1, sizeof(*wc));
497 wc->channel = AddChannel(name, now, NULL, NULL);
498 change = mod_chanmode_alloc(1);
500 change->args[0].mode = MODE_CHANOP;
501 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
502 mod_chanmode_announce(watchdog, wc->channel, change);
503 mod_chanmode_free(change);
504 dict_insert(chanlist, wc->channel->name, wc);
509 free_chanlist_entry(void *data)
511 struct watchdog_channel *wc = data;
517 watchdog_conf_read(void)
522 str = "modules/watchdog";
523 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
524 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
528 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
529 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
532 watchdog_conf.nick = str;
534 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
535 watchdog_conf.modes = (str ? str : NULL);
537 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
538 watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
540 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
541 watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
543 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
544 watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
546 str = database_get_data(conf_node, "alert_chan", RECDB_QSTRING);
549 watchdog_conf.alert_channel = AddChannel(str, now, "+tinms", NULL);
553 watchdog_conf.alert_channel = NULL;
556 str = database_get_data(conf_node, "oper_chan", RECDB_QSTRING);
559 watchdog_conf.oper_channel = AddChannel(str, now, "+tinms", NULL);
563 watchdog_conf.oper_channel = NULL;
568 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
570 struct record_data *rd = data;
572 char *triggered, *action, *alert;
574 if (rd->type == RECDB_OBJECT) {
575 dict_t obj = GET_RECORD_OBJECT(rd);
576 /* new style structure */
577 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
578 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
579 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
580 alert = database_get_data(obj, KEY_BADWOR_ALERT, RECDB_QSTRING);
582 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), strtoul(alert, NULL, 0), name);
588 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
590 struct record_data *rd = data;
592 if (rd->type == RECDB_OBJECT) {
593 //dict_t obj = GET_RECORD_OBJECT(rd);
594 /* nothing in here, yet */
602 watchdog_saxdb_read(struct dict *db)
606 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
607 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
609 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
610 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
612 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
613 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
619 watchdog_saxdb_write(struct saxdb_context *ctx)
623 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
625 if (dict_size(shitlist)) {
626 saxdb_start_record(ctx, KEY_BADWORDS, 1);
627 for (it = dict_first(shitlist); it; it = iter_next(it)) {
628 struct badword *badword = iter_data(it);
629 if(badword && badword->badword_mask) {
630 saxdb_start_record(ctx, iter_key(it), 0);
632 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
633 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
634 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
635 saxdb_write_int(ctx, KEY_BADWOR_ALERT, badword->alert);
637 saxdb_end_record(ctx);
640 saxdb_end_record(ctx);
643 if (dict_size(chanlist)) {
644 saxdb_start_record(ctx, KEY_CHANNELS, 1);
645 for (it = dict_first(chanlist); it; it = iter_next(it)) {
646 struct watchdog_channel *wc = iter_data(it);
647 if(wc && wc->channel && wc->channel->name) {
648 saxdb_start_record(ctx, wc->channel->name, 0);
650 saxdb_end_record(ctx);
653 saxdb_end_record(ctx);
660 watchdog_cleanup(void)
662 dict_delete(shitlist);
663 dict_delete(chanlist);
669 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
671 /* set up shitlist dict */
672 dict_delete(shitlist);
673 shitlist = dict_new();
674 dict_set_free_data(shitlist, free_shitlist_entry);
675 /* set up chanlist dict */
676 dict_delete(chanlist);
677 chanlist = dict_new();
678 dict_set_free_data(chanlist, free_chanlist_entry);
680 const char *nick, *modes;
681 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
682 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
683 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
684 watchdog_service = service_register(watchdog);
685 watchdog_service->trigger = ',';
686 reg_allchanmsg_func(watchdog, watchdog_channel_message);
689 conf_register_reload(watchdog_conf_read);
690 reg_exit_func(watchdog_cleanup);
691 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
693 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
694 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
695 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
696 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
697 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
698 modcmd_register(watchdog_module, "register", cmd_register, 1, MODCMD_REQUIRE_AUTHED, "flags", "+acceptchan,+helping", NULL);
699 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
700 message_register_table(msgtab);
706 watchdog_finalize(void) {
710 str = "modules/watchdog";
711 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
712 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
716 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
717 if (str) watchdog_conf.nick = str;
719 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
720 if (str) watchdog_conf.modes = str;
722 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
723 if (str) watchdog_conf.ban_duration = ParseInterval(str);
725 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
726 if (str) watchdog_conf.gline_duration = ParseInterval(str);
728 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
729 if (str) watchdog_conf.punishment_reason = str;