added structure for future fun-commands
[NeonServV5.git] / src / modcmd.c
index 3f51761a0f30bbf5e3e47abdc3b82027fcffe0ba..8b9c72c4573ab60000075d2e6a14e20df36861f9 100644 (file)
@@ -35,6 +35,13 @@ struct trigger_callback {
     struct trigger_callback *next;
 };
 
+struct cmd_bot_alias {
+    int botid;
+    char *alias;
+    
+    struct cmd_bot_alias *next;
+};
+
 struct command_check_user_cache {
     struct ClientSocket *client, *textclient;
     struct UserNode *user;
@@ -48,6 +55,7 @@ struct command_check_user_cache {
 static struct cmd_binding **cmd_binds;
 static struct cmd_function *cmd_functions = NULL;
 static struct trigger_callback *trigger_callbacks = NULL;
+static struct cmd_bot_alias *bot_aliases = NULL;
 static struct ClientSocket *tmp_text_client;
 
 static const struct default_language_entry msgtab[] = {
@@ -103,7 +111,7 @@ static char* get_channel_trigger(int botid, struct ChanNode *chan) {
         return 0;
     }
     trigger->botid = botid;
-    trigger->trigger = strdup(triggerStr);
+    trigger->trigger = (triggerStr[0] ? strdup(triggerStr) : NULL);
     trigger->next = chan->trigger;
     chan->trigger = trigger;
     return trigger->trigger;
@@ -145,6 +153,11 @@ 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 && stricmp(cbind->cmd, message) == 0) {
+            if((cbind->flags & CMDFLAG_FUNCMD)) {
+                if(!sent_chan)
+                    break;
+                chan = sent_chan;
+            }
             //get a text bot
             tmp_text_client = get_prefered_bot(client->botid);
             //parse the arguments...
@@ -291,8 +304,8 @@ static void handle_command_async(struct ClientSocket *client, struct UserNode *u
         printf_mysql_query("SELECT `channel_pubcmd` FROM `channels` WHERE `channel_name` = '%s'", escape_string(sent_chan->name));
         res = mysql_use();
         if ((row = mysql_fetch_row(res)) != NULL) {
-            uaccess = getChannelAccess(user, sent_chan, 1);
-            if(row[0] && uaccess < atoi(row[0])) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
+            uaccess = getChannelAccess(user, sent_chan);
+            if(row[0] && uaccess < atoi(row[0]) && !isGodMode(user)) { //NOTE: HARDCODED DEFAULT: pubcmd = 0
                 reply(tmp_text_client, user, "MODCMD_PUBCMD", sent_chan->name);
                 return;
             }
@@ -351,7 +364,7 @@ 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, 0);
+                    uaccess = getChannelAccess(user, chan);
                     if(uaccess < minaccess && isGodMode(user)) {
                         eventflags |= CMDFLAG_OPLOG;
                     } else if(uaccess < minaccess) {
@@ -422,7 +435,7 @@ static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *mess
         if(isUserOnChan(client->user, chan) && (client->flags & SOCKET_FLAG_PREFERRED) && !FD_ISSET(client->botid, &fds)) {
             FD_SET(client->botid, &fds);
             trigger = get_channel_trigger(client->botid, chan);
-            if(stricmplen(message, trigger, strlen(trigger)) == 0) {
+            if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
                 handle_command(client, user, chan, message + strlen(trigger));
             }
         }
@@ -431,7 +444,7 @@ static void got_chanmsg(struct UserNode *user, struct ChanNode *chan, char *mess
         if(isUserOnChan(client->user, chan) && !FD_ISSET(client->botid, &fds)) {
             FD_SET(client->botid, &fds);
             trigger = get_channel_trigger(client->botid, chan);
-            if(stricmplen(message, trigger, strlen(trigger)) == 0) {
+            if(trigger && stricmplen(message, trigger, strlen(trigger)) == 0) {
                 handle_command(client, user, chan, message + strlen(trigger));
             }
         }
@@ -450,7 +463,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, 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)
+        if((cmdfunc->botid == botid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, name) == 0)
             return 0;
     }
     cmdfunc = malloc(sizeof(*cmdfunc));
@@ -528,8 +541,22 @@ int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
 
 int bind_cmd_to_command(int botid, char *cmd, char *func) {
     struct cmd_function *cmdfunc;
+    int fbotid = botid;
+    char *c;
+    if((c = strstr(func, "."))) {
+        *c = '\0';
+        struct cmd_bot_alias *botalias;
+        for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+            if(!stricmp(botalias->alias, func)) {
+                fbotid = botalias->botid;
+                break;
+            }
+        }
+        *c = '.';
+        func = c+1;
+    }
     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
-        if(cmdfunc->botid == botid && strcmp(cmdfunc->name, func) == 0)
+        if((cmdfunc->botid == fbotid || cmdfunc->botid == 0) && strcmp(cmdfunc->name, func) == 0)
             break;
     }
     if(!cmdfunc) return 0;
@@ -581,8 +608,21 @@ int unbind_cmd(int botid, char *cmd) {
 
 struct cmd_function *find_cmd_function(int botid, char *name) {
     struct cmd_function *cmdfunc;
+    char *c;
+    if((c = strstr(name, "."))) {
+        *c = '\0';
+        struct cmd_bot_alias *botalias;
+        for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+            if(!stricmp(botalias->alias, name)) {
+                botid = botalias->botid;
+                break;
+            }
+        }
+        *c = '.';
+        name = c+1;
+    }
     for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
-        if(cmdfunc->botid == botid && stricmp(cmdfunc->name, name) == 0)
+        if((cmdfunc->botid == botid || cmdfunc->botid == 0) && stricmp(cmdfunc->name, name) == 0)
             break;
     }
     return cmdfunc;
@@ -628,8 +668,15 @@ void free_modcmd() {
         next_cb = cb->next;
         free(next_cb);
     }
+    struct cmd_bot_alias *botalias, *next_botalias;
+    for(botalias = bot_aliases; botalias; botalias = next_botalias) {
+        next_botalias = botalias->next;
+        free(botalias->alias);
+        free(botalias);
+    }
     cmd_functions = NULL;
     trigger_callbacks = NULL;
+    bot_aliases = NULL;
 }
 
 void bind_set_parameters(int botid, char *cmd, char *parameters) {
@@ -701,3 +748,62 @@ struct cmd_binding *find_cmd_binding(int botid, char *cmd) {
     return NULL;
 }
 
+void bind_unbound_required_functions(int botid) {
+    struct cmd_function *cmdfunc;
+    int i, found;
+    struct cmd_binding *cbind;
+    for(cmdfunc = cmd_functions; cmdfunc; cmdfunc = cmdfunc->next) {
+        if((cmdfunc->flags & CMDFLAG_REQUIRED)) {
+            found = 0;
+            for(i = 0; i < 27; i++) {
+                for(cbind = cmd_binds[i]; cbind; cbind = cbind->next) {
+                    if(cbind->botid == botid && cbind->func == cmdfunc) {
+                        found = 1;
+                        break;
+                    }
+                }
+                if(found)
+                    break;
+            }
+            if(!found && bind_cmd_to_function(botid, cmdfunc->name, cmdfunc)) {
+                cbind = find_cmd_binding(botid, cmdfunc->name);
+                cbind->flags |= CMDFLAG_TEMPONARY_BIND;
+            }
+        }
+    }
+}
+
+void register_command_alias(int botid, char *alias) {
+    struct cmd_bot_alias *botalias;
+    for(botalias = bot_aliases; botalias; botalias = botalias->next) {
+        if(!stricmp(botalias->alias, alias))
+            return;
+    }
+    botalias = malloc(sizeof(*botalias));
+    if (!botalias) {
+        perror("malloc() failed");
+        return;
+    }
+    botalias->botid = botid;
+    botalias->alias = strdup(alias);
+    botalias->next = bot_aliases;
+    bot_aliases = botalias;
+}
+
+struct cmd_binding *getAllBinds(struct cmd_binding *last) {
+    int bind_index;
+    if(last) {
+        if(last->next)
+            return last->next;
+        bind_index = get_binds_index(last->cmd[0]) + 1;
+        if(bind_index > 26)
+            return NULL;
+    } else
+        bind_index = 0;
+    do {
+        if(cmd_binds[bind_index])
+            return cmd_binds[bind_index];
+        bind_index++;
+    } while(bind_index < 27);
+    return NULL;
+}