Merge branch 'development'
[NeonServV5.git] / src / modcmd.c
index 2edabf17968f1b29a4bac9268382db08a02d6d0a..8ec2517ef9c640bcbeb3f909a306980c02800d94 100644 (file)
@@ -1,4 +1,4 @@
-/* modcmd.c - NeonServ v5.4
+/* modcmd.c - NeonServ v5.6
  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
@@ -27,6 +27,8 @@
 #include "mysqlConn.h"
 #include "DBHelper.h"
 #include "EventLogger.h"
+#include "tools.h"
+#include "log.h"
 
 struct trigger_callback {
     int botid;
@@ -112,7 +114,7 @@ static char* get_channel_trigger(int botid, int clientid, struct ChanNode *chan)
         triggerStr[0] = '\0';
     trigger = malloc(sizeof(*trigger));
     if (!trigger) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return 0;
     }
     trigger->botid = botid;
@@ -179,6 +181,36 @@ static struct cmd_binding *modcmd_linker_command(struct ClientSocket *client, st
     }
 }
 
+static struct cmd_binding *modcmd_sub_linker_command(struct ClientSocket *client, struct ClientSocket *textclient, 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 = strchr(args, ' ');
+        if(args) {
+            *args = '\0';
+            args++;
+        }
+        sprintf(command, "%s %s", parent_bind->cmd, subcmd);
+        if(args)
+            args[-1] = ' ';
+        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) {
+            *args_ptr = args;
+            if(cbind->func->func == modcmd_linker)
+                parent_bind = modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
+            else
+                parent_bind = cbind;
+        }
+    }
+    return parent_bind;
+}
+
 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
     struct ChanNode *sent_chan = chan;
     if(message[0] == '#') {
@@ -193,7 +225,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
     }
     message = strdup(message);
     int bind_index = get_binds_index(message[0]);
-    char *args = strstr(message, " ");
+    char *args = strchr(message, ' ');
     char *args_buffer = NULL; //we need this to save a possible pointer to a allocation we need to free
     if(args) {
         *args = '\0';
@@ -209,9 +241,9 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
             if(cbind->func->func == modcmd_linker) {
                 cbind = modcmd_linker_command(client, textclient, user, cbind, bind_index, &args);
                 if(cbind == NULL) break;
-            }
-            if(statistics_enabled)
-                statistics_commands++;
+            } else if(cbind->flags & CMDFLAG_SUB_LINKER)
+                cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args);
+            statistics_commands++;
             total_triggered++;
             cbind->triggered++;
             if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
@@ -353,7 +385,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 struct command_check_user_cache *data = malloc(sizeof(*data));
                 char **temp_argv = malloc(argc*sizeof(*temp_argv));
                 if (!data || !temp_argv) {
-                    perror("malloc() failed");
+                    printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
                     break;
                 }
                 memcpy(temp_argv, argv, argc*sizeof(*temp_argv));
@@ -598,12 +630,12 @@ static void got_privmsg(struct UserNode *user, struct UserNode *target, char *me
 int register_command(int botid, char *name, int module_id, cmd_bind_t *func, int paramcount, char *channel_access, int global_access, unsigned int flags) {
     struct cmd_function *cmdfunc;
     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
-        if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
+        if((cmdfunc->botid == botid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, name))
             return 0;
     }
     cmdfunc = malloc(sizeof(*cmdfunc));
     if (!cmdfunc) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return 0;
     }
     cmdfunc->botid = botid;
@@ -628,7 +660,7 @@ int set_trigger_callback(int botid, int module_id, trigger_callback_t *func) {
     if(!cb) {
         cb = malloc(sizeof(*cb));
         if (!cb) {
-            perror("malloc() failed");
+            printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
             return 0;
         }
         cb->botid = botid;
@@ -677,12 +709,12 @@ int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
+        if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
             return 0;
     }
     cbind = malloc(sizeof(*cbind));
     if (!cbind) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return 0;
     }
     cbind->botid = botid;
@@ -716,19 +748,19 @@ int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func)
         func = c+1;
     }
     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
-        if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
+        if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && !stricmp(cmdfunc->name, func))
             break;
     }
     if(!cmdfunc) return 0;
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && strcmp(cbind->cmd, cmd) == 0)
+        if(((botid && cbind->botid == botid) || (botid == 0 && clientid == cbind->clientid)) && !stricmp(cbind->cmd, cmd))
             return 0;
     }
     cbind = malloc(sizeof(*cbind));
     if (!cbind) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return 0;
     }
     cbind->botid = botid;
@@ -749,7 +781,7 @@ int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind, *last = NULL;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             if(last)
                 last->next = cbind->next;
             else
@@ -871,7 +903,7 @@ void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *param
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             if(cbind->paramcount) {
                 int i;
                 for(i = 0; i < cbind->paramcount; i++)
@@ -896,7 +928,7 @@ void bind_botwise_set_global_access(int botid, int clientid, char *cmd, int gacc
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             if(gaccess > -1) {
                 cbind->global_access = gaccess;
                 cbind->flags |= CMDFLAG_OVERRIDE_GLOBAL_ACCESS;
@@ -912,7 +944,7 @@ void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *c
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             if(cbind->channel_access)
                 free(cbind->channel_access);
             if(chanaccess) {
@@ -931,7 +963,7 @@ void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned in
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             cbind->flags |= flags;
             return;
         }
@@ -942,7 +974,7 @@ struct cmd_binding *find_botwise_cmd_binding(int botid, int clientid, char *cmd)
     int bind_index = get_binds_index(cmd[0]);
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && strcmp(cbind->cmd, cmd) == 0) {
+        if(cbind->botid == botid && (botid || clientid == cbind->clientid) && !stricmp(cbind->cmd, cmd)) {
             return cbind;
         }
     }
@@ -982,7 +1014,7 @@ void register_command_alias(int botid, char *alias) {
     }
     botalias = malloc(sizeof(*botalias));
     if (!botalias) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return;
     }
     botalias->botid = botid;