src/irc_log.c
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 01:17:22 +0200
branchmodules
changeset 55 6f7f6ae729d0
parent 45 71e65564afd2
child 56 942370000450
permissions -rw-r--r--
'working' modules code, and convert irc_log to use said interface, but we've hit the limits on our Makefile, and the compiled module doesn't really work
#include "irc_log.h"
#include "log.h"

#include <string.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,
};

void* irc_log_init (struct modules *modules, struct error_info *err)
{
    struct irc_log_ctx *ctx;

    (void) modules;
    (void) err;
        
    // XXX: static pointer
    ctx = &_ctx;

    // ok
    return ctx;
}

err_t irc_log_conf (struct module *module, const char *name, char *value)
{
    struct irc_log_ctx *ctx = module->ctx;
    struct nexus *nexus = module->modules->nexus;
    err_t err;

    if (strcmp(name, "db_info") == 0) {
        log_info("connect to database: %s", value);

        if ((ctx->db = evsql_new_pq(nexus->ev_base, value, NULL, NULL)) == NULL)
           return ERR_EVSQL_NEW_PQ;

    } else if (strcmp(name, "channel") == 0) {
        const char *network = strsep(&value, ":");
        const char *channel = value;

        struct irc_chan *chan;
        
        // kill missing tokens
        if (!network || !channel) 
            // XXX: need to fix the error crap
            return -1;

        // get the channel?
        if ((chan = irc_client_get_chan(nexus->client, network, channel)) == NULL)
            return -1;

        // add channel callbacks
        if ((err = irc_chan_add_callbacks(chan, &_chan_callbacks, ctx)))
            return err;

    } else {
        return -1;

    }

    // ok
    return SUCCESS;
}