src/irc_cmd.c
author Tero Marttila <terom@fixme.fi>
Sun, 15 Mar 2009 23:01:12 +0200
branchmodules
changeset 56 942370000450
parent 37 4fe4a3c4496e
child 69 6f298b6e0d5f
permissions -rw-r--r--
compiling, working, but still ugly module code
#include "irc_cmd.h"

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

void irc_cmd_init (irc_cmd_handlers_t *handlers)
{
    CHAIN_INIT(handlers);
}

err_t irc_cmd_add (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg)
{
    return chain_add(handlers, list, arg);
}

void irc_cmd_invoke (irc_cmd_handlers_t *handlers, const struct irc_line *line)
{
    struct chain_head *head;
    const struct irc_cmd_handler *handler;
    
    CHAIN_FOREACH(handlers, head) {
        // look up appropriate handler
        for (handler = head->chain; 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, head->arg);

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

void irc_cmd_free (irc_cmd_handlers_t *handlers)
{
    chain_free(handlers);
}