*** VERSION 5.1.0 ***
[NeonServV5.git] / src / modcmd.c
index a80ccca59424b9bd39097e36d2942b4ac44df7bc..eeaee94974644a18764c2aca0a1b69a7d82dddaa 100644 (file)
@@ -1,4 +1,4 @@
-/* modcmd.c - NeonServ v5.0
+/* modcmd.c - NeonServ v5.1
  * Copyright (C) 2011  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -168,22 +168,23 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                     chan = chan2;
                 }
             }
-            if(cbind->parameters) {
+            if(cbind->paramcount) {
                 //userdefined parameters...
                 char *uargs[MAXNUMPARAMS];
                 int uargc = 0;
-                char *a,*b = cbind->parameters;
+                char *b;
+                int i;
                 int allargs, argi;
-                do {
-                    a = strstr(b, " ");
-                    if(a) *a = '\0';
+                for(i = 0; i < cbind->paramcount; i++) {
+                    b = cbind->parameters[i];
                     if(b[0] == '%') {
                         b++;
                         if(b[strlen(b)-1] == '-') {
-                            allargs = 1;
-                            b[strlen(b)-1] = '\0';
+                            allargs = strlen(b)-1;
+                            b[allargs] = '\0';
                             argi = atoi(b);
-                            b[strlen(b)-1] = '-';
+                            b[allargs] = '-';
+                            allargs = 1;
                         } else {
                             allargs = 0;
                             argi = atoi(b);
@@ -204,11 +205,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                     } else {
                         uargs[uargc++] = b;
                     }
-                    if(a) {
-                        *a = ' ';
-                        b = a+1;
-                    }
-                } while(a);
+                }
                 argv = uargs;
                 argc = uargc;
             }
@@ -488,7 +485,7 @@ int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
     cbind->botid = botid;
     cbind->cmd = strdup(cmd);
     cbind->func = func;
-    cbind->parameters = NULL;
+    cbind->paramcount = 0;
     cbind->global_access = 0;
     cbind->channel_access = NULL;
     cbind->flags = 0;
@@ -519,7 +516,7 @@ int bind_cmd_to_command(int botid, char *cmd, char *func) {
     cbind->cmd = strdup(cmd);
     cbind->func = cmdfunc;
     cbind->next = cmd_binds[bind_index];
-    cbind->parameters = NULL;
+    cbind->paramcount = 0;
     cbind->global_access = 0;
     cbind->channel_access = NULL;
     cbind->flags = 0;
@@ -537,8 +534,11 @@ int unbind_cmd(int botid, char *cmd) {
             else
                 cmd_binds[bind_index] = cbind->next;
             free(cbind->cmd);
-            if(cbind->parameters)
-                free(cbind->parameters);
+            if(cbind->paramcount) {
+                int i;
+                for(i = 0; i < cbind->paramcount; i++)
+                    free(cbind->parameters[i]);
+            }
             free(cbind);
             return 1;
         } else
@@ -574,8 +574,11 @@ void free_modcmd() {
         for(cbind = cmd_binds[i]; cbind; cbind = next) {
             next = cbind->next;
             free(cbind->cmd);
-            if(cbind->parameters)
-                free(cbind->parameters);
+            if(cbind->paramcount) {
+                int j;
+                for(j = 0; j < cbind->paramcount; j++)
+                    free(cbind->parameters[j]);
+            }
             if(cbind->channel_access)
                 free(cbind->channel_access);
             free(cbind);
@@ -602,9 +605,19 @@ void bind_set_parameters(int botid, char *cmd, char *parameters) {
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
         if(cbind->botid == botid && strcmp(cbind->cmd, cmd) == 0) {
-            if(cbind->parameters)
-                free(cbind->parameters);
-            cbind->parameters = strdup(parameters);
+            if(cbind->paramcount) {
+                int i;
+                for(i = 0; i < cbind->paramcount; i++)
+                    free(cbind->parameters[i]);
+                cbind->paramcount = 0;
+            }
+            char *a, *b = parameters;
+            do {
+                a = strstr(b, " ");
+                if(a) *a = '\0';
+                cbind->parameters[cbind->paramcount++] = strdup(b);
+                if(a) b = a+1;
+            } while(a);
             return;
         }
     }