Another year is about to end... So we have to update these damn copyright information :P
[NeonServV5.git] / src / modcmd.c
index 9846db20115da854a7f7f0861214ac840762bee2..d4ed08b1ace4809a3a9794c2fc699a6887905e82 100644 (file)
@@ -1,5 +1,5 @@
-/* modcmd.c - NeonServ v5.2
- * Copyright (C) 2011  Philipp Kreil (pk910)
+/* modcmd.c - NeonServ v5.3
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
  * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -69,7 +69,8 @@ static const struct default_language_entry msgtab[] = {
     {"MODCMD_PUBCMD",           "Public commands in $b%s$b are restricted."}, /* {ARGS: "#TestChan"} */
     {"MODCMD_ACCESS_DENIED",    "Access denied."},
     {"MODCMD_SUBCOMMANDS",      "Subcommands of %s: %s"}, /* {ARGS: "bot", "ADD, DEL, EDIT"} */
-    {"MODCMD_CROSSCHAN",        "You must be in %s (or on its userlist) to use this command."},
+    {"MODCMD_CROSSCHAN",        "You must be in %s (or on its userlist) to use this command."}, /* {ARGS: "#TestChan"} */
+    {"MODCMD_UNKNOWN",          "$b%s$b is an unknown command."}, /* {ARGS: "bla"} */
     {NULL, NULL}
 };
 
@@ -161,8 +162,12 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
         args++;
     }
     struct cmd_binding *cbind;
+    int found_cmd = 0;
     for(cbind = cmd_binds[bind_index]; cbind; cbind = cbind->next) {
         if(cbind->botid == client->botid && (cbind->botid || cbind->clientid == client->clientid) && stricmp(cbind->cmd, message) == 0) {
+            found_cmd = 1;
+            //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];
@@ -191,7 +196,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                             subcompos += sprintf(subcommands + subcompos, (subcompos ? ", %s" : "%s"), cbind->cmd + commandlen);
                         }
                     }
-                    reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, subcommands);
+                    reply(tmp_text_client, user, "MODCMD_SUBCOMMANDS", parent_bind->cmd, (subcompos ? subcommands : "\1dnone\1d"));
                     break;
                 }
             }
@@ -199,13 +204,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 statistics_commands++;
             total_triggered++;
             cbind->triggered++;
-            if((cbind->func->flags & CMDFLAG_FUNCMD)) {
+            if((BIND_FLAGS(cbind) & CMDFLAG_FUNCMD)) {
                 if(!sent_chan)
                     break;
                 chan = sent_chan;
             }
-            //get a text bot
-            tmp_text_client = get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0));
             //parse the arguments...
             char *arga[MAXNUMPARAMS];
             char **argv;
@@ -221,7 +224,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                     if (argc >= MAXNUMPARAMS)
                         break;
                     while ((escape || *args != ' ') && *args) {
-                        if((cbind->func->flags & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
+                        if((BIND_FLAGS(cbind) & CMDFLAG_ESCAPE_ARGS) && *args == '\\') {
                             escape = 1;
                             offset++;
                         } else if(escape)
@@ -239,7 +242,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 }
             }
             argv = arga;
-            if(argc != 0 && argv[0][0] == '#' && !(cbind->func->flags & CMDFLAG_CHAN_PARAM)) {
+            if(argc != 0 && argv[0][0] == '#' && !(BIND_FLAGS(cbind) & CMDFLAG_CHAN_PARAM)) {
                 struct ChanNode *chan2 = getChanByName(argv[0]);
                 if(chan2) {
                     argv += 1;
@@ -304,7 +307,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                                     }
                                     args_pos++;
                                 }
-                            } else if((cbind->func->flags & CMDFLAG_EMPTY_ARGS)) {
+                            } else if((BIND_FLAGS(cbind) & CMDFLAG_EMPTY_ARGS)) {
                                 uargs[uargc++] = args_buffer + args_pos;
                                 args_buffer[args_pos++] = '\0';
                             } else if(c) {
@@ -331,11 +334,11 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
                 break;
             }
-            if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
+            if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_CHAN) && !chan) {
                 reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
                 break;
             }
-            if(((cbind->func->flags & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
+            if(((BIND_FLAGS(cbind) & CMDFLAG_CHECK_AUTH) || (chan && chan != sent_chan && !isUserOnChan(user, chan))) && !(user->flags & USERFLAG_ISAUTHED)) {
                 //check auth...
                 struct command_check_user_cache *data = malloc(sizeof(*data));
                 char **temp_argv = malloc(argc*sizeof(*temp_argv));
@@ -361,6 +364,8 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
             break;
         }
     }
+    if(!found_cmd && !sent_chan)
+        reply(get_botwise_prefered_bot(client->botid, (client->botid == 0 ? client->clientid : 0)), user, "MODCMD_UNKNOWN", message);
     free(message);
     if(args_buffer)
         free(args_buffer);
@@ -371,8 +376,8 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
     MYSQL_ROW row;
     int uaccess;
     char requested_uaccess = 0;
-    int eventflags = (cbind->func->flags & (CMDFLAG_LOG | CMDFLAG_OPLOG));
-    if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
+    int eventflags = (BIND_FLAGS(cbind) & (CMDFLAG_LOG | CMDFLAG_OPLOG));
+    if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
         reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
         return;
     }
@@ -393,8 +398,12 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
             requested_uaccess = 1;
             uaccess = getChannelAccess(user, chan);
             if(!uaccess) {
-                reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
-                return;
+                if(isGodMode(user)) {
+                    eventflags |= CMDFLAG_OPLOG;
+                } else {
+                    reply(tmp_text_client, user, "MODCMD_CROSSCHAN", chan->name);
+                    return;
+                }
             }
         }
     }
@@ -426,7 +435,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
             return;
         }
     }
-    if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
+    if((BIND_FLAGS(cbind) & CMDFLAG_REGISTERED_CHAN)) {
         MYSQL_ROW defaults = NULL;
         char access_list[256];
         int access_pos = 0;
@@ -517,7 +526,7 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
             return;
         }
     }
-    if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
+    if((BIND_FLAGS(cbind) & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
         reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
         return;
     }
@@ -531,6 +540,7 @@ static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *mess
     struct ClientSocket *client;
     FD_ZERO(&fds);
     FD_ZERO(&fds2);
+    got_chanmsg_loop1:
     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2))))  {
             FD_SET(client->clientid, &fds);
@@ -538,16 +548,19 @@ static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *mess
             trigger = get_channel_trigger(client->botid, client->clientid, chan);
             if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
                 handle_command(client, user, chan, message + strlen(trigger));
+                goto got_chanmsg_loop1; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
             }
         }
     }
+    got_chanmsg_loop2:
     for(client = getBots(SOCKET_FLAG_READY, NULL); client; client = getBots(SOCKET_FLAG_READY, client)) {
         if(isUserOnChan(client->user, chan) && ((client->botid == 0 && !FD_ISSET(client->clientid, &fds)) || (client->botid && !FD_ISSET(client->botid, &fds2)))) {
-            FD_SET(client->botid, &fds);
+            FD_SET(client->clientid, &fds);
             FD_SET(client->botid, &fds2);
             trigger = get_channel_trigger(client->botid, client->clientid, chan);
             if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
                 handle_command(client, user, chan, message + strlen(trigger));
+                goto got_chanmsg_loop2; //Thats really really bad, i know... But we can't count on the "getBots" list anymore after executing a command
             }
         }
     }
@@ -605,6 +618,27 @@ int set_trigger_callback(int botid, trigger_callback_t *func) {
     return 1;
 }
 
+int flush_trigger_cache(int botid, int clientid) {
+    struct ChanNode *chan;
+    struct trigger_cache *trigger, *last;
+    for(chan = getAllChans(NULL); chan; chan = getAllChans(chan)) {
+        last = NULL;
+        for(trigger = chan->trigger; trigger; trigger = trigger->next) {
+            if(trigger->botid == botid && (botid || trigger->clientid == clientid)) {
+                if(last)
+                    last->next = trigger->next;
+                else
+                    chan->trigger = trigger->next;
+                free(trigger->trigger);
+                free(trigger);
+                break;
+            } else
+                last = trigger;
+        }
+    }
+    return 1;
+}
+
 int changeBotwiseChannelTrigger(int botid, int clientid, struct ChanNode *chan, char *new_trigger) {
     struct trigger_cache *trigger;
     for(trigger = chan->trigger; trigger; trigger = trigger->next) {
@@ -704,6 +738,8 @@ int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
                 for(i = 0; i < cbind->paramcount; i++)
                     free(cbind->parameters[i]);
             }
+            if(cbind->channel_access)
+                free(cbind->channel_access);
             free(cbind);
             return 1;
         } else
@@ -712,6 +748,33 @@ int unbind_botwise_cmd(int botid, int clientid, char *cmd) {
     return 0;
 }
 
+int unbind_botwise_allcmd(int clientid) {
+    int i;
+    for(i = 0; i < 27; i++) {
+        struct cmd_binding *cbind, *next, *last = NULL;
+        for(cbind = cmd_binds[i]; cbind; cbind = next) {
+            next = cbind->next;
+            if(clientid == cbind->clientid) {
+                if(last)
+                    last->next = cbind->next;
+                else
+                    cmd_binds[i] = cbind->next;
+                free(cbind->cmd);
+                if(cbind->paramcount) {
+                    int j;
+                    for(j = 0; j < cbind->paramcount; j++)
+                        free(cbind->parameters[j]);
+                }
+                if(cbind->channel_access)
+                    free(cbind->channel_access);
+                free(cbind);
+            } else
+                last = cbind;
+        }
+    }
+    return 1;
+}
+
 struct cmd_function *find_cmd_function(int botid, char *name) {
     struct cmd_function *cmdfunc;
     char *c;
@@ -797,13 +860,15 @@ void bind_botwise_set_parameters(int botid, int clientid, char *cmd, char *param
                     free(cbind->parameters[i]);
                 cbind->paramcount = 0;
             }
-            char *a, *b = parameters;
-            do {
-                a = strstr(b, " ");
-                if(a) *a = '\0';
-                cbind->parameters[cbind->paramcount++] = strdup(b);
-                if(a) b = a+1;
-            } while(a);
+            if(parameters) {
+                char *a, *b = parameters;
+                do {
+                    a = strstr(b, " ");
+                    if(a) *a = '\0';
+                    cbind->parameters[cbind->paramcount++] = strdup(b);
+                    if(a) b = a+1;
+                } while(a);
+            }
             return;
         }
     }
@@ -844,6 +909,17 @@ void bind_botwise_set_channel_access(int botid, int clientid, char *cmd, char *c
     }
 }
 
+void bind_botwise_set_bind_flags(int botid, int clientid, char *cmd, unsigned int flags) {
+    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) {
+            cbind->flags |= flags;
+            return;
+        }
+    }
+}
+
 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;