src/nexus.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 23:54:03 +0200
changeset 48 4841f4398fd2
parent 26 aec062af155d
child 53 12d806823775
permissions -rw-r--r--
add irc_net_quit and signal handling

#include "irc_net.h"
#include "irc_log.h"
#include "signals.h"
#include "log.h"

#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <getopt.h>
#include <signal.h>

#include <event2/event.h>


#define DEFAULT_HOST "irc.fixme.fi"
#define DEFAULT_PORT "6667"
#define DEFAULT_PORT_SSL "6697"

enum option_code {
    _OPT_LOG_BEGIN      = 0x00ff,

    OPT_LOG_DATABASE,
    OPT_LOG_CHANNEL,
};

static struct option options[] = {
    {"help",            0,  NULL,   'h'                 },
    {"hostname",        1,  NULL,   'H'                 },
    {"port",            1,  NULL,   'P'                 },
    {"ssl",             0,  NULL,   'S'                 },
    {"log-database",    1,  NULL,   OPT_LOG_DATABASE    },
    {"log-channel",     1,  NULL,   OPT_LOG_CHANNEL     },
    {0,                 0,  0,      0                   },
};

void usage (const char *exe) 
{
    printf("Usage: %s [OPTIONS]\n", exe);
    printf("\n");
    printf(" --help / -h            display this message\n");
    printf(" --hostname / -H HOST   set hostname to connect to\n");
    printf(" --port / -P PORT       set service port to connect to\n");
    printf(" --ssl / -S             use SSL\n");
    printf(" --log-database         database connection string for logging\n");
    printf(" --log-channel          channel to log\n");
}

/**
 * Context for async nexus operation
 */
struct nexus_ctx {
    /** The libevent base */
    struct event_base *ev_base;

    /** The one IRC network */
    struct irc_net *net;
};

void on_sigint (evutil_socket_t sig, short what, void *arg)
{
    struct nexus_ctx *ctx = arg;

    (void) sig;
    (void) what;
    
    if (ctx->net && ctx->net->conn && !ctx->net->conn->quitting) {
        log_info("Quitting...");

        // quit it
        irc_net_quit(ctx->net, "Goodbye, cruel world ;(");

    } else {
        log_error("Aborting");
        
        // die
        if (ctx->net) {
            irc_net_destroy(ctx->net);
            ctx->net = NULL;
        }

        // exit
        event_base_loopexit(ctx->ev_base, NULL);
    }
}

int main (int argc, char **argv) 
{
    int opt, option_index;
    struct nexus_ctx ctx;
    struct signals *signals;
    struct error_info err;

    struct irc_net_info net_info = {
        .network                    = NULL,
        .hostname                   = DEFAULT_HOST,
        .service                    = DEFAULT_PORT,
        .use_ssl                    = false,
        .register_info              = {
            .nickname               = "SpBotDev",
            .username               = "spbot-dev",
            .realname               = "SpBot (development version)",
        }
    };

    struct irc_chan_info log_chan_info = {
        .channel                    = NULL, 
    };

    struct irc_log_info log_info = {
        .db_info                    = NULL,
        .channel                    = NULL,
    };

    bool port_default = true;
    
    // parse options
    while ((opt = getopt_long(argc, argv, "hH:P:S", options, &option_index)) != -1) {
        switch (opt) {
            case 'h':
                usage(argv[0]);
                return EXIT_SUCCESS;

            case 'H':
                net_info.hostname = optarg;
                break;
            
            case 'P':
                net_info.service = optarg;
                port_default = false;
                break;

            case 'S':
                net_info.use_ssl = true;

                if (port_default)
                    net_info.service = DEFAULT_PORT_SSL;

                break;
            
            case OPT_LOG_DATABASE:
                log_info.db_info = optarg;
                break;

            case OPT_LOG_CHANNEL:
                log_chan_info.channel = optarg;
                break;

            case '?':
                usage(argv[0]);
                return EXIT_FAILURE;
        }
    }

    // initialize libevent
    if ((ctx.ev_base = event_base_new()) == NULL)
        FATAL("event_base_new");
    
    // initialize signal handlers
    if ((ERROR_CODE(&err) = signals_create(&signals, ctx.ev_base)))
        FATAL("signals_create");

    // initialize sock module
    if (sock_init(ctx.ev_base, &err))
        FATAL_ERROR(&err, "sock_init");
    
    // the IRC network
    if (irc_net_create(&ctx.net, &net_info, &err))
        FATAL_ERROR(&err, "irc_net_create");

    // add our signal handlers
    if (
            (ERROR_CODE(&err) = signals_add(signals, SIGPIPE, &signals_ignore, signals))
        ||  (ERROR_CODE(&err) = signals_add(signals, SIGINT, &on_sigint, &ctx))
    )
        FATAL_ERROR(&err, "signals_add");
    
    // logging?
    if (log_info.db_info || log_chan_info.channel) {
        // get the channel
        if (log_chan_info.channel && (log_info.channel = irc_net_add_chan(ctx.net, &log_chan_info)) == NULL)
            FATAL("irc_net_add_chan");
        
        // init the irc_log module
        if ((ERROR_CODE(&err) = irc_log_init(ctx.ev_base, &log_info)))
            FATAL_ERROR(&err, "irc_log_init");
    }

    // run event loop
    if (event_base_dispatch(ctx.ev_base))
        FATAL("event_base_dispatch");
    
    // ok, no cleanup
    return 0;
}