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 badword->triggered = 0;
153 reply("WDMSG_BADWORD_SET_DONE");
155 else if(!strcmp("ACTION",setting)) {
156 if (!strcmp("1",value) || !strcmp("KICK",value)) {
157 badword->action = BADACTION_KICK;
158 reply("WDMSG_BADWORD_SET_DONE");
159 } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
160 badword->action = BADACTION_BAN;
161 reply("WDMSG_BADWORD_SET_DONE");
162 } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
163 badword->action = BADACTION_KILL;
164 reply("WDMSG_BADWORD_SET_DONE");
165 } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
166 badword->action = BADACTION_GLINE;
167 reply("WDMSG_BADWORD_SET_DONE");
169 reply("WDMSG_BADWORD_SET_INVALID", setting);
172 reply("WDMSG_BADWORD_SETTING_INVALID", setting);
176 reply("WDMSG_BADWORD_SET", badword->id);
177 reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
178 switch(badword->action) {
180 reply("WDMSG_BADWORD_SET_ACTION", "KICK");
183 reply("WDMSG_BADWORD_SET_ACTION", "BAN");
186 reply("WDMSG_BADWORD_SET_ACTION", "KILL");
188 case BADACTION_GLINE:
189 reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
192 reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
196 reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
203 badwords_sort(const void *pa, const void *pb)
205 struct badword *a = *(struct badword**)pa;
206 struct badword *b = *(struct badword**)pb;
208 return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
211 static MODCMD_FUNC(cmd_listbad)
213 struct helpfile_table tbl;
214 unsigned int count = 0, ii = 0;
215 struct badword **badwords;
218 for (it = dict_first(shitlist); it; it = iter_next(it)) {
221 tbl.length = count+1;
224 tbl.flags = TABLE_NO_FREE;
225 tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
226 tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
227 tbl.contents[0][0] = "#";
228 tbl.contents[0][1] = "Badword";
229 tbl.contents[0][2] = "Action";
230 tbl.contents[0][3] = "(Triggered)";
233 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
235 free(tbl.contents[0]);
239 badwords = alloca(count * sizeof(badwords[0]));
240 for (it = dict_first(shitlist); it; it = iter_next(it)) {
241 struct badword *bw = iter_data(it);
244 qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
245 for (ii = 1; ii <= count; ii++) {
246 struct badword *bw = badwords[ii-1];
247 tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
248 tbl.contents[ii][0] = strdup(bw->id);
249 tbl.contents[ii][1] = strdup(bw->badword_mask);
252 tbl.contents[ii][2] = "KICK";
255 tbl.contents[ii][2] = "BAN";
258 tbl.contents[ii][2] = "KILL";
260 case BADACTION_GLINE:
261 tbl.contents[ii][2] = "GLINE";
264 tbl.contents[ii][2] = "*undef*";
266 tbl.contents[ii][3] = strtab(bw->triggered);
268 table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
269 for(ii = 1; ii < tbl.length; ++ii)
271 free(tbl.contents[ii]);
273 free(tbl.contents[0]);
278 static MODCMD_FUNC(cmd_register)
282 if((argc < 2) || !IsChannelName(argv[1]))
284 reply("MSG_NOT_CHANNEL_NAME");
288 if(opserv_bad_channel(argv[1]))
290 reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
294 channel = AddChannel(argv[1], now, NULL, NULL);
296 for (it = dict_first(chanlist); it; it = iter_next(it)) {
297 struct watchdog_channel *chan = iter_data(it);
298 if(chan->channel == channel) {
299 reply("CSMSG_ALREADY_REGGED", channel->name);
304 add_channel(channel->name);
305 reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
309 static MODCMD_FUNC(cmd_unregister)
311 struct watchdog_channel *chan = NULL;
314 for (it = dict_first(chanlist); it; it = iter_next(it)) {
315 chan = iter_data(it);
316 if(chan->channel == channel)
320 if(chan && chan->channel == channel) {
321 //found, unregister it!
322 DelChannelUser(watchdog, channel, "unregistered.", 0);
323 dict_remove(chanlist, channel->name);
324 reply("CSMSG_UNREG_SUCCESS", channel->name);
327 reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
334 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice)
339 static struct badword*
340 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
342 struct badword *badword;
344 badword = calloc(1, sizeof(*badword));
350 badword->id = strtab(last_badword_id);
352 badword->id = strdup(id);
353 badword->badword_mask = strdup(badword_mask);
354 badword->triggered = triggered;
355 badword->action = action;
356 dict_insert(shitlist, badword->id, badword);
361 free_shitlist_entry(void *data)
363 struct badword *badword = data;
365 free(badword->badword_mask);
369 static struct watchdog_channel*
370 add_channel(const char *name)
372 struct watchdog_channel *wc;
373 struct mod_chanmode *change;
375 if(!watchdog) //module disabled
378 wc = calloc(1, sizeof(*wc));
382 wc->channel = AddChannel(name, now, NULL, NULL);
383 change = mod_chanmode_alloc(1);
385 change->args[0].mode = MODE_CHANOP;
386 change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
387 mod_chanmode_announce(watchdog, wc->channel, change);
388 mod_chanmode_free(change);
389 dict_insert(chanlist, wc->channel->name, wc);
394 free_chanlist_entry(void *data)
396 struct watchdog_channel *wc = data;
402 watchdog_conf_read(void)
407 str = "modules/watchdog";
408 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
409 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
413 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
414 if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
417 watchdog_conf.nick = str;
419 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
420 watchdog_conf.modes = (str ? str : NULL);
424 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
426 struct record_data *rd = data;
428 char *triggered, *action;
430 if (rd->type == RECDB_OBJECT) {
431 dict_t obj = GET_RECORD_OBJECT(rd);
432 /* new style structure */
433 badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
434 triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
435 action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
437 add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
443 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
445 struct record_data *rd = data;
447 if (rd->type == RECDB_OBJECT) {
448 dict_t obj = GET_RECORD_OBJECT(rd);
449 /* nothing in here, yet */
457 watchdog_saxdb_read(struct dict *db)
461 str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
462 last_badword_id = str ? strtoul(str, NULL, 0) : 0;
464 if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
465 dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
467 if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
468 dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
474 watchdog_saxdb_write(struct saxdb_context *ctx)
479 saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
481 if (dict_size(shitlist)) {
482 saxdb_start_record(ctx, KEY_BADWORDS, 1);
483 for (it = dict_first(shitlist); it; it = iter_next(it)) {
484 struct badword *badword = iter_data(it);
485 saxdb_start_record(ctx, iter_key(it), 0);
487 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
488 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
489 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
491 saxdb_end_record(ctx);
493 saxdb_end_record(ctx);
496 if (dict_size(chanlist)) {
497 saxdb_start_record(ctx, KEY_CHANNELS, 1);
498 for (it = dict_first(chanlist); it; it = iter_next(it)) {
499 struct watchdog_channel *wc = iter_data(it);
500 saxdb_start_record(ctx, wc->channel->name, 0);
502 saxdb_end_record(ctx);
504 saxdb_end_record(ctx);
511 watchdog_cleanup(void)
513 dict_delete(shitlist);
514 dict_delete(chanlist);
520 MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
522 /* set up shitlist dict */
523 dict_delete(shitlist);
524 shitlist = dict_new();
525 dict_set_free_data(shitlist, free_shitlist_entry);
526 /* set up chanlist dict */
527 dict_delete(chanlist);
528 chanlist = dict_new();
529 dict_set_free_data(chanlist, free_chanlist_entry);
531 const char *nick, *modes;
532 if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
533 modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
534 watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
535 watchdog_service = service_register(watchdog);
536 watchdog_service->trigger = ',';
537 reg_allchanmsg_func(watchdog, watchdog_channel_message);
540 conf_register_reload(watchdog_conf_read);
541 reg_exit_func(watchdog_cleanup);
542 saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
544 watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
545 modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
546 modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
547 modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
548 modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
549 modcmd_register(watchdog_module, "register", cmd_register, 2, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
550 modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
551 message_register_table(msgtab);
557 watchdog_finalize(void) {
561 str = "modules/watchdog";
562 if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
563 log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
567 str = database_get_data(conf_node, "nick", RECDB_QSTRING);
568 if (str) watchdog_conf.nick = str;
570 str = database_get_data(conf_node, "modes", RECDB_QSTRING);
571 if (str) watchdog_conf.modes = str;