added alert for badwords
[srvx.git] / src / mod-watchdog.c
1 /* mod-watchdog.c - Watchdog module for srvx
2  * Copyright 2003-2004 Martijn Smit and srvx Development Team
3  *
4  * This file is part of srvx.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 /* Adds new section to srvx.conf:
22  * "modules" {
23  *     "watchdog" {
24  *         "nick" "Watchdog";
25  *         "modes" "+iok";
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.";
29  *     };
30  *  };
31  *
32  */
33
34 #include "chanserv.h"
35 #include "opserv.h"
36 #include "conf.h"
37 #include "modcmd.h"
38 #include "saxdb.h"
39 #include "timeq.h"
40 #include "gline.h"
41
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"
49
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_ALERT", "%s used badword '%s' in channel: %s" },
64     { NULL, NULL }
65 };
66
67 struct badword {
68     char *id;
69     char *badword_mask;
70     unsigned int triggered : 29;
71     unsigned int action : 3;
72     unsigned int alert;
73 };
74
75 struct watchdog_channel {
76     struct chanNode *channel;
77     //struct shitList *shitlist;
78 };
79
80 /* badword.action fields */
81 #define BADACTION_KICK   0
82 #define BADACTION_BAN    1
83 #define BADACTION_KILL   2
84 #define BADACTION_GLINE  3
85
86 static struct {
87     const char *nick;
88     const char *modes;
89     const char *punishment_reason;
90     unsigned long ban_duration;
91     unsigned long gline_duration;
92 } watchdog_conf;
93
94 const char *watchdog_module_deps[] = { NULL };
95 struct userNode *watchdog;
96 static struct module *watchdog_module;
97 static struct service *watchdog_service;
98 static dict_t shitlist;
99 static dict_t chanlist;
100 static struct log_type *MS_LOG;
101 static unsigned int last_badword_id = 0;
102
103 static struct watchdog_channel *add_channel(const char *name);
104 static struct badword *add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id);
105 #define watchdog_notice(target, format...) send_message(target , watchdog , ## format)
106
107 static MODCMD_FUNC(cmd_addbad)
108 {
109     dict_iterator_t it;
110     char *mask = unsplit_string(argv + 1, argc - 1, NULL);
111     for (it = dict_first(shitlist); it; it = iter_next(it)) {
112         struct badword *badword = iter_data(it);
113         if(match_ircglob(mask,badword->badword_mask)) {
114             reply("WDMSG_ALREADY_ADDED", mask, badword->id);
115             return 1;
116         }
117     }
118
119     struct badword *new_badword = add_badword(mask, 0, BADACTION_KICK, NULL);
120     for (it = dict_first(shitlist); it; it = iter_next(it)) {
121         struct badword *badword = iter_data(it);
122         if(match_ircglob(badword->badword_mask, new_badword->badword_mask) && badword != new_badword) {
123             dict_remove(shitlist, badword->id);
124         }
125     }
126
127     reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
128     return 1;
129 }
130
131 static MODCMD_FUNC(cmd_delbad)
132 {
133     unsigned int n;
134
135     for (n=1; n<argc; n++) {
136         struct badword *badword = dict_find(shitlist, argv[n], NULL);
137         if (!badword) {
138             reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
139             continue;
140         }
141         reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
142         dict_remove(shitlist, argv[n]);
143     }
144     return 1;
145 }
146
147 static MODCMD_FUNC(cmd_setbad)
148 {
149     struct badword *badword;
150     if ((badword = dict_find(shitlist, argv[1], NULL))) {
151         if (argc > 3) {
152             unsigned int ii;
153             char *setting = argv[2];
154             char *value = argv[3];
155             for( ii = 0; setting[ ii ]; ii++)
156                 setting[ ii ] = toupper( setting[ ii ] );
157             for( ii = 0; value[ ii ]; ii++)
158                 value[ ii ] = toupper( value[ ii ] );
159             if(!strcmp("MASK",setting)) {
160                   free(badword->badword_mask);
161                   badword->badword_mask = strdup(argv[3]);
162                   badword->triggered = 0;
163                   reply("WDMSG_BADWORD_SET_DONE");
164             }
165             else if(!strcmp("ACTION",setting)) {
166                  if (!strcmp("1",value) || !strcmp("KICK",value)) {
167                     badword->action = BADACTION_KICK;
168                     reply("WDMSG_BADWORD_SET_DONE");
169                  } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
170                     badword->action = BADACTION_BAN;
171                     reply("WDMSG_BADWORD_SET_DONE");
172                  } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
173                     badword->action = BADACTION_KILL;
174                     reply("WDMSG_BADWORD_SET_DONE");
175                  } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
176                     badword->action = BADACTION_GLINE;
177                     reply("WDMSG_BADWORD_SET_DONE");
178                  } else {
179                     reply("WDMSG_BADWORD_SET_INVALID", setting);
180                  }
181             }
182             else if(!strcmp("ALERT",setting)) {
183                 if (!strcmp("0",value)) {
184                         badword->alert = 0;
185                 } else if (!strcmp("1",value)) {
186                         badword->alert = 1;
187                 } else {
188                         reply("WDMSG_BADWORD_SET_INVALID", setting);
189                 }
190             } else {
191                  reply("WDMSG_BADWORD_SETTING_INVALID", setting);
192             }
193             
194         } else {
195             reply("WDMSG_BADWORD_SET", badword->id);
196             reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
197             switch(badword->action) {
198                 case BADACTION_KICK:
199                   reply("WDMSG_BADWORD_SET_ACTION", "KICK");
200                   break;
201                 case BADACTION_BAN:
202                   reply("WDMSG_BADWORD_SET_ACTION", "BAN");
203                   break;
204                 case BADACTION_KILL:
205                   reply("WDMSG_BADWORD_SET_ACTION", "KILL");
206                   break;
207                 case BADACTION_GLINE:
208                   reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
209                   break;
210                 default:
211                   reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
212             }
213         }
214     } else {
215         reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
216         return 0;
217     }
218     return 1;
219 }
220
221 int
222 badwords_sort(const void *pa, const void *pb)
223 {
224         struct badword *a = *(struct badword**)pa;
225         struct badword *b = *(struct badword**)pb;
226
227         return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
228 }
229
230 static MODCMD_FUNC(cmd_listbad)
231 {
232     struct helpfile_table tbl;
233     unsigned int count = 0, ii = 0;
234     struct badword **badwords;
235     
236     dict_iterator_t it;
237     for (it = dict_first(shitlist); it; it = iter_next(it)) {
238         count++;
239     }
240     tbl.length = count+1;
241     tbl.width = 5;
242     tbl.flags = 0;
243     tbl.flags = TABLE_NO_FREE;
244     tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
245     tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
246     tbl.contents[0][0] = "#";
247     tbl.contents[0][1] = "Badword";
248     tbl.contents[0][2] = "Action";
249     tbl.contents[0][3] = "(Triggered)";
250     tbl.contents[0][4] = "Alert";
251     if(!count)
252     {
253         table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
254         reply("MSG_NONE");
255         free(tbl.contents[0]);
256         free(tbl.contents);
257         return 0;
258     }
259     badwords = alloca(count * sizeof(badwords[0]));
260     for (it = dict_first(shitlist); it; it = iter_next(it)) {
261         struct badword *bw = iter_data(it);
262         badwords[ii++] = bw;
263     }
264     qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
265     for (ii = 1; ii <= count; ii++) {
266         struct badword *bw = badwords[ii-1];
267         tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
268         tbl.contents[ii][0] = strdup(bw->id);
269         tbl.contents[ii][1] = strdup(bw->badword_mask);
270         switch(bw->action) {
271             case BADACTION_KICK:
272               tbl.contents[ii][2] = "KICK";
273               break;
274             case BADACTION_BAN:
275               tbl.contents[ii][2] = "BAN";
276               break;
277             case BADACTION_KILL:
278               tbl.contents[ii][2] = "KILL";
279               break;
280             case BADACTION_GLINE:
281               tbl.contents[ii][2] = "GLINE";
282               break;
283             default:
284               tbl.contents[ii][2] = "*undef*";
285         }
286         tbl.contents[ii][3] = strtab(bw->triggered);
287         tbl.contents[ii][4] = strtab(bw->alert);
288     }
289     table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
290     for(ii = 1; ii < tbl.length; ++ii)
291     {
292         free(tbl.contents[ii]);
293     }
294     free(tbl.contents[0]);
295     free(tbl.contents);
296     return 1;
297 }
298
299 static MODCMD_FUNC(cmd_register)
300 {
301     dict_iterator_t it;
302     struct modeNode *mn;
303
304     if(channel)
305     {
306
307         if(channel->bad_channel)
308         {
309             reply("CSMSG_ILLEGAL_CHANNEL", channel->name);
310             return 0;
311         }
312
313         if(!IsHelping(user)
314            && (!(mn = GetUserMode(channel, user)) || !(mn->modes & MODE_CHANOP)))
315         {
316             reply("CSMSG_MUST_BE_OPPED", channel->name);
317             return 0;
318         }
319
320     }
321     else
322     {
323
324         if((argc < 2) || !IsChannelName(argv[1]))
325         {
326             reply("MSG_NOT_CHANNEL_NAME");
327             return 0;
328         }
329
330         if(opserv_bad_channel(argv[1]))
331         {
332             reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
333             return 0;
334         }
335
336         channel = AddChannel(argv[1], now, NULL, NULL);
337     }
338
339     for (it = dict_first(chanlist); it; it = iter_next(it)) {
340         struct watchdog_channel *chan = iter_data(it);
341         if(chan->channel == channel) {
342             reply("CSMSG_ALREADY_REGGED", channel->name);
343             return 0;
344         }
345     }
346
347     add_channel(channel->name);
348     reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
349     return 1;
350 }
351
352 static MODCMD_FUNC(cmd_unregister)
353 {
354     struct watchdog_channel *chan = NULL;
355     dict_iterator_t it;
356     
357     for (it = dict_first(chanlist); it; it = iter_next(it)) {
358         chan = iter_data(it);
359         if(chan->channel == channel)
360             break;
361     }
362     
363     if(chan && chan->channel == channel) {
364         //found, unregister it!
365         char reason[MAXLEN];
366         sprintf(reason, "Unregistered by %s.", user->handle_info->handle);
367         DelChannelUser(watchdog, channel, reason, 0);
368         dict_remove(chanlist, channel->name);
369         reply("CSMSG_UNREG_SUCCESS", channel->name);
370         return 1;
371     } else {
372         reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
373         return 0;
374     }
375
376 }
377
378 static void
379 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword) 
380 {
381     char *hostmask;
382     char *reason = watchdog_conf.punishment_reason;
383     char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
384     if(!IsOper(user)) {
385         if(badword->alert == 1) {
386                 log_module(MS_LOG, LOG_WARNING, "WDMSG_BADWORD_ALERT", user->nick, badword->badword_mask, channel->name);
387         }
388                 switch(badword->action) {
389                         case BADACTION_BAN:
390                                 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
391                                 sanitize_ircmask(hostmask);
392                                 if(chan->channel_info) {
393                                         //registered channel
394                                         add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
395                                 }
396                                 struct mod_chanmode change;
397                                 mod_chanmode_init(&change);
398                                 change.argc = 1;
399                                 change.args[0].mode = MODE_BAN;
400                                 change.args[0].u.hostmask = hostmask;
401                                 mod_chanmode_announce(watchdog, chan, &change);
402                                 free(hostmask);
403
404                         case BADACTION_KICK:
405                                 if(GetUserMode(chan, user))
406                                         KickChannelUser(user, chan, watchdog, reason);
407                                 break;
408                         case BADACTION_KILL:
409                                 DelUser(user, watchdog, 1, reason);
410                                 break;
411                         case BADACTION_GLINE:
412                                 irc_ntop(mask + 2, sizeof(mask) - 2, &user->ip);
413                                 gline_add(watchdog->nick, mask, watchdog_conf.gline_duration, reason, now, now, 0, 1);
414                                 break;
415                         default:
416                                 //error?
417                                 break;
418                         }
419     }
420 }
421
422 static void
423 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
424 {
425     dict_iterator_t it;
426
427     if(!watchdog || !dict_find(chanlist, chan->name, NULL))
428         return;
429
430     for (it = dict_first(shitlist); it; it = iter_next(it)) {
431         struct badword *badword = iter_data(it);
432         if(match_ircglob(text, badword->badword_mask)) {
433             watchdog_detected_badword(user, chan, badword);
434         }
435     }
436 }
437
438 static struct badword*
439 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id)
440 {
441     struct badword *badword;
442
443     badword = calloc(1, sizeof(*badword));
444     if (!badword)
445         return NULL;
446
447     if(!id) {
448         last_badword_id++;
449         badword->id = strtab(last_badword_id);
450     } else
451         badword->id = strdup(id);
452     badword->badword_mask = strdup(badword_mask);
453     badword->triggered = triggered;
454     badword->action = action;
455     badword->alert = alert;
456     dict_insert(shitlist, badword->id, badword);
457     return badword;
458 }
459
460 static void
461 free_shitlist_entry(void *data)
462 {
463     struct badword *badword = data;
464     free(badword->id);
465     free(badword->badword_mask);
466     free(badword);
467 }
468
469 static struct watchdog_channel*
470 add_channel(const char *name)
471 {
472     struct watchdog_channel *wc;
473     struct mod_chanmode *change;
474
475     if(!watchdog) //module disabled
476         return NULL;
477
478     wc = calloc(1, sizeof(*wc));
479     if (!wc)
480         return NULL;
481
482     wc->channel = AddChannel(name, now, NULL, NULL);
483     change = mod_chanmode_alloc(1);
484     change->argc = 1;
485     change->args[0].mode = MODE_CHANOP;
486     change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
487     mod_chanmode_announce(watchdog, wc->channel, change);
488         mod_chanmode_free(change);
489     dict_insert(chanlist, wc->channel->name, wc);
490     return wc;
491 }
492
493 static void
494 free_chanlist_entry(void *data)
495 {
496     struct watchdog_channel *wc = data;
497     
498     free(wc);
499 }
500
501 static void
502 watchdog_conf_read(void)
503 {
504     dict_t conf_node;
505     const char *str;
506
507     str = "modules/watchdog";
508     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
509         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
510         return;
511     }
512
513     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
514     if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
515         //nick changed
516     }
517     watchdog_conf.nick = str;
518     
519     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
520     watchdog_conf.modes = (str ? str : NULL);
521     
522     str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
523         watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
524     
525     str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
526         watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
527     
528     str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
529         watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
530     
531 }
532
533 static int
534 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
535 {
536     struct record_data *rd = data;
537     char *badword;
538     char *triggered, *action;
539
540      if (rd->type == RECDB_OBJECT) {
541         dict_t obj = GET_RECORD_OBJECT(rd);
542         /* new style structure */
543         badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
544         triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
545         action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
546         alert = database_get_data(obj, KEY_BADWOR_ALERT, RECDB_QSTRING);
547
548         add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), strtoul(alert, NULL, 0), name);
549     }
550     return 0;
551 }
552
553 static int
554 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
555 {
556     struct record_data *rd = data;
557
558      if (rd->type == RECDB_OBJECT) {
559         //dict_t obj = GET_RECORD_OBJECT(rd);
560         /* nothing in here, yet */
561
562         add_channel(name);
563     }
564     return 0;
565 }
566
567 static int
568 watchdog_saxdb_read(struct dict *db)
569 {
570     struct dict *object;
571     char *str;
572     str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
573     last_badword_id = str ? strtoul(str, NULL, 0) : 0;
574
575     if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
576         dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
577
578     if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
579         dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
580
581     return 1;
582 }
583
584 static int
585 watchdog_saxdb_write(struct saxdb_context *ctx)
586 {
587     dict_iterator_t it;
588
589     saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
590
591     if (dict_size(shitlist)) {
592         saxdb_start_record(ctx, KEY_BADWORDS, 1);
593         for (it = dict_first(shitlist); it; it = iter_next(it)) {
594             struct badword *badword = iter_data(it);
595             if(badword && badword->badword_mask) {
596                 saxdb_start_record(ctx, iter_key(it), 0);
597                 
598                 saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
599                 saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
600                 saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
601                 saxdb_write_int(ctx, KEY_BADWOR_ALERT, badword->alert);
602                 
603                 saxdb_end_record(ctx);
604             }
605         }
606         saxdb_end_record(ctx);
607     }
608
609     if (dict_size(chanlist)) {
610         saxdb_start_record(ctx, KEY_CHANNELS, 1);
611         for (it = dict_first(chanlist); it; it = iter_next(it)) {
612             struct watchdog_channel *wc = iter_data(it);
613             if(wc && wc->channel && wc->channel->name) {
614                 saxdb_start_record(ctx, wc->channel->name, 0);
615                 //anything else?
616                 saxdb_end_record(ctx);
617             }
618         }
619         saxdb_end_record(ctx);
620     }
621     
622     return 0;
623 }
624
625 static void
626 watchdog_cleanup(void)
627 {
628     dict_delete(shitlist);
629     dict_delete(chanlist);
630 }
631
632 int
633 watchdog_init(void)
634 {
635     MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
636     
637     /* set up shitlist dict */
638     dict_delete(shitlist);
639     shitlist = dict_new();
640     dict_set_free_data(shitlist, free_shitlist_entry);
641     /* set up chanlist dict */
642     dict_delete(chanlist);
643     chanlist = dict_new();
644     dict_set_free_data(chanlist, free_chanlist_entry);
645
646     const char *nick, *modes;
647     if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
648         modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
649         watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
650         watchdog_service = service_register(watchdog);
651         watchdog_service->trigger = ',';
652         reg_allchanmsg_func(watchdog, watchdog_channel_message);
653     }
654
655     conf_register_reload(watchdog_conf_read);
656     reg_exit_func(watchdog_cleanup);
657     saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
658
659     watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
660     modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
661     modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
662     modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
663     modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
664     modcmd_register(watchdog_module, "register", cmd_register, 1, MODCMD_REQUIRE_AUTHED, "flags", "+acceptchan,+helping", NULL);
665     modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
666     message_register_table(msgtab);
667
668     return 1;
669 }
670
671 int
672 watchdog_finalize(void) {
673     dict_t conf_node;
674     const char *str;
675
676     str = "modules/watchdog";
677     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
678         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
679         return 0;
680     }
681
682     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
683     if (str) watchdog_conf.nick = str;
684     
685     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
686     if (str) watchdog_conf.modes = str;
687     
688     str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
689         if (str) watchdog_conf.ban_duration = ParseInterval(str);
690     
691     str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
692         if (str) watchdog_conf.gline_duration = ParseInterval(str);
693     
694     str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
695         if (str) watchdog_conf.punishment_reason = str;
696     
697     return 1;
698 }