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>
17 #include <tre/regex.h>
19 /* $ModDesc: Regex Provider Module for TRE Regular Expressions */
20 /* $CompileFlags: pkgconfincludes("tre","tre/regex.h","") */
21 /* $LinkerFlags: pkgconflibs("tre","/libtre.so","-ltre") rpath("pkg-config --libs tre") */
22 /* $ModDep: m_regex.h */
24 class TRERegexException : public ModuleException
27 TRERegexException(const std::string& rx, const std::string& error)
28 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
33 class TRERegex : public Regex
39 TRERegex(const std::string& rx) : Regex(rx)
41 int flags = REG_EXTENDED | REG_NOSUB;
43 errcode = regcomp(®buf, rx.c_str(), flags);
46 // Get the error string into a std::string. YUCK this involves at least 2 string copies.
49 size_t sz = regerror(errcode, ®buf, NULL, 0);
50 errbuf = new char[sz + 1];
51 memset(errbuf, 0, sz + 1);
52 regerror(errcode, ®buf, errbuf, sz + 1);
56 throw TRERegexException(rx, error);
65 virtual bool Matches(const std::string& text)
67 if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0)
76 class ModuleRegexTRE : public Module
80 ServerInstance->Modules->PublishInterface("RegularExpression", this);
83 virtual Version GetVersion()
85 return Version("Regex Provider Module for TRE Regular Expressions", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
88 virtual ~ModuleRegexTRE()
90 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
93 void OnRequest(Request& request)
95 if (strcmp("REGEX-NAME", request.id) == 0)
97 static_cast<RegexNameRequest&>(request).result = "tre";
99 else if (strcmp("REGEX", request.id) == 0)
101 RegexFactoryRequest& rfr = (RegexFactoryRequest&)request;
102 std::string rx = rfr.GetRegex();
103 rfr.result = new TRERegex(rx);
108 MODULE_INIT(ModuleRegexTRE)