]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_tre.cpp
Update the module descriptions.
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_tre.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2016-2017, 2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24 /// $CompilerFlags: find_compiler_flags("tre")
25 /// $LinkerFlags: find_linker_flags("tre" "-ltre")
26
27 /// $PackageInfo: require_system("arch") pkgconf tre
28 /// $PackageInfo: require_system("darwin") pkg-config tre
29 /// $PackageInfo: require_system("debian") libtre-dev pkg-config
30 /// $PackageInfo: require_system("ubuntu") libtre-dev pkg-config
31
32 #include "inspircd.h"
33 #include "modules/regex.h"
34 #include <sys/types.h>
35 #include <tre/regex.h>
36
37 class TRERegex : public Regex
38 {
39         regex_t regbuf;
40
41 public:
42         TRERegex(const std::string& rx) : Regex(rx)
43         {
44                 int flags = REG_EXTENDED | REG_NOSUB;
45                 int errcode;
46                 errcode = regcomp(&regbuf, rx.c_str(), flags);
47                 if (errcode)
48                 {
49                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
50                         std::string error;
51                         char* errbuf;
52                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
53                         errbuf = new char[sz + 1];
54                         memset(errbuf, 0, sz + 1);
55                         regerror(errcode, &regbuf, errbuf, sz + 1);
56                         error = errbuf;
57                         delete[] errbuf;
58                         regfree(&regbuf);
59                         throw RegexException(rx, error);
60                 }
61         }
62
63         ~TRERegex()
64         {
65                 regfree(&regbuf);
66         }
67
68         bool Matches(const std::string& text)  CXX11_OVERRIDE
69         {
70                 return (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0);
71         }
72 };
73
74 class TREFactory : public RegexFactory
75 {
76  public:
77         TREFactory(Module* m) : RegexFactory(m, "regex/tre") {}
78         Regex* Create(const std::string& expr) CXX11_OVERRIDE
79         {
80                 return new TRERegex(expr);
81         }
82 };
83
84 class ModuleRegexTRE : public Module
85 {
86         TREFactory trf;
87
88  public:
89         ModuleRegexTRE() : trf(this)
90         {
91         }
92
93         Version GetVersion() CXX11_OVERRIDE
94         {
95                 return Version("Provides the tre regular expression engine which uses the TRE library.", VF_VENDOR);
96         }
97 };
98
99 MODULE_INIT(ModuleRegexTRE)