// 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);
{
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;
}
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;
* 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
}
}
-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())
{
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]);
+ return CMD_SUCCESS;
+ }
+ else
+ {
+ user->WriteServ("NOTICE %s :*** Could not reload command '%s'", user->nick, parameters[0]);
+ return CMD_FAILURE;
}
}
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)
{
}
closedir(library);
}
+
+ this->CreateCommand(new cmd_reload(ServerInstance));
}