2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 #include "modules/regex.h"
27 #include <sys/types.h>
30 class POSIXRegex : public Regex
35 POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
37 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
39 errcode = regcomp(®buf, rx.c_str(), flags);
42 // Get the error string into a std::string. YUCK this involves at least 2 string copies.
45 size_t sz = regerror(errcode, ®buf, NULL, 0);
46 errbuf = new char[sz + 1];
47 memset(errbuf, 0, sz + 1);
48 regerror(errcode, ®buf, errbuf, sz + 1);
52 throw RegexException(rx, error);
61 bool Matches(const std::string& text) CXX11_OVERRIDE
63 return (regexec(®buf, text.c_str(), 0, NULL, 0) == 0);
67 class PosixFactory : public RegexFactory
71 PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
72 Regex* Create(const std::string& expr) CXX11_OVERRIDE
74 return new POSIXRegex(expr, extended);
78 class ModuleRegexPOSIX : public Module
83 ModuleRegexPOSIX() : ref(this)
87 Version GetVersion() CXX11_OVERRIDE
89 return Version("Provides a regular expression engine which uses the POSIX.2 regular expression matching system.", VF_VENDOR);
92 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
94 ref.extended = ServerInstance->Config->ConfValue("posix")->getBool("extended");
98 MODULE_INIT(ModuleRegexPOSIX)