]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/command_parse.cpp
Add irc::dynamicbitmask class. Feel free to take a look and offer suggestions, as...
[user/henk/code/inspircd.git] / src / command_parse.cpp
index 600ce2e6198b21f45de5d16891c113ffaa4f2f83..d2b40806d3aa044574cd267990d24731b9e9d86d 100644 (file)
@@ -24,7 +24,6 @@
 #include "wildcard.h"
 #include "xline.h"
 #include "socketengine.h"
-#include "userprocess.h"
 #include "socket.h"
 #include "command_parse.h"
 
@@ -211,11 +210,17 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
        if (!strchr(parameters[splithere],','))
                return 0;
 
+       /** Some lame ircds will weed out dupes using some shitty O(n^2) algorithm.
+        * By using std::map (thanks for the idea w00t) we can cut this down a ton.
+        * ...VOOODOOOO!
+        */
+       std::map<irc::string, bool> dupes;
+
        /* Create two lists, one for channel names, one for keys
         */
        irc::commasepstream items1(parameters[splithere]);
        irc::commasepstream items2(parameters[extra]);
-       std::string item = "";
+       std::string item = "*";
        unsigned int max = 0;
 
        /* Attempt to iterate these lists and call the command objech
@@ -224,10 +229,23 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
         */
        while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
        {
-               std::string extrastuff = items2.GetToken();
-               parameters[splithere] = item.c_str();
-               parameters[extra] = extrastuff.c_str();
-               CommandObj->Handle(parameters,pcnt,user);
+               if (dupes.find(item.c_str()) == dupes.end())
+               {
+                       const char* new_parameters[127];
+
+                       for (int t = 0; (t < pcnt) && (t < 127); t++)
+                               new_parameters[t] = parameters[t];
+
+                       std::string extrastuff = items2.GetToken();
+
+                       new_parameters[splithere] = item.c_str();
+                       new_parameters[extra] = extrastuff.c_str();
+
+                       if (CommandObj->Handle(new_parameters,pcnt,user) == CMD_USER_DELETED)
+                               return 1;
+
+                       dupes[item.c_str()] = true;
+               }
        }
        return 1;
 }
@@ -240,9 +258,12 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
        if (!strchr(parameters[splithere],','))
                return 0;
 
+       std::map<irc::string, bool> dupes;
+
        /* Only one commasepstream here */
+       ServerInstance->Log(DEBUG,"Splitting '%s'",parameters[splithere]);
        irc::commasepstream items1(parameters[splithere]);
-       std::string item = "";
+       std::string item = "*";
        unsigned int max = 0;
 
        /* Parse the commasepstream until there are no tokens remaining.
@@ -251,8 +272,26 @@ int CommandParser::LoopCall(userrec* user, command_t* CommandObj, const char** p
         */
        while (((item = items1.GetToken()) != "") && (max++ < ServerInstance->Config->MaxTargets))
        {
-               parameters[splithere] = item.c_str();
-               CommandObj->Handle(parameters,pcnt,user);
+               if (dupes.find(item.c_str()) == dupes.end())
+               {
+                       const char* new_parameters[127];
+
+                       for (int t = 0; (t < pcnt) && (t < 127); t++)
+                               new_parameters[t] = parameters[t];
+
+                       new_parameters[splithere] = item.c_str();
+
+                       parameters[splithere] = item.c_str();
+
+                       /* Execute the command handler over and over. If someone pulls our user
+                        * record out from under us (e.g. if we /kill a comma sep list, and we're
+                        * in that list ourselves) abort if we're gone.
+                        */
+                       if (CommandObj->Handle(new_parameters,pcnt,user) == CMD_USER_DELETED)
+                               return 1;
+
+                       dupes[item.c_str()] = true;
+               }
        }
        /* By returning 1 we tell our caller that nothing is to be done,
         * as all the previous calls handled the data. This makes the parent
@@ -282,9 +321,18 @@ bool CommandParser::IsValidCommand(const std::string &commandname, int pcnt, use
        return false;
 }
 
+command_t* CommandParser::GetHandler(const std::string &commandname)
+{
+       nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
+       if (n != cmdlist.end())
+               return n->second;
+
+       return NULL;
+}
+
 // calls a handler function for a command
 
-bool CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
+CmdResult CommandParser::CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user)
 {
        nspace::hash_map<std::string,command_t*>::iterator n = cmdlist.find(commandname);
 
@@ -298,36 +346,36 @@ bool CommandParser::CallHandler(const std::string &commandname,const char** para
                                {
                                        if ((user->HasPermission(commandname)) || (!IS_LOCAL(user)))
                                        {
-                                               n->second->Handle(parameters,pcnt,user);
-                                               return true;
+                                               return n->second->Handle(parameters,pcnt,user);
                                        }
                                }
                                else
                                {
-                                       n->second->Handle(parameters,pcnt,user);
-                                       return true;
+                                       return n->second->Handle(parameters,pcnt,user);
                                }
                        }
                }
        }
-       return false;
+       return CMD_INVALID;
 }
 
 void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
 {
        const char *command_p[127];
        int items = 0;
-       std::string para[127];
        irc::tokenstream tokens(cmd);
        std::string command = tokens.GetToken();
 
        while (((para[items] = tokens.GetToken()) != "") && (items < 127))
-               command_p[items] = para[items++].c_str();
+       {
+               command_p[items] = para[items].c_str();
+               items++;
+       }
 
        std::transform(command.begin(), command.end(), command.begin(), ::toupper);
                
        int MOD_RESULT = 0;
-       FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false));
+       FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,false,cmd));
        if (MOD_RESULT == 1) {
                return;
        }
@@ -344,7 +392,7 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                        {
                                if (!user->IsModeSet(cm->second->flags_needed))
                                {
-                                       user->WriteServ("481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
+                                       user->WriteServ("481 %s :Permission Denied- You do not have the required operator privileges",user->nick);
                                        return;
                                }
                                if (!user->HasPermission(command))
@@ -374,7 +422,7 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                                cm->second->total_bytes += cmd.length();
 
                                int MOD_RESULT = 0;
-                               FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true));
+                               FOREACH_RESULT(I_OnPreCommand,OnPreCommand(command,command_p,items,user,true,cmd));
                                if (MOD_RESULT == 1)
                                        return;
 
@@ -383,8 +431,12 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                                 * command handler call, as the handler
                                 * may free the user structure!
                                 */
+                               CmdResult result = cm->second->Handle(command_p,items,user);
 
-                               cm->second->Handle(command_p,items,user);
+                               if (result != CMD_USER_DELETED)
+                               {
+                                       FOREACH_MOD(I_OnPostCommand,OnPostCommand(command, command_p, items, user, result,cmd));
+                               }
                                return;
                        }
                        else
@@ -472,18 +524,20 @@ bool CommandParser::CreateCommand(command_t *f, void* so_handle)
 
 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
 {
+       para.resize(128);
        this->SetupCommandTable();
 }
 
-void CommandParser::FindSym(void** v, void* h)
+bool CommandParser::FindSym(void** v, void* h)
 {
        *v = dlsym(h, "init_command");
        const char* err = dlerror();
        if (err)
        {
-               printf("ERROR: %s\n",err);
-               exit(0);
+               ServerInstance->Log(SPARSE, "Error loading core command: %s\n", err);
+               return false;
        }
+       return true;
 }
 
 bool CommandParser::ReloadCommand(const char* cmd)
@@ -510,8 +564,9 @@ bool CommandParser::ReloadCommand(const char* cmd)
 
                delete cmdptr;
                dlclose(command->second);
+               RFCCommands.erase(command);
 
-               sprintf(filename, "cmd_%s.so", commandname);
+               snprintf(filename, MAXBUF, "cmd_%s.so", commandname);
                this->LoadCommand(filename);
 
                return true;
@@ -520,13 +575,19 @@ bool CommandParser::ReloadCommand(const char* cmd)
        return false;
 }
 
-void cmd_reload::Handle(const char** parameters, int pcnt, userrec *user)
+CmdResult cmd_reload::Handle(const char** parameters, int pcnt, userrec *user)
 {
        user->WriteServ("NOTICE %s :*** Reloading command '%s'",user->nick, parameters[0]);
        if (ServerInstance->Parser->ReloadCommand(parameters[0]))
+       {
                user->WriteServ("NOTICE %s :*** Successfully reloaded command '%s'", user->nick, parameters[0]);
+               return CMD_SUCCESS;
+       }
        else
+       {
                user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
+               return CMD_FAILURE;
+       }
 }
 
 void CommandParser::LoadCommand(const char* name)
@@ -541,13 +602,16 @@ void CommandParser::LoadCommand(const char* name)
        h = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
 
        if (!h)
+       {
+               ServerInstance->Log(SPARSE, "Error loading core command: %s", dlerror());
                return;
+       }
 
-       this->FindSym((void **)&cmd_factory_func, h);
-
-       command_t* newcommand = cmd_factory_func(ServerInstance);
-
-       this->CreateCommand(newcommand, h);
+       if (this->FindSym((void **)&cmd_factory_func, h))
+       {
+               command_t* newcommand = cmd_factory_func(ServerInstance);
+               this->CreateCommand(newcommand, h);
+       }
 }
 
 void CommandParser::SetupCommandTable()