removed debug output from mysqlConn.c
[NeonServV5.git] / src / modcmd.c
index d4ed08b1ace4809a3a9794c2fc699a6887905e82..8846d900197bc75b53c1e0181a04b062a093058a 100644 (file)
@@ -128,7 +128,8 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
 static USERAUTH_CALLBACK(command_checked_auth) {
     struct command_check_user_cache *cache = data;
     tmp_text_client = cache->textclient;
-    handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
+    if(user)
+        handle_command_async(cache->client, user, cache->chan, cache->sent_chan, cache->cbind, cache->argv, cache->argc);
     free(cache->message);
     if(cache->args_buffer)
         free(cache->args_buffer);
@@ -141,6 +142,44 @@ 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 && 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) {
+                if(strstr(cbind->cmd + commandlen, " ")) continue; //ignore if there is another space in the command (sub-sub-command :D)
+                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 +208,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++;
@@ -242,14 +253,6 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 }
             }
             argv = arga;
-            if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
-                struct ChanNode *chan2 = getChanByName(argv[0]);
-                if(chan2) {
-                    argv += 1;
-                    argc -= 1;
-                    chan = chan2;
-                }
-            }
             if(cbind->paramcount) {
                 //userdefined parameters...
                 args_buffer = malloc(MAXLEN * 2 * sizeof(*args_buffer));
@@ -330,6 +333,14 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 argv = uargs;
                 argc = uargc;
             }
+            if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
+                struct ChanNode *chan2 = getChanByName(argv[0]);
+                if(chan2) {
+                    argv += 1;
+                    argc -= 1;
+                    chan = chan2;
+                }
+            }
             if(argc < cbind->func->paramcount) {
                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
                 break;
@@ -364,7 +375,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
             break;
         }
     }
-    if(!found_cmd && !sent_chan)
+    if(!found_cmd && !sent_chan && !(client->flags & SOCKET_FLAG_SILENT))
         reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
     free(message);
     if(args_buffer)
@@ -381,7 +392,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
         return;
     }
-    if(chan && sent_chan != chan && !isUserOnChan(user, chan)) {
+    if(chan && sent_chan != chan && (BIND_FLAGS(cbind) & CMDFLAG_NO_CROSSCHAN) && !isUserOnChan(user, chan)) {
         char user_in_chan = 0;
         if((user->flags & USERFLAG_ISAUTHED)) {
             //maybe there's another user authed to user->auth on the channel...
@@ -446,6 +457,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
             str_b = cbind->channel_access;
         access_list[0] = '\0';
         if(str_b) {
+            struct ChanUser *chanuser = getChanUser(user, chan);
             str_c = strdup(str_b);
             str_b = str_c;
             while((str_a = str_b)) {
@@ -454,6 +466,15 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
                     *str_b = '\0';
                     str_b++;
                 }
+                if(*str_a == '@' || *str_a == '+') {
+                    //privs can override this access requirement
+                    int priv = 0;
+                    if(*str_a == '@') priv = CHANUSERFLAG_OPPED;
+                    else if(*str_a == '%') priv = CHANUSERFLAG_HALFOPPED;
+                    else if(*str_a == '+') priv = CHANUSERFLAG_VOICED;
+                    if(chanuser && (chanuser->flags & priv)) continue;
+                    str_a++;
+                }
                 if(*str_a == '#') {
                     str_a++;
                     access_pos += sprintf(access_list+access_pos, ", `%s`", str_a);