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)
37 regexcl.assign(rx, fltype | std::regex::optimize);
39 catch(std::regex_error rxerr)
41 throw RegexException(rx, rxerr.what());
45 bool Matches(const std::string& text) CXX11_OVERRIDE
47 return std::regex_search(text, regexcl);
51 class StdRegexFactory : public RegexFactory
54 std::regex::flag_type regextype;
55 StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
56 Regex* Create(const std::string& expr) CXX11_OVERRIDE
58 return new StdRegex(expr, regextype);
62 class ModuleRegexStd : public Module
66 ModuleRegexStd() : ref(this)
70 Version GetVersion() CXX11_OVERRIDE
72 return Version("Provides a regular expression engine which uses the C++11 std::regex regular expression matching system.", VF_VENDOR);
75 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
77 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
79 const std::string regextype = Conf->getString("type", "ecmascript", 1);
80 if (stdalgo::string::equalsci(regextype, "bre"))
81 ref.regextype = std::regex::basic;
82 else if (stdalgo::string::equalsci(regextype, "ere"))
83 ref.regextype = std::regex::extended;
84 else if (stdalgo::string::equalsci(regextype, "awk"))
85 ref.regextype = std::regex::awk;
86 else if (stdalgo::string::equalsci(regextype, "grep"))
87 ref.regextype = std::regex::grep;
88 else if (stdalgo::string::equalsci(regextype, "egrep"))
89 ref.regextype = std::regex::egrep;
92 if (!stdalgo::string::equalsci(regextype, "ecmascript"))
93 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Nonexistent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
94 ref.regextype = std::regex::ECMAScript;
99 MODULE_INIT(ModuleRegexStd)