completed cmd_adduser and added CMDFLAG_REGISTERED_CHAN flag
[NeonServV5.git] / modcmd.c
index 98c9e2ea6ee4ce45cde164193cd77aca6e28bec2..9273f0265b999b010435b3bc64da26c8824266dd 100644 (file)
--- a/modcmd.c
+++ b/modcmd.c
@@ -1,11 +1,13 @@
 
 #include "modcmd.h"
 #include "IRCEvents.h"
+#include "IRCParser.h"
 #include "ClientSocket.h"
 #include "UserNode.h"
 #include "ChanNode.h"
 #include "ChanUser.h"
 #include "WHOHandler.h"
+#include "lang.h"
 
 struct trigger_callback {
     int botid;
@@ -15,7 +17,7 @@ struct trigger_callback {
 };
 
 struct command_check_user_cache {
-    struct ClientSocket *client;
+    struct ClientSocket *client, *textclient;
     struct UserNode *user;
     struct ChanNode *chan;
     char **argv;
@@ -27,6 +29,17 @@ 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 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_AUTH_REQUIRED",    "You need to be authenticated with AuthServ to use this command."},
+    {"MODCMD_PRIVILEGED",       "§b%s§b is a privileged command."},
+    
+    
+    {NULL, NULL}
+};
 
 static int get_binds_index(char first_char) {
     if(tolower(first_char) >= 'a' && tolower(first_char) <= 'z') {
@@ -75,8 +88,13 @@ static char* get_channel_trigger(int botid, struct ChanNode *chan) {
 static USERAUTH_CALLBACK(command_checked_auth) {
     struct command_check_user_cache *cache = data;
     int execute_cmd = 1;
+    tmp_text_client = cache->textclient;
     if((cache->cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
-        //AUTH_REQUIRED
+        reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
+        execute_cmd = 0;
+    }
+    else if((cache->cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
+        reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cache->cbind->cmd);
         execute_cmd = 0;
     }
     if(execute_cmd) {
@@ -107,6 +125,8 @@ 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) {
+            //get a text bot
+            tmp_text_client = get_prefered_bot(client->botid);
             //parse the arguments...
             char *arga[MAXNUMPARAMS];
             char **argv;
@@ -155,7 +175,7 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                             uarga[uargpos++] = argv[argi-1];
                             if(allargs) {
                                 for(;argi < argc; argi++)
-                                    uarga[uargpos++] = argv[argi-1]
+                                    uarga[uargpos++] = argv[argi-1];
                             }
                         } else if(!strcmp(prev_ppos, "c"))
                             uarga[uargpos++] = (chan ? chan->name : NULL);
@@ -171,13 +191,29 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 argc = uargpos;
             }
             if(argc < cbind->func->paramcount) {
-                //LESS_PARAM_COUNT
+                reply(tmp_text_client, user, "MODCMD_LESS_PARAM_COUNT");
                 break;
             }
             if((cbind->func->flags & CMDFLAG_REQUIRE_CHAN) && !chan) {
-                //CHAN_REQUIRED
+                reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
                 break;
-            } 
+            }
+            if((cbind->func->flags & CMDFLAG_REGISTERED_CHAN)) {
+                load_channel_settings(chan);
+                if(!(chan->flags & CHANFLAG_CHAN_REGISTERED)) {
+                    reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
+                    break;
+                }
+                check_mysql();
+                MYSQL_RES *res;
+                MYSQL_ROW row;
+                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);
+                res = mysql_use();
+                if ((row = mysql_fetch_row(res)) == NULL) {
+                    reply(tmp_text_client, user, "MODCMD_CHAN_REQUIRED");
+                    break;
+                }
+            }
             if((cbind->func->flags & CMDFLAG_CHECK_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
                 //check auth...
                 struct command_check_user_cache *data = malloc(sizeof(*data));
@@ -194,11 +230,16 @@ static void handle_command(struct ClientSocket *client, struct UserNode *user, s
                 data->chan = chan;
                 data->message = message;
                 data->cbind = cbind;
+                data->textclient = tmp_text_client;
                 get_userauth(user, command_checked_auth, data);
                 return;
             }
             if((cbind->func->flags & CMDFLAG_REQUIRE_AUTH) && !(user->flags & USERFLAG_ISAUTHED)) {
-                //AUTH_REQUIRED
+                reply(tmp_text_client, user, "MODCMD_AUTH_REQUIRED");
+                break;
+            }
+            if((cbind->func->flags & CMDFLAG_REQUIRE_GOD) && !isGodMode(user)) {
+                reply(tmp_text_client, user, "MODCMD_PRIVILEGED", cbind->cmd);
                 break;
             }
             cbind->func->func(client, user, chan, argv, argc);
@@ -311,6 +352,7 @@ int bind_cmd_to_function(int botid, char *cmd, struct cmd_function *func) {
     cbind->cmd = strdup(cmd);
     cbind->func = func;
     cbind->parameters = NULL;
+    cbind->gaccess = 0;
     cbind->flags = 0;
     cbind->next = cmd_binds[bind_index];
     cmd_binds[bind_index] = cbind;
@@ -340,6 +382,7 @@ int bind_cmd_to_command(int botid, char *cmd, char *func) {
     cbind->func = cmdfunc;
     cbind->next = cmd_binds[bind_index];
     cbind->parameters = NULL;
+    cbind->gaccess = 0;
     cbind->flags = 0;
     cmd_binds[bind_index] = cbind;
     return 1;
@@ -365,10 +408,15 @@ int unbind_cmd(int botid, char *cmd) {
     return 0;
 }
 
+struct ClientSocket *getTextBot() {
+    return tmp_text_client;
+}
+
 void init_modcmd() {
     cmd_binds = calloc(27, sizeof(*cmd_binds));
     bind_chanmsg(got_chanmsg);
     bind_privmsg(got_privmsg);
+    register_default_language_table(msgtab);
 }
 
 void free_modcmd() {
@@ -399,3 +447,11 @@ void free_modcmd() {
     trigger_callbacks = NULL;
 }
 
+void bind_set_parameters(int botid, char *cmd, char *parameters) {
+    
+}
+
+void bind_set_gaccess(int botid, char *cmd, int gaccess) {
+    
+}
+