src/irc_net.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 18:11:44 +0200
changeset 37 4fe4a3c4496e
parent 28 9c1050bc8709
child 38 0c2e0cb46c3a
permissions -rw-r--r--
change irc_chan.state into bool fields, move irc_cmd implementation from irc_conn.c into irc_cmd.c, remove irc_conn arg from irc_cmd_handler, add irc_conn.nickname tracking, and handle irc_chan JOINs
#include "irc_net.h"
#include "log.h"

#include <stdlib.h>
#include <string.h>

static void irc_net_conn_registered (struct irc_conn *conn, void *arg)
{
    struct irc_net *net = arg;
    struct irc_chan *chan = NULL;
    err_t err;

    (void) conn;

    // join our channels
    TAILQ_FOREACH(chan, &net->channels, node) {
        if ((err = irc_chan_join(chan)))
            // XXX: fuck...
            FATAL_ERR(err, "irc_chan_join failed");
    }
}

/**
 * Our irc_conn_callbacks list
 */
struct irc_conn_callbacks _conn_callbacks = {
    .on_registered  = &irc_net_conn_registered,
};

/**
 * :nm JOIN <channel>
 */ 
static void irc_net_on_JOIN (const struct irc_line *line, void *arg)
{
    struct irc_net *net = arg;
    struct irc_chan *chan;

    // look up channel
    if ((chan = irc_net_get_chan(net, line->args[0])) == NULL) {
        log_warn("unkown channel: %s", line->args[0]);
        return;
    }

    // propagate
    irc_cmd_invoke(&chan->handlers, line);
}

/**
 * Our irc_cmd handler list
 */
static struct irc_cmd_handler _cmd_handlers[] = {
    {   "JOIN",     &irc_net_on_JOIN        },
    {   NULL,       NULL                    }
};

err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
{
    struct irc_net *net;
    struct sock_stream *sock = NULL;
    
    // allocate
    if ((net = calloc(1, sizeof(*net))) == NULL)
        return SET_ERROR(err, ERR_CALLOC);

    // initialize
    TAILQ_INIT(&net->channels);

    // XXX: over-simplified blocking connect
    if (info->use_ssl) {
        log_info("connecting to [%s]:%s using SSL", info->hostname, info->service);

        if (sock_ssl_connect(&sock, info->hostname, info->service, err))
            goto error;

    } else {
        log_info("connecting to [%s]:%s", info->hostname, info->service);

        if (sock_tcp_connect(&sock, info->hostname, info->service, err))
            goto error;

    }

    // create the irc connection state
    if (irc_conn_create(&net->conn, sock, &_conn_callbacks, net, err))
        goto error;

    // add our command handlers
    if ((ERROR_CODE(err) = irc_conn_add_cmd_handlers (net->conn, _cmd_handlers, net)))
        goto error;

    // register
    log_info("connected, registering");

    if ((ERROR_CODE(err) = irc_conn_register(net->conn, &info->register_info)))
        goto error;
    
    // ok
    *net_ptr = net;

    return SUCCESS;

error:
    // cleanup
    if (net->conn)
        // irc_conn takes care of the socket as well
        irc_conn_destroy(net->conn);
    
    else if (sock)
        // we need to clean up the socket ourself
        sock_stream_release(sock);
    
    // release our state
    free(net);

    return ERROR_CODE(err);
}

struct irc_chan* irc_net_add_chan (struct irc_net *net, const struct irc_chan_info *info)
{
    struct irc_chan *chan;
    struct error_info err;

    // create the new irc_chan struct
    if (irc_chan_create(&chan, net, info, &err))
        // XXX: we lose error info
        return NULL;
    
    // add to network list
    TAILQ_INSERT_TAIL(&net->channels, chan, node);
    
    // currently connected?
    if (net->conn && net->conn->registered) {
        // then join
        if ((ERROR_CODE(&err) = irc_chan_join(chan)))
            // XXX
            return NULL;
    }

    // ok, return
    return chan;
}

struct irc_chan* irc_net_get_chan (struct irc_net *net, const char *channel)
{
    struct irc_chan *chan = NULL;

    // look for it...
    TAILQ_FOREACH(chan, &net->channels, node) {
        if (strcasecmp(chan->info.channel, channel) == 0)
            // found it
            return chan;
    }

    // no such channel
    return NULL;

}