]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_stdlib.cpp
Release v2.0.25
[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 "m_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: m_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 private:
42         std::regex regexcl;
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                 ServerInstance->Modules->AddService(ref);
78                 Implementation eventlist[] = { I_OnRehash };
79                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
80                 OnRehash(NULL);
81         }
82
83         Version GetVersion()
84         {
85                 return Version("Regex Provider Module for std::regex", VF_VENDOR);
86         }
87         
88         void OnRehash(User* u)
89         {
90                 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
91                 std::string regextype = Conf->getString("type", "ecmascript");
92                 
93                 if(regextype == "bre")
94                         ref.regextype = std::regex::basic;
95                 else if(regextype == "ere")
96                         ref.regextype = std::regex::extended;
97                 else if(regextype == "awk")
98                         ref.regextype = std::regex::awk;
99                 else if(regextype == "grep")
100                         ref.regextype = std::regex::grep;
101                 else if(regextype == "egrep")
102                         ref.regextype = std::regex::egrep;
103                 else
104                 {
105                         if(regextype != "ecmascript")
106                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Non-existent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
107                         ref.regextype = std::regex::ECMAScript;
108                 }
109         }
110 };
111
112 MODULE_INIT(ModuleRegexStd)