m_regex_pcre.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "inspircd.h"
00015 #include <pcre.h>
00016 #include "m_regex.h"
00017
00018
00019
00020
00021
00022
00023 #ifdef WINDOWS
00024 #pragma comment(lib, "pcre.lib")
00025 #endif
00026
00027 class PCREException : public ModuleException
00028 {
00029 public:
00030 PCREException(const std::string& rx, const std::string& error, int erroffset)
00031 : ModuleException(std::string("Error in regex ") + rx + " at offset " + ConvToStr(erroffset) + ": " + error)
00032 {
00033 }
00034 };
00035
00036 class PCRERegex : public Regex
00037 {
00038 private:
00039 pcre* regex;
00040
00041 public:
00042 PCRERegex(const std::string& rx, InspIRCd* Me) : Regex(rx, Me)
00043 {
00044 const char* error;
00045 int erroffset;
00046 regex = pcre_compile(rx.c_str(), 0, &error, &erroffset, NULL);
00047 if (!regex)
00048 {
00049 Me->Logs->Log("REGEX", DEBUG, "pcre_compile failed: /%s/ [%d] %s", rx.c_str(), erroffset, error);
00050 throw PCREException(rx, error, erroffset);
00051 }
00052 }
00053
00054 virtual ~PCRERegex()
00055 {
00056 pcre_free(regex);
00057 }
00058
00059 virtual bool Matches(const std::string& text)
00060 {
00061 if (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
00062 {
00063
00064 return true;
00065 }
00066 return false;
00067 }
00068 };
00069
00070 class ModuleRegexPCRE : public Module
00071 {
00072 public:
00073 ModuleRegexPCRE(InspIRCd* Me) : Module(Me)
00074 {
00075 Me->Modules->PublishInterface("RegularExpression", this);
00076 Implementation eventlist[] = { I_OnRequest };
00077 Me->Modules->Attach(eventlist, this, 1);
00078 }
00079
00080 virtual Version GetVersion()
00081 {
00082 return Version("$Id: m_regex_pcre.cpp 10593 2008-09-22 15:22:15Z brain $", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
00083 }
00084
00085 virtual ~ModuleRegexPCRE()
00086 {
00087 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
00088 }
00089
00090 virtual const char* OnRequest(Request* request)
00091 {
00092 if (strcmp("REGEX-NAME", request->GetId()) == 0)
00093 {
00094 return "pcre";
00095 }
00096 else if (strcmp("REGEX", request->GetId()) == 0)
00097 {
00098 RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
00099 std::string rx = rfr->GetRegex();
00100 rfr->result = new PCRERegex(rx, ServerInstance);
00101 return "OK";
00102 }
00103 return NULL;
00104 }
00105 };
00106
00107 MODULE_INIT(ModuleRegexPCRE)