2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* $ModDesc: Regex Provider Module for std::regex Regular Expressions */
24 /* $ModConfig: <stdregex type="ecmascript">
25 * Specify the Regular Expression engine to use here. Valid settings are
26 * bre, ere, awk, grep, egrep, ecmascript (default if not specified)*/
27 /* $CompileFlags: -std=c++11 */
28 /* $ModDep: m_regex.h */
30 class StdRegexException : public ModuleException
33 StdRegexException(const std::string& rx, const std::string& error)
34 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
39 class StdRegex : public Regex
44 StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
47 regexcl.assign(rx, fltype | std::regex::optimize);
49 catch(std::regex_error rxerr)
51 throw StdRegexException(rx, rxerr.what());
55 virtual bool Matches(const std::string& text)
57 return std::regex_search(text, regexcl);
61 class StdRegexFactory : public RegexFactory
64 std::regex::flag_type regextype;
65 StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
66 Regex* Create(const std::string& expr)
68 return new StdRegex(expr, regextype);
72 class ModuleRegexStd : public Module
76 ModuleRegexStd() : ref(this) {
77 ServerInstance->Modules->AddService(ref);
78 Implementation eventlist[] = { I_OnRehash };
79 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
85 return Version("Regex Provider Module for std::regex", VF_VENDOR);
88 void OnRehash(User* u)
90 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
91 std::string regextype = Conf->getString("type", "ecmascript");
93 if(regextype == "bre")
94 ref.regextype = std::regex::basic;
95 else if(regextype == "ere")
96 ref.regextype = std::regex::extended;
97 else if(regextype == "awk")
98 ref.regextype = std::regex::awk;
99 else if(regextype == "grep")
100 ref.regextype = std::regex::grep;
101 else if(regextype == "egrep")
102 ref.regextype = std::regex::egrep;
105 if(regextype != "ecmascript")
106 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
107 ref.regextype = std::regex::ECMAScript;
112 MODULE_INIT(ModuleRegexStd)