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