added support for multi-interleaved commands
authorpk910 <philipp@zoelle1.de>
Sat, 7 Jan 2012 00:33:09 +0000 (01:33 +0100)
committerpk910 <philipp@zoelle1.de>
Sat, 7 Jan 2012 00:37:39 +0000 (01:37 +0100)
src/modcmd.c

index 5d6bae3b62a04d68a97031fd004d0608ec0d83ba..78b04c810df89b08c37f82d5a986a9d93fc21a2b 100644 (file)
@@ -141,6 +141,43 @@ static CMD_BIND(modcmd_linker) {
     //just empty
 }
 
+static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, struct UserNode *user, struct cmd_binding *cbind, int bind_index, char **args_ptr) {
+    //links subcommands
+    char command[MAXLEN];
+    struct cmd_binding *parent_bind = cbind;
+    char *args = *args_ptr;
+    if(args) {
+        char *subcmd = args;
+        args = strstr(args, " ");
+        if(args) {
+            *args = '\0';
+            args++;
+        }
+        sprintf(command, "%s %s", parent_bind->cmd, subcmd);
+        for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+            if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
+                break;
+        }
+        *args_ptr = args;
+        if(cbind->func->func == modcmd_linker) {
+            return modcmd_linker_command(client, user, cbind, bind_index, args_ptr);
+        }
+        return cbind;
+    } else {
+        //list all sub commands
+        int commandlen = sprintf(command, "%s ", parent_bind->cmd);
+        char subcommands[MAXLEN];
+        int subcompos = 0;
+        for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
+            if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
+                subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
+            }
+        }
+        reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "\1dnone\1d"));
+        return NULL;
+    }
+}
+
 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
     struct ChanNode *sent_chan = chan;
     if(message[0] == '#') {
@@ -169,36 +206,8 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
             //get a text bot
             tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
             if(cbind->func->func == modcmd_linker) {
-                //links subcommands
-                char command[MAXLEN];
-                struct cmd_binding *parent_bind = cbind;
-                if(args) {
-                    char *subcmd = args;
-                    args = strstr(args, " ");
-                    if(args) {
-                        *args = '\0';
-                        args++;
-                    }
-                    sprintf(command, "%s %s", parent_bind->cmd, subcmd);
-                    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-                        if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, command) == 0)
-                            break;
-                    }
-                    if(!cbind)
-                        break;
-                } else {
-                    //list all sub commands
-                    int commandlen = sprintf(command, "%s ", parent_bind->cmd);
-                    char subcommands[MAXLEN];
-                    int subcompos = 0;
-                    for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-                        if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmplen(cbind->cmd, command, commandlen) == 0) {
-                            subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
-                        }
-                    }
-                    reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "\1dnone\1d"));
-                    break;
-                }
+                cbind = modcmd_linker_command(client, user, cbind, bind_index, &args);
+                if(cbind == NULL) break;
             }
             if(statistics_enabled)
                 statistics_commands++;