1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 #include <sys/types.h>
19 /* $ModDesc: Regex Provider Module for POSIX Regular Expressions */
20 /* $ModDep: m_regex.h */
22 class POSIXRegexException : public ModuleException
25 POSIXRegexException(const std::string& rx, const std::string& error)
26 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
31 class POSIXRegex : public Regex
37 POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
39 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
41 errcode = regcomp(®buf, rx.c_str(), flags);
44 // Get the error string into a std::string. YUCK this involves at least 2 string copies.
47 size_t sz = regerror(errcode, ®buf, NULL, 0);
48 errbuf = new char[sz + 1];
49 memset(errbuf, 0, sz + 1);
50 regerror(errcode, ®buf, errbuf, sz + 1);
54 throw POSIXRegexException(rx, error);
63 virtual bool Matches(const std::string& text)
65 if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0)
74 class PosixFactory : public RegexFactory
78 PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
79 Regex* Create(const std::string& expr)
81 return new POSIXRegex(expr, extended);
85 class ModuleRegexPOSIX : public Module
89 ModuleRegexPOSIX() : ref(this) {
90 ServerInstance->Modules->AddService(ref);
91 Implementation eventlist[] = { I_OnRehash };
92 ServerInstance->Modules->Attach(eventlist, this, 1);
98 return Version("Regex Provider Module for POSIX Regular Expressions", VF_VENDOR);
101 void OnRehash(User* u)
104 ref.extended = Conf.ReadFlag("posix", "extended", 0);
108 MODULE_INIT(ModuleRegexPOSIX)