src/irc_cmd.c
author Tero Marttila <terom@fixme.fi>
Mon, 04 May 2009 20:55:04 +0300
branchnew-transport
changeset 168 a58ad50911fc
parent 69 6f298b6e0d5f
child 171 b54f393c3df0
permissions -rw-r--r--
refactor test.c into tests/*
#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_remove (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg)
{
    chain_remove(handlers, list, arg);
}

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