]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/command_parse.cpp
Cut down on debug on restart
[user/henk/code/inspircd.git] / src / command_parse.cpp
index 13ba64169f71f85844eb71f663c704ff20dc68c7..cb43df1a1bb15eb25896b061b450d6b03139cfbe 100644 (file)
@@ -1,13 +1,10 @@
-/*       +------------------------------------+
+       /*       +------------------------------------+
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *                <Craig@chatspike.net>
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
  *
- * Written by Craig Edwards, Craig McLure, and others.
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  *
@@ -24,7 +21,6 @@
 #include "wildcard.h"
 #include "xline.h"
 #include "socketengine.h"
-#include "userprocess.h"
 #include "socket.h"
 #include "command_parse.h"
 
@@ -38,10 +34,10 @@ bool InspIRCd::ULine(const char* server)
        return (find(Config->ulines.begin(),Config->ulines.end(),server) != Config->ulines.end());
 }
 
-int InspIRCd::OperPassCompare(const char* data,const char* input)
+int InspIRCd::OperPassCompare(const char* data,const char* input, int tagnumber)
 {
        int MOD_RESULT = 0;
-       FOREACH_RESULT_I(this,I_OnOperCompare,OnOperCompare(data,input))
+       FOREACH_RESULT_I(this,I_OnOperCompare,OnOperCompare(data, input, tagnumber))
        Log(DEBUG,"OperPassCompare: %d",MOD_RESULT);
        if (MOD_RESULT == 1)
                return 0;
@@ -211,11 +207,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 +226,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 +255,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 +269,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 +318,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 +343,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 +389,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 +419,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 +428,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
@@ -403,28 +452,33 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
 
 bool CommandParser::RemoveCommands(const char* source)
 {
-       bool go_again = true;
-
-       while (go_again)
+       nspace::hash_map<std::string,command_t*>::iterator i,safei;
+       for (i = cmdlist.begin(); i != cmdlist.end(); i++)
        {
-               go_again = false;
-
-               for (nspace::hash_map<std::string,command_t*>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
+               safei = i;
+               safei++;
+               if (safei != cmdlist.end())
                {
-                       command_t* x = i->second;
-                       if (x->source == std::string(source))
-                       {
-                               ServerInstance->Log(DEBUG,"removecommands(%s) Removing dependent command: %s",x->source.c_str(),x->command.c_str());
-                               cmdlist.erase(i);
-                               go_again = true;
-                               break;
-                       }
+                       RemoveCommand(safei, source);
                }
        }
-
+       safei = cmdlist.begin();
+       if (safei != cmdlist.end())
+       {
+               RemoveCommand(safei, source);
+       }
        return true;
 }
 
+void CommandParser::RemoveCommand(nspace::hash_map<std::string,command_t*>::iterator safei, const char* source)
+{
+       command_t* x = safei->second;
+       if (x->source == std::string(source))
+       {
+               cmdlist.erase(safei);
+       }
+}
+
 void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
 {
        std::string::size_type a;
@@ -444,8 +498,22 @@ void CommandParser::ProcessBuffer(std::string &buffer,userrec *user)
        }
 }
 
-bool CommandParser::CreateCommand(command_t *f)
+bool CommandParser::CreateCommand(command_t *f, void* so_handle)
 {
+       if (so_handle)
+       {
+               if (RFCCommands.find(f->command) == RFCCommands.end())
+               {
+                       RFCCommands[f->command] = so_handle;
+                       ServerInstance->Log(DEFAULT,"Monitoring RFC-specified reloadable command at %8x",so_handle);
+               }
+               else
+               {
+                       ServerInstance->Log(DEFAULT,"ERK! Somehow, we loaded a cmd_*.so file twice! Only the first instance is being recorded.");
+                       return false;
+               }
+       }
+
        /* create the command and push it onto the table */
        if (cmdlist.find(f->command) == cmdlist.end())
        {
@@ -458,17 +526,70 @@ bool CommandParser::CreateCommand(command_t *f)
 
 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)
+{
+       char filename[MAXBUF];
+       char commandname[MAXBUF];
+       int y = 0;
+
+       for (const char* x = cmd; *x; x++, y++)
+               commandname[y] = toupper(*x);
+
+       commandname[y] = 0;
+
+       SharedObjectList::iterator command = RFCCommands.find(commandname);
+
+       if (command != RFCCommands.end())
+       {
+               command_t* cmdptr = cmdlist.find(commandname)->second;
+               cmdlist.erase(cmdlist.find(commandname));
+
+               for (char* x = commandname; *x; x++)
+                       *x = tolower(*x);
+
+
+               delete cmdptr;
+               dlclose(command->second);
+               RFCCommands.erase(command);
+
+               snprintf(filename, MAXBUF, "cmd_%s.so", commandname);
+               this->LoadCommand(filename);
+
+               return true;
+       }
+
+       return false;
+}
+
+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]);
+               ServerInstance->WriteOpers("*** RELOAD: %s reloaded the '%s' command.", user->nick, parameters[0]);
+               return CMD_SUCCESS;
+       }
+       else
+       {
+               user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
+               return CMD_FAILURE;
        }
 }
 
@@ -484,17 +605,22 @@ 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);
+       if (this->FindSym((void **)&cmd_factory_func, h))
+       {
+               command_t* newcommand = cmd_factory_func(ServerInstance);
+               this->CreateCommand(newcommand, h);
+       }
 }
 
 void CommandParser::SetupCommandTable()
 {
+       RFCCommands.clear();
+
        DIR* library = opendir(LIBRARYDIR);
        if (library)
        {
@@ -508,5 +634,7 @@ void CommandParser::SetupCommandTable()
                }
                closedir(library);
        }
+
+       this->CreateCommand(new cmd_reload(ServerInstance));
 }