src/irc_conn.c
author Tero Marttila <terom@fixme.fi>
Thu, 12 Mar 2009 22:50:08 +0200
changeset 45 71e65564afd2
parent 39 a4891d71aca9
child 47 7d4094eb3117
permissions -rw-r--r--
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)
#include "irc_conn.h"
#include "irc_cmd.h"
#include "irc_proto.h"
#include "log.h"

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

/**
 * Handle an async error on this IRC connection that we could not recover from any other way, the protocol is now dead,
 * and should be considered as destroyed after this returns.
 *
 * For conveniance, this returns the ERROR_CODE
 */
static err_t irc_conn_set_error (struct irc_conn *conn, struct error_info *err)
{
    // notify user callback
    conn->callbacks.on_error(conn, err, conn->cb_arg);

    return ERROR_CODE(err);
}


/**
 * Update irc_conn.nickname
 */
static err_t irc_conn_set_nickname (struct irc_conn *conn, const char *nickname)
{
    struct error_info err;

    // strdup
    if ((conn->nickname = strdup(nickname)) == NULL) {
        SET_ERROR(&err, ERR_STRDUP);

        // notify
        return irc_conn_set_error(conn, &err);
    }

    // ok
    return SUCCESS;
}

/**
 * 001 <nick> :Welcome to the Internet Relay Network <nick>!<user>@<host>
 */
static void on_RPL_WELCOME (const struct irc_line *line, void *arg)
{
    struct irc_conn *conn = arg;

    // update state
    conn->registering = false;
    conn->registered = true;
    
    // set our real nickname from the message target
    if (irc_conn_set_nickname(conn, line->args[0]))
        return;

    // trigger callback
    if (conn->callbacks.on_registered)
        conn->callbacks.on_registered(conn, conn->cb_arg);
}

/**
 * PING <server1> [ <server2> ]
 *
 * Send a 'PONG <server1>` reply right away.
 */ 
static void on_PING (const struct irc_line *line, void *arg)
{
    struct irc_conn *conn = arg;

    // just reply
    irc_conn_PONG(conn, line->args[0]);
}

/**
 * NICK <nickname>
 *
 * If the prefix is us, then update our nickname
 */
static void on_NICK (const struct irc_line *line, void *arg)
{
    struct irc_conn *conn = arg;
    char nickname[IRC_NICK_MAX];

    // parse nickname, ignoring errors
    if (irc_prefix_parse_nick(line->prefix, nickname)) {
        log_warn("invalid prefix: %s", line->prefix);
        return;
    }

    // ignore if it's not us
    if (irc_cmp_nick(nickname, conn->nickname))
        return;

    // update our nickname
    irc_conn_set_nickname(conn, nickname);
}

/**
 * Our command handlers
 */
static struct irc_cmd_handler _cmd_handlers[] = {
    {   IRC_RPL_WELCOME,    &on_RPL_WELCOME     },
    {   "PING",             &on_PING            },
    {   "NICK",             &on_NICK            },
    {   NULL,               NULL,               },
};

/**
 * Incoming line handler
 */
void irc_conn_on_line (char *line_buf, void *arg) 
{
    struct irc_conn *conn = arg;
    struct irc_line line;
    int err;
    
    // log
    log_debug("%s", line_buf);

    // parse
    if ((err = irc_line_parse(&line, line_buf))) {
        log_warn("invalid line: %s: %s\n", line_buf, error_name(err));
        return;
    }

    // invoke command handlers
    irc_cmd_invoke(&conn->handlers, &line);
}

/**
 * Transport failed
 */
void irc_conn_on_error (struct error_info *err, void *arg)
{
    struct irc_conn *conn = arg;

    // log
    log_err_info(err, "line_proto error");
    
    // trash ourselves
    irc_conn_set_error(conn, err);
}

static struct line_proto_callbacks _lp_callbacks = {
    .on_line        = &irc_conn_on_line,
    .on_error       = &irc_conn_on_error,
};

err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_callbacks *callbacks, 
        void *cb_arg, struct error_info *err)
{
    struct irc_conn *conn;

    // alloc new state struct
    if ((conn = calloc(1, sizeof(struct irc_conn))) == NULL)
        return SET_ERROR(err, ERR_CALLOC);

    // init state
    conn->callbacks = *callbacks;
    conn->cb_arg = cb_arg;

    // initialize command handlers
    irc_cmd_init(&conn->handlers);
    
    // add the core handlers 
    if ((ERROR_CODE(err) = irc_conn_add_cmd_handlers(conn, _cmd_handlers, conn)))
        goto error;

    // create the line_proto, with our on_line handler
    if (line_proto_create(&conn->lp, sock, IRC_LINE_MAX * 1.5, &_lp_callbacks, conn, err))
        goto error;

    // ok
    *conn_ptr = conn;

    return SUCCESS;

error:
    // release
    irc_conn_destroy(conn);

    return ERROR_CODE(err);    
}

void irc_conn_destroy (struct irc_conn *conn)
{
    // release the line_proto
    if (conn->lp)
        line_proto_release(conn->lp);
    
    // free the command handlers
    irc_cmd_free(&conn->handlers);

    // free the irc_conn itself
    free(conn);
}

err_t irc_conn_add_cmd_handlers (struct irc_conn *conn, struct irc_cmd_handler *handlers, void *arg)
{
    // use the irc_cmd stuff
    return irc_cmd_add(&conn->handlers, handlers, arg);
}

err_t irc_conn_register (struct irc_conn *conn, const struct irc_conn_register_info *info)
{
    err_t err;

    // assert state
    if (conn->registering || conn->registered)
        // XXX: stupid error code
        return ERR_IRC_CONN_REGISTER_STATE;

    // send the initial messages
    if (
            (err = irc_conn_NICK(conn, info->nickname))
        ||  (err = irc_conn_USER(conn, info->username, info->realname))
    )
        return err;

    // set state
    conn->registering = true;
    
    // ok
    return SUCCESS;
}

err_t irc_conn_send (struct irc_conn *conn, const struct irc_line *line)
{
    char line_buf[IRC_LINE_MAX + 2];
    err_t err;
    int ret;

    // format
    if ((err = irc_line_build(line, line_buf)))
        return err;
    
    // log
    log_debug("%s", line_buf);

    // add CRLF
    strcat(line_buf, "\r\n");

    // send using line_proto
    // XXX: ignore output-buffering
    return (ret = line_proto_send(conn->lp, line_buf)) < 0 ? -ret : SUCCESS;
}

err_t irc_conn_NICK (struct irc_conn *conn, const char *nickname)
{
    // NICK <nickname>
    struct irc_line line = {
        NULL, "NICK", { nickname, NULL }
    };
    
    return irc_conn_send(conn, &line);
}

err_t irc_conn_USER (struct irc_conn *conn, const char *username, const char *realname)
{
    // USER <user> <mode> <unused> <realname>
    struct irc_line line = {
        NULL, "USER", { username, "0", "*", realname, NULL }
    };
    
    return irc_conn_send(conn, &line);
}

err_t irc_conn_PONG (struct irc_conn *conn, const char *target)
{
    // PONG <server> [ <server2> ]
    // params are actually the wrong way around now, but nobody cares
    struct irc_line line = {
        NULL, "PONG", { target, NULL }
    };

    return irc_conn_send(conn, &line);
}

err_t irc_conn_JOIN (struct irc_conn *conn, const char *channel)
{
    // JOIN ( <channel> *( "," <channel> ) [ <key> *( "," <key> ) ] ) / "0"
    struct irc_line line = {
        NULL, "JOIN", { channel, NULL }
    };

    return irc_conn_send(conn, &line);
}