]> 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 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                         regexcl.assign(rx, fltype | std::regex::optimize);
38                 }
39                 catch(std::regex_error rxerr)
40                 {
41                         throw RegexException(rx, rxerr.what());
42                 }
43         }
44
45         bool Matches(const std::string& text) CXX11_OVERRIDE
46         {
47                 return std::regex_search(text, regexcl);
48         }
49 };
50
51 class StdRegexFactory : public RegexFactory
52 {
53  public:
54         std::regex::flag_type regextype;
55         StdRegexFactory(Module* m) : RegexFactory(m, "regex/stdregex") {}
56         Regex* Create(const std::string& expr) CXX11_OVERRIDE
57         {
58                 return new StdRegex(expr, regextype);
59         }
60 };
61
62 class ModuleRegexStd : public Module
63 {
64 public:
65         StdRegexFactory ref;
66         ModuleRegexStd() : ref(this)
67         {
68         }
69
70         Version GetVersion() CXX11_OVERRIDE
71         {
72                 return Version("Provides a regular expression engine which uses the C++11 std::regex regular expression matching system.", VF_VENDOR);
73         }
74
75         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
76         {
77                 ConfigTag* Conf = ServerInstance->Config->ConfValue("stdregex");
78
79                 const std::string regextype = Conf->getString("type", "ecmascript", 1);
80                 if (stdalgo::string::equalsci(regextype, "bre"))
81                         ref.regextype = std::regex::basic;
82                 else if (stdalgo::string::equalsci(regextype, "ere"))
83                         ref.regextype = std::regex::extended;
84                 else if (stdalgo::string::equalsci(regextype, "awk"))
85                         ref.regextype = std::regex::awk;
86                 else if (stdalgo::string::equalsci(regextype, "grep"))
87                         ref.regextype = std::regex::grep;
88                 else if (stdalgo::string::equalsci(regextype, "egrep"))
89                         ref.regextype = std::regex::egrep;
90                 else
91                 {
92                         if (!stdalgo::string::equalsci(regextype, "ecmascript"))
93                                 ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Nonexistent regex engine '%s' specified. Falling back to ECMAScript.", regextype.c_str());
94                         ref.regextype = std::regex::ECMAScript;
95                 }
96         }
97 };
98
99 MODULE_INIT(ModuleRegexStd)