]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_re2.cpp
Add RE2 regex module.
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_re2.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Peter Powell <petpow@saberuk.com>
5  *   Copyright (C) 2012 ChrisTX <chris@rev-crew.info>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #if defined __GNUC__
22 # pragma GCC diagnostic ignored "-Wshadow"
23 #endif
24
25 #include "inspircd.h"
26 #include "modules/regex.h"
27 #include <re2/re2.h>
28
29
30 /* $ModDesc: Regex Provider Module for RE2 Regular Expressions */
31 /* $CompileFlags: -std=c++11 */
32 /* $LinkerFlags: -lre2 */
33 /* $ModDep: modules/regex.h */
34
35 class RE2Exception : public ModuleException
36 {
37  public:
38          RE2Exception(const std::string& rx, const std::string& error)
39                 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
40         {
41         }
42 };
43
44 class RE2Regex : public Regex
45 {
46         RE2 regexcl;
47
48  public:
49         RE2Regex(const std::string& rx) : Regex(rx), regexcl(rx, RE2::Quiet)
50         {
51                 if (!regexcl.ok())
52                 {
53                         throw RE2Exception(rx, regexcl.error());
54                 }
55         }
56
57         bool Matches(const std::string& text)
58         {
59                 return RE2::FullMatch(text, regexcl);
60         }
61 };
62
63 class RE2Factory : public RegexFactory
64 {
65  public:
66         RE2Factory(Module* m) : RegexFactory(m, "regex/re2") { }
67         Regex* Create(const std::string& expr)
68         {
69                 return new RE2Regex(expr);
70         }
71 };
72
73 class ModuleRegexRE2 : public Module
74 {
75         RE2Factory ref;
76
77  public:
78         ModuleRegexRE2() : ref(this)
79         {
80                 ServerInstance->Modules->AddService(ref);
81         }
82
83         Version GetVersion() CXX11_OVERRIDE
84         {
85                 return Version("Regex Provider Module for RE2", VF_VENDOR);
86         }
87 };
88
89 MODULE_INIT(ModuleRegexRE2)