src/irc_log.c
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 23:22:57 +0200
branchmodules
changeset 57 ce1accba5fc7
parent 56 942370000450
child 61 4ba21936518a
permissions -rw-r--r--
slight cleanup to move module funcs to a 'struct module_funcs'
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
     1
#include "module.h"
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
     2
#include "irc_chan.h"
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
     3
#include "error.h"
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
#include "log.h"
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
     6
#include <stdlib.h>
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
     7
#include <string.h>
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
     8
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
     9
#include <event2/event.h>
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
// XXX: fix this err_t crap
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
#define LIB_ERR_H
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
#include <evsql.h>
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
/**
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    15
 * The irc_log module state
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
 */
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    17
struct irc_log_ctx {
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    18
    /** The nexus this module is loaded for */
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    19
    struct nexus *nexus;
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    20
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
    /** The database connection */
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
    struct evsql *db;
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    24
};
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    26
static void on_chan_msg (struct irc_chan *chan, const struct irc_nm *source, const char *message, void *arg)
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
{
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
    struct irc_log_ctx *ctx = arg;
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
26
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents: 23
diff changeset
    30
    (void) ctx;
aec062af155d add irc_chan module
Tero Marttila <terom@fixme.fi>
parents: 23
diff changeset
    31
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
    // log it! :P
45
71e65564afd2 remove irc_chan.state, modify irc_chan_callbacks.on_msg to take a irc_nm, improve error handling a bit further (up to irc_net now)
Tero Marttila <terom@fixme.fi>
parents: 38
diff changeset
    33
    log_debug("%s: %s: %s", source ? source->nickname : "???", irc_chan_name(chan), message);
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
}
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
38
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    36
static struct irc_chan_callbacks _chan_callbacks = {
0c2e0cb46c3a implement irc_chan_callbacks, and add on_msg
Tero Marttila <terom@fixme.fi>
parents: 37
diff changeset
    37
    .on_msg         = on_chan_msg,
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
};
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    40
static err_t irc_log_init (struct nexus *nexus, void **ctx_ptr, struct error_info *err)
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
{
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    42
    struct irc_log_ctx *ctx;
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    43
    
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    44
    // allocate
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    45
    if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    46
        return SET_ERROR(err, ERR_CALLOC);
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    47
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    48
    // initialize
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    49
    memset(ctx, 0, sizeof(*ctx));
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    50
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    51
    // store
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    52
    ctx->nexus = nexus;
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    53
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    54
    // ok
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    55
    *ctx_ptr = ctx;
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    56
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    57
    return SET_ERROR(err, SUCCESS);
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    58
}
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    59
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    60
static err_t irc_log_conf (void *mod_ctx, const char *name, char *value, struct error_info *err)
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    61
{
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    62
    struct irc_log_ctx *ctx = mod_ctx;
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    64
    if (strcmp(name, "db_info") == 0) {
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    65
        log_info("connect to database: %s", value);
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    67
        if ((ctx->db = evsql_new_pq(ctx->nexus->ev_base, value, NULL, NULL)) == NULL)
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
           return ERR_EVSQL_NEW_PQ;
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    69
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    70
    } else if (strcmp(name, "channel") == 0) {
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    71
        const char *network = strsep(&value, "/");
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    72
        const char *channel = value;
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    73
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    74
        struct irc_chan *chan;
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    75
        
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    76
        // kill missing tokens
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    77
        if (!network || !channel) 
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    78
            RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "invalid '<network>/<channel>' value");
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    79
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    80
        // get the channel?
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    81
        if ((chan = irc_client_get_chan(ctx->nexus->client, network, channel)) == NULL)
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    82
            RETURN_SET_ERROR_STR(err, ERR_MODULE_CONF, "unknown channel name");
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    83
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    84
        // add channel callbacks
56
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    85
        if ((ERROR_CODE(err) = irc_chan_add_callbacks(chan, &_chan_callbacks, ctx)))
942370000450 compiling, working, but still ugly module code
Tero Marttila <terom@fixme.fi>
parents: 55
diff changeset
    86
            return ERROR_CODE(err);
55
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    87
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    88
    } else {
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    89
        return -1;
6f7f6ae729d0 '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
Tero Marttila <terom@fixme.fi>
parents: 45
diff changeset
    90
23
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    91
    }
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    92
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    93
    // ok
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    94
    return SUCCESS;
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    95
}
542c73d07d3c add a simple irc_log module (with evsql code) that joins a channel and log_info's PRIVMSGs
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    96
57
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    97
/**
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    98
 * The module function table
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
    99
 */
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   100
struct module_funcs irc_log_funcs = {
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   101
    .init       = &irc_log_init,
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   102
    .conf       = &irc_log_conf,
ce1accba5fc7 slight cleanup to move module funcs to a 'struct module_funcs'
Tero Marttila <terom@fixme.fi>
parents: 56
diff changeset
   103
};