src/irc_conn.c
author Tero Marttila <terom@fixme.fi>
Sun, 01 Mar 2009 01:48:14 +0200
changeset 21 0911d0b828d4
parent 20 d9c4c2980a0d
child 22 c339c020fd33
permissions -rw-r--r--
add basic log.c module

#include "irc_conn.h"
#include "irc_cmd.h"

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

/*
 * "Welcome to the Internet Relay Network <nick>!<user>@<host>"
 */
static void on_RPL_WELCOME (struct irc_conn *conn, const struct irc_line *line)
{
    // update state
    conn->registered = true;
}

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

/*
 * Our command handlers
 */
struct irc_cmd_handler {
    /* The command name */
    const char *command;

    /* The handler function */
    void (*func) (struct irc_conn *conn, const struct irc_line *line);

} _cmd_handlers[] = {
    { IRC_RPL_WELCOME,  on_RPL_WELCOME      },
    { "PING",           on_PING             },
    { NULL,             NULL,               },
};

void irc_conn_on_line (char *line_buf, void *arg) 
{
    struct irc_conn *conn = arg;
    struct irc_line line;
    struct irc_cmd_handler *handler;
    int err;
    
    // log
    printf("<<< %s\n", line_buf);

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

    // look up appropriate handler
    for (handler = _cmd_handlers; handler->command; handler++) {
        // the command is alpha-only, so normal case-insensitive cmp is fine
        if (strcasecmp(handler->command, line.command) == 0) {
            // invoke the func
            handler->func(conn, &line);
            break;
        }
    }
}

err_t irc_conn_create (struct irc_conn **conn_ptr, struct sock_stream *sock, const struct irc_conn_config *config, 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);

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

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

    // ok
    *conn_ptr = conn;

    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;

    // format
    if ((err = irc_line_build(line, line_buf)))
        return err;
    
    // log
    printf(">>> %s\n", line_buf);

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

    // send using line_proto
    // XXX: ignore output-buffering
    return (err = line_proto_send(conn->lp, line_buf)) < 0 ? err : 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> ]
    // XXX: 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);
}