reset triggered counter when changing mask (Watchdog BadWord)
[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  *     };
27  *  };
28  *
29  */
30
31 #include "chanserv.h"
32 #include "opserv.h"
33 #include "conf.h"
34 #include "modcmd.h"
35 #include "saxdb.h"
36 #include "timeq.h"
37
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"
44
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" },
58     { NULL, NULL }
59 };
60
61 struct badword {
62     char *id;
63     char *badword_mask;
64     unsigned int triggered : 29;
65     unsigned int action : 3;
66 };
67
68 struct watchdog_channel {
69     struct chanNode *channel;
70     //struct shitList *shitlist;
71 };
72
73 /* badword.action fields */
74 #define BADACTION_KICK   0
75 #define BADACTION_BAN    1
76 #define BADACTION_KILL   2
77 #define BADACTION_GLINE  3
78
79 static struct {
80     const char *nick;
81     const char *modes;
82 } watchdog_conf;
83
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;
92
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)
96
97 static MODCMD_FUNC(cmd_addbad)
98 {
99     dict_iterator_t it;
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);
105             return 1;
106         }
107     }
108
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);
114         }
115     }
116
117     reply("WDMSG_BADWORD_ADDED", new_badword->badword_mask, new_badword->id);
118     return 1;
119 }
120
121 static MODCMD_FUNC(cmd_delbad)
122 {
123     unsigned int n;
124
125     for (n=1; n<argc; n++) {
126         struct badword *badword = dict_find(shitlist, argv[n], NULL);
127         if (!badword) {
128             reply("WDMSG_BADWORD_NOT_FOUND", argv[n]);
129             continue;
130         }
131         reply("WDMSG_BADWORD_REMOVED", argv[n], badword->badword_mask);
132         dict_remove(shitlist, argv[n]);
133     }
134     return 1;
135 }
136
137 static MODCMD_FUNC(cmd_setbad)
138 {
139     struct badword *badword;
140     if ((badword = dict_find(shitlist, argv[1], NULL))) {
141         if (argc > 3) {
142             unsigned int ii;
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");
154             }
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");
168                  } else {
169                     reply("WDMSG_BADWORD_SET_INVALID", setting);
170                  }
171             } else {
172                  reply("WDMSG_BADWORD_SETTING_INVALID", setting);
173             }
174             
175         } else {
176             reply("WDMSG_BADWORD_SET", badword->id);
177             reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
178             switch(badword->action) {
179                 case BADACTION_KICK:
180                   reply("WDMSG_BADWORD_SET_ACTION", "KICK");
181                   break;
182                 case BADACTION_BAN:
183                   reply("WDMSG_BADWORD_SET_ACTION", "BAN");
184                   break;
185                 case BADACTION_KILL:
186                   reply("WDMSG_BADWORD_SET_ACTION", "KILL");
187                   break;
188                 case BADACTION_GLINE:
189                   reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
190                   break;
191                 default:
192                   reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
193             }
194         }
195     } else {
196         reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
197         return 0;
198     }
199     return 1;
200 }
201
202 int
203 badwords_sort(const void *pa, const void *pb)
204 {
205         struct badword *a = *(struct badword**)pa;
206         struct badword *b = *(struct badword**)pb;
207
208         return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
209 }
210
211 static MODCMD_FUNC(cmd_listbad)
212 {
213     struct helpfile_table tbl;
214     unsigned int count = 0, ii = 0;
215     struct badword **badwords;
216     
217     dict_iterator_t it;
218     for (it = dict_first(shitlist); it; it = iter_next(it)) {
219         count++;
220     }
221     tbl.length = count+1;
222     tbl.width = 4;
223     tbl.flags = 0;
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)";
231     if(!count)
232     {
233         table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
234         reply("MSG_NONE");
235         free(tbl.contents[0]);
236         free(tbl.contents);
237         return 0;
238     }
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);
242         badwords[ii++] = bw;
243     }
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);
250         switch(bw->action) {
251             case BADACTION_KICK:
252               tbl.contents[ii][2] = "KICK";
253               break;
254             case BADACTION_BAN:
255               tbl.contents[ii][2] = "BAN";
256               break;
257             case BADACTION_KILL:
258               tbl.contents[ii][2] = "KILL";
259               break;
260             case BADACTION_GLINE:
261               tbl.contents[ii][2] = "GLINE";
262               break;
263             default:
264               tbl.contents[ii][2] = "*undef*";
265         }
266         tbl.contents[ii][3] = strtab(bw->triggered);
267     }
268     table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
269     for(ii = 1; ii < tbl.length; ++ii)
270     {
271         free(tbl.contents[ii]);
272     }
273     free(tbl.contents[0]);
274     free(tbl.contents);
275     return 1;
276 }
277
278 static MODCMD_FUNC(cmd_register)
279 {
280     dict_iterator_t it;
281
282     if((argc < 2) || !IsChannelName(argv[1]))
283     {
284         reply("MSG_NOT_CHANNEL_NAME");
285         return 0;
286     }
287
288     if(opserv_bad_channel(argv[1]))
289     {
290         reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
291         return 0;
292     }
293
294     channel = AddChannel(argv[1], now, NULL, NULL);
295
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);
300             return 0;
301         }
302     }
303
304     add_channel(channel->name);
305     reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
306     return 1;
307 }
308
309 static MODCMD_FUNC(cmd_unregister)
310 {
311     struct watchdog_channel *chan = NULL;
312     dict_iterator_t it;
313     
314     for (it = dict_first(chanlist); it; it = iter_next(it)) {
315         chan = iter_data(it);
316         if(chan->channel == channel)
317             break;
318     }
319     
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);
325         return 1;
326     } else {
327         reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
328         return 0;
329     }
330
331 }
332
333 static void
334 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice)
335 {
336     //to be continued...
337 }
338
339 static struct badword*
340 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
341 {
342     struct badword *badword;
343
344     badword = calloc(1, sizeof(*badword));
345     if (!badword)
346         return NULL;
347
348     if(!id) {
349         last_badword_id++;
350         badword->id = strtab(last_badword_id);
351     } else
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);
357     return badword;
358 }
359
360 static void
361 free_shitlist_entry(void *data)
362 {
363     struct badword *badword = data;
364     free(badword->id);
365     free(badword->badword_mask);
366     free(badword);
367 }
368
369 static struct watchdog_channel*
370 add_channel(const char *name)
371 {
372     struct watchdog_channel *wc;
373     struct mod_chanmode *change;
374
375     if(!watchdog) //module disabled
376         return NULL;
377
378     wc = calloc(1, sizeof(*wc));
379     if (!wc)
380         return NULL;
381
382     wc->channel = AddChannel(name, now, NULL, NULL);
383     change = mod_chanmode_alloc(1);
384     change->argc = 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);
390     return wc;
391 }
392
393 static void
394 free_chanlist_entry(void *data)
395 {
396     struct watchdog_channel *wc = data;
397     
398     free(wc);
399 }
400
401 static void
402 watchdog_conf_read(void)
403 {
404     dict_t conf_node;
405     const char *str;
406
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);
410         return;
411     }
412
413     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
414     if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
415         //nick changed
416     }
417     watchdog_conf.nick = str;
418     
419     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
420     watchdog_conf.modes = (str ? str : NULL);
421 }
422
423 static int
424 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
425 {
426     struct record_data *rd = data;
427     char *badword;
428     char *triggered, *action;
429
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);
436
437         add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
438     }
439     return 0;
440 }
441
442 static int
443 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
444 {
445     struct record_data *rd = data;
446
447      if (rd->type == RECDB_OBJECT) {
448         dict_t obj = GET_RECORD_OBJECT(rd);
449         /* nothing in here, yet */
450
451         add_channel(name);
452     }
453     return 0;
454 }
455
456 static int
457 watchdog_saxdb_read(struct dict *db)
458 {
459     struct dict *object;
460     char *str;
461     str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
462     last_badword_id = str ? strtoul(str, NULL, 0) : 0;
463
464     if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
465         dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
466
467     if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
468         dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
469
470     return 1;
471 }
472
473 static int
474 watchdog_saxdb_write(struct saxdb_context *ctx)
475 {
476     char str[10];
477     dict_iterator_t it;
478
479     saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
480
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);
486             
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);
490             
491             saxdb_end_record(ctx);
492         }
493         saxdb_end_record(ctx);
494     }
495
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);
501             //anything else?
502             saxdb_end_record(ctx);
503         }
504         saxdb_end_record(ctx);
505     }
506     
507     return 0;
508 }
509
510 static void
511 watchdog_cleanup(void)
512 {
513     dict_delete(shitlist);
514     dict_delete(chanlist);
515 }
516
517 int
518 watchdog_init(void)
519 {
520     MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
521     
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);
530
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);
538     }
539
540     conf_register_reload(watchdog_conf_read);
541     reg_exit_func(watchdog_cleanup);
542     saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
543
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);
552
553     return 1;
554 }
555
556 int
557 watchdog_finalize(void) {
558     dict_t conf_node;
559     const char *str;
560
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);
564         return 0;
565     }
566
567     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
568     if (str) watchdog_conf.nick = str;
569     
570     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
571     if (str) watchdog_conf.modes = str;
572     return 1;
573 }