From 1621a84f963c9fbfd61a52a85015fd8f2255dbf4 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Tue, 28 Jul 2020 16:43:59 +0100 Subject: Rewrite the regex system from scratch. * Move everything to the Regex namespace: - Regex -> Regex::Pattern - RegexException -> Regex::Exception - RegexFactory -> Regex::Engine * Add support for regex flags. - Regex::OPT_CASE_INSENSITIVE performs case-insensitive matching. * Add the Regex::EngineReference class as a friendly wrapper around dynamic_reference_nocheck. * Add the Regex::SimpleEngine template class for automating the implementation of regex factory classes. * Use std::shared_ptr for Regex::Pattern objects instead of making users manage memory manually. --- src/modules/m_rline.cpp | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) (limited to 'src/modules/m_rline.cpp') diff --git a/src/modules/m_rline.cpp b/src/modules/m_rline.cpp index ce271df23..eb152d7c0 100644 --- a/src/modules/m_rline.cpp +++ b/src/modules/m_rline.cpp @@ -46,7 +46,7 @@ class RLine : public XLine * @param regex Pattern to match with * @ */ - RLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference& rxfactory) + RLine(time_t s_time, unsigned long d, const std::string& src, const std::string& re, const std::string& regexs, Regex::EngineReference& rxfactory) : XLine(s_time, d, src, re, "R") , matchtext(regexs) { @@ -56,13 +56,6 @@ class RLine : public XLine regex = rxfactory->Create(regexs); } - /** Destructor - */ - ~RLine() - { - delete regex; - } - bool Matches(User* u) override { LocalUser* lu = IS_LOCAL(u); @@ -71,12 +64,12 @@ class RLine : public XLine const std::string host = u->nick + "!" + u->ident + "@" + u->GetRealHost() + " " + u->GetRealName(); const std::string ip = u->nick + "!" + u->ident + "@" + u->GetIPString() + " " + u->GetRealName(); - return (regex->Matches(host) || regex->Matches(ip)); + return (regex->IsMatch(host) || regex->IsMatch(ip)); } bool Matches(const std::string& compare) override { - return regex->Matches(compare); + return regex->IsMatch(compare); } void Apply(User* u) override @@ -104,7 +97,7 @@ class RLine : public XLine std::string matchtext; - Regex *regex; + Regex::PatternPtr regex; }; @@ -113,8 +106,8 @@ class RLine : public XLine class RLineFactory : public XLineFactory { public: - dynamic_reference& rxfactory; - RLineFactory(dynamic_reference& rx) : XLineFactory("R"), rxfactory(rx) + Regex::EngineReference& rxfactory; + RLineFactory(Regex::EngineReference& rx) : XLineFactory("R"), rxfactory(rx) { } @@ -226,18 +219,18 @@ class ModuleRLine , public Stats::EventListener { private: - dynamic_reference rxfactory; + Regex::EngineReference rxfactory; RLineFactory f; CommandRLine r; bool MatchOnNickChange; bool initing = true; - RegexFactory* factory; + Regex::Engine* factory; public: ModuleRLine() : Module(VF_VENDOR | VF_COMMON, "Adds the /RLINE command which allows server operators to prevent users matching a nickname!username@hostname+realname regular expression from connecting to the server.") , Stats::EventListener(this) - , rxfactory(this, "regex") + , rxfactory(this) , f(rxfactory) , r(this, f) { @@ -284,11 +277,7 @@ class ModuleRLine factory = rxfactory ? (rxfactory.operator->()) : NULL; - if (newrxengine.empty()) - rxfactory.SetProvider("regex"); - else - rxfactory.SetProvider("regex/" + newrxengine); - + rxfactory.SetEngine(newrxengine); if (!rxfactory) { if (newrxengine.empty()) -- cgit v1.3.1-10-gc9f91