2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
5 * Copyright (C) 2013, 2016, 2018, 2020 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /// $CompilerFlags: -std=c++11
26 #include "modules/regex.h"
29 class StdRegex : public Regex
34 StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
38 regexcl.assign(rx, fltype | std::regex::optimize);
40 catch(const std::regex_error& rxerr)
42 throw RegexException(rx, rxerr.what());
46 bool Matches(const std::string& text) CXX11_OVERRIDE
48 return std::regex_search(text, regexcl);
52 class StdRegexFactory : public RegexFactory
55 std::regex::flag_type regextype;
56 StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
57 Regex* Create(const std::string& expr) CXX11_OVERRIDE
59 return new StdRegex(expr, regextype);
63 class ModuleRegexStd : public Module
67 ModuleRegexStd() : ref(this)
71 Version GetVersion() CXX11_OVERRIDE
73 return Version("Provides the stdregex regular expression engine which uses the C++11 std::regex regular expression matching system.", VF_VENDOR);
76 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
78 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
80 const std::string regextype = Conf->getString("type", "ecmascript", 1);
81 if (stdalgo::string::equalsci(regextype, "bre"))
82 ref.regextype = std::regex::basic;
83 else if (stdalgo::string::equalsci(regextype, "ere"))
84 ref.regextype = std::regex::extended;
85 else if (stdalgo::string::equalsci(regextype, "awk"))
86 ref.regextype = std::regex::awk;
87 else if (stdalgo::string::equalsci(regextype, "grep"))
88 ref.regextype = std::regex::grep;
89 else if (stdalgo::string::equalsci(regextype, "egrep"))
90 ref.regextype = std::regex::egrep;
93 if (!stdalgo::string::equalsci(regextype, "ecmascript"))
94 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Nonexistent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
95 ref.regextype = std::regex::ECMAScript;
100 MODULE_INIT(ModuleRegexStd)