src/irc_cmd.c
author Tero Marttila <terom@fixme.fi>
Wed, 27 May 2009 23:57:48 +0300
branchnew-lib-errors
changeset 217 7728d6ec3abf
parent 171 b54f393c3df0
permissions -rw-r--r--
nexus.c compiles
#include "irc_cmd.h"

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

void irc_cmd_init (struct irc_cmd_handlers *handlers)
{
    CHAIN_INIT(handlers);
}

err_t irc_cmd_add (struct irc_cmd_handlers *handlers, const struct irc_cmd_handler list[], void *arg)
{
    struct irc_cmd_table *table;

    // alloc/add
    if ((table = CHAIN_ADD_TAIL(handlers)) == NULL)
        return ERR_MEM;

    // store
    table->list = list;
    table->arg = arg;

    // ok
    return SUCCESS;
}

void irc_cmd_invoke (struct irc_cmd_handlers *handlers, const struct irc_line *line)
{
    struct irc_cmd_table *table;
    const struct irc_cmd_handler *handler;
    
    CHAIN_FOREACH_SAFE(handlers, table) {
        // look up appropriate handler
        for (handler = table->list; 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(line, table->arg);

                // ...only one per chain
                break;
            }
        }
    }
}

void irc_cmd_remove (struct irc_cmd_handlers *handlers, const struct irc_cmd_handler list[], void *arg)
{
    struct irc_cmd_table *table;

    // delete all matching items
    CHAIN_DELETE_WHICH(handlers, table, table->list == list && table->arg == arg);
}

void irc_cmd_clear (struct irc_cmd_handlers *handlers)
{
    CHAIN_CLEAR(handlers);
}