src/nexus.c
author Tero Marttila <terom@fixme.fi>
Sun, 01 Mar 2009 02:02:48 +0200
changeset 22 c339c020fd33
parent 21 0911d0b828d4
child 23 542c73d07d3c
permissions -rw-r--r--
add missing irc_cmd.h, and modify line_proto/irc_conn to use log

#include "log.h"
#include "sock.h"
#include "irc_conn.h"

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

#include <event2/event.h>


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

static struct option options[] = {
    {"help",            0,  NULL,   'h' },
    {"hostname",        1,  NULL,   'H' },
    {"port",            1,  NULL,   'P' },
    {"ssl",             0,  NULL,   'S' },
    {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");
}

int main (int argc, char **argv) 
{
    int opt, option_index;
    struct event_base *ev_base;
    struct sock_stream *sock;
    struct irc_conn *conn;
    struct error_info err;

    const char *hostname = DEFAULT_HOST, *portname = DEFAULT_PORT;
    bool ssl = 0;
    struct irc_conn_config conn_config = {
        .nickname       = "SpBotDev",
        .username       = "spbot-dev",
        .realname       = "SpBot (development version)",
    };

    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':
                hostname = optarg;
                break;
            
            case 'P':
                portname = optarg;
                port_default = false;
                break;

            case 'S':
                ssl = true;

                if (port_default)
                    portname = DEFAULT_PORT_SSL;

                break;

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

    // initialize libevent
    if ((ev_base = event_base_new()) == NULL)
        FATAL("event_base_new");

    // initialize sock module
    if (sock_init(ev_base, &err))
        FATAL_ERROR(&err, "sock_init");

    // over-simplified connect
    if (ssl) {
        log_info("SSL connecting to [%s]:%s", hostname, portname);

        if (sock_ssl_connect(&sock, hostname, portname, &err))
            FATAL_ERROR(&err, "sock_ssl_connect(%s, %s)", hostname, portname);

    } else {
        log_info("connecting to [%s]:%s", hostname, portname);

        if (sock_tcp_connect(&sock, hostname, portname, &err))
            FATAL_ERROR(&err, "sock_tcp_connect(%s, %s)", hostname, portname);

    }

    log_info("connected, registering");

    // create the irc connection state
    if (irc_conn_create(&conn, sock, &conn_config, &err))
        FATAL_ERROR(&err, "irc_conn_create");

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