]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_pcre.cpp
fd5a30cde7b536c58bbca86449f2467b29682fca
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_pcre.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2016, 2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2012 ChrisTX <xpipe@hotmail.de>
8  *   Copyright (C) 2011 Adam <Adam@anope.org>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
11  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26 /// $CompilerFlags: execute("pcre-config --cflags" "PCRE_CXXFLAGS")
27 /// $LinkerFlags: execute("pcre-config --libs" "PCRE_LDFLAGS" "-lpcre")
28
29 /// $PackageInfo: require_system("arch") pcre
30 /// $PackageInfo: require_system("centos") pcre-devel
31 /// $PackageInfo: require_system("darwin") pcre
32 /// $PackageInfo: require_system("debian") libpcre3-dev
33 /// $PackageInfo: require_system("ubuntu") libpcre3-dev
34
35
36 #include "inspircd.h"
37 #include <pcre.h>
38 #include "modules/regex.h"
39
40 #ifdef _WIN32
41 # pragma comment(lib, "libpcre.lib")
42 #endif
43
44 class PCRERegex : public Regex
45 {
46         pcre* regex;
47
48  public:
49         PCRERegex(const std::string& rx) : Regex(rx)
50         {
51                 const char* error;
52                 int erroffset;
53                 regex = pcre_compile(rx.c_str(), 0, &error, &erroffset, NULL);
54                 if (!regex)
55                 {
56                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "pcre_compile failed: /%s/ [%d] %s", rx.c_str(), erroffset, error);
57                         throw RegexException(rx, error, erroffset);
58                 }
59         }
60
61         ~PCRERegex()
62         {
63                 pcre_free(regex);
64         }
65
66         bool Matches(const std::string& text) CXX11_OVERRIDE
67         {
68                 return (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) >= 0);
69         }
70 };
71
72 class PCREFactory : public RegexFactory
73 {
74  public:
75         PCREFactory(Module* m) : RegexFactory(m, "regex/pcre") {}
76         Regex* Create(const std::string& expr) CXX11_OVERRIDE
77         {
78                 return new PCRERegex(expr);
79         }
80 };
81
82 class ModuleRegexPCRE : public Module
83 {
84  public:
85         PCREFactory ref;
86         ModuleRegexPCRE() : ref(this)
87         {
88         }
89
90         Version GetVersion() CXX11_OVERRIDE
91         {
92                 return Version("Provides a regular expression engine which uses the PCRE library.", VF_VENDOR);
93         }
94 };
95
96 MODULE_INIT(ModuleRegexPCRE)