src/irc_cmd.c
changeset 37 4fe4a3c4496e
child 69 6f298b6e0d5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/irc_cmd.c	Thu Mar 12 18:11:44 2009 +0200
@@ -0,0 +1,39 @@
+#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_free (irc_cmd_handlers_t *handlers)
+{
+    chain_free(handlers);
+}