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 * ---------------------------------------------------
18 /* $ModDesc: Regex Provider Module for PCRE */
19 /* $ModDep: m_regex.h */
20 /* $CompileFlags: exec("pcre-config --cflags") */
21 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
24 #pragma comment(lib, "pcre.lib")
27 class PCREException : public ModuleException
30 PCREException(const std::string& rx, const std::string& error, int erroffset)
31 : ModuleException(std::string("Error in regex ") + rx + " at offset " + ConvToStr(erroffset) + ": " + error)
36 class PCRERegex : public Regex
42 PCRERegex(const std::string& rx, InspIRCd* Me) : Regex(rx, Me)
46 regex = pcre_compile(rx.c_str(), 0, &error, &erroffset, NULL);
49 Me->Logs->Log("REGEX", DEBUG, "pcre_compile failed: /%s/ [%d] %s", rx.c_str(), erroffset, error);
50 throw PCREException(rx, error, erroffset);
59 virtual bool Matches(const std::string& text)
61 if (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
70 class ModuleRegexPCRE : public Module
73 ModuleRegexPCRE(InspIRCd* Me) : Module(Me)
75 Me->Modules->PublishInterface("RegularExpression", this);
76 Implementation eventlist[] = { I_OnRequest };
77 Me->Modules->Attach(eventlist, this, 1);
80 virtual Version GetVersion()
82 return Version("$Id$", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
85 virtual ~ModuleRegexPCRE()
87 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
90 virtual const char* OnRequest(Request* request)
92 if (strcmp("REGEX-NAME", request->GetId()) == 0)
96 else if (strcmp("REGEX", request->GetId()) == 0)
98 RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
99 std::string rx = rfr->GetRegex();
100 rfr->result = new PCRERegex(rx, ServerInstance);
107 MODULE_INIT(ModuleRegexPCRE)