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