From 37a820efcb53b9d6bda8f6821a5a0c0142b776a3 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 27 Oct 2024 14:41:27 +0000 Subject: [PATCH] Add support for regex flags to the filter module. --- docs/conf/modules.example.conf | 8 +++++++- src/modules/m_filter.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/docs/conf/modules.example.conf b/docs/conf/modules.example.conf index 3bd950f73..66593afa5 100644 --- a/docs/conf/modules.example.conf +++ b/docs/conf/modules.example.conf @@ -983,6 +983,9 @@ # stdregex - stdlib regexps, provided via regex_stdlib, see comment # # at the tag for info on availability. # # # +# If enableflags is set, you can specify flags that modify matching # +# of the regular expression. # +# # # If notifyuser is set to no, the user will not be notified when # # their message is blocked. # # # @@ -991,7 +994,10 @@ # warning will be sent to opers instead. This stops spambots which # # send their spam message to themselves first to check if it is being # # filtered by the server. # -# +# # # # Your choice of regex engine must match on all servers network-wide. # # # diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index a84fce2b6..8fe610e7a 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -85,7 +85,7 @@ public: bool flag_strip_color; bool flag_no_registered; - FilterResult(Regex::EngineReference& RegexEngine, const std::string& free, const std::string& rea, FilterAction act, unsigned long gt, const std::string& fla, bool cfg) + FilterResult(Regex::EngineReference& RegexEngine, const std::string& free, const std::string& rea, FilterAction act, unsigned long gt, const std::string& fla, bool cfg, bool ef) : freeform(free) , reason(rea) , action(act) @@ -94,7 +94,7 @@ public: { if (!RegexEngine) throw ModuleException(thismod, "Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!"); - regex = RegexEngine->Create(free); + regex = ef ? RegexEngine->CreateHuman(free) : RegexEngine->Create(free); this->FillFlags(fla); } @@ -202,6 +202,7 @@ private: Account::API accountapi; bool initing = true; + bool enableflags; bool notifyuser; bool warnonselfmsg; bool dirty = false; @@ -233,6 +234,7 @@ public: bool DeleteFilter(const std::string& freeform, std::string& reason); std::pair AddFilter(const std::string& freeform, FilterAction type, const std::string& reason, unsigned long duration, const std::string& flags, bool config = false); void ReadConfig(ConfigStatus& status) override; + void CompareLinkData(const LinkData& otherdata, LinkDataDiff& diffs) override; void GetLinkData(LinkData& data, std::string& compatdata) override; static std::string EncodeFilter(const FilterResult& filter); FilterResult DecodeFilter(const std::string& data); @@ -645,6 +647,7 @@ void ModuleFilter::ReadConfig(ConfigStatus& status) const auto& tag = ServerInstance->Config->ConfValue("filteropts"); std::string newrxengine = tag->getString("engine"); + enableflags = tag->getBool("enableflags"); notifyuser = tag->getBool("notifyuser", true); warnonselfmsg = tag->getBool("warnonselfmsg"); filterconf = tag->getString("filename"); @@ -680,6 +683,20 @@ void ModuleFilter::ReadConfig(ConfigStatus& status) ReadFilters(); } +void ModuleFilter::CompareLinkData(const LinkData& otherdata, LinkDataDiff& diffs) +{ + Module::CompareLinkData(otherdata, diffs); + + auto it = diffs.find("flags"); + if (it == diffs.end()) + return; // Should never happen. + + if (!it->second.first) + it->second.first = "no"; + if (!it->second.second) + it->second.second = "no"; +} + void ModuleFilter::GetLinkData(LinkData& data, std::string& compatdata) { if (RegexEngine) @@ -689,6 +706,9 @@ void ModuleFilter::GetLinkData(LinkData& data, std::string& compatdata) } else data["regex"] = "broken"; + + if (enableflags) + data["flags"] = "yes"; } std::string ModuleFilter::EncodeFilter(const FilterResult& filter) @@ -815,7 +835,7 @@ std::pair ModuleFilter::AddFilter(const std::string& freeform try { - filters.emplace_back(RegexEngine, freeform, reason, type, duration, flgs, config); + filters.emplace_back(RegexEngine, freeform, reason, type, duration, flgs, config, enableflags); dirty = true; } catch (const ModuleException& e)