2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
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.
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
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/>.
23 #include <sys/types.h>
26 /* $ModDesc: Regex Provider Module for POSIX Regular Expressions */
27 /* $ModDep: m_regex.h */
29 class POSIXRegexException : public ModuleException
32 POSIXRegexException(const std::string& rx, const std::string& error)
33 : ModuleException("Error in regex " + rx + ": " + error)
38 class POSIXRegex : public Regex
44 POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
46 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
48 errcode = regcomp(®buf, rx.c_str(), flags);
51 // Get the error string into a std::string. YUCK this involves at least 2 string copies.
54 size_t sz = regerror(errcode, ®buf, NULL, 0);
55 errbuf = new char[sz + 1];
56 memset(errbuf, 0, sz + 1);
57 regerror(errcode, ®buf, errbuf, sz + 1);
61 throw POSIXRegexException(rx, error);
70 virtual bool Matches(const std::string& text)
72 if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0)
81 class PosixFactory : public RegexFactory
85 PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
86 Regex* Create(const std::string& expr)
88 return new POSIXRegex(expr, extended);
92 class ModuleRegexPOSIX : public Module
96 ModuleRegexPOSIX() : ref(this) {
97 ServerInstance->Modules->AddService(ref);
98 Implementation eventlist[] = { I_OnRehash };
99 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
105 return Version("Regex Provider Module for POSIX Regular Expressions", VF_VENDOR);
108 void OnRehash(User* u)
110 ref.extended = ServerInstance->Config->ConfValue("posix")->getBool("extended");
114 MODULE_INIT(ModuleRegexPOSIX)