src/irc_cmd.h
author Tero Marttila <terom@fixme.fi>
Tue, 28 Apr 2009 22:08:59 +0300
branchnew-transport
changeset 156 6534a4ac957b
parent 132 f2ece471fb07
child 171 b54f393c3df0
permissions -rw-r--r--
add transport/sock/line_proto/etc code compiles
#ifndef IRC_CMD_H
#define IRC_CMD_H

/**
 * @file
 *
 * Command handlers callback lists for use with irc_line's
 *
 * XXX: irc_cmd_remove called from iniside irc_cmd_invoke?
 */

#include "irc_line.h"
#include "chain.h"

/**
 * A single command name + handler function lookup entry. This defines the irc_line::command to handle, and the function used to handle it.
 *
 * Note that when an irc_line is matched against an array of these, only the *first* matching handler is invoked.
 */
struct irc_cmd_handler {
    /** The command name to match */
    const char *command;

    /**
     * The handler function.
     *
     * @param line the irc_line that matched the command
     * @param arg the context arg, as given to irc_cmd_add.
     */
    void (*func) (const struct irc_line *line, void *arg);
};

/**
 * A dynamic list of irc_cmd_handler's
 */
typedef struct chain_list irc_cmd_handlers_t;

/**
 * Initialize a irc_cmd_handlers list.
 */
void irc_cmd_init (irc_cmd_handlers_t *handlers);

/**
 * Append the given NULL-termianted array of irc_cmd_handler's to the irc_cmd_handlers list, without copying it.
 *
 * @param handlers the irc_cmd_handlers_t
 * @param list the { NULL, NULL } termianted array of irc_cmd_handlers
 * @param arg the opaque context argument, will be passed to the func's of the given list when invoked
 */
err_t irc_cmd_add (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg);

/**
 * Trigger all relevant handlers for the given irc_line.
 *
 * @param handlers the irc_cmd_handlers_t
 * @param line the line to match against the handlers and invoke the func with
 */
void irc_cmd_invoke (irc_cmd_handlers_t *handlers, const struct irc_line *line);

/**
 * Remove a previously added handler list.
 *
 * @param handlers the irc_cmd_handlers_t
 * @param list the list given to irc_cmd_add, compared as pointer value
 * @param arg the context arg given to irc_cmd_add, compared as pointer value
 */
void irc_cmd_remove (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg);

/**
 * Cleanup an irc_cmd_handlers list, releasing all memory.
 *
 * @param handlers the irc_cmd_handlers_t to cleanup
 */
void irc_cmd_free (irc_cmd_handlers_t *handlers);

#endif /* IRC_CMD_H */