2 #define IRC_CMD_H |
2 #define IRC_CMD_H |
3 |
3 |
4 /** |
4 /** |
5 * @file |
5 * @file |
6 * |
6 * |
7 * Flexible command handlers callback lists for use with irc_lines |
7 * Command handlers callback lists for use with irc_line's |
8 */ |
8 */ |
9 |
9 |
10 #include "irc_line.h" |
10 #include "irc_line.h" |
11 #include "chain.h" |
11 #include "chain.h" |
12 |
12 |
13 /** |
13 /** |
14 * Single command -> handler mapping for lookup |
14 * A single command name + handler function lookup entry. This defines the irc_line::command to handle, and the function used to handle it. |
|
15 * |
|
16 * Note that when an irc_line is matched against an array of these, only the *first* matching handler is invoked. |
15 */ |
17 */ |
16 struct irc_cmd_handler { |
18 struct irc_cmd_handler { |
17 /** The command name to match */ |
19 /** The command name to match */ |
18 const char *command; |
20 const char *command; |
19 |
21 |
20 /** The handler function */ |
22 /** |
|
23 * The handler function. |
|
24 * |
|
25 * @param line the irc_line that matched the command |
|
26 * @param arg the context arg, as given to irc_cmd_add. |
|
27 */ |
21 void (*func) (const struct irc_line *line, void *arg); |
28 void (*func) (const struct irc_line *line, void *arg); |
22 }; |
29 }; |
23 |
30 |
24 /** |
31 /** |
25 * @struct irc_cmd_handlers |
32 * A dynamic list of irc_cmd_handler's |
26 * |
|
27 * A dynamic list of irc_cmd_chain handlers |
|
28 */ |
33 */ |
29 typedef struct chain_list irc_cmd_handlers_t; |
34 typedef struct chain_list irc_cmd_handlers_t; |
30 |
35 |
31 /** |
36 /** |
32 * Initialize a irc_cmd_handlers list. |
37 * Initialize a irc_cmd_handlers list. |
33 */ |
38 */ |
34 void irc_cmd_init (irc_cmd_handlers_t *handlers); |
39 void irc_cmd_init (irc_cmd_handlers_t *handlers); |
35 |
40 |
36 /** |
41 /** |
37 * Add an irc_cmd_chain to the irc_cmd_handlers list for the given handlers/arg |
42 * Append the given NULL-termianted array of irc_cmd_handler's to the irc_cmd_handlers list, without copying it. |
|
43 * |
|
44 * @param handlers the irc_cmd_handlers_t |
|
45 * @param list the { NULL, NULL } termianted array of irc_cmd_handlers |
|
46 * @param arg the opaque context argument, will be passed to the func's of the given list when invoked |
38 */ |
47 */ |
39 err_t irc_cmd_add (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg); |
48 err_t irc_cmd_add (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg); |
40 |
49 |
41 /** |
50 /** |
42 * Trigger irc_cmd_chain callbacks for the given irc_line |
51 * Trigger all relevant handlers for the given irc_line. |
|
52 * |
|
53 * @param handlers the irc_cmd_handlers_t |
|
54 * @param line the line to match against the handlers and invoke the func with |
43 */ |
55 */ |
44 void irc_cmd_invoke (irc_cmd_handlers_t *handlers, const struct irc_line *line); |
56 void irc_cmd_invoke (irc_cmd_handlers_t *handlers, const struct irc_line *line); |
45 |
57 |
46 /** |
58 /** |
47 * Remove a previously added chain_head from the irc_cmd_handlers list with the given list/arg |
59 * Remove a previously added handler list. |
|
60 * |
|
61 * @param handlers the irc_cmd_handlers_t |
|
62 * @param list the list given to irc_cmd_add, compared as pointer value |
|
63 * @param arg the context arg given to irc_cmd_add, compared as pointer value |
48 */ |
64 */ |
49 void irc_cmd_remove (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg); |
65 void irc_cmd_remove (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg); |
50 |
66 |
51 /** |
67 /** |
52 * Cleanup an irc_cmd_handlers list |
68 * Cleanup an irc_cmd_handlers list, releasing all memory. |
|
69 * |
|
70 * @param handlers the irc_cmd_handlers_t to cleanup |
53 */ |
71 */ |
54 void irc_cmd_free (irc_cmd_handlers_t *handlers); |
72 void irc_cmd_free (irc_cmd_handlers_t *handlers); |
55 |
73 |
56 #endif /* IRC_CMD_H */ |
74 #endif /* IRC_CMD_H */ |