From f1d04a2f8e4e9077d07a94b64478bb331b49bbc0 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 6 Dec 2010 20:53:15 -0500 Subject: [PATCH] Allow command aliases to be redirected to different pseudo clients --- data/example.conf | 38 +++++++++++++++------------- include/modules.h | 2 +- modules/core/os_defcon.cpp | 2 +- modules/extra/m_alias.cpp | 51 ++++++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 40 deletions(-) diff --git a/data/example.conf b/data/example.conf index df6bcee3d..ddbdbbc42 100644 --- a/data/example.conf +++ b/data/example.conf @@ -1871,37 +1871,41 @@ m_xmlrpc module { name = "m_alias" } alias { - /* Set to yes to make this alias triggerabe by fantasy commands. - */ + /* Set to yes to make this alias triggerable by fantasy commands. */ fantasy = no - - /* Target client the alias should be for (if not using fantasy). - */ - client = "NickServ" - - /* Alias name and command */ - alias = "ID" - command = "IDENTIFY" - /* Set to yes to make this alias oper only */ operonly = no + + /* Source client and command. + */ + source_client = "NickServ" + source_command = "ID" + + /* Target client and command. + */ + target_client = "NickServ" + target_command = "IDENTIFY" } /* Provides the !k fantasy command */ alias { - fantasy = true - alias = "K" - command = "KICK" + fantasy = yes + source_command = "K" + + target_client = "ChanServ" + target_command = "KICK" } /* Provides the !kb fantasy command */ alias { - fantasy = true - alias = "KB" - command = "BAN" + fantasy = yes + source_command = "KB" + + target_client = "ChanServ" + target_command = "BAN" } /* diff --git a/include/modules.h b/include/modules.h index c96c1ae27..55697dbb9 100644 --- a/include/modules.h +++ b/include/modules.h @@ -367,7 +367,7 @@ class CoreExport Module : public Extensible * @param ci If a tanasy command, the channel the comman was used on * @return EVENT_CONTINUE to let other modules decide, EVENT_STOP to halt the command and not process it */ - virtual EventReturn OnPreCommandRun(User *u, BotInfo *bi, Anope::string &command, Anope::string &message, ChannelInfo *ci) { return EVENT_CONTINUE; } + virtual EventReturn OnPreCommandRun(User *&u, BotInfo *&bi, Anope::string &command, Anope::string &message, ChannelInfo *&ci) { return EVENT_CONTINUE; } /** Called before a command is due to be executed. * @param source The source of the command diff --git a/modules/core/os_defcon.cpp b/modules/core/os_defcon.cpp index cc8809586..88f7b4d79 100644 --- a/modules/core/os_defcon.cpp +++ b/modules/core/os_defcon.cpp @@ -206,7 +206,7 @@ class OSDefcon : public Module return EVENT_CONTINUE; } - EventReturn OnPreCommandRun(User *u, BotInfo *bi, Anope::string &command, Anope::string &message, ChannelInfo *ci) + EventReturn OnPreCommandRun(User *&u, BotInfo *&bi, Anope::string &command, Anope::string &message, ChannelInfo *&ci) { if (!u->HasMode(UMODE_OPER) && (CheckDefCon(DEFCON_OPER_ONLY) || CheckDefCon(DEFCON_SILENT_OPER_ONLY))) { diff --git a/modules/extra/m_alias.cpp b/modules/extra/m_alias.cpp index 3b9556772..53f04e3b4 100644 --- a/modules/extra/m_alias.cpp +++ b/modules/extra/m_alias.cpp @@ -11,14 +11,15 @@ struct CommandAlias { bool fantasy; bool operonly; - Anope::string client; - Anope::string alias; - Anope::string command; + Anope::string source_client; + Anope::string source_command; + Anope::string target_client; + Anope::string target_command; }; class ModuleAlias : public Module { - std::map > aliases; + std::multimap > aliases; public: ModuleAlias(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator) { @@ -38,40 +39,48 @@ class ModuleAlias : public Module { bool fantasy = config.ReadFlag("alias", "fantasy", "no", i); bool operonly = config.ReadFlag("alias", "operonly", "no", i); - Anope::string client = config.ReadValue("alias", "client", "", i); - Anope::string aliasname = config.ReadValue("alias", "alias", "", i); - Anope::string command = config.ReadValue("alias", "command", "", i); + Anope::string source_client = config.ReadValue("alias", "source_client", "", i); + Anope::string source_command = config.ReadValue("alias", "source_command", "", i); + Anope::string target_client = config.ReadValue("alias", "target_client", "", i); + Anope::string target_command = config.ReadValue("alias", "target_command", "", i); - if (aliasname.empty() || command.empty()) + if ((!fantasy &&source_client.empty()) || source_command.empty() || target_client.empty() || target_command.empty()) continue; CommandAlias alias; alias.fantasy = fantasy; alias.operonly = operonly; - alias.client = client; - alias.alias = aliasname; - alias.command = command; + alias.source_client = source_client; + alias.source_command = source_command; + alias.target_client = target_client; + alias.target_command = target_command; - this->aliases.insert(std::make_pair(aliasname, alias)); + this->aliases.insert(std::make_pair(source_command, alias)); } } - EventReturn OnPreCommandRun(User *u, BotInfo *bi, Anope::string &command, Anope::string &message, ChannelInfo *ci) + EventReturn OnPreCommandRun(User *&u, BotInfo *&bi, Anope::string &command, Anope::string &message, ChannelInfo *&ci) { bool fantasy = ci != NULL; - std::map >::const_iterator it = aliases.find(command); - if (it != aliases.end()) + std::map >::const_iterator it = aliases.find(command), + it_end = aliases.upper_bound(command); + for (; it != it_end; ++it) { const CommandAlias &alias = it->second; - if (fantasy != alias.fantasy) - return EVENT_CONTINUE; + + if (!fantasy && !bi->nick.equals_ci(alias.source_client)) + continue; + else if (fantasy != alias.fantasy) + continue; else if (!u->HasMode(UMODE_OPER) && alias.operonly) - return EVENT_CONTINUE; - else if (!fantasy && !bi->nick.equals_ci(alias.client)) - return EVENT_CONTINUE; + continue; - command = alias.command; + BotInfo *target = findbot(alias.target_client); + if (target) + bi = target; + command = alias.target_command; + break; } return EVENT_CONTINUE;