]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_stdlib.cpp
Merge insp20
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_stdlib.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19 #include "inspircd.h"
20 #include "modules/regex.h"
21 #include <regex>
22
23 /* $CompileFlags: -std=c++11 */
24
25 class StdRegex : public Regex
26 {
27         std::regex regexcl;
28
29  public:
30         StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
31         {
32                 try{
33                         regexcl.assign(rx, fltype | std::regex::optimize);
34                 }
35                 catch(std::regex_error rxerr)
36                 {
37                         throw RegexException(rx, rxerr.what());
38                 }
39         }
40
41         bool Matches(const std::string& text) CXX11_OVERRIDE
42         {
43                 return std::regex_search(text, regexcl);
44         }
45 };
46
47 class StdRegexFactory : public RegexFactory
48 {
49  public:
50         std::regex::flag_type regextype;
51         StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
52         Regex* Create(const std::string& expr) CXX11_OVERRIDE
53         {
54                 return new StdRegex(expr, regextype);
55         }
56 };
57
58 class ModuleRegexStd : public Module
59 {
60 public:
61         StdRegexFactory ref;
62         ModuleRegexStd() : ref(this)
63         {
64         }
65
66         Version GetVersion() CXX11_OVERRIDE
67         {
68                 return Version("Regex Provider Module for std::regex", VF_VENDOR);
69         }
70
71         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
72         {
73                 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
74                 std::string regextype = Conf->getString("type", "ecmascript");
75
76                 if(regextype == "bre")
77                         ref.regextype = std::regex::basic;
78                 else if(regextype == "ere")
79                         ref.regextype = std::regex::extended;
80                 else if(regextype == "awk")
81                         ref.regextype = std::regex::awk;
82                 else if(regextype == "grep")
83                         ref.regextype = std::regex::grep;
84                 else if(regextype == "egrep")
85                         ref.regextype = std::regex::egrep;
86                 else
87                 {
88                         if(regextype != "ecmascript")
89                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
90                         ref.regextype = std::regex::ECMAScript;
91                 }
92         }
93 };
94
95 MODULE_INIT(ModuleRegexStd)