#include "inspircd.h"
#include "configreader.h"
#include <algorithm>
+#include <dirent.h>
+#include <dlfcn.h>
#include "users.h"
#include "modules.h"
#include "wildcard.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)
{
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,
// 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;
}
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;
* 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();
}
+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));
}