src/irc_log.c
branchmodules
changeset 57 ce1accba5fc7
parent 56 942370000450
child 61 4ba21936518a
equal deleted inserted replaced
56:942370000450 57:ce1accba5fc7
     1 #include "irc_log.h"
     1 #include "module.h"
       
     2 #include "irc_chan.h"
       
     3 #include "error.h"
     2 #include "log.h"
     4 #include "log.h"
     3 
     5 
       
     6 #include <stdlib.h>
     4 #include <string.h>
     7 #include <string.h>
     5 
     8 
       
     9 #include <event2/event.h>
     6 // XXX: fix this err_t crap
    10 // XXX: fix this err_t crap
     7 #define LIB_ERR_H
    11 #define LIB_ERR_H
     8 #include <evsql.h>
    12 #include <evsql.h>
     9 
    13 
    10 /**
    14 /**
    11  * The core irc_log state
    15  * The irc_log module state
    12  */
    16  */
    13 static struct irc_log_ctx {
    17 struct irc_log_ctx {
    14     /** The nexus this module is loaded for */
    18     /** The nexus this module is loaded for */
    15     struct nexus *nexus;
    19     struct nexus *nexus;
    16 
    20 
    17     /** The database connection */
    21     /** The database connection */
    18     struct evsql *db;
    22     struct evsql *db;
    19 
    23 
    20 } _ctx;
    24 };
    21 
    25 
    22 static void on_chan_msg (struct irc_chan *chan, const struct irc_nm *source, const char *message, void *arg)
    26 static void on_chan_msg (struct irc_chan *chan, const struct irc_nm *source, const char *message, void *arg)
    23 {
    27 {
    24     struct irc_log_ctx *ctx = arg;
    28     struct irc_log_ctx *ctx = arg;
    25 
    29 
    31 
    35 
    32 static struct irc_chan_callbacks _chan_callbacks = {
    36 static struct irc_chan_callbacks _chan_callbacks = {
    33     .on_msg         = on_chan_msg,
    37     .on_msg         = on_chan_msg,
    34 };
    38 };
    35 
    39 
    36 void* irc_log_init (struct modules *modules, struct error_info *err)
    40 static err_t irc_log_init (struct nexus *nexus, void **ctx_ptr, struct error_info *err)
    37 {
    41 {
    38     struct irc_log_ctx *ctx;
    42     struct irc_log_ctx *ctx;
    39 
    43     
    40     (void) modules;
    44     // allocate
    41     (void) err;
    45     if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
    42         
    46         return SET_ERROR(err, ERR_CALLOC);
    43     // XXX: static pointer
       
    44     ctx = &_ctx;
       
    45 
    47 
    46     // initialize
    48     // initialize
    47     memset(ctx, 0, sizeof(*ctx));
    49     memset(ctx, 0, sizeof(*ctx));
    48 
    50 
    49     // store
    51     // store
    50     ctx->nexus = modules->nexus;
    52     ctx->nexus = nexus;
    51 
    53 
    52     // ok
    54     // ok
    53     return ctx;
    55     *ctx_ptr = ctx;
       
    56 
       
    57     return SET_ERROR(err, SUCCESS);
    54 }
    58 }
    55 
    59 
    56 err_t irc_log_conf (void *mod_ctx, const char *name, char *value, struct error_info *err)
    60 static err_t irc_log_conf (void *mod_ctx, const char *name, char *value, struct error_info *err)
    57 {
    61 {
    58     struct irc_log_ctx *ctx = mod_ctx;
    62     struct irc_log_ctx *ctx = mod_ctx;
    59 
    63 
    60     if (strcmp(name, "db_info") == 0) {
    64     if (strcmp(name, "db_info") == 0) {
    61         log_info("connect to database: %s", value);
    65         log_info("connect to database: %s", value);
    88 
    92 
    89     // ok
    93     // ok
    90     return SUCCESS;
    94     return SUCCESS;
    91 }
    95 }
    92 
    96 
       
    97 /**
       
    98  * The module function table
       
    99  */
       
   100 struct module_funcs irc_log_funcs = {
       
   101     .init       = &irc_log_init,
       
   102     .conf       = &irc_log_conf,
       
   103 };