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_NOT_REGISTERED", "$b%s$b is not registered with %s." },
53 { "WDMSG_ALREADY_ADDED", "$b%s$b is already added. (ID: %s)" },
54 { "WDMSG_BADWORD_ADDED", "added '$b%s$b' to the badword list with ID %s." },
55 { "WDMSG_BADWORD_NOT_FOUND", "badword with ID %s does not exist." },
56 { "WDMSG_BADWORD_REMOVED", "badword ID $b%s$b has been removed (mask: '%s')" },
57 { "WDMSG_BADWORD_SET_DONE", "Done." },
58 { "WDMSG_BADWORD_SET_INVALID", "Invalid Option for setting %s" },
59 { "OSMSG_BADWORD_SETTING_INVALID", "unknown setting $b%s$b." },
60 { "WDMSG_BADWORD_SET", "Settings for BadWord entry $b%s$b" },
61 { "WDMSG_BADWORD_SET_MASK", "$bMASK$b: %s" },
62 { "WDMSG_BADWORD_SET_ACTION", "$bACTION$b: %s" },
63 { "WDMSG_BADWORD_SET_ALERT", "$bALERT$b: %d" },
70 unsigned int triggered : 29;
71 unsigned int action : 3;
75 struct watchdog_channel {
76 struct chanNode *channel;
77 //struct shitList *shitlist;
80 /* badword.action fields */
81 #define BADACTION_KICK 0
82 #define BADACTION_BAN 1
83 #define BADACTION_KILL 2
84 #define BADACTION_GLINE 3
86 #define WDMSG_BADWORD_ALERT "%s used badword '%s' in channel: %s"
91 const char *punishment_reason;
92 unsigned long ban_duration;
93 unsigned long gline_duration;
94 struct chanNode *alert_channel;
97 const char *watchdog_module_deps[] = { NULL };
98 struct userNode *watchdog;
99 static struct module *watchdog_module;
100 static struct service *watchdog_service;
101 static dict_t shitlist;
102 static dict_t chanlist;
103 static struct log_type *MS_LOG;
104 static unsigned int last_badword_id = 0;
106 static struct watchdog_channel *add_channel(const char *name);
107 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id);
108 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
109 #define watchdog_debug(format...) do { if(watchdog_conf.alert_channel) send_channel_message(watchdog_conf.alert_channel , watchdog , ## format); } while(0)
111 static MODCMD_FUNC(cmd_addbad)
114 char *mask = unsplit_string(argv + 1, argc - 1, NULL);
115 for (it = dict_first(shitlist); it; it = iter_next(it)) {
116 struct badword *badword = iter_data(it);
117 if(match_ircglob(mask,badword->badword_mask)) {
118 reply("WDMSG_ALREADY_ADDED", mask, badword->id);
123 struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, 0, NULL);
124 for (it = dict_first(shitlist); it; it = iter_next(it)) {
125 struct badword *badword = iter_data(it);
126 if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
127 dict_remove(shitlist, badword->id);
131 reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
135 static MODCMD_FUNC(cmd_delbad)
139 for (n=1; n<argc; n++) {
140 struct badword *badword = dict_find(shitlist, argv[n], NULL);
142 reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
145 reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
146 dict_remove(shitlist, argv[n]);
151 static MODCMD_FUNC(cmd_setbad)
153 struct badword *badword;
154 if ((badword = dict_find(shitlist, argv[1], NULL))) {
157 char *setting = argv[2];
158 char *value = argv[3];
159 for( ii = 0; setting[ ii ]; ii++)
160 setting[ ii ] = toupper( setting[ ii ] );
161 for( ii = 0; value[ ii ]; ii++)
162 value[ ii ] = toupper( value[ ii ] );
163 if(!strcmp("MASK",setting)) {
164 free(badword->badword_mask);
165 badword->badword_mask = strdup(argv[3]);
166 badword->triggered = 0;
167 reply("WDMSG_BADWORD_SET_DONE");
169 else if(!strcmp("ACTION",setting)) {
170 if (!strcmp("1",value) || !strcmp("KICK",value)) {
171 badword->action = BADACTION_KICK;
172 reply("WDMSG_BADWORD_SET_DONE");
173 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
174 badword->action = BADACTION_BAN;
175 reply("WDMSG_BADWORD_SET_DONE");
176 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
177 badword->action = BADACTION_KILL;
178 reply("WDMSG_BADWORD_SET_DONE");
179 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
180 badword->action = BADACTION_GLINE;
181 reply("WDMSG_BADWORD_SET_DONE");
183 reply("WDMSG_BADWORD_SET_INVALID", setting);
186 else if(!strcmp("ALERT",setting)) {
187 if (!strcmp("0",value)) {
189 reply("WDMSG_BADWORD_SET_DONE");
190 } else if (!strcmp("1",value)) {
192 reply("WDMSG_BADWORD_SET_DONE");
194 reply("WDMSG_BADWORD_SET_INVALID", setting);
197 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
201 reply("WDMSG_BADWORD_SET", badword->id);
202 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
203 switch(badword->action) {
205 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
208 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
211 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
213 case BADACTION_GLINE:
214 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
217 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
219 reply("WDMSG_BADWORD_SET_ALERT", badword->alert);
222 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
229 badwords_sort(const void *pa, const void *pb)
231 struct badword *a = *(struct badword**)pa;
232 struct badword *b = *(struct badword**)pb;
234 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
237 static MODCMD_FUNC(cmd_listbad)
239 struct helpfile_table tbl;
240 unsigned int count = 0, ii = 0;
241 struct badword **badwords;
244 for (it = dict_first(shitlist); it; it = iter_next(it)) {
247 tbl.length = count+1;
250 tbl.flags = TABLE_NO_FREE;
251 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
252 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
253 tbl.contents[0][0] = "#";
254 tbl.contents[0][1] = "Badword";
255 tbl.contents[0][2] = "Action";
256 tbl.contents[0][3] = "(Triggered)";
257 tbl.contents[0][4] = "Alert";
260 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
262 free(tbl.contents[0]);
266 badwords = alloca(count * sizeof(badwords[0]));
267 for (it = dict_first(shitlist); it; it = iter_next(it)) {
268 struct badword *bw = iter_data(it);
271 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
272 for (ii = 1; ii <= count; ii++) {
273 struct badword *bw = badwords[ii-1];
274 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
275 tbl.contents[ii][0] = strdup(bw->id);
276 tbl.contents[ii][1] = strdup(bw->badword_mask);
279 tbl.contents[ii][2] = "KICK";
282 tbl.contents[ii][2] = "BAN";
285 tbl.contents[ii][2] = "KILL";
287 case BADACTION_GLINE:
288 tbl.contents[ii][2] = "GLINE";
291 tbl.contents[ii][2] = "*undef*";
293 tbl.contents[ii][3] = strtab(bw->triggered);
294 tbl.contents[ii][4] = strtab(bw->alert);
296 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
297 for(ii = 1; ii < tbl.length; ++ii)
299 free(tbl.contents[ii]);
301 free(tbl.contents[0]);
306 static MODCMD_FUNC(cmd_register)
314 if(channel->bad_channel)
316 reply("CSMSG_ILLEGAL_CHANNEL", channel->name);
321 && (!(mn = GetUserMode(channel, user)) || !(mn->modes & MODE_CHANOP)))
323 reply("CSMSG_MUST_BE_OPPED", channel->name);
331 if((argc < 2) || !IsChannelName(argv[1]))
333 reply("MSG_NOT_CHANNEL_NAME");
337 if(opserv_bad_channel(argv[1]))
339 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
343 channel = AddChannel(argv[1], now, NULL, NULL);
346 for (it = dict_first(chanlist); it; it = iter_next(it)) {
347 struct watchdog_channel *chan = iter_data(it);
348 if(chan->channel == channel) {
349 reply("CSMSG_ALREADY_REGGED", channel->name);
354 add_channel(channel->name);
355 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
359 static MODCMD_FUNC(cmd_unregister)
361 struct watchdog_channel *chan = NULL;
364 for (it = dict_first(chanlist); it; it = iter_next(it)) {
365 chan = iter_data(it);
366 if(chan->channel == channel)
370 if(chan && chan->channel == channel) {
371 //found, unregister it!
373 sprintf(reason, "Unregistered by %s.", user->handle_info->handle);
374 DelChannelUser(watchdog, channel, reason, 0);
375 dict_remove(chanlist, channel->name);
376 reply("CSMSG_UNREG_SUCCESS", channel->name);
379 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
386 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword)
389 char *reason = watchdog_conf.punishment_reason;
390 char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
392 if(badword->alert == 1) {
393 log_module(MS_LOG, LOG_WARNING, "%s used badword '%s' in channel: %s", user->nick, badword->badword_mask, chan->name);
394 watchdog_debug(WDMSG_BADWORD_ALERT, user->nick, badword->badword_mask, chan->name);
396 switch(badword->action) {
398 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
399 sanitize_ircmask(hostmask);
400 if(chan->channel_info) {
402 add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
404 struct mod_chanmode change;
405 mod_chanmode_init(&change);
407 change.args[0].mode = MODE_BAN;
408 change.args[0].u.hostmask = hostmask;
409 mod_chanmode_announce(watchdog, chan, &change);
413 if(GetUserMode(chan, user))
414 KickChannelUser(user, chan, watchdog, reason);
417 DelUser(user, watchdog, 1, reason);
419 case BADACTION_GLINE:
420 irc_ntop(mask + 2, sizeof(mask) - 2, &user->ip);
421 gline_add(watchdog->nick, mask, watchdog_conf.gline_duration, reason, now, now, 0, 1);
431 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
435 if(!watchdog || !dict_find(chanlist, chan->name, NULL))
438 for (it = dict_first(shitlist); it; it = iter_next(it)) {
439 struct badword *badword = iter_data(it);
440 if(match_ircglob(text, badword->badword_mask)) {
441 watchdog_detected_badword(user, chan, badword);
446 static struct badword*
447 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id)
449 struct badword *badword;
451 badword = calloc(1, sizeof(*badword));
457 badword->id = strtab(last_badword_id);
459 badword->id = strdup(id);
460 badword->badword_mask = strdup(badword_mask);
461 badword->triggered = triggered;
462 badword->action = action;
463 badword->alert = alert;
464 dict_insert(shitlist, badword->id, badword);
469 free_shitlist_entry(void *data)
471 struct badword *badword = data;
473 free(badword->badword_mask);
477 static struct watchdog_channel*
478 add_channel(const char *name)
480 struct watchdog_channel *wc;
481 struct mod_chanmode *change;
483 if(!watchdog) //module disabled
486 wc = calloc(1, sizeof(*wc));
490 wc->channel = AddChannel(name, now, NULL, NULL);
491 change = mod_chanmode_alloc(1);
493 change->args[0].mode = MODE_CHANOP;
494 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
495 mod_chanmode_announce(watchdog, wc->channel, change);
496 mod_chanmode_free(change);
497 dict_insert(chanlist, wc->channel->name, wc);
502 free_chanlist_entry(void *data)
504 struct watchdog_channel *wc = data;
510 watchdog_conf_read(void)
515 str = "modules/watchdog";
516 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
517 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
521 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
522 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
525 watchdog_conf.nick = str;
527 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
528 watchdog_conf.modes = (str ? str : NULL);
530 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
531 watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
533 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
534 watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
536 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
537 watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
539 str = database_get_data(conf_node, "alert_chan", RECDB_QSTRING);
542 watchdog_conf.alert_channel = AddChannel(str, now, "+tinms", NULL);
546 watchdog_conf.alert_channel = NULL;
552 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
554 struct record_data *rd = data;
556 char *triggered, *action, *alert;
558 if (rd->type == RECDB_OBJECT) {
559 dict_t obj = GET_RECORD_OBJECT(rd);
560 /* new style structure */
561 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
562 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
563 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
564 alert = database_get_data(obj, KEY_BADWOR_ALERT, RECDB_QSTRING);
566 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), strtoul(alert, NULL, 0), name);
572 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
574 struct record_data *rd = data;
576 if (rd->type == RECDB_OBJECT) {
577 //dict_t obj = GET_RECORD_OBJECT(rd);
578 /* nothing in here, yet */
586 watchdog_saxdb_read(struct dict *db)
590 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
591 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
593 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
594 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
596 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
597 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
603 watchdog_saxdb_write(struct saxdb_context *ctx)
607 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
609 if (dict_size(shitlist)) {
610 saxdb_start_record(ctx, KEY_BADWORDS, 1);
611 for (it = dict_first(shitlist); it; it = iter_next(it)) {
612 struct badword *badword = iter_data(it);
613 if(badword && badword->badword_mask) {
614 saxdb_start_record(ctx, iter_key(it), 0);
616 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
617 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
618 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
619 saxdb_write_int(ctx, KEY_BADWOR_ALERT, badword->alert);
621 saxdb_end_record(ctx);
624 saxdb_end_record(ctx);
627 if (dict_size(chanlist)) {
628 saxdb_start_record(ctx, KEY_CHANNELS, 1);
629 for (it = dict_first(chanlist); it; it = iter_next(it)) {
630 struct watchdog_channel *wc = iter_data(it);
631 if(wc && wc->channel && wc->channel->name) {
632 saxdb_start_record(ctx, wc->channel->name, 0);
634 saxdb_end_record(ctx);
637 saxdb_end_record(ctx);
644 watchdog_cleanup(void)
646 dict_delete(shitlist);
647 dict_delete(chanlist);
653 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
655 /* set up shitlist dict */
656 dict_delete(shitlist);
657 shitlist = dict_new();
658 dict_set_free_data(shitlist, free_shitlist_entry);
659 /* set up chanlist dict */
660 dict_delete(chanlist);
661 chanlist = dict_new();
662 dict_set_free_data(chanlist, free_chanlist_entry);
664 const char *nick, *modes;
665 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
666 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
667 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
668 watchdog_service = service_register(watchdog);
669 watchdog_service->trigger = ',';
670 reg_allchanmsg_func(watchdog, watchdog_channel_message);
673 conf_register_reload(watchdog_conf_read);
674 reg_exit_func(watchdog_cleanup);
675 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
677 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
678 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
679 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
680 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
681 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
682 modcmd_register(watchdog_module, "register", cmd_register, 1, MODCMD_REQUIRE_AUTHED, "flags", "+acceptchan,+helping", NULL);
683 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
684 message_register_table(msgtab);
690 watchdog_finalize(void) {
694 str = "modules/watchdog";
695 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
696 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
700 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
701 if (str) watchdog_conf.nick = str;
703 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
704 if (str) watchdog_conf.modes = str;
706 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
707 if (str) watchdog_conf.ban_duration = ParseInterval(str);
709 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
710 if (str) watchdog_conf.gline_duration = ParseInterval(str);
712 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
713 if (str) watchdog_conf.punishment_reason = str;