]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_stdlib.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_stdlib.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
5  *   Copyright (C) 2013, 2016, 2018, 2020-2021 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 /// $CompilerFlags: -std=c++11
23
24
25 #include "inspircd.h"
26 #include "modules/regex.h"
27 #include <regex>
28
29 class StdRegex : public Regex
30 {
31         std::regex regexcl;
32
33  public:
34         StdRegex(const std::string& rx, std::regex::flag_type fltype) : Regex(rx)
35         {
36                 try
37                 {
38                         regexcl.assign(rx, fltype | std::regex::optimize);
39                 }
40                 catch(const std::regex_error& rxerr)
41                 {
42                         throw RegexException(rx, rxerr.what());
43                 }
44         }
45
46         bool Matches(const std::string& text) CXX11_OVERRIDE
47         {
48                 return std::regex_search(text, regexcl);
49         }
50 };
51
52 class StdRegexFactory : public RegexFactory
53 {
54  public:
55         std::regex::flag_type regextype;
56         StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
57         Regex* Create(const std::string& expr) CXX11_OVERRIDE
58         {
59                 return new StdRegex(expr, regextype);
60         }
61 };
62
63 class ModuleRegexStd : public Module
64 {
65 public:
66         StdRegexFactory ref;
67         ModuleRegexStd() : ref(this)
68         {
69         }
70
71         Version GetVersion() CXX11_OVERRIDE
72         {
73                 return Version("Provides the stdregex regular expression engine which uses the C++11 std::regex regular expression matching system.", VF_VENDOR);
74         }
75
76         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
77         {
78                 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
79
80                 const std::string regextype = Conf->getString("type", "ecmascript", 1);
81                 if (stdalgo::string::equalsci(regextype, "bre"))
82                         ref.regextype = std::regex::basic;
83                 else if (stdalgo::string::equalsci(regextype, "ere"))
84                         ref.regextype = std::regex::extended;
85                 else if (stdalgo::string::equalsci(regextype, "awk"))
86                         ref.regextype = std::regex::awk;
87                 else if (stdalgo::string::equalsci(regextype, "grep"))
88                         ref.regextype = std::regex::grep;
89                 else if (stdalgo::string::equalsci(regextype, "egrep"))
90                         ref.regextype = std::regex::egrep;
91                 else
92                 {
93                         if (!stdalgo::string::equalsci(regextype, "ecmascript"))
94                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Nonexistent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
95                         ref.regextype = std::regex::ECMAScript;
96                 }
97         }
98 };
99
100 MODULE_INIT(ModuleRegexStd)