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