src/irc_cmd.h
author Tero Marttila <terom@fixme.fi>
Tue, 31 Mar 2009 22:09:53 +0300
changeset 98 f357f835f0d5
parent 87 f0db6ebf18b9
child 132 f2ece471fb07
permissions -rw-r--r--
add irc_client_defaults to apply default values for irc_client_add_net irc_net_info, implement --defaults cmd opt and lua_client_connect
#ifndef IRC_CMD_H
#define IRC_CMD_H

/**
 * @file
 *
 * Command handlers callback lists for use with irc_line's
 */

#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 */