]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/command_parse.cpp
Whoops, make /stats s work
[user/henk/code/inspircd.git] / src / command_parse.cpp
index 7febff972904d059b40235ae35e101e5f6b17f27..b67b269d20bd42e3ec115a5a510eaeec4a6f8a9c 100644 (file)
 #include "inspircd.h"
 #include "configreader.h"
 #include <algorithm>
+#include <dirent.h>
+#include <dlfcn.h>
 #include "users.h"
 #include "modules.h"
 #include "wildcard.h"
 #include "xline.h"
 #include "socketengine.h"
-#include "userprocess.h"
 #include "socket.h"
 #include "command_parse.h"
-#define nspace __gnu_cxx
-
-/*       XXX Serious WTFness XXX
- *
- * Well, unless someone invents a wildcard or
- * regexp #include, and makes it a standard,
- * we're stuck with this way of including all
- * the commands.
- */
-
-#include "commands/cmd_admin.h"
-#include "commands/cmd_away.h"
-#include "commands/cmd_commands.h"
-#include "commands/cmd_connect.h"
-#include "commands/cmd_die.h"
-#include "commands/cmd_eline.h"
-#include "commands/cmd_gline.h"
-#include "commands/cmd_info.h"
-#include "commands/cmd_invite.h"
-#include "commands/cmd_ison.h"
-#include "commands/cmd_join.h"
-#include "commands/cmd_kick.h"
-#include "commands/cmd_kill.h"
-#include "commands/cmd_kline.h"
-#include "commands/cmd_links.h"
-#include "commands/cmd_list.h"
-#include "commands/cmd_loadmodule.h"
-#include "commands/cmd_lusers.h"
-#include "commands/cmd_map.h"
-#include "commands/cmd_modules.h"
-#include "commands/cmd_motd.h"
-#include "commands/cmd_names.h"
-#include "commands/cmd_nick.h"
-#include "commands/cmd_notice.h"
-#include "commands/cmd_oper.h"
-#include "commands/cmd_part.h"
-#include "commands/cmd_pass.h"
-#include "commands/cmd_ping.h"
-#include "commands/cmd_pong.h"
-#include "commands/cmd_privmsg.h"
-#include "commands/cmd_qline.h"
-#include "commands/cmd_quit.h"
-#include "commands/cmd_rehash.h"
-#include "commands/cmd_restart.h"
-#include "commands/cmd_rules.h"
-#include "commands/cmd_server.h"
-#include "commands/cmd_squit.h"
-#include "commands/cmd_stats.h"
-#include "commands/cmd_summon.h"
-#include "commands/cmd_time.h"
-#include "commands/cmd_topic.h"
-#include "commands/cmd_trace.h"
-#include "commands/cmd_unloadmodule.h"
-#include "commands/cmd_user.h"
-#include "commands/cmd_userhost.h"
-#include "commands/cmd_users.h"
-#include "commands/cmd_version.h"
-#include "commands/cmd_wallops.h"
-#include "commands/cmd_who.h"
-#include "commands/cmd_whois.h"
-#include "commands/cmd_whowas.h"
-#include "commands/cmd_zline.h"
 
 bool InspIRCd::ULine(const char* server)
 {
@@ -253,16 +192,6 @@ bool InspIRCd::NickMatchesEveryone(const std::string &nick, userrec* user)
        return false;
 }
 
-
-
-
-
-/* Special commands which may occur without registration of the user */
-cmd_user* command_user;
-cmd_nick* command_nick;
-cmd_pass* command_pass;
-
-
 /* LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
  * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
  * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
@@ -281,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
@@ -294,10 +229,22 @@ 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();
+
+                       CommandObj->Handle(new_parameters,pcnt,user);
+
+                       dupes[item.c_str()] = true;
+               }
        }
        return 1;
 }
@@ -310,9 +257,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.
@@ -321,8 +271,20 @@ 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();
+                       CommandObj->Handle(new_parameters,pcnt,user);
+
+                       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
@@ -352,9 +314,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);
 
@@ -368,36 +339,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;
        }
@@ -437,14 +408,14 @@ void CommandParser::ProcessCommand(userrec *user, std::string &cmd)
                                        user->WriteServ("304 %s :SYNTAX %s %s", user->nick, cm->second->command.c_str(), cm->second->syntax.c_str());
                                return;
                        }
-                       if ((user->registered == REG_ALL) || (cm->second == command_user) || (cm->second == command_nick) || (cm->second == command_pass))
+                       if ((user->registered == REG_ALL) || (cm->second->WorksBeforeReg()))
                        {
                                /* ikky /stats counters */
                                cm->second->use_count++;
                                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;
 
@@ -453,8 +424,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
@@ -514,8 +489,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())
        {
@@ -528,73 +517,114 @@ bool CommandParser::CreateCommand(command_t *f)
 
 CommandParser::CommandParser(InspIRCd* Instance) : ServerInstance(Instance)
 {
+       para.resize(128);
        this->SetupCommandTable();
 }
 
+bool CommandParser::FindSym(void** v, void* h)
+{
+       *v = dlsym(h, "init_command");
+       const char* err = dlerror();
+       if (err)
+       {
+               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]);
+               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)
+{
+       char filename[MAXBUF];
+       void* h;
+       command_t* (*cmd_factory_func)(InspIRCd*);
+
+       snprintf(filename, MAXBUF, "%s/%s", LIBRARYDIR, name);
+       ServerInstance->Log(DEBUG,"Load command: %s", filename);
+
+       h = dlopen(filename, RTLD_NOW | RTLD_GLOBAL);
+
+       if (!h)
+       {
+               ServerInstance->Log(SPARSE, "Error loading core command: %s", dlerror());
+               return;
+       }
+
+       if (this->FindSym((void **)&cmd_factory_func, h))
+       {
+               command_t* newcommand = cmd_factory_func(ServerInstance);
+               this->CreateCommand(newcommand, h);
+       }
+}
+
 void CommandParser::SetupCommandTable()
 {
-       /* These three are special (can occur without
-        * full user registration) and so are saved
-        * for later use.
-        */
-       command_user = new cmd_user(ServerInstance);
-       command_nick = new cmd_nick(ServerInstance);
-       command_pass = new cmd_pass(ServerInstance);
-       this->CreateCommand(command_user);
-       this->CreateCommand(command_nick);
-       this->CreateCommand(command_pass);
-
-       /* The rest of these arent special. boo hoo.
-        */
-       this->CreateCommand(new cmd_quit(ServerInstance));
-       this->CreateCommand(new cmd_version(ServerInstance));
-       this->CreateCommand(new cmd_ping(ServerInstance));
-       this->CreateCommand(new cmd_pong(ServerInstance));
-       this->CreateCommand(new cmd_admin(ServerInstance));
-       this->CreateCommand(new cmd_privmsg(ServerInstance));
-       this->CreateCommand(new cmd_info(ServerInstance));
-       this->CreateCommand(new cmd_time(ServerInstance));
-       this->CreateCommand(new cmd_whois(ServerInstance));
-       this->CreateCommand(new cmd_wallops(ServerInstance));
-       this->CreateCommand(new cmd_notice(ServerInstance));
-       this->CreateCommand(new cmd_join(ServerInstance));
-       this->CreateCommand(new cmd_names(ServerInstance));
-       this->CreateCommand(new cmd_part(ServerInstance));
-       this->CreateCommand(new cmd_kick(ServerInstance));
-       this->CreateCommand(new cmd_mode(ServerInstance));
-       this->CreateCommand(new cmd_topic(ServerInstance));
-       this->CreateCommand(new cmd_who(ServerInstance));
-       this->CreateCommand(new cmd_motd(ServerInstance));
-       this->CreateCommand(new cmd_rules(ServerInstance));
-       this->CreateCommand(new cmd_oper(ServerInstance));
-       this->CreateCommand(new cmd_list(ServerInstance));
-       this->CreateCommand(new cmd_die(ServerInstance));
-       this->CreateCommand(new cmd_restart(ServerInstance));
-       this->CreateCommand(new cmd_kill(ServerInstance));
-       this->CreateCommand(new cmd_rehash(ServerInstance));
-       this->CreateCommand(new cmd_lusers(ServerInstance));
-       this->CreateCommand(new cmd_stats(ServerInstance));
-       this->CreateCommand(new cmd_userhost(ServerInstance));
-       this->CreateCommand(new cmd_away(ServerInstance));
-       this->CreateCommand(new cmd_ison(ServerInstance));
-       this->CreateCommand(new cmd_summon(ServerInstance));
-       this->CreateCommand(new cmd_users(ServerInstance));
-       this->CreateCommand(new cmd_invite(ServerInstance));
-       this->CreateCommand(new cmd_trace(ServerInstance));
-       this->CreateCommand(new cmd_whowas(ServerInstance));
-       this->CreateCommand(new cmd_connect(ServerInstance));
-       this->CreateCommand(new cmd_squit(ServerInstance));
-       this->CreateCommand(new cmd_modules(ServerInstance));
-       this->CreateCommand(new cmd_links(ServerInstance));
-       this->CreateCommand(new cmd_map(ServerInstance));
-       this->CreateCommand(new cmd_kline(ServerInstance));
-       this->CreateCommand(new cmd_gline(ServerInstance));
-       this->CreateCommand(new cmd_zline(ServerInstance));
-       this->CreateCommand(new cmd_qline(ServerInstance));
-       this->CreateCommand(new cmd_eline(ServerInstance));
-       this->CreateCommand(new cmd_loadmodule(ServerInstance));
-       this->CreateCommand(new cmd_unloadmodule(ServerInstance));
-       this->CreateCommand(new cmd_server(ServerInstance));
-       this->CreateCommand(new cmd_commands(ServerInstance));
+       RFCCommands.clear();
+
+       DIR* library = opendir(LIBRARYDIR);
+       if (library)
+       {
+               dirent* entry = NULL;
+               while ((entry = readdir(library)))
+               {
+                       if (match(entry->d_name, "cmd_*.so"))
+                       {
+                               this->LoadCommand(entry->d_name);
+                       }
+               }
+               closedir(library);
+       }
+
+       this->CreateCommand(new cmd_reload(ServerInstance));
 }