src/irc_log.c
changeset 23 542c73d07d3c
child 26 aec062af155d
equal deleted inserted replaced
22:c339c020fd33 23:542c73d07d3c
       
     1 #include "irc_log.h"
       
     2 #include "log.h"
       
     3 
       
     4 // XXX: fix this err_t crap
       
     5 #define LIB_ERR_H
       
     6 #include <evsql.h>
       
     7 
       
     8 /**
       
     9  * The core irc_log state
       
    10  */
       
    11 static struct irc_log_ctx {
       
    12     /** The database connection */
       
    13     struct evsql *db;
       
    14 
       
    15 } _ctx;
       
    16 
       
    17 static void on_PRIVMSG (struct irc_conn *conn, const struct irc_line *line, void *arg)
       
    18 {
       
    19     struct irc_log_ctx *ctx = arg;
       
    20 
       
    21     // log it! :P
       
    22     log_debug("%s: %s: %s", line->prefix, line->args[0], line->args[1]);
       
    23 }
       
    24 
       
    25 static struct irc_cmd_handler _cmd_handlers[] = {
       
    26     {   "PRIVMSG",  &on_PRIVMSG     },
       
    27     {   NULL,       NULL            }
       
    28 };
       
    29 
       
    30 err_t irc_log_init (struct event_base *ev_base, const char *db_info, struct irc_conn *irc, const char *channel)
       
    31 {
       
    32     struct irc_log_ctx *ctx = &_ctx;
       
    33     err_t err;
       
    34 
       
    35     // open the database connection
       
    36     if (db_info) {
       
    37         log_info("connect to database: %s", db_info);
       
    38 
       
    39         if ((ctx->db = evsql_new_pq(ev_base, db_info, NULL, NULL)) == NULL)
       
    40            return ERR_EVSQL_NEW_PQ;
       
    41     }
       
    42     
       
    43     if (channel) {
       
    44         log_info("join channel: %s", channel);
       
    45 
       
    46         // join the channel
       
    47         if ((err = irc_conn_JOIN(irc, channel)))
       
    48             return err;
       
    49     }
       
    50 
       
    51     // register for events
       
    52     if ((err = irc_conn_register_handler_chain(irc, _cmd_handlers, ctx)))
       
    53         return err;
       
    54 
       
    55     // ok
       
    56     return SUCCESS;
       
    57 }
       
    58