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, InspIRCd* Me, bool extended) : Regex(rx, Me)
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
79 ModuleRegexPOSIX(InspIRCd* Me) : Module(Me)
81 Me->Modules->PublishInterface("RegularExpression", this);
82 Implementation eventlist[] = { I_OnRequest, I_OnRehash };
83 Me->Modules->Attach(eventlist, this, 2);
87 virtual Version GetVersion()
89 return Version("$Id$", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
92 virtual ~ModuleRegexPOSIX()
94 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
97 virtual void OnRehash(User* u)
99 ConfigReader Conf(ServerInstance);
100 extended = Conf.ReadFlag("posix", "extended", 0);
103 virtual const char* OnRequest(Request* request)
105 if (strcmp("REGEX-NAME", request->GetId()) == 0)
109 else if (strcmp("REGEX", request->GetId()) == 0)
111 RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
112 std::string rx = rfr->GetRegex();
113 rfr->result = new POSIXRegex(rx, ServerInstance, extended);
120 MODULE_INIT(ModuleRegexPOSIX)