]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
m_spanningtree Remove SpanningTreeUtilities* fields and parameters
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_posix.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 <regex.h>
25
26 class POSIXRegexException : public ModuleException
27 {
28  public:
29         POSIXRegexException(const std::string& rx, const std::string& error)
30                 : ModuleException("Error in regex " + rx + ": " + error)
31         {
32         }
33 };
34
35 class POSIXRegex : public Regex
36 {
37         regex_t regbuf;
38
39  public:
40         POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
41         {
42                 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
43                 int errcode;
44                 errcode = regcomp(&regbuf, rx.c_str(), flags);
45                 if (errcode)
46                 {
47                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
48                         std::string error;
49                         char* errbuf;
50                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
51                         errbuf = new char[sz + 1];
52                         memset(errbuf, 0, sz + 1);
53                         regerror(errcode, &regbuf, errbuf, sz + 1);
54                         error = errbuf;
55                         delete[] errbuf;
56                         regfree(&regbuf);
57                         throw POSIXRegexException(rx, error);
58                 }
59         }
60
61         ~POSIXRegex()
62         {
63                 regfree(&regbuf);
64         }
65
66         bool Matches(const std::string& text)
67         {
68                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
69                 {
70                         // Bang. :D
71                         return true;
72                 }
73                 return false;
74         }
75 };
76
77 class PosixFactory : public RegexFactory
78 {
79  public:
80         bool extended;
81         PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
82         Regex* Create(const std::string& expr)
83         {
84                 return new POSIXRegex(expr, extended);
85         }
86 };
87
88 class ModuleRegexPOSIX : public Module
89 {
90         PosixFactory ref;
91
92  public:
93         ModuleRegexPOSIX() : ref(this)
94         {
95                 ServerInstance->Modules->AddService(ref);
96                 OnRehash(NULL);
97         }
98
99         Version GetVersion() CXX11_OVERRIDE
100         {
101                 return Version("Regex Provider Module for POSIX Regular Expressions", VF_VENDOR);
102         }
103
104         void OnRehash(User* u) CXX11_OVERRIDE
105         {
106                 ref.extended = ServerInstance->Config->ConfValue("posix")->getBool("extended");
107         }
108 };
109
110 MODULE_INIT(ModuleRegexPOSIX)