src/irc_log.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 22:50:08 +0200
changeset 45 71e65564afd2
parent 38 0c2e0cb46c3a
child 55 6f7f6ae729d0
permissions -rw-r--r--
remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
#include "irc_log.h"
#include "log.h"

// XXX: fix this err_t crap
#define LIB_ERR_H
#include <evsql.h>

/**
 * The core irc_log state
 */
static struct irc_log_ctx {
    /** The database connection */
    struct evsql *db;

} _ctx;

static void on_chan_msg (struct irc_chan *chan, const struct irc_nm *source, const char *message, void *arg)
{
    struct irc_log_ctx *ctx = arg;

    (void) ctx;

    // log it! :P
    log_debug("%s: %s: %s", source ? source->nickname : "???", irc_chan_name(chan), message);
}

static struct irc_chan_callbacks _chan_callbacks = {
    .on_msg         = on_chan_msg,
};

err_t irc_log_init (struct event_base *ev_base, const struct irc_log_info *info)
{
    struct irc_log_ctx *ctx = &_ctx;
    err_t err;

    // open the database connection
    if (info->db_info) {
        log_info("connect to database: %s", info->db_info);

        if ((ctx->db = evsql_new_pq(ev_base, info->db_info, NULL, NULL)) == NULL)
           return ERR_EVSQL_NEW_PQ;
    }
    
    if (info->channel) {
        log_info("log channel: %s", irc_chan_name(info->channel));
    }

    // add channel callbacks
    if ((err = irc_chan_add_callbacks(info->channel, &_chan_callbacks, ctx)))
        goto error;

    // ok
    return SUCCESS;

error:
    // XXX: cleanup
    
    return err;
}