src/irc_cmd.c
changeset 37 4fe4a3c4496e
child 69 6f298b6e0d5f
equal deleted inserted replaced
36:791d7a5532e2 37:4fe4a3c4496e
       
     1 #include "irc_cmd.h"
       
     2 
       
     3 #include <stdlib.h>
       
     4 #include <string.h>
       
     5 
       
     6 void irc_cmd_init (irc_cmd_handlers_t *handlers)
       
     7 {
       
     8     CHAIN_INIT(handlers);
       
     9 }
       
    10 
       
    11 err_t irc_cmd_add (irc_cmd_handlers_t *handlers, const struct irc_cmd_handler *list, void *arg)
       
    12 {
       
    13     return chain_add(handlers, list, arg);
       
    14 }
       
    15 
       
    16 void irc_cmd_invoke (irc_cmd_handlers_t *handlers, const struct irc_line *line)
       
    17 {
       
    18     struct chain_head *head;
       
    19     const struct irc_cmd_handler *handler;
       
    20     
       
    21     CHAIN_FOREACH(handlers, head) {
       
    22         // look up appropriate handler
       
    23         for (handler = head->chain; handler->command; handler++) {
       
    24             // the command is alpha-only, so normal case-insensitive cmp is fine
       
    25             if (strcasecmp(handler->command, line->command) == 0) {
       
    26                 // invoke the func
       
    27                 handler->func(line, head->arg);
       
    28 
       
    29                 // ...only one per chain
       
    30                 break;
       
    31             }
       
    32         }
       
    33     }
       
    34 }
       
    35 
       
    36 void irc_cmd_free (irc_cmd_handlers_t *handlers)
       
    37 {
       
    38     chain_free(handlers);
       
    39 }