summaryrefslogtreecommitdiff
path: root/modules/extra/m_rewrite.cpp
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2013-01-08 20:25:01 -0500
committerAdam <Adam@anope.org>2013-01-08 20:25:01 -0500
commit9931ec299446c3d9e149f91a953748350c38eba6 (patch)
treefb3969d5aeb2ce23a7d2cde9fd9e47841b125c4b /modules/extra/m_rewrite.cpp
parent5f3dfc296041540960f372b010eea623bdd7d335 (diff)
Use m_rewrite to rewrite op, deop, halfop, ... etc
Diffstat (limited to 'modules/extra/m_rewrite.cpp')
-rw-r--r--modules/extra/m_rewrite.cpp129
1 files changed, 96 insertions, 33 deletions
diff --git a/modules/extra/m_rewrite.cpp b/modules/extra/m_rewrite.cpp
index 70710bd8a..669abb229 100644
--- a/modules/extra/m_rewrite.cpp
+++ b/modules/extra/m_rewrite.cpp
@@ -9,7 +9,7 @@
struct Rewrite
{
- Anope::string client, source_message, target_message;
+ Anope::string client, source_message, target_message, desc;
bool Matches(const std::vector<Anope::string> &message)
{
@@ -17,13 +17,13 @@ struct Rewrite
spacesepstream(this->source_message).GetTokens(sm);
for (unsigned i = 0; i < sm.size(); ++i)
- if (sm[i] != "$" && !sm[i].equals_ci(message[i]))
+ if (i >= message.size() || (sm[i] != "$" && !sm[i].equals_ci(message[i])))
return false;
return true;
}
- Anope::string Process(const std::vector<Anope::string> &params)
+ Anope::string Process(CommandSource &source, const std::vector<Anope::string> &params)
{
spacesepstream sep(this->target_message);
Anope::string token, message;
@@ -32,6 +32,8 @@ struct Rewrite
{
if (token[0] != '$')
message += " " + token;
+ else if (token == "$me")
+ message += " " + source.GetNick();
else
{
int num = -1, end = -1;
@@ -66,20 +68,97 @@ struct Rewrite
message.trim();
return message;
}
+
+ static std::vector<Rewrite> rewrites;
+
+ static Rewrite *Find(const Anope::string &client, const Anope::string &cmd)
+ {
+ for (unsigned i = 0; i < rewrites.size(); ++i)
+ {
+ Rewrite &r = rewrites[i];
+
+ if ((client.empty() || r.client.equals_ci(client)) && (r.source_message.equals_ci(cmd) || r.source_message.find_ci(cmd + " ") == 0))
+ return &r;
+ }
+
+ return NULL;
+ }
+
+ static Rewrite *Match(const Anope::string &client, const std::vector<Anope::string> &params)
+ {
+ for (unsigned i = 0; i < rewrites.size(); ++i)
+ {
+ Rewrite &r = rewrites[i];
+
+ if ((client.empty() || r.client.equals_ci(client)) && r.Matches(params))
+ return &r;
+ }
+
+ return NULL;
+ }
+};
+
+std::vector<Rewrite> Rewrite::rewrites;
+
+class RewriteCommand : public Command
+{
+ public:
+ RewriteCommand(Module *creator) : Command(creator, "rewrite", 0, 0) { }
+
+ void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
+ {
+ std::vector<Anope::string> full_params = params;
+ full_params.insert(full_params.begin(), source.command);
+
+ Rewrite *r = Rewrite::Match(!source.c ? source.service->nick : "", full_params);
+ if (r != NULL)
+ {
+ Anope::string new_message = r->Process(source, full_params);
+ Log(LOG_DEBUG) << "m_rewrite: Rewrote '" << source.command << (!params.empty() ? " " + params[0] : "") << "' to '" << new_message << "' using '" << r->source_message << "'";
+ source.service = BotInfo::Find(r->client);
+ if (!source.service)
+ return;
+ RunCommand(source, new_message);
+ }
+ else
+ Log() << "m_rewrite: Unable to rewrite '" << source.command << (!params.empty() ? " " + params[0] : "") << "'";
+ }
+
+ void OnServHelp(CommandSource &source) anope_override
+ {
+ Rewrite *r = Rewrite::Find(!source.c ? source.service->nick : "", source.command);
+ if (r != NULL && !r->desc.empty())
+ {
+ this->SetDesc(r->desc);
+ Command::OnServHelp(source);
+ }
+ }
+
+ bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
+ {
+ Rewrite *r = Rewrite::Find(!source.c ? source.service->nick : "", source.command);
+ if (r != NULL && !r->desc.empty())
+ {
+ source.Reply(r->desc);
+ size_t sz = r->target_message.find(' ');
+ source.Reply(_("This command is an alias to the command %s."), sz != Anope::string::npos ? r->target_message.substr(0, sz).c_str() : r->target_message.c_str());
+ return true;
+ }
+ return false;
+ }
};
class ModuleRewrite : public Module
{
- std::vector<Rewrite> rewrites;
+ RewriteCommand cmdrewrite;
public:
- ModuleRewrite(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED)
+ ModuleRewrite(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, SUPPORTED), cmdrewrite(this)
{
this->SetAuthor("Anope");
- Implementation i[] = { I_OnReload, I_OnBotPrivmsg };
+ Implementation i[] = { I_OnReload };
ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
- ModuleManager::SetPriority(this, PRIORITY_FIRST);
this->OnReload();
}
@@ -88,42 +167,26 @@ class ModuleRewrite : public Module
{
ConfigReader config;
- this->rewrites.clear();
+ Rewrite::rewrites.clear();
- for (int i = 0; i < config.Enumerate("rewrite"); ++i)
+ for (int i = 0; i < config.Enumerate("command"); ++i)
{
+ if (!config.ReadFlag("command", "rewrite", "no", i))
+ continue;
+
Rewrite rw;
- rw.client = config.ReadValue("rewrite", "client", "", i);
- rw.source_message = config.ReadValue("rewrite", "source_message", "", i),
- rw.target_message = config.ReadValue("rewrite", "target_message", "", i);
+ rw.client = config.ReadValue("command", "service", "", i);
+ rw.source_message = config.ReadValue("command", "rewrite_source", "", i),
+ rw.target_message = config.ReadValue("command", "rewrite_target", "", i);
+ rw.desc = config.ReadValue("command", "rewrite_description", "", i);
if (rw.client.empty() || rw.source_message.empty() || rw.target_message.empty())
continue;
- this->rewrites.push_back(rw);
+ Rewrite::rewrites.push_back(rw);
}
}
-
- EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &message) anope_override
- {
- std::vector<Anope::string> tokens;
- spacesepstream(message).GetTokens(tokens);
- for (unsigned i = 0; i < this->rewrites.size(); ++i)
- {
- Rewrite &rw = this->rewrites[i];
-
- if (rw.client == bi->nick && rw.Matches(tokens))
- {
- Anope::string new_message = rw.Process(tokens);
- Log(LOG_DEBUG) << "m_rewrite: Rewrote '" << message << "' to '" << new_message << "'";
- message = new_message;
- break;
- }
- }
-
- return EVENT_CONTINUE;
- }
};
MODULE_INIT(ModuleRewrite)