added free functions to free everything (maybe a restart function later?)
[NeonServV5.git] / modcmd.c
index 5efe968e38a7ec832f40101f0e2adaf8137620b1..ead1efd8029735ddbad468e3f65d5b8e3e2efaa6 100644 (file)
--- a/modcmd.c
+++ b/modcmd.c
@@ -62,6 +62,52 @@ static char* get_channel_trigger(int botid, struct ChanNode *chan) {
 }
 
 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
+    if(message[0] == '#') {
+        char *chanName = message;
+        message = strstr(message, ' ');
+        if(!message) return;
+        *message = '\0';
+        message++;
+        struct ChanNode *chan2 = getChanByName(chanName);
+        if(chan2)
+            chan = chan2;
+    }
+    int bind_index = get_binds_index(message[0]);
+    char *args = strstr(message, " ");
+    if(args) {
+        *args = '\0';
+        args++;
+    }
+    struct cmd_binding *cbind;
+    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+        if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
+            struct cmd_function *cmdfunc = cbind->func;
+            //parse the arguments...
+            char *argv[MAXNUMPARAMS];
+            int argc = 0;
+            while(*args) {
+                //skip leading spaces
+                while (*args == ' ')
+                    *args++ = 0;
+                argv[argc++] = args;
+                if (argc >= MAXNUMPARAMS)
+                    break;
+                while (*args != ' ' && *args)
+                    args++;
+            }
+            if(argc != 0 && args[0][0] == '#') {
+                struct ChanNode *chan2 = getChanByName(args[0]);
+                if(chan2) {
+                    argv += 1;
+                    argc -= 1;
+                    chan = chan2;
+                }
+            }
+            cmdfunc->func(client, user, chan, argv, argc);
+            return;
+        }
+    }
+    
     if(!strcmp(message, "users")) {
         struct ChanUser *chanuser;
         putsock(client, "PRIVMSG %s :[BOT JOIN] Users on this Channel:", chan->name);
@@ -231,3 +277,29 @@ void init_modcmd() {
     bind_privmsg(got_privmsg);
 }
 
+void free_modcmd() {
+    int i;
+    for(i = 0; i < 27; i++) {
+        struct cmd_binding *cbind, *next;
+        for(cbind = cmd_binds[i]; cbind; cbind = next) {
+            next = cbind->next;
+            free(cbind->cmd);
+            free(cbind);
+        }
+    }
+    free(cmd_binds);
+    struct cmd_function *cmdfunct, *next;
+    for(cmdfunct = cmd_functions; cmdfunct; cmdfunct = next) {
+        next = cmdfunct->next;
+        free(cmdfunct->name);
+        free(cmdfunct);
+    }
+    struct trigger_callback *cb, *next_cb;
+    for(cb = trigger_callbacks; cb; cb = next_cb) {
+        next_cb = cb->next;
+        free(next_cb);
+    }
+    cmd_functions = NULL;
+    trigger_callbacks = NULL;
+}
+