fixed modcmd parameter handling
authorpk910 <philipp@zoelle1.de>
Mon, 3 Oct 2011 17:34:04 +0000 (19:34 +0200)
committerpk910 <philipp@zoelle1.de>
Mon, 3 Oct 2011 17:58:27 +0000 (19:58 +0200)
src/cmd_neonserv_command.c
src/modcmd.c
src/modcmd.h

index e3e47a242388c1ddc11e09e8c37f443835e26a9e..3f1d214f362c3722d3c0d28f72e5b57b92c633a2 100644 (file)
@@ -33,7 +33,15 @@ CMD_BIND(neonserv_cmd_command) {
         return;
     }
     ident = argv[0];
-    reply(getTextBot(), user, "NS_COMMAND_BINDING", cbind->cmd, cbind->func->name, (cbind->parameters ? cbind->parameters : ""));
+    char parameters[MAXLEN];
+    if(cbind->paramcount) {
+        int i, parampos = 0;
+        for(i = 0; i < cbind->paramcount; i++) {
+            parampos += sprintf(parameters + parampos, (i ? " %s" : "%s"), cbind->parameters[i]);
+        }
+    } else
+        parameters[0] = '\0';
+    reply(getTextBot(), user, "NS_COMMAND_BINDING", cbind->cmd, cbind->func->name, parameters);
     if(chan)
         reply(getTextBot(), user, "NS_COMMAND_ACCESS", neonserv_cmd_command_chanaccess(cbind, chan), neonserv_cmd_command_operaccess(cbind));
     printf_mysql_query("SELECT `user_lang` FROM `users` WHERE `user_user` = '%s'", escape_string(user->auth));
index a80ccca59424b9bd39097e36d2942b4ac44df7bc..ac3e400fc13e50758c2b04841fa9f50168e24abd 100644 (file)
@@ -168,15 +168,15 @@ 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] == '-') {
@@ -204,11 +204,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 +484,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 +515,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 +533,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 +573,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 +604,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;
         }
     }
index e33a54a6002aa69fcccdba03b4e38f6c8b56bac1..7df181c7e5992ac4712b3996b8bfd9817fb0ab6c 100644 (file)
@@ -18,6 +18,8 @@
 #define _modcmd_h
 #include "main.h"
 
+#define MAXPARAMETERS 50
+
 #define CMDFLAG_REQUIRE_CHAN            0x0001
 #define CMDFLAG_REQUIRE_AUTH            0x0002
 #define CMDFLAG_REQUIRE_GOD             0x0004
@@ -55,7 +57,8 @@ struct cmd_binding {
     int botid;
     struct cmd_function *func;
     unsigned int flags;
-    char *parameters;
+    char *parameters[MAXPARAMETERS];
+    int paramcount;
     int global_access;
     char *channel_access;