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