src/irc_log.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 18:11:44 +0200
changeset 37 4fe4a3c4496e
parent 29 3f0f2898fea3
child 38 0c2e0cb46c3a
permissions -rw-r--r--
change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
#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 (const struct irc_line *line, void *arg)
{
    struct irc_log_ctx *ctx = arg;

    (void) ctx;

    // 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 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));
    }

    // register for events
    // XXX: need irc_chan API for this
    if ((err = irc_conn_add_cmd_handlers(info->channel->net->conn, _cmd_handlers, ctx)))
        return err;

    // ok
    return SUCCESS;
}