Merge branch 'development'
[NeonServV5.git] / src / modcmd.c
index e19d261318cd71f169f894bee1efc2a1ea5259c8..8ec2517ef9c640bcbeb3f909a306980c02800d94 100644 (file)
@@ -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;
@@ -192,17 +194,21 @@ static struct cmd_binding *modcmd_sub_linker_command(struct ClientSocket *client
             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;
         }
-        *args_ptr = args;
-        if(cbind && cbind->func->func == modcmd_linker) {
-            return modcmd_linker_command(client, textclient, user, cbind, bind_index, args_ptr);
+        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 (cbind ? cbind : parent_bind);
-    } else
-        return parent_bind;
+    }
+    return parent_bind;
 }
 
 static void handle_command(struct ClientSocket *client, struct UserNode *user, struct ChanNode *chan, char *message) {
@@ -237,8 +243,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 if(cbind == NULL) break;
             } else if(cbind->flags & CMDFLAG_SUB_LINKER)
                 cbind = modcmd_sub_linker_command(client, textclient, user, cbind, bind_index, &args);
-            if(statistics_enabled)
-                statistics_commands++;
+            statistics_commands++;
             total_triggered++;
             cbind->triggered++;
             if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
@@ -380,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));
@@ -630,7 +635,7 @@ int register_command(int botid, char *name, int module_id, cmd_bind_t *func, int
     }
     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;
@@ -655,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;
@@ -709,7 +714,7 @@ int bind_botwise_cmd_to_function(int botid, int clientid, char *cmd, struct cmd_
     }
     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;
@@ -755,7 +760,7 @@ int bind_botwise_cmd_to_command(int botid, int clientid, char *cmd, char *func)
     }
     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;
@@ -1009,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;