src/irc_log.c
author Tero Marttila <terom@fixme.fi>
Sun, 08 Mar 2009 17:17:37 +0200
changeset 23 542c73d07d3c
child 26 aec062af155d
permissions -rw-r--r--
add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
#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_PRIVMSG (struct irc_conn *conn, const struct irc_line *line, void *arg)
{
    struct irc_log_ctx *ctx = arg;

    // log it! :P
    log_debug("%s: %s: %s", line->prefix, line->args[0], line->args[1]);
}

static struct irc_cmd_handler _cmd_handlers[] = {
    {   "PRIVMSG",  &on_PRIVMSG     },
    {   NULL,       NULL            }
};

err_t irc_log_init (struct event_base *ev_base, const char *db_info, struct irc_conn *irc, const char *channel)
{
    struct irc_log_ctx *ctx = &_ctx;
    err_t err;

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

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

        // join the channel
        if ((err = irc_conn_JOIN(irc, channel)))
            return err;
    }

    // register for events
    if ((err = irc_conn_register_handler_chain(irc, _cmd_handlers, ctx)))
        return err;

    // ok
    return SUCCESS;
}