]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_tre.cpp
Merge pull request #591 from SaberUK/master+config-tweaks
[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 /* $ModDep: modules/regex.h */
29
30 class TRERegexException : public ModuleException
31 {
32  public:
33         TRERegexException(const std::string& rx, const std::string& error)
34                 : ModuleException("Error in regex " + rx + ": " + error)
35         {
36         }
37 };
38
39 class TRERegex : public Regex
40 {
41         regex_t regbuf;
42
43 public:
44         TRERegex(const std::string& rx) : Regex(rx)
45         {
46                 int flags = REG_EXTENDED | REG_NOSUB;
47                 int errcode;
48                 errcode = regcomp(&regbuf, rx.c_str(), flags);
49                 if (errcode)
50                 {
51                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
52                         std::string error;
53                         char* errbuf;
54                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
55                         errbuf = new char[sz + 1];
56                         memset(errbuf, 0, sz + 1);
57                         regerror(errcode, &regbuf, errbuf, sz + 1);
58                         error = errbuf;
59                         delete[] errbuf;
60                         regfree(&regbuf);
61                         throw TRERegexException(rx, error);
62                 }
63         }
64
65         ~TRERegex()
66         {
67                 regfree(&regbuf);
68         }
69
70         bool Matches(const std::string& text)
71         {
72                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
73                 {
74                         // Bang. :D
75                         return true;
76                 }
77                 return false;
78         }
79 };
80
81 class TREFactory : public RegexFactory
82 {
83  public:
84         TREFactory(Module* m) : RegexFactory(m, "regex/tre") {}
85         Regex* Create(const std::string& expr)
86         {
87                 return new TRERegex(expr);
88         }
89 };
90
91 class ModuleRegexTRE : public Module
92 {
93         TREFactory trf;
94
95  public:
96         ModuleRegexTRE() : trf(this)
97         {
98                 ServerInstance->Modules->AddService(trf);
99         }
100
101         Version GetVersion() CXX11_OVERRIDE
102         {
103                 return Version("Regex Provider Module for TRE Regular Expressions", VF_VENDOR);
104         }
105 };
106
107 MODULE_INIT(ModuleRegexTRE)