src/irc_log.c
changeset 69 6f298b6e0d5f
parent 68 591a574f390e
child 70 a9a4c5e6aa30
--- a/src/irc_log.c	Mon Mar 16 21:47:18 2009 +0200
+++ b/src/irc_log.c	Mon Mar 16 22:06:39 2009 +0200
@@ -5,7 +5,6 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h> //<<< XXX: remove
 
 #include <event2/event.h>
 #include <evsql.h>
@@ -220,6 +219,62 @@
     .on_msg             = NULL,
 };
 
+/**
+ * Release resources associated with the given irc_log_chan without doing any clean shutdown stuff
+ */
+static void irc_log_chan_destroy (struct irc_log_chan *chan_ctx)
+{
+    // remove any handlers/callbacks
+    irc_cmd_remove(&chan_ctx->chan->handlers, _chan_cmd_handlers, chan_ctx);
+    irc_chan_remove_callbacks(chan_ctx->chan, &_chan_callbacks, chan_ctx);
+
+    // free ourselves
+    free(chan_ctx);
+}
+
+/**
+ * Begin logging the given channel
+ */
+static err_t irc_log_chan (struct irc_log_ctx *ctx, struct irc_chan *chan, struct error_info *err)
+{
+    struct irc_log_chan *chan_ctx;
+
+    // alloc
+    if ((chan_ctx = calloc(1, sizeof(*chan_ctx))) == NULL)
+        return SET_ERROR(err, ERR_CALLOC);
+
+    // store
+    chan_ctx->ctx = ctx;
+    chan_ctx->chan = chan;
+
+    // add low-level handlers
+    if ((ERROR_CODE(err) = irc_cmd_add(&chan_ctx->chan->handlers, _chan_cmd_handlers, chan_ctx)))
+        goto error;
+
+    // add channel callbacks
+    if ((ERROR_CODE(err) = irc_chan_add_callbacks(chan_ctx->chan, &_chan_callbacks, chan_ctx)))
+        goto error;
+    
+    // log an OPEN message
+    // XXX: move this to when we first JOIN the channel
+    if ((ERROR_CODE(err) = irc_log_event(ctx, chan_ctx->chan, NULL, "OPEN", NULL, NULL)))
+        goto error;
+
+    // ok
+    log_info("logging channel %s:%s", chan_ctx->chan->net->info.network, irc_chan_name(chan_ctx->chan));
+
+    return SUCCESS;
+
+error:
+    // cleanup
+    irc_log_chan_destroy(chan_ctx);
+    
+    return ERROR_CODE(err);
+}
+
+/**
+ * Allocate and initialize an irc_log_ctx. This doesn't do very much, the real magic happens in irc_log_conf.
+ */
 static err_t irc_log_init (struct nexus *nexus, void **ctx_ptr, struct error_info *err)
 {
     struct irc_log_ctx *ctx;
@@ -228,9 +283,6 @@
     if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
         return SET_ERROR(err, ERR_CALLOC);
 
-    // initialize
-    memset(ctx, 0, sizeof(*ctx));
-
     // store
     ctx->nexus = nexus;
 
@@ -239,7 +291,7 @@
     // ok
     *ctx_ptr = ctx;
 
-    return SET_ERROR(err, SUCCESS);
+    return SUCCESS;
 }
 
 /**
@@ -276,8 +328,7 @@
 static err_t irc_log_conf_channel (struct irc_log_ctx *ctx, char *value, struct error_info *err)
 {
     const char *network, *channel;
-    
-    struct irc_log_chan *chan_ctx;
+    struct irc_chan *chan;
     
     // parse required args
     if ((network = strsep(&value, ":")) == NULL)
@@ -294,42 +345,16 @@
     if (!ctx->db)
         RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "irc_log.channel used without any irc_log.db_info");
 
-    // alloc
-    if ((chan_ctx = calloc(1, sizeof(*chan_ctx))) == NULL)
-        return SET_ERROR(err, ERR_CALLOC);
-
-    // store
-    chan_ctx->ctx = ctx;
-
     // get the channel?
-    if ((chan_ctx->chan = irc_client_get_chan(ctx->nexus->client, network, channel)) == NULL)
-        JUMP_SET_ERROR_STR(err, ERR_MODULE_CONF, "unknown channel name");
+    if ((chan = irc_client_get_chan(ctx->nexus->client, network, channel)) == NULL)
+        RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "unknown channel name");
 
-    // add low-level handlers
-    if ((ERROR_CODE(err) = irc_cmd_add(&chan_ctx->chan->handlers, _chan_cmd_handlers, chan_ctx)))
-        goto error;
-
-    // add channel callbacks
-    if ((ERROR_CODE(err) = irc_chan_add_callbacks(chan_ctx->chan, &_chan_callbacks, chan_ctx)))
-        goto error;
-    
-    // log an OPEN message
-    // XXX: move this to when we first JOIN the channel
-    if ((ERROR_CODE(err) = irc_log_event(ctx, chan_ctx->chan, NULL, "OPEN", NULL, NULL)))
-        goto error;
+    // begin logging it
+    if (irc_log_chan(ctx, chan, err))
+        return ERROR_CODE(err);
     
     // ok
-    log_info("logging channel %s:%s", chan_ctx->chan->net->info.network, irc_chan_name(chan_ctx->chan));
-
     return SUCCESS;
-
-error:
-    // XXX: remove callbacks
-    assert(!chan_ctx->chan);
-
-    free(chan_ctx);
-
-    return ERROR_CODE(err);    
 }
 
 /**