1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 ModuleRegexPOSIX : public Module
80 ServerInstance->Modules->PublishInterface("RegularExpression", this);
81 Implementation eventlist[] = { I_OnRehash };
82 ServerInstance->Modules->Attach(eventlist, this, 1);
86 virtual Version GetVersion()
88 return Version("Regex Provider Module for POSIX Regular Expressions", VF_COMMON | VF_VENDOR);
91 virtual ~ModuleRegexPOSIX()
93 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
96 virtual void OnRehash(User* u)
99 extended = Conf.ReadFlag("posix", "extended", 0);
102 void OnRequest(Request& request)
104 if (strcmp("REGEX-NAME", request.id) == 0)
106 static_cast<RegexNameRequest&>(request).result = "posix";
108 else if (strcmp("REGEX", request.id) == 0)
110 RegexFactoryRequest& rfr = (RegexFactoryRequest&)request;
111 std::string rx = rfr.GetRegex();
112 rfr.result = new POSIXRegex(rx, extended);
117 MODULE_INIT(ModuleRegexPOSIX)