Add support for regex flags to the filter module.

This commit is contained in:
Sadie Powell 2024-10-27 14:41:27 +00:00
parent bce693bfe3
commit 37a820efcb
2 changed files with 30 additions and 4 deletions

View File

@ -983,6 +983,9 @@
# stdregex - stdlib regexps, provided via regex_stdlib, see comment # # stdregex - stdlib regexps, provided via regex_stdlib, see comment #
# at the <module> tag for info on availability. # # at the <module> 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 # # If notifyuser is set to no, the user will not be notified when #
# their message is blocked. # # their message is blocked. #
# # # #
@ -991,7 +994,10 @@
# warning will be sent to opers instead. This stops spambots which # # warning will be sent to opers instead. This stops spambots which #
# send their spam message to themselves first to check if it is being # # send their spam message to themselves first to check if it is being #
# filtered by the server. # # filtered by the server. #
#<filteropts engine="stdregex" notifyuser="yes" warnonselfmsg="no"> #<filteropts engine="stdregex"
# enableflags="yes"
# notifyuser="yes"
# warnonselfmsg="no">
# # # #
# Your choice of regex engine must match on all servers network-wide. # # Your choice of regex engine must match on all servers network-wide. #
# # # #

View File

@ -85,7 +85,7 @@ public:
bool flag_strip_color; bool flag_strip_color;
bool flag_no_registered; 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) : freeform(free)
, reason(rea) , reason(rea)
, action(act) , action(act)
@ -94,7 +94,7 @@ public:
{ {
if (!RegexEngine) if (!RegexEngine)
throw ModuleException(thismod, "Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!"); 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); this->FillFlags(fla);
} }
@ -202,6 +202,7 @@ private:
Account::API accountapi; Account::API accountapi;
bool initing = true; bool initing = true;
bool enableflags;
bool notifyuser; bool notifyuser;
bool warnonselfmsg; bool warnonselfmsg;
bool dirty = false; bool dirty = false;
@ -233,6 +234,7 @@ public:
bool DeleteFilter(const std::string& freeform, std::string& reason); bool DeleteFilter(const std::string& freeform, std::string& reason);
std::pair<bool, std::string> AddFilter(const std::string& freeform, FilterAction type, const std::string& reason, unsigned long duration, const std::string& flags, bool config = false); std::pair<bool, std::string> 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 ReadConfig(ConfigStatus& status) override;
void CompareLinkData(const LinkData& otherdata, LinkDataDiff& diffs) override;
void GetLinkData(LinkData& data, std::string& compatdata) override; void GetLinkData(LinkData& data, std::string& compatdata) override;
static std::string EncodeFilter(const FilterResult& filter); static std::string EncodeFilter(const FilterResult& filter);
FilterResult DecodeFilter(const std::string& data); FilterResult DecodeFilter(const std::string& data);
@ -645,6 +647,7 @@ void ModuleFilter::ReadConfig(ConfigStatus& status)
const auto& tag = ServerInstance->Config->ConfValue("filteropts"); const auto& tag = ServerInstance->Config->ConfValue("filteropts");
std::string newrxengine = tag->getString("engine"); std::string newrxengine = tag->getString("engine");
enableflags = tag->getBool("enableflags");
notifyuser = tag->getBool("notifyuser", true); notifyuser = tag->getBool("notifyuser", true);
warnonselfmsg = tag->getBool("warnonselfmsg"); warnonselfmsg = tag->getBool("warnonselfmsg");
filterconf = tag->getString("filename"); filterconf = tag->getString("filename");
@ -680,6 +683,20 @@ void ModuleFilter::ReadConfig(ConfigStatus& status)
ReadFilters(); 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) void ModuleFilter::GetLinkData(LinkData& data, std::string& compatdata)
{ {
if (RegexEngine) if (RegexEngine)
@ -689,6 +706,9 @@ void ModuleFilter::GetLinkData(LinkData& data, std::string& compatdata)
} }
else else
data["regex"] = "broken"; data["regex"] = "broken";
if (enableflags)
data["flags"] = "yes";
} }
std::string ModuleFilter::EncodeFilter(const FilterResult& filter) std::string ModuleFilter::EncodeFilter(const FilterResult& filter)
@ -815,7 +835,7 @@ std::pair<bool, std::string> ModuleFilter::AddFilter(const std::string& freeform
try try
{ {
filters.emplace_back(RegexEngine, freeform, reason, type, duration, flgs, config); filters.emplace_back(RegexEngine, freeform, reason, type, duration, flgs, config, enableflags);
dirty = true; dirty = true;
} }
catch (const ModuleException& e) catch (const ModuleException& e)