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_CHANNELS "channel"
47 #define KEY_BADWORDID "badwordid"
49 static const struct message_entry msgtab[] = {
50 { "WDMSG_REGISTER_SUCCESS", "$b%s$b is now registered with %s." },
51 { "WDMSG_NOT_REGISTERED", "$b%s$b is not registered with %s." },
52 { "WDMSG_ALREADY_ADDED", "$b%s$b is already added. (ID: %s)" },
53 { "WDMSG_BADWORD_ADDED", "added '$b%s$b' to the badword list with ID %s." },
54 { "WDMSG_BADWORD_NOT_FOUND", "badword with ID %s does not exist." },
55 { "WDMSG_BADWORD_REMOVED", "badword ID $b%s$b has been removed (mask: '%s')" },
56 { "WDMSG_BADWORD_SET_DONE", "Done." },
57 { "WDMSG_BADWORD_SET_INVALID", "Invalid Option for setting %s" },
58 { "OSMSG_BADWORD_SETTING_INVALID", "unknown setting $b%s$b." },
59 { "WDMSG_BADWORD_SET", "Settings for BadWord entry $b%s$b" },
60 { "WDMSG_BADWORD_SET_MASK", "$bMASK$b: %s" },
61 { "WDMSG_BADWORD_SET_ACTION", "$bACTION$b: %s" },
68 unsigned int triggered : 29;
69 unsigned int action : 3;
72 struct watchdog_channel {
73 struct chanNode *channel;
74 //struct shitList *shitlist;
77 /* badword.action fields */
78 #define BADACTION_KICK 0
79 #define BADACTION_BAN 1
80 #define BADACTION_KILL 2
81 #define BADACTION_GLINE 3
86 const char *punishment_reason;
87 unsigned long ban_duration;
88 unsigned long gline_duration;
91 const char *watchdog_module_deps[] = { NULL };
92 struct userNode *watchdog;
93 static struct module *watchdog_module;
94 static struct service *watchdog_service;
95 static dict_t shitlist;
96 static dict_t chanlist;
97 static struct log_type *MS_LOG;
98 static unsigned int last_badword_id = 0;
100 static struct watchdog_channel *add_channel(const char *name);
101 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id);
102 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
104 static MODCMD_FUNC(cmd_addbad)
107 char *mask = unsplit_string(argv + 1, argc - 1, NULL);
108 for (it = dict_first(shitlist); it; it = iter_next(it)) {
109 struct badword *badword = iter_data(it);
110 if(match_ircglob(mask,badword->badword_mask)) {
111 reply("WDMSG_ALREADY_ADDED", mask, badword->id);
116 struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, NULL);
117 for (it = dict_first(shitlist); it; it = iter_next(it)) {
118 struct badword *badword = iter_data(it);
119 if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
120 dict_remove(shitlist, badword->id);
124 reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
128 static MODCMD_FUNC(cmd_delbad)
132 for (n=1; n<argc; n++) {
133 struct badword *badword = dict_find(shitlist, argv[n], NULL);
135 reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
138 reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
139 dict_remove(shitlist, argv[n]);
144 static MODCMD_FUNC(cmd_setbad)
146 struct badword *badword;
147 if ((badword = dict_find(shitlist, argv[1], NULL))) {
150 char *setting = argv[2];
151 char *value = argv[3];
152 for( ii = 0; setting[ ii ]; ii++)
153 setting[ ii ] = toupper( setting[ ii ] );
154 for( ii = 0; value[ ii ]; ii++)
155 value[ ii ] = toupper( value[ ii ] );
156 if(!strcmp("MASK",setting)) {
157 free(badword->badword_mask);
158 badword->badword_mask = strdup(argv[3]);
159 badword->triggered = 0;
160 reply("WDMSG_BADWORD_SET_DONE");
162 else if(!strcmp("ACTION",setting)) {
163 if (!strcmp("1",value) || !strcmp("KICK",value)) {
164 badword->action = BADACTION_KICK;
165 reply("WDMSG_BADWORD_SET_DONE");
166 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
167 badword->action = BADACTION_BAN;
168 reply("WDMSG_BADWORD_SET_DONE");
169 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
170 badword->action = BADACTION_KILL;
171 reply("WDMSG_BADWORD_SET_DONE");
172 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
173 badword->action = BADACTION_GLINE;
174 reply("WDMSG_BADWORD_SET_DONE");
176 reply("WDMSG_BADWORD_SET_INVALID", setting);
179 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
183 reply("WDMSG_BADWORD_SET", badword->id);
184 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
185 switch(badword->action) {
187 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
190 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
193 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
195 case BADACTION_GLINE:
196 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
199 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
203 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
210 badwords_sort(const void *pa, const void *pb)
212 struct badword *a = *(struct badword**)pa;
213 struct badword *b = *(struct badword**)pb;
215 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
218 static MODCMD_FUNC(cmd_listbad)
220 struct helpfile_table tbl;
221 unsigned int count = 0, ii = 0;
222 struct badword **badwords;
225 for (it = dict_first(shitlist); it; it = iter_next(it)) {
228 tbl.length = count+1;
231 tbl.flags = TABLE_NO_FREE;
232 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
233 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
234 tbl.contents[0][0] = "#";
235 tbl.contents[0][1] = "Badword";
236 tbl.contents[0][2] = "Action";
237 tbl.contents[0][3] = "(Triggered)";
240 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
242 free(tbl.contents[0]);
246 badwords = alloca(count * sizeof(badwords[0]));
247 for (it = dict_first(shitlist); it; it = iter_next(it)) {
248 struct badword *bw = iter_data(it);
251 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
252 for (ii = 1; ii <= count; ii++) {
253 struct badword *bw = badwords[ii-1];
254 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
255 tbl.contents[ii][0] = strdup(bw->id);
256 tbl.contents[ii][1] = strdup(bw->badword_mask);
259 tbl.contents[ii][2] = "KICK";
262 tbl.contents[ii][2] = "BAN";
265 tbl.contents[ii][2] = "KILL";
267 case BADACTION_GLINE:
268 tbl.contents[ii][2] = "GLINE";
271 tbl.contents[ii][2] = "*undef*";
273 tbl.contents[ii][3] = strtab(bw->triggered);
275 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
276 for(ii = 1; ii < tbl.length; ++ii)
278 free(tbl.contents[ii]);
280 free(tbl.contents[0]);
285 static MODCMD_FUNC(cmd_register)
289 if((argc < 2) || !IsChannelName(argv[1]))
291 reply("MSG_NOT_CHANNEL_NAME");
295 if(opserv_bad_channel(argv[1]))
297 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
301 channel = AddChannel(argv[1], now, NULL, NULL);
303 for (it = dict_first(chanlist); it; it = iter_next(it)) {
304 struct watchdog_channel *chan = iter_data(it);
305 if(chan->channel == channel) {
306 reply("CSMSG_ALREADY_REGGED", channel->name);
311 add_channel(channel->name);
312 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
316 static MODCMD_FUNC(cmd_unregister)
318 struct watchdog_channel *chan = NULL;
321 for (it = dict_first(chanlist); it; it = iter_next(it)) {
322 chan = iter_data(it);
323 if(chan->channel == channel)
327 if(chan && chan->channel == channel) {
328 //found, unregister it!
329 DelChannelUser(watchdog, channel, "unregistered.", 0);
330 dict_remove(chanlist, channel->name);
331 reply("CSMSG_UNREG_SUCCESS", channel->name);
334 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
341 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword)
344 char *reason = watchdog_conf.punishment_reason;
345 char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
346 switch(badword->action) {
348 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
349 sanitize_ircmask(hostmask);
350 if(chan->channel_info) {
352 add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
354 struct mod_chanmode change;
355 mod_chanmode_init(&change);
357 change.args[0].mode = MODE_BAN;
358 change.args[0].u.hostmask = hostmask;
359 mod_chanmode_announce(watchdog, chan, &change);
363 if(GetUserMode(chan, user))
364 KickChannelUser(user, chan, watchdog, reason);
367 DelUser(user, watchdog, 1, reason);
369 case BADACTION_GLINE:
370 irc_ntop(mask + 2, sizeof(mask) - 2, &user->ip);
371 gline_add(watchdog->nick, mask, watchdog_conf.gline_duration, reason, now, now, 0, 1);
380 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
387 for (it = dict_first(shitlist); it; it = iter_next(it)) {
388 struct badword *badword = iter_data(it);
389 if(match_ircglob(text, badword->badword_mask)) {
390 watchdog_detected_badword(user, chan, badword);
395 static struct badword*
396 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
398 struct badword *badword;
400 badword = calloc(1, sizeof(*badword));
406 badword->id = strtab(last_badword_id);
408 badword->id = strdup(id);
409 badword->badword_mask = strdup(badword_mask);
410 badword->triggered = triggered;
411 badword->action = action;
412 dict_insert(shitlist, badword->id, badword);
417 free_shitlist_entry(void *data)
419 struct badword *badword = data;
421 free(badword->badword_mask);
425 static struct watchdog_channel*
426 add_channel(const char *name)
428 struct watchdog_channel *wc;
429 struct mod_chanmode *change;
431 if(!watchdog) //module disabled
434 wc = calloc(1, sizeof(*wc));
438 wc->channel = AddChannel(name, now, NULL, NULL);
439 change = mod_chanmode_alloc(1);
441 change->args[0].mode = MODE_CHANOP;
442 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
443 mod_chanmode_announce(watchdog, wc->channel, change);
444 mod_chanmode_free(change);
445 dict_insert(chanlist, wc->channel->name, wc);
450 free_chanlist_entry(void *data)
452 struct watchdog_channel *wc = data;
458 watchdog_conf_read(void)
463 str = "modules/watchdog";
464 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
465 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
469 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
470 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
473 watchdog_conf.nick = str;
475 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
476 watchdog_conf.modes = (str ? str : NULL);
478 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
479 watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
481 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
482 watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
484 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
485 watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
490 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
492 struct record_data *rd = data;
494 char *triggered, *action;
496 if (rd->type == RECDB_OBJECT) {
497 dict_t obj = GET_RECORD_OBJECT(rd);
498 /* new style structure */
499 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
500 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
501 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
503 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
509 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
511 struct record_data *rd = data;
513 if (rd->type == RECDB_OBJECT) {
514 //dict_t obj = GET_RECORD_OBJECT(rd);
515 /* nothing in here, yet */
523 watchdog_saxdb_read(struct dict *db)
527 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
528 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
530 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
531 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
533 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
534 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
540 watchdog_saxdb_write(struct saxdb_context *ctx)
544 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
546 if (dict_size(shitlist)) {
547 saxdb_start_record(ctx, KEY_BADWORDS, 1);
548 for (it = dict_first(shitlist); it; it = iter_next(it)) {
549 struct badword *badword = iter_data(it);
550 saxdb_start_record(ctx, iter_key(it), 0);
552 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
553 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
554 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
556 saxdb_end_record(ctx);
558 saxdb_end_record(ctx);
561 if (dict_size(chanlist)) {
562 saxdb_start_record(ctx, KEY_CHANNELS, 1);
563 for (it = dict_first(chanlist); it; it = iter_next(it)) {
564 struct watchdog_channel *wc = iter_data(it);
565 saxdb_start_record(ctx, wc->channel->name, 0);
567 saxdb_end_record(ctx);
569 saxdb_end_record(ctx);
576 watchdog_cleanup(void)
578 dict_delete(shitlist);
579 dict_delete(chanlist);
585 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
587 /* set up shitlist dict */
588 dict_delete(shitlist);
589 shitlist = dict_new();
590 dict_set_free_data(shitlist, free_shitlist_entry);
591 /* set up chanlist dict */
592 dict_delete(chanlist);
593 chanlist = dict_new();
594 dict_set_free_data(chanlist, free_chanlist_entry);
596 const char *nick, *modes;
597 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
598 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
599 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
600 watchdog_service = service_register(watchdog);
601 watchdog_service->trigger = ',';
602 reg_allchanmsg_func(watchdog, watchdog_channel_message);
605 conf_register_reload(watchdog_conf_read);
606 reg_exit_func(watchdog_cleanup);
607 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
609 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
610 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
611 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
612 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
613 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
614 modcmd_register(watchdog_module, "register", cmd_register, 2, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
615 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
616 message_register_table(msgtab);
622 watchdog_finalize(void) {
626 str = "modules/watchdog";
627 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
628 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
632 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
633 if (str) watchdog_conf.nick = str;
635 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
636 if (str) watchdog_conf.modes = str;
638 str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
639 if (str) watchdog_conf.ban_duration = ParseInterval(str);
641 str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
642 if (str) watchdog_conf.gline_duration = ParseInterval(str);
644 str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
645 if (str) watchdog_conf.punishment_reason = str;