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:
38 #define KEY_BADWORDS "badwords"
39 #define KEY_BADWORD_MASK "mask"
40 #define KEY_BADWORD_TRIGGERED "count"
41 #define KEY_BADWORD_ACTION "action"
42 #define KEY_CHANNELS "channel"
43 #define KEY_BADWORDID "badwordid"
45 static const struct message_entry msgtab[] = {
46 { "WDMSG_REGISTER_SUCCESS", "$b%s$b is now registered with %s." },
47 { "WDMSG_NOT_REGISTERED", "$b%s$b is not registered with %s." },
48 { "WDMSG_ALREADY_ADDED", "$b%s$b is already added. (ID: %s)" },
49 { "WDMSG_BADWORD_ADDED", "added '$b%s$b' to the badword list with ID %s." },
50 { "WDMSG_BADWORD_NOT_FOUND", "badword with ID %s does not exist." },
51 { "WDMSG_BADWORD_REMOVED", "badword ID $b%s$b has been removed (mask: '%s')" },
52 { "WDMSG_BADWORD_SET_DONE", "Done." },
53 { "WDMSG_BADWORD_SET_INVALID", "Invalid Option for setting %s" },
54 { "OSMSG_BADWORD_SETTING_INVALID", "unknown setting $b%s$b." },
55 { "WDMSG_BADWORD_SET", "Settings for BadWord entry $b%s$b" },
56 { "WDMSG_BADWORD_SET_MASK", "$bMASK$b: %s" },
57 { "WDMSG_BADWORD_SET_ACTION", "$bACTION$b: %s" },
64 unsigned int triggered : 29;
65 unsigned int action : 3;
68 struct watchdog_channel {
69 struct chanNode *channel;
70 //struct shitList *shitlist;
73 /* badword.action fields */
74 #define BADACTION_KICK 0
75 #define BADACTION_BAN 1
76 #define BADACTION_KILL 2
77 #define BADACTION_GLINE 3
84 const char *watchdog_module_deps[] = { NULL };
85 struct userNode *watchdog;
86 static struct module *watchdog_module;
87 static struct service *watchdog_service;
88 static dict_t shitlist;
89 static dict_t chanlist;
90 static struct log_type *MS_LOG;
91 static unsigned int last_badword_id = 0;
93 static struct watchdog_channel *add_channel(const char *name);
94 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id);
95 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
97 static MODCMD_FUNC(cmd_addbad)
100 char *mask = unsplit_string(argv + 1, argc - 1, NULL);
101 for (it = dict_first(shitlist); it; it = iter_next(it)) {
102 struct badword *badword = iter_data(it);
103 if(match_ircglob(mask,badword->badword_mask)) {
104 reply("WDMSG_ALREADY_ADDED", mask, badword->id);
109 struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, NULL);
110 for (it = dict_first(shitlist); it; it = iter_next(it)) {
111 struct badword *badword = iter_data(it);
112 if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
113 dict_remove(shitlist, badword->id);
117 reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
121 static MODCMD_FUNC(cmd_delbad)
125 for (n=1; n<argc; n++) {
126 struct badword *badword = dict_find(shitlist, argv[n], NULL);
128 reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
131 reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
132 dict_remove(shitlist, argv[n]);
137 static MODCMD_FUNC(cmd_setbad)
139 struct badword *badword;
140 if ((badword = dict_find(shitlist, argv[1], NULL))) {
143 char *setting = argv[2];
144 char *value = argv[3];
145 for( ii = 0; setting[ ii ]; ii++)
146 setting[ ii ] = toupper( setting[ ii ] );
147 for( ii = 0; value[ ii ]; ii++)
148 value[ ii ] = toupper( value[ ii ] );
149 if(!strcmp("MASK",setting)) {
150 free(badword->badword_mask);
151 badword->badword_mask = strdup(argv[3]);
152 reply("WDMSG_BADWORD_SET_DONE");
154 else if(!strcmp("ACTION",setting)) {
155 if (!strcmp("1",value) || !strcmp("KICK",value)) {
156 badword->action = BADACTION_KICK;
157 reply("WDMSG_BADWORD_SET_DONE");
158 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
159 badword->action = BADACTION_BAN;
160 reply("WDMSG_BADWORD_SET_DONE");
161 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
162 badword->action = BADACTION_KILL;
163 reply("WDMSG_BADWORD_SET_DONE");
164 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
165 badword->action = BADACTION_GLINE;
166 reply("WDMSG_BADWORD_SET_DONE");
168 reply("WDMSG_BADWORD_SET_INVALID", setting);
171 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
175 reply("WDMSG_BADWORD_SET", badword->id);
176 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
177 switch(badword->action) {
179 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
182 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
185 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
187 case BADACTION_GLINE:
188 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
191 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
195 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
202 badwords_sort(const void *pa, const void *pb)
204 struct badword *a = *(struct badword**)pa;
205 struct badword *b = *(struct badword**)pb;
207 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
210 static MODCMD_FUNC(cmd_listbad)
212 struct helpfile_table tbl;
213 unsigned int count = 0, ii = 0;
214 struct badword **badwords;
217 for (it = dict_first(shitlist); it; it = iter_next(it)) {
220 tbl.length = count+1;
223 tbl.flags = TABLE_NO_FREE;
224 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
225 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
226 tbl.contents[0][0] = "#";
227 tbl.contents[0][1] = "Badword";
228 tbl.contents[0][2] = "Action";
229 tbl.contents[0][3] = "(Triggered)";
232 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
234 free(tbl.contents[0]);
238 badwords = alloca(count * sizeof(badwords[0]));
239 for (it = dict_first(shitlist); it; it = iter_next(it)) {
240 struct badword *bw = iter_data(it);
243 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
244 for (ii = 1; ii <= count; ii++) {
245 struct badword *bw = badwords[ii-1];
246 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
247 tbl.contents[ii][0] = strdup(bw->id);
248 tbl.contents[ii][1] = strdup(bw->badword_mask);
251 tbl.contents[ii][2] = "KICK";
254 tbl.contents[ii][2] = "BAN";
257 tbl.contents[ii][2] = "KILL";
259 case BADACTION_GLINE:
260 tbl.contents[ii][2] = "GLINE";
263 tbl.contents[ii][2] = "*undef*";
265 tbl.contents[ii][3] = strtab(bw->triggered);
267 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
268 for(ii = 1; ii < tbl.length; ++ii)
270 free(tbl.contents[ii]);
272 free(tbl.contents[0]);
277 static MODCMD_FUNC(cmd_register)
281 if((argc < 2) || !IsChannelName(argv[1]))
283 reply("MSG_NOT_CHANNEL_NAME");
287 if(opserv_bad_channel(argv[1]))
289 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
293 channel = AddChannel(argv[1], now, NULL, NULL);
295 for (it = dict_first(chanlist); it; it = iter_next(it)) {
296 struct watchdog_channel *chan = iter_data(it);
297 if(chan->channel == channel) {
298 reply("CSMSG_ALREADY_REGGED", channel->name);
303 add_channel(channel->name);
304 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
308 static MODCMD_FUNC(cmd_unregister)
310 struct watchdog_channel *chan = NULL;
313 for (it = dict_first(chanlist); it; it = iter_next(it)) {
314 chan = iter_data(it);
315 if(chan->channel == channel)
319 if(chan && chan->channel == channel) {
320 //found, unregister it!
321 DelChannelUser(watchdog, channel, "unregistered.", 0);
322 dict_remove(chanlist, channel->name);
323 reply("CSMSG_UNREG_SUCCESS", channel->name);
326 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
333 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice)
338 static struct badword*
339 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
341 struct badword *badword;
343 badword = calloc(1, sizeof(*badword));
349 badword->id = strtab(last_badword_id);
351 badword->id = strdup(id);
352 badword->badword_mask = strdup(badword_mask);
353 badword->triggered = triggered;
354 badword->action = action;
355 dict_insert(shitlist, badword->id, badword);
360 free_shitlist_entry(void *data)
362 struct badword *badword = data;
364 free(badword->badword_mask);
368 static struct watchdog_channel*
369 add_channel(const char *name)
371 struct watchdog_channel *wc;
372 struct mod_chanmode *change;
374 if(!watchdog) //module disabled
377 wc = calloc(1, sizeof(*wc));
381 wc->channel = AddChannel(name, now, NULL, NULL);
382 change = mod_chanmode_alloc(1);
384 change->args[0].mode = MODE_CHANOP;
385 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
386 mod_chanmode_announce(watchdog, wc->channel, change);
387 mod_chanmode_free(change);
388 dict_insert(chanlist, wc->channel->name, wc);
393 free_chanlist_entry(void *data)
395 struct watchdog_channel *wc = data;
401 watchdog_conf_read(void)
406 str = "modules/watchdog";
407 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
408 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
412 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
413 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
416 watchdog_conf.nick = str;
418 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
419 watchdog_conf.modes = (str ? str : NULL);
423 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
425 struct record_data *rd = data;
427 char *triggered, *action;
429 if (rd->type == RECDB_OBJECT) {
430 dict_t obj = GET_RECORD_OBJECT(rd);
431 /* new style structure */
432 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
433 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
434 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
436 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
442 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
444 struct record_data *rd = data;
446 if (rd->type == RECDB_OBJECT) {
447 dict_t obj = GET_RECORD_OBJECT(rd);
448 /* nothing in here, yet */
456 watchdog_saxdb_read(struct dict *db)
460 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
461 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
463 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
464 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
466 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
467 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
473 watchdog_saxdb_write(struct saxdb_context *ctx)
478 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
480 if (dict_size(shitlist)) {
481 saxdb_start_record(ctx, KEY_BADWORDS, 1);
482 for (it = dict_first(shitlist); it; it = iter_next(it)) {
483 struct badword *badword = iter_data(it);
484 saxdb_start_record(ctx, iter_key(it), 0);
486 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
487 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
488 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
490 saxdb_end_record(ctx);
492 saxdb_end_record(ctx);
495 if (dict_size(chanlist)) {
496 saxdb_start_record(ctx, KEY_CHANNELS, 1);
497 for (it = dict_first(chanlist); it; it = iter_next(it)) {
498 struct watchdog_channel *wc = iter_data(it);
499 saxdb_start_record(ctx, wc->channel->name, 0);
501 saxdb_end_record(ctx);
503 saxdb_end_record(ctx);
510 watchdog_cleanup(void)
512 dict_delete(shitlist);
513 dict_delete(chanlist);
519 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
521 /* set up shitlist dict */
522 dict_delete(shitlist);
523 shitlist = dict_new();
524 dict_set_free_data(shitlist, free_shitlist_entry);
525 /* set up chanlist dict */
526 dict_delete(chanlist);
527 chanlist = dict_new();
528 dict_set_free_data(chanlist, free_chanlist_entry);
530 const char *nick, *modes;
531 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
532 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
533 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
534 watchdog_service = service_register(watchdog);
535 watchdog_service->trigger = ',';
536 reg_allchanmsg_func(watchdog, watchdog_channel_message);
539 conf_register_reload(watchdog_conf_read);
540 reg_exit_func(watchdog_cleanup);
541 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
543 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
544 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
545 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
546 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
547 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
548 modcmd_register(watchdog_module, "register", cmd_register, 2, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
549 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
550 message_register_table(msgtab);
556 watchdog_finalize(void) {
560 str = "modules/watchdog";
561 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
562 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
566 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
567 if (str) watchdog_conf.nick = str;
569 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
570 if (str) watchdog_conf.modes = str;