fixed compiler error
[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_SET_ALERT",  "$bALERT$b:  %d" },
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 #define WDMSG_BADWORD_ALERT "%s used badword '%s' in channel: %s"
87
88 static struct {
89     const char *nick;
90     const char *modes;
91     const char *punishment_reason;
92     unsigned long ban_duration;
93     unsigned long gline_duration;
94     struct chanNode *alert_channel;
95 } watchdog_conf;
96
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;
105
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)
110
111 static MODCMD_FUNC(cmd_addbad)
112 {
113     dict_iterator_t it;
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);
119             return 1;
120         }
121     }
122
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);
128         }
129     }
130
131     reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
132     return 1;
133 }
134
135 static MODCMD_FUNC(cmd_delbad)
136 {
137     unsigned int n;
138
139     for (n=1; n<argc; n++) {
140         struct badword *badword = dict_find(shitlist, argv[n], NULL);
141         if (!badword) {
142             reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
143             continue;
144         }
145         reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
146         dict_remove(shitlist, argv[n]);
147     }
148     return 1;
149 }
150
151 static MODCMD_FUNC(cmd_setbad)
152 {
153     struct badword *badword;
154     if ((badword = dict_find(shitlist, argv[1], NULL))) {
155         if (argc > 3) {
156             unsigned int ii;
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");
168             }
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");
182                  } else {
183                     reply("WDMSG_BADWORD_SET_INVALID", setting);
184                  }
185             }
186             else if(!strcmp("ALERT",setting)) {
187                 if (!strcmp("0",value)) {
188                         badword->alert = 0;
189                         reply("WDMSG_BADWORD_SET_DONE");
190                 } else if (!strcmp("1",value)) {
191                         badword->alert = 1;
192                         reply("WDMSG_BADWORD_SET_DONE");
193                 } else {
194                         reply("WDMSG_BADWORD_SET_INVALID", setting);
195                 }
196             } else {
197                  reply("WDMSG_BADWORD_SETTING_INVALID", setting);
198             }
199             
200         } else {
201             reply("WDMSG_BADWORD_SET", badword->id);
202             reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
203             switch(badword->action) {
204                 case BADACTION_KICK:
205                   reply("WDMSG_BADWORD_SET_ACTION", "KICK");
206                   break;
207                 case BADACTION_BAN:
208                   reply("WDMSG_BADWORD_SET_ACTION", "BAN");
209                   break;
210                 case BADACTION_KILL:
211                   reply("WDMSG_BADWORD_SET_ACTION", "KILL");
212                   break;
213                 case BADACTION_GLINE:
214                   reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
215                   break;
216                 default:
217                   reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
218             }
219             reply("WDMSG_BADWORD_SET_ALERT", badword->alert);
220         }
221     } else {
222         reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
223         return 0;
224     }
225     return 1;
226 }
227
228 int
229 badwords_sort(const void *pa, const void *pb)
230 {
231         struct badword *a = *(struct badword**)pa;
232         struct badword *b = *(struct badword**)pb;
233
234         return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
235 }
236
237 static MODCMD_FUNC(cmd_listbad)
238 {
239     struct helpfile_table tbl;
240     unsigned int count = 0, ii = 0;
241     struct badword **badwords;
242     
243     dict_iterator_t it;
244     for (it = dict_first(shitlist); it; it = iter_next(it)) {
245         count++;
246     }
247     tbl.length = count+1;
248     tbl.width = 5;
249     tbl.flags = 0;
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";
258     if(!count)
259     {
260         table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
261         reply("MSG_NONE");
262         free(tbl.contents[0]);
263         free(tbl.contents);
264         return 0;
265     }
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);
269         badwords[ii++] = bw;
270     }
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);
277         switch(bw->action) {
278             case BADACTION_KICK:
279               tbl.contents[ii][2] = "KICK";
280               break;
281             case BADACTION_BAN:
282               tbl.contents[ii][2] = "BAN";
283               break;
284             case BADACTION_KILL:
285               tbl.contents[ii][2] = "KILL";
286               break;
287             case BADACTION_GLINE:
288               tbl.contents[ii][2] = "GLINE";
289               break;
290             default:
291               tbl.contents[ii][2] = "*undef*";
292         }
293         tbl.contents[ii][3] = strtab(bw->triggered);
294         tbl.contents[ii][4] = strtab(bw->alert);
295     }
296     table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
297     for(ii = 1; ii < tbl.length; ++ii)
298     {
299         free(tbl.contents[ii]);
300     }
301     free(tbl.contents[0]);
302     free(tbl.contents);
303     return 1;
304 }
305
306 static MODCMD_FUNC(cmd_register)
307 {
308     dict_iterator_t it;
309     struct modeNode *mn;
310
311     if(channel)
312     {
313
314         if(channel->bad_channel)
315         {
316             reply("CSMSG_ILLEGAL_CHANNEL", channel->name);
317             return 0;
318         }
319
320         if(!IsHelping(user)
321            && (!(mn = GetUserMode(channel, user)) || !(mn->modes & MODE_CHANOP)))
322         {
323             reply("CSMSG_MUST_BE_OPPED", channel->name);
324             return 0;
325         }
326
327     }
328     else
329     {
330
331         if((argc < 2) || !IsChannelName(argv[1]))
332         {
333             reply("MSG_NOT_CHANNEL_NAME");
334             return 0;
335         }
336
337         if(opserv_bad_channel(argv[1]))
338         {
339             reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
340             return 0;
341         }
342
343         channel = AddChannel(argv[1], now, NULL, NULL);
344     }
345
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);
350             return 0;
351         }
352     }
353
354     add_channel(channel->name);
355     reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
356     return 1;
357 }
358
359 static MODCMD_FUNC(cmd_unregister)
360 {
361     struct watchdog_channel *chan = NULL;
362     dict_iterator_t it;
363     
364     for (it = dict_first(chanlist); it; it = iter_next(it)) {
365         chan = iter_data(it);
366         if(chan->channel == channel)
367             break;
368     }
369     
370     if(chan && chan->channel == channel) {
371         //found, unregister it!
372         char reason[MAXLEN];
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);
377         return 1;
378     } else {
379         reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
380         return 0;
381     }
382
383 }
384
385 static void
386 watchdog_detected_badword(struct userNode *user, struct chanNode *chan, struct badword *badword) 
387 {
388     char *hostmask;
389     char *reason = watchdog_conf.punishment_reason;
390     char mask[IRC_NTOP_MAX_SIZE+3] = { '*', '@', '\0' };
391     if(!IsOper(user)) {
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);
395         }
396                 switch(badword->action) {
397                         case BADACTION_BAN:
398                                 hostmask = generate_hostmask(user, GENMASK_STRICT_HOST | GENMASK_ANY_IDENT);
399                                 sanitize_ircmask(hostmask);
400                                 if(chan->channel_info) {
401                                         //registered channel
402                                         add_channel_ban(chan->channel_info, hostmask, watchdog->nick, now, now, now + watchdog_conf.ban_duration, reason);
403                                 }
404                                 struct mod_chanmode change;
405                                 mod_chanmode_init(&change);
406                                 change.argc = 1;
407                                 change.args[0].mode = MODE_BAN;
408                                 change.args[0].u.hostmask = hostmask;
409                                 mod_chanmode_announce(watchdog, chan, &change);
410                                 free(hostmask);
411
412                         case BADACTION_KICK:
413                                 if(GetUserMode(chan, user))
414                                         KickChannelUser(user, chan, watchdog, reason);
415                                 break;
416                         case BADACTION_KILL:
417                                 DelUser(user, watchdog, 1, reason);
418                                 break;
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);
422                                 break;
423                         default:
424                                 //error?
425                                 break;
426                         }
427     }
428 }
429
430 static void
431 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, UNUSED_ARG(struct userNode *bot), UNUSED_ARG(unsigned int is_notice))
432 {
433     dict_iterator_t it;
434
435     if(!watchdog || !dict_find(chanlist, chan->name, NULL))
436         return;
437
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);
442         }
443     }
444 }
445
446 static struct badword*
447 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, unsigned int alert, const char *id)
448 {
449     struct badword *badword;
450
451     badword = calloc(1, sizeof(*badword));
452     if (!badword)
453         return NULL;
454
455     if(!id) {
456         last_badword_id++;
457         badword->id = strtab(last_badword_id);
458     } else
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);
465     return badword;
466 }
467
468 static void
469 free_shitlist_entry(void *data)
470 {
471     struct badword *badword = data;
472     free(badword->id);
473     free(badword->badword_mask);
474     free(badword);
475 }
476
477 static struct watchdog_channel*
478 add_channel(const char *name)
479 {
480     struct watchdog_channel *wc;
481     struct mod_chanmode *change;
482
483     if(!watchdog) //module disabled
484         return NULL;
485
486     wc = calloc(1, sizeof(*wc));
487     if (!wc)
488         return NULL;
489
490     wc->channel = AddChannel(name, now, NULL, NULL);
491     change = mod_chanmode_alloc(1);
492     change->argc = 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);
498     return wc;
499 }
500
501 static void
502 free_chanlist_entry(void *data)
503 {
504     struct watchdog_channel *wc = data;
505     
506     free(wc);
507 }
508
509 static void
510 watchdog_conf_read(void)
511 {
512     dict_t conf_node;
513     const char *str;
514
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);
518         return;
519     }
520
521     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
522     if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
523         //nick changed
524     }
525     watchdog_conf.nick = str;
526     
527     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
528     watchdog_conf.modes = (str ? str : NULL);
529     
530     str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
531         watchdog_conf.ban_duration = str ? ParseInterval(str) : ParseInterval("2h");
532     
533     str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
534         watchdog_conf.gline_duration = str ? ParseInterval(str) : ParseInterval("1h");
535     
536     str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
537         watchdog_conf.punishment_reason = (str ? str : "Your message contained a forbidden word.");
538
539         str = database_get_data(conf_node, "alert_chan", RECDB_QSTRING);
540         if(str)
541         {
542                 watchdog_conf.alert_channel = AddChannel(str, now, "+tinms", NULL);
543         }
544         else
545         {
546                 watchdog_conf.alert_channel = NULL;
547         }
548     
549 }
550
551 static int
552 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
553 {
554     struct record_data *rd = data;
555     char *badword;
556     char *triggered, *action, *alert;
557
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);
565
566         add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), strtoul(alert, NULL, 0), name);
567     }
568     return 0;
569 }
570
571 static int
572 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
573 {
574     struct record_data *rd = data;
575
576      if (rd->type == RECDB_OBJECT) {
577         //dict_t obj = GET_RECORD_OBJECT(rd);
578         /* nothing in here, yet */
579
580         add_channel(name);
581     }
582     return 0;
583 }
584
585 static int
586 watchdog_saxdb_read(struct dict *db)
587 {
588     struct dict *object;
589     char *str;
590     str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
591     last_badword_id = str ? strtoul(str, NULL, 0) : 0;
592
593     if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
594         dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
595
596     if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
597         dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
598
599     return 1;
600 }
601
602 static int
603 watchdog_saxdb_write(struct saxdb_context *ctx)
604 {
605     dict_iterator_t it;
606
607     saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
608
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);
615                 
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);
620                 
621                 saxdb_end_record(ctx);
622             }
623         }
624         saxdb_end_record(ctx);
625     }
626
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);
633                 //anything else?
634                 saxdb_end_record(ctx);
635             }
636         }
637         saxdb_end_record(ctx);
638     }
639     
640     return 0;
641 }
642
643 static void
644 watchdog_cleanup(void)
645 {
646     dict_delete(shitlist);
647     dict_delete(chanlist);
648 }
649
650 int
651 watchdog_init(void)
652 {
653     MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
654     
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);
663
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);
671     }
672
673     conf_register_reload(watchdog_conf_read);
674     reg_exit_func(watchdog_cleanup);
675     saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
676
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);
685
686     return 1;
687 }
688
689 int
690 watchdog_finalize(void) {
691     dict_t conf_node;
692     const char *str;
693
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);
697         return 0;
698     }
699
700     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
701     if (str) watchdog_conf.nick = str;
702     
703     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
704     if (str) watchdog_conf.modes = str;
705     
706     str = database_get_data(conf_node, "ban_duration", RECDB_QSTRING);
707         if (str) watchdog_conf.ban_duration = ParseInterval(str);
708     
709     str = database_get_data(conf_node, "gline_duration", RECDB_QSTRING);
710         if (str) watchdog_conf.gline_duration = ParseInterval(str);
711     
712     str = database_get_data(conf_node, "punishment_reason", RECDB_QSTRING);
713         if (str) watchdog_conf.punishment_reason = str;
714     
715     return 1;
716 }