]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_tre.cpp
Add ConfigTag::getUInt for reading unsigned config values.
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_tre.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
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 /// $CompilerFlags: find_compiler_flags("tre")
21 /// $LinkerFlags: find_linker_flags("tre" "-ltre")
22
23 /// $PackageInfo: require_system("darwin") pkg-config tre
24 /// $PackageInfo: require_system("debian") libtre-dev pkg-config
25 /// $PackageInfo: require_system("ubuntu") libtre-dev pkg-config
26
27 #include "inspircd.h"
28 #include "modules/regex.h"
29 #include <sys/types.h>
30 #include <tre/regex.h>
31
32 class TRERegex : public Regex
33 {
34         regex_t regbuf;
35
36 public:
37         TRERegex(const std::string& rx) : Regex(rx)
38         {
39                 int flags = REG_EXTENDED | REG_NOSUB;
40                 int errcode;
41                 errcode = regcomp(&regbuf, rx.c_str(), flags);
42                 if (errcode)
43                 {
44                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
45                         std::string error;
46                         char* errbuf;
47                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
48                         errbuf = new char[sz + 1];
49                         memset(errbuf, 0, sz + 1);
50                         regerror(errcode, &regbuf, errbuf, sz + 1);
51                         error = errbuf;
52                         delete[] errbuf;
53                         regfree(&regbuf);
54                         throw RegexException(rx, error);
55                 }
56         }
57
58         ~TRERegex()
59         {
60                 regfree(&regbuf);
61         }
62
63         bool Matches(const std::string& text)  CXX11_OVERRIDE
64         {
65                 return (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0);
66         }
67 };
68
69 class TREFactory : public RegexFactory
70 {
71  public:
72         TREFactory(Module* m) : RegexFactory(m, "regex/tre") {}
73         Regex* Create(const std::string& expr) CXX11_OVERRIDE
74         {
75                 return new TRERegex(expr);
76         }
77 };
78
79 class ModuleRegexTRE : public Module
80 {
81         TREFactory trf;
82
83  public:
84         ModuleRegexTRE() : trf(this)
85         {
86         }
87
88         Version GetVersion() CXX11_OVERRIDE
89         {
90                 return Version("Regex Provider Module for TRE Regular Expressions", VF_VENDOR);
91         }
92 };
93
94 MODULE_INIT(ModuleRegexTRE)