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