changed Makefile; build all commands as an own file
[NeonServV5.git] / modcmd.c
index a5a3e67a5514ddd1d3e288591ac1107c0aa08ca1..5476d5e00a9798fff30610f4f01c2f5f70a4465c 100644 (file)
--- a/modcmd.c
+++ b/modcmd.c
@@ -10,6 +10,7 @@
 #include "lang.h"
 #include "mysqlConn.h"
 #include "DBHelper.h"
+#include "EventLogger.h"
 
 struct trigger_callback {
     int botid;
@@ -35,17 +36,18 @@ static struct ClientSocket *tmp_text_client;
 
 static const struct default_language_entry msgtab[] = {
     {"MODCMD_LESS_PARAM_COUNT", "This command requires more parameters."},
-    {"MODCMD_CHAN_REQUIRED",    "You must provide the name of a channel that exists."},
+    {"MODCMD_CHAN_REQUIRED",    "You must provide the name of a channel that exists and the bot is on."},
     {"MODCMD_AUTH_REQUIRED",    "You need to be authenticated with AuthServ to use this command."},
-    {"MODCMD_PRIVILEGED",       "\002%s\002 is a privileged command."},
-    {"MODCMD_PUBCMD",           "Public commands in \002%s\002 are restricted."},
+    {"MODCMD_CHAN_SUSPENDED",   "This channel is currently suspended."},
+    {"MODCMD_PRIVILEGED",       "$b%s$b is a privileged command."}, /* {ARGS: "god"} */
+    {"MODCMD_PUBCMD",           "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
     {"MODCMD_ACCESS_DENIED",    "Access denied."},
     {NULL, NULL}
 };
 
 static int get_binds_index(char first_char) {
     if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
-        return tolower(first_char - 'a');
+        return tolower(first_char) - 'a';
     }
     return 26;
 }
@@ -118,7 +120,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
     }
     struct cmd_binding *cbind;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
-        if(cbind->botid == client->botid && strcmp(cbind->cmd, message) == 0) {
+        if(cbind->botid == client->botid && stricmp(cbind->cmd, message) == 0) {
             //get a text bot
             tmp_text_client = get_prefered_bot(client->botid);
             //parse the arguments...
@@ -138,7 +140,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 }
             }
             argv = arga;
-            if(argc != 0 && argv[0][0] == '#') {
+            if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
                 struct ChanNode *chan2 = getChanByName(argv[0]);
                 if(chan2) {
                     argv += 1;
@@ -148,41 +150,47 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
             }
             if(cbind->parameters) {
                 //userdefined parameters...
-                char *uarga[MAXNUMPARAMS];
-                char params[strlen(cbind->parameters)+1];
-                strcpy(params, cbind->parameters);
-                int uargpos = 0, argi, allargs = 0;
-                char *ppos = params;
-                char *prev_ppos = params;
-                while((ppos = strstr(ppos, " "))) {
-                    *ppos = '\0';
-                    if(prev_ppos[0] == '%') {
-                        prev_ppos++;
-                        if(prev_ppos[strlen(prev_ppos)-1] == '-') {
+                char *uargs[MAXNUMPARAMS];
+                int uargc = 0;
+                char *a,*b = cbind->parameters;
+                int allargs, argi;
+                do {
+                    a = strstr(b, " ");
+                    if(a) *a = '\0';
+                    if(b[0] == '%') {
+                        b++;
+                        if(b[strlen(b)-1] == '-') {
                             allargs = 1;
-                            prev_ppos[strlen(prev_ppos)-1] = '\0';
-                        } else
+                            b[strlen(b)-1] = '\0';
+                            argi = atoi(b);
+                            b[strlen(b)-1] = '-';
+                        } else {
                             allargs = 0;
-                        argi = atoi(prev_ppos);
+                            argi = atoi(b);
+                        }
                         if(argi > 0) {
-                            if(argi <= argc) continue;
-                            uarga[uargpos++] = argv[argi-1];
-                            if(allargs) {
-                                for(;argi < argc; argi++)
-                                    uarga[uargpos++] = argv[argi-1];
+                            if(argi <= argc) {
+                                uargs[uargc++] = argv[argi-1];
+                                if(allargs) {
+                                    for(argi++; argi <= argc; argi++)
+                                        uargs[uargc++] = argv[argi-1];
+                                }
                             }
-                        } else if(!strcmp(prev_ppos, "c"))
-                            uarga[uargpos++] = (chan ? chan->name : NULL);
-                        else if(!strcmp(prev_ppos, "n")) 
-                            uarga[uargpos++] = user->nick;
+                        } else if(!strcmp(b, "c")) {
+                            uargs[uargc++] = (chan ? chan->name : NULL);
+                        } else if(!strcmp(b, "n")) {
+                            uargs[uargc++] = user->nick;
+                        }
                     } else {
-                        uarga[uargpos++] = prev_ppos;
+                        uargs[uargc++] = b;
                     }
-                    ppos++;
-                    prev_ppos = ppos;
-                }
-                argv = uarga;
-                argc = uargpos;
+                    if(a) {
+                        *a = ' ';
+                        b = a+1;
+                    }
+                } while(a);
+                argv = uargs;
+                argc = uargc;
             }
             if(argc < cbind->func->paramcount) {
                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
@@ -224,6 +232,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
     MYSQL_RES *res;
     MYSQL_ROW row;
     int uaccess;
+    int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
     if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
         return;
@@ -257,7 +266,6 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
         }
     }
     if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
-        check_mysql();
         MYSQL_ROW defaults = NULL;
         char access_list[256];
         int access_pos = 0;
@@ -294,8 +302,10 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
                 chan->flags |= CHANFLAG_CHAN_REGISTERED;
                 chan->channel_id = atoi(row[0]);
                 if((sent_chan && sent_chan == chan) || access_count || minaccess) {
-                    uaccess = getChannelAccess(user, chan, 1);
-                    if(uaccess < minaccess) {
+                    uaccess = getChannelAccess(user, chan, 0);
+                    if(uaccess < minaccess && isGodMode(user)) {
+                        eventflags |= CMDFLAG_OPLOG;
+                    } else if(uaccess < minaccess) {
                         //ACCESS DENIED
                         reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
                         return;
@@ -305,9 +315,13 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
                         defaults = mysql_fetch_row(mysql_use());
                     }
                     if(sent_chan && (sent_chan == chan) && uaccess < (row[1] ? atoi(row[1]) : atoi(defaults[1]))) {
-                        //PUBCMD
-                        reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
-                        return;
+                        if(isGodMode(user)) {
+                            eventflags |= CMDFLAG_OPLOG;
+                        } else {
+                            //PUBCMD
+                            reply(tmp_text_client, user, "MODCMD_PUBCMD", chan->name);
+                            return;
+                        }
                     }
                     int i;
                     for(i = 0; i < access_count; i++) {
@@ -316,8 +330,12 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
                             defaults = mysql_fetch_row(mysql_use());
                         }
                         if(uaccess < (row[2+i] ? atoi(row[2+i]) : atoi(defaults[2+i]))) {
-                            reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
-                            return;
+                            if(isGodMode(user)) {
+                                eventflags |= CMDFLAG_OPLOG;
+                            } else {
+                                reply(tmp_text_client, user, "MODCMD_ACCESS_DENIED");
+                                return;
+                            }
                         }
                     }
                 }
@@ -328,18 +346,22 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
             return;
         }
-        printf_mysql_query("SELECT `botid` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
+        printf_mysql_query("SELECT `botid`, `suspended` FROM `bot_channels` LEFT JOIN `bots` ON `bot_channels`.`botid` = `bots`.`id` WHERE `chanid` = '%d' AND `botclass` = '%d'", chan->channel_id, client->botid);
         res = mysql_use();
         if ((row = mysql_fetch_row(res)) == NULL) {
             reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
             return;
+        } else if(!strcmp(row[1], "1")) {
+            reply(tmp_text_client, user, "MODCMD_CHAN_SUSPENDED");
+            return;
         }
     }
     if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
         reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
         return;
     }
-    cbind->func->func(client, user, chan, argv, argc);
+    struct Event *event = createEvent(client, user, chan, cbind->func->name, argv, argc, eventflags);
+    cbind->func->func(client, user, chan, argv, argc, event);
 }
 
 static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *message) {
@@ -376,7 +398,7 @@ static void got_privmsg(struct UserNode *user, struct UserNode *target, char *me
     }
 }
 
-int register_command(int botid, char *name, cmd_bind_t *func, int paramcount, unsigned int flags, char *channel_access, int global_access) {
+int register_command(int botid, char *name, 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 && strcmp(cmdfunc->name, name) == 0)
@@ -505,6 +527,15 @@ int unbind_cmd(int botid, char *cmd) {
     return 0;
 }
 
+struct cmd_function *find_cmd_function(int botid, char *name) {
+    struct cmd_function *cmdfunc;
+    for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
+        if(cmdfunc->botid == botid && stricmp(cmdfunc->name, name) == 0)
+            break;
+    }
+    return cmdfunc;
+}
+
 struct ClientSocket *getTextBot() {
     return tmp_text_client;
 }
@@ -593,3 +624,15 @@ void bind_set_channel_access(int botid, char *cmd, char *chanaccess) {
         }
     }
 }
+
+struct cmd_binding *find_cmd_binding(int botid, 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 && strcmp(cbind->cmd, cmd) == 0) {
+            return cbind;
+        }
+    }
+    return NULL;
+}
+