added chanlist to mod-watchdog.c
authorpk910 <philipp@zoelle1.de>
Wed, 20 Jul 2011 21:44:54 +0000 (23:44 +0200)
committerpk910 <philipp@zoelle1.de>
Wed, 20 Jul 2011 22:00:00 +0000 (00:00 +0200)
src/mod-watchdog.c

index 1d70cf6f1746d774f173a22af0ee0a63eb51eb03..91985b534c01e6d0d382ec2a5aae7f4cb139766b 100644 (file)
 #define KEY_BADWORD_MASK "mask"
 #define KEY_BADWORD_TRIGGERED "count"
 #define KEY_BADWORD_ACTION "action"
+#define KEY_CHANNELS "channel"
 
 static const struct message_entry msgtab[] = {
     { "WDMSG_NULL", "null." },
     { NULL, NULL }
 };
 
+DECLARE_LIST(shitList, struct badword*);
+DEFINE_LIST(shitList, struct badword*)
+
 struct badword {
     char *badword_mask;
     unsigned int triggered : 29;
     unsigned int action : 3;
 };
 
-DECLARE_LIST(shitList, struct badword*);
-DEFINE_LIST(shitList, struct badword*)
+struct watchdog_channel {
+    struct chanNode *channel;
+    //struct shitList *shitlist;
+};
 
 /* badword.action fields */
 #define BADACTION_KICK   1
@@ -68,6 +74,7 @@ struct userNode *watchdog;
 static struct module *watchdog_module;
 static struct service *watchdog_service;
 static struct shitList shitlist;
+static dict_t chanlist;
 static struct log_type *MS_LOG;
 
 
@@ -95,6 +102,18 @@ static MODCMD_FUNC(cmd_listbad)
     return 1;
 }
 
+static MODCMD_FUNC(cmd_register)
+{
+    //to be continued...
+    return 1;
+}
+
+static MODCMD_FUNC(cmd_unregister)
+{
+    //to be continued...
+    return 1;
+}
+
 static void
 watchdog_channel_message(struct userNode *user, struct chanNode *chan, const char *text, struct userNode *bot, unsigned int is_notice)
 {
@@ -125,6 +144,29 @@ delete_badword(struct badword *badword)
     free(badword);
 }
 
+static struct watchdog_channel*
+add_channel(const char *name)
+{
+    struct watchdog_channel *wc;
+
+    wc = calloc(1, sizeof(*wc));
+    if (!wc)
+        return NULL;
+
+    wc->channel = AddChannel(name, now, NULL, NULL);
+    AddChannelUser(watchdog, wc->channel)->modes |= MODE_CHANOP;
+    dict_insert(chanlist, wc->channel->name, wc);
+    return wc;
+}
+
+static void
+free_chanlist_entry(void *data)
+{
+    struct watchdog_channel *wc = data;
+    
+    free(wc);
+}
+
 static void
 watchdog_conf_read(void)
 {
@@ -167,12 +209,29 @@ watchdog_saxdb_read_shitlist(const char *name, void *data, UNUSED_ARG(void *extr
         return 0;
 }
 
+static int
+watchdog_saxdb_read_chanlist(const char *name, void *data, UNUSED_ARG(void *extra))
+{
+    struct record_data *rd = data;
+
+     if (rd->type == RECDB_OBJECT) {
+        dict_t obj = GET_RECORD_OBJECT(rd);
+        /* nothing in here, yet */
+
+        add_channel(name);
+        return 1;
+    } else
+        return 0;
+}
+
 static int
 watchdog_saxdb_read(struct dict *db)
 {
     struct dict *object;
     if ((object = database_get_data(db, KEY_BADWORDS, RECDB_OBJECT)))
         dict_foreach(object, watchdog_saxdb_read_shitlist, NULL);
+    if ((object = database_get_data(db, KEY_CHANNELS, RECDB_OBJECT)))
+        dict_foreach(object, watchdog_saxdb_read_chanlist, NULL);
     return 1;
 }
 
@@ -182,6 +241,7 @@ watchdog_saxdb_write(struct saxdb_context *ctx)
     struct badword *badword;
     char str[17];
     unsigned int id = 0, ii;
+    dict_iterator_t it;
 
     saxdb_start_record(ctx, KEY_BADWORDS, 1);
     for (ii = 0; ii < shitlist.used; ++ii) {
@@ -195,6 +255,17 @@ watchdog_saxdb_write(struct saxdb_context *ctx)
     }
     saxdb_end_record(ctx);
 
+    if (dict_size(chanlist)) {
+        saxdb_start_record(ctx, KEY_CHANNELS, 1);
+        for (it = dict_first(chanlist); it; it = iter_next(it)) {
+            struct watchdog_channel *wc = iter_data(it);
+            saxdb_start_record(ctx, wc->channel->name, 0);
+            //anything else?
+            saxdb_end_record(ctx);
+        }
+        saxdb_end_record(ctx);
+    }
+    
     return 0;
 }
 
@@ -204,13 +275,20 @@ watchdog_cleanup(void)
     while (shitlist.used)
         delete_badword(shitlist.list[0]);
     shitList_clean(&shitlist);
+    dict_delete(chanlist);
 }
 
 int
 watchdog_init(void)
 {
     MS_LOG = log_register_type("Watchdog", "file:watchdog.log");
+    
     shitList_init(&shitlist);
+    /* set up chanlist dict */
+    dict_delete(chanlist);
+    chanlist = dict_new();
+    dict_set_free_data(chanlist, free_chanlist_entry);
+    
     conf_register_reload(watchdog_conf_read);
     reg_exit_func(watchdog_cleanup);
     saxdb_register("Watchdog", watchdog_saxdb_read, watchdog_saxdb_write);
@@ -228,6 +306,8 @@ watchdog_init(void)
     modcmd_register(watchdog_module, "delbad", cmd_delbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
     modcmd_register(watchdog_module, "editbad", cmd_editbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
     modcmd_register(watchdog_module, "listbad", cmd_listbad, 1, MODCMD_REQUIRE_AUTHED, "flags", "+oper", NULL);
+    modcmd_register(watchdog_module, "register", cmd_register, 1, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
+    modcmd_register(watchdog_module, "unregister", cmd_unregister, 1, MODCMD_REQUIRE_AUTHED, "flags", "+helping", NULL);
     message_register_table(msgtab);
 
     return 1;