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