]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_stdlib.cpp
m_spanningtree Remove SpanningTreeUtilities* fields and parameters
[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 StdRegexException : public ModuleException
26 {
27  public:
28         StdRegexException(const std::string& rx, const std::string& error)
29                 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
30         {
31         }
32 };
33
34 class StdRegex : public Regex
35 {
36         std::regex regexcl;
37
38  public:
39         StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
40         {
41                 try{
42                         regexcl.assign(rx, fltype | std::regex::optimize);
43                 }
44                 catch(std::regex_error rxerr)
45                 {
46                         throw StdRegexException(rx, rxerr.what());
47                 }
48         }
49
50         bool Matches(const std::string& text)
51         {
52                 return std::regex_search(text, regexcl);
53         }
54 };
55
56 class StdRegexFactory : public RegexFactory
57 {
58  public:
59         std::regex::flag_type regextype;
60         StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
61         Regex* Create(const std::string& expr)
62         {
63                 return new StdRegex(expr, regextype);
64         }
65 };
66
67 class ModuleRegexStd : public Module
68 {
69 public:
70         StdRegexFactory ref;
71         ModuleRegexStd() : ref(this)
72         {
73                 ServerInstance->Modules->AddService(ref);
74                 OnRehash(NULL);
75         }
76
77         Version GetVersion() CXX11_OVERRIDE
78         {
79                 return Version("Regex Provider Module for std::regex", VF_VENDOR);
80         }
81
82         void OnRehash(User* u) CXX11_OVERRIDE
83         {
84                 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
85                 std::string regextype = Conf->getString("type", "ecmascript");
86
87                 if(regextype == "bre")
88                         ref.regextype = std::regex::basic;
89                 else if(regextype == "ere")
90                         ref.regextype = std::regex::extended;
91                 else if(regextype == "awk")
92                         ref.regextype = std::regex::awk;
93                 else if(regextype == "grep")
94                         ref.regextype = std::regex::grep;
95                 else if(regextype == "egrep")
96                         ref.regextype = std::regex::egrep;
97                 else
98                 {
99                         if(regextype != "ecmascript")
100                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
101                         ref.regextype = std::regex::ECMAScript;
102                 }
103         }
104 };
105
106 MODULE_INIT(ModuleRegexStd)