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