added cmd_setbad
[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                   reply("WDMSG_BADWORD_SET_DONE");
153             }
154             else if(!strcmp("ACTION",setting)) {
155                  if (!strcmp("1",value) || !strcmp("KICK",value)) {
156                     badword->action = BADACTION_KICK;
157                     reply("WDMSG_BADWORD_SET_DONE");
158                  } else if (!strcmp("2",value) || !strcmp("BAN",value)) {
159                     badword->action = BADACTION_BAN;
160                     reply("WDMSG_BADWORD_SET_DONE");
161                  } else if (!strcmp("3",value) || !strcmp("KILL",value)) {
162                     badword->action = BADACTION_KILL;
163                     reply("WDMSG_BADWORD_SET_DONE");
164                  } else if (!strcmp("4",value) || !strcmp("GLINE",value)) {
165                     badword->action = BADACTION_GLINE;
166                     reply("WDMSG_BADWORD_SET_DONE");
167                  } else {
168                     reply("WDMSG_BADWORD_SET_INVALID", setting);
169                  }
170             } else {
171                  reply("WDMSG_BADWORD_SETTING_INVALID", setting);
172             }
173             
174         } else {
175             reply("WDMSG_BADWORD_SET", badword->id);
176             reply("WDMSG_BADWORD_SET_MASK", badword->badword_mask);
177             switch(badword->action) {
178                 case BADACTION_KICK:
179                   reply("WDMSG_BADWORD_SET_ACTION", "KICK");
180                   break;
181                 case BADACTION_BAN:
182                   reply("WDMSG_BADWORD_SET_ACTION", "BAN");
183                   break;
184                 case BADACTION_KILL:
185                   reply("WDMSG_BADWORD_SET_ACTION", "KILL");
186                   break;
187                 case BADACTION_GLINE:
188                   reply("WDMSG_BADWORD_SET_ACTION", "GLINE");
189                   break;
190                 default:
191                   reply("WDMSG_BADWORD_SET_ACTION", "*undef*");
192             }
193         }
194     } else {
195         reply("WDMSG_BADWORD_NOT_FOUND", argv[1]);
196         return 0;
197     }
198     return 1;
199 }
200
201 int
202 badwords_sort(const void *pa, const void *pb)
203 {
204         struct badword *a = *(struct badword**)pa;
205         struct badword *b = *(struct badword**)pb;
206
207         return strtoul(a->id, NULL, 0) - strtoul(b->id, NULL, 0);
208 }
209
210 static MODCMD_FUNC(cmd_listbad)
211 {
212     struct helpfile_table tbl;
213     unsigned int count = 0, ii = 0;
214     struct badword **badwords;
215     
216     dict_iterator_t it;
217     for (it = dict_first(shitlist); it; it = iter_next(it)) {
218         count++;
219     }
220     tbl.length = count+1;
221     tbl.width = 4;
222     tbl.flags = 0;
223     tbl.flags = TABLE_NO_FREE;
224     tbl.contents = malloc(tbl.length * sizeof(tbl.contents[0]));
225     tbl.contents[0] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
226     tbl.contents[0][0] = "#";
227     tbl.contents[0][1] = "Badword";
228     tbl.contents[0][2] = "Action";
229     tbl.contents[0][3] = "(Triggered)";
230     if(!count)
231     {
232         table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
233         reply("MSG_NONE");
234         free(tbl.contents[0]);
235         free(tbl.contents);
236         return 0;
237     }
238     badwords = alloca(count * sizeof(badwords[0]));
239     for (it = dict_first(shitlist); it; it = iter_next(it)) {
240         struct badword *bw = iter_data(it);
241         badwords[ii++] = bw;
242     }
243     qsort(badwords, count, sizeof(badwords[0]), badwords_sort);
244     for (ii = 1; ii <= count; ii++) {
245         struct badword *bw = badwords[ii-1];
246         tbl.contents[ii] = malloc(tbl.width * sizeof(tbl.contents[0][0]));
247         tbl.contents[ii][0] = strdup(bw->id);
248         tbl.contents[ii][1] = strdup(bw->badword_mask);
249         switch(bw->action) {
250             case BADACTION_KICK:
251               tbl.contents[ii][2] = "KICK";
252               break;
253             case BADACTION_BAN:
254               tbl.contents[ii][2] = "BAN";
255               break;
256             case BADACTION_KILL:
257               tbl.contents[ii][2] = "KILL";
258               break;
259             case BADACTION_GLINE:
260               tbl.contents[ii][2] = "GLINE";
261               break;
262             default:
263               tbl.contents[ii][2] = "*undef*";
264         }
265         tbl.contents[ii][3] = strtab(bw->triggered);
266     }
267     table_send(cmd->parent->bot, user->nick, 0, NULL, tbl);
268     for(ii = 1; ii < tbl.length; ++ii)
269     {
270         free(tbl.contents[ii]);
271     }
272     free(tbl.contents[0]);
273     free(tbl.contents);
274     return 1;
275 }
276
277 static MODCMD_FUNC(cmd_register)
278 {
279     dict_iterator_t it;
280
281     if((argc < 2) || !IsChannelName(argv[1]))
282     {
283         reply("MSG_NOT_CHANNEL_NAME");
284         return 0;
285     }
286
287     if(opserv_bad_channel(argv[1]))
288     {
289         reply("CSMSG_ILLEGAL_CHANNEL", argv[1]);
290         return 0;
291     }
292
293     channel = AddChannel(argv[1], now, NULL, NULL);
294
295     for (it = dict_first(chanlist); it; it = iter_next(it)) {
296         struct watchdog_channel *chan = iter_data(it);
297         if(chan->channel == channel) {
298             reply("CSMSG_ALREADY_REGGED", channel->name);
299             return 0;
300         }
301     }
302
303     add_channel(channel->name);
304     reply("WDMSG_REGISTER_SUCCESS", channel->name, watchdog->nick);
305     return 1;
306 }
307
308 static MODCMD_FUNC(cmd_unregister)
309 {
310     struct watchdog_channel *chan = NULL;
311     dict_iterator_t it;
312     
313     for (it = dict_first(chanlist); it; it = iter_next(it)) {
314         chan = iter_data(it);
315         if(chan->channel == channel)
316             break;
317     }
318     
319     if(chan && chan->channel == channel) {
320         //found, unregister it!
321         DelChannelUser(watchdog, channel, "unregistered.", 0);
322         dict_remove(chanlist, channel->name);
323         reply("CSMSG_UNREG_SUCCESS", channel->name);
324         return 1;
325     } else {
326         reply("WDMSG_NOT_REGISTERED", channel->name, watchdog->nick);
327         return 0;
328     }
329
330 }
331
332 static void
333 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice)
334 {
335     //to be continued...
336 }
337
338 static struct badword*
339 add_badword(const char *badword_mask, unsigned int triggered, unsigned int action, const char *id)
340 {
341     struct badword *badword;
342
343     badword = calloc(1, sizeof(*badword));
344     if (!badword)
345         return NULL;
346
347     if(!id) {
348         last_badword_id++;
349         badword->id = strtab(last_badword_id);
350     } else
351         badword->id = strdup(id);
352     badword->badword_mask = strdup(badword_mask);
353     badword->triggered = triggered;
354     badword->action = action;
355     dict_insert(shitlist, badword->id, badword);
356     return badword;
357 }
358
359 static void
360 free_shitlist_entry(void *data)
361 {
362     struct badword *badword = data;
363     free(badword->id);
364     free(badword->badword_mask);
365     free(badword);
366 }
367
368 static struct watchdog_channel*
369 add_channel(const char *name)
370 {
371     struct watchdog_channel *wc;
372     struct mod_chanmode *change;
373
374     if(!watchdog) //module disabled
375         return NULL;
376
377     wc = calloc(1, sizeof(*wc));
378     if (!wc)
379         return NULL;
380
381     wc->channel = AddChannel(name, now, NULL, NULL);
382     change = mod_chanmode_alloc(1);
383     change->argc = 1;
384     change->args[0].mode = MODE_CHANOP;
385     change->args[0].u.member = AddChannelUser(watchdog, wc->channel);
386     mod_chanmode_announce(watchdog, wc->channel, change);
387         mod_chanmode_free(change);
388     dict_insert(chanlist, wc->channel->name, wc);
389     return wc;
390 }
391
392 static void
393 free_chanlist_entry(void *data)
394 {
395     struct watchdog_channel *wc = data;
396     
397     free(wc);
398 }
399
400 static void
401 watchdog_conf_read(void)
402 {
403     dict_t conf_node;
404     const char *str;
405
406     str = "modules/watchdog";
407     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
408         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
409         return;
410     }
411
412     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
413     if(watchdog_conf.nick && strcmp(watchdog_conf.nick, str)) {
414         //nick changed
415     }
416     watchdog_conf.nick = str;
417     
418     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
419     watchdog_conf.modes = (str ? str : NULL);
420 }
421
422 static int
423 watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extra))
424 {
425     struct record_data *rd = data;
426     char *badword;
427     char *triggered, *action;
428
429      if (rd->type == RECDB_OBJECT) {
430         dict_t obj = GET_RECORD_OBJECT(rd);
431         /* new style structure */
432         badword = database_get_data(obj, KEY_BADWORD_MASK, RECDB_QSTRING);
433         triggered = database_get_data(obj, KEY_BADWORD_TRIGGERED, RECDB_QSTRING);
434         action = database_get_data(obj, KEY_BADWORD_ACTION, RECDB_QSTRING);
435
436         add_badword(badword, strtoul(triggered, NULL, 0), strtoul(action, NULL, 0), name);
437     }
438     return 0;
439 }
440
441 static int
442 watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
443 {
444     struct record_data *rd = data;
445
446      if (rd->type == RECDB_OBJECT) {
447         dict_t obj = GET_RECORD_OBJECT(rd);
448         /* nothing in here, yet */
449
450         add_channel(name);
451     }
452     return 0;
453 }
454
455 static int
456 watchdog_saxdb_read(struct dict *db)
457 {
458     struct dict *object;
459     char *str;
460     str = database_get_data(db, KEY_BADWORDID, RECDB_QSTRING);
461     last_badword_id = str ? strtoul(str, NULL, 0) : 0;
462
463     if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
464         dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
465
466     if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
467         dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
468
469     return 1;
470 }
471
472 static int
473 watchdog_saxdb_write(struct saxdb_context *ctx)
474 {
475     char str[10];
476     dict_iterator_t it;
477
478     saxdb_write_int(ctx, KEY_BADWORDID, last_badword_id);
479
480     if (dict_size(shitlist)) {
481         saxdb_start_record(ctx, KEY_BADWORDS, 1);
482         for (it = dict_first(shitlist); it; it = iter_next(it)) {
483             struct badword *badword = iter_data(it);
484             saxdb_start_record(ctx, iter_key(it), 0);
485             
486             saxdb_write_string(ctx, KEY_BADWORD_MASK, badword->badword_mask);
487             saxdb_write_int(ctx, KEY_BADWORD_TRIGGERED, badword->triggered);
488             saxdb_write_int(ctx, KEY_BADWORD_ACTION, badword->action);
489             
490             saxdb_end_record(ctx);
491         }
492         saxdb_end_record(ctx);
493     }
494
495     if (dict_size(chanlist)) {
496         saxdb_start_record(ctx, KEY_CHANNELS, 1);
497         for (it = dict_first(chanlist); it; it = iter_next(it)) {
498             struct watchdog_channel *wc = iter_data(it);
499             saxdb_start_record(ctx, wc->channel->name, 0);
500             //anything else?
501             saxdb_end_record(ctx);
502         }
503         saxdb_end_record(ctx);
504     }
505     
506     return 0;
507 }
508
509 static void
510 watchdog_cleanup(void)
511 {
512     dict_delete(shitlist);
513     dict_delete(chanlist);
514 }
515
516 int
517 watchdog_init(void)
518 {
519     MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
520     
521     /* set up shitlist dict */
522     dict_delete(shitlist);
523     shitlist = dict_new();
524     dict_set_free_data(shitlist, free_shitlist_entry);
525     /* set up chanlist dict */
526     dict_delete(chanlist);
527     chanlist = dict_new();
528     dict_set_free_data(chanlist, free_chanlist_entry);
529
530     const char *nick, *modes;
531     if((nick = conf_get_data("modules/watchdog/nick", RECDB_QSTRING))) {
532         modes = conf_get_data("modules/watchdog/modes", RECDB_QSTRING);
533         watchdog = AddLocalUser(nick, nick, NULL, "Watchdog Service", modes);
534         watchdog_service = service_register(watchdog);
535         watchdog_service->trigger = ',';
536         reg_allchanmsg_func(watchdog, watchdog_channel_message);
537     }
538
539     conf_register_reload(watchdog_conf_read);
540     reg_exit_func(watchdog_cleanup);
541     saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
542
543     watchdog_module = module_register("Watchdog", MS_LOG, "mod-watchdog.help", NULL);
544     modcmd_register(watchdog_module, "addbad", cmd_addbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
545     modcmd_register(watchdog_module, "delbad", cmd_delbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
546     modcmd_register(watchdog_module, "setbad", cmd_setbad, 2, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
547     modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
548     modcmd_register(watchdog_module, "register", cmd_register, 2, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
549     modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED | MODCMD_REQUIRE_CHANNEL, "flags", "+helping", NULL);
550     message_register_table(msgtab);
551
552     return 1;
553 }
554
555 int
556 watchdog_finalize(void) {
557     dict_t conf_node;
558     const char *str;
559
560     str = "modules/watchdog";
561     if (!(conf_node = conf_get_data(str, RECDB_OBJECT))) {
562         log_module(MS_LOG, LOG_ERROR, "config node `%s' is missing or has wrong type.", str);
563         return 0;
564     }
565
566     str = database_get_data(conf_node, "nick", RECDB_QSTRING);
567     if (str) watchdog_conf.nick = str;
568     
569     str = database_get_data(conf_node, "modes", RECDB_QSTRING);
570     if (str) watchdog_conf.modes = str;
571     return 1;
572 }