]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
19b4f3eaa1a2df1c17b785211eb7d7157355f649
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_posix.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2013 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
9  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26 #include "modules/regex.h"
27 #include <sys/types.h>
28 #include <regex.h>
29
30 class POSIXRegex : public Regex
31 {
32         regex_t regbuf;
33
34  public:
35         POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
36         {
37                 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
38                 int errcode;
39                 errcode = regcomp(&regbuf, rx.c_str(), flags);
40                 if (errcode)
41                 {
42                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
43                         std::string error;
44                         char* errbuf;
45                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
46                         errbuf = new char[sz + 1];
47                         memset(errbuf, 0, sz + 1);
48                         regerror(errcode, &regbuf, errbuf, sz + 1);
49                         error = errbuf;
50                         delete[] errbuf;
51                         regfree(&regbuf);
52                         throw RegexException(rx, error);
53                 }
54         }
55
56         ~POSIXRegex()
57         {
58                 regfree(&regbuf);
59         }
60
61         bool Matches(const std::string& text) CXX11_OVERRIDE
62         {
63                 return (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0);
64         }
65 };
66
67 class PosixFactory : public RegexFactory
68 {
69  public:
70         bool extended;
71         PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
72         Regex* Create(const std::string& expr) CXX11_OVERRIDE
73         {
74                 return new POSIXRegex(expr, extended);
75         }
76 };
77
78 class ModuleRegexPOSIX : public Module
79 {
80         PosixFactory ref;
81
82  public:
83         ModuleRegexPOSIX() : ref(this)
84         {
85         }
86
87         Version GetVersion() CXX11_OVERRIDE
88         {
89                 return Version("Provides a regular expression engine which uses the POSIX.2 regular expression matching system.", VF_VENDOR);
90         }
91
92         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
93         {
94                 ref.extended = ServerInstance->Config->ConfValue("posix")->getBool("extended");
95         }
96 };
97
98 MODULE_INIT(ModuleRegexPOSIX)