m_regex_posix.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 "m_regex.h"
00016 #include <sys/types.h>
00017 #include <regex.h>
00018
00019
00020
00021
00022 class POSIXRegexException : public ModuleException
00023 {
00024 public:
00025 POSIXRegexException(const std::string& rx, const std::string& error)
00026 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
00027 {
00028 }
00029 };
00030
00031 class POSIXRegex : public Regex
00032 {
00033 private:
00034 regex_t regbuf;
00035
00036 public:
00037 POSIXRegex(const std::string& rx, InspIRCd* Me, bool extended) : Regex(rx, Me)
00038 {
00039 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
00040 int errcode;
00041 errcode = regcomp(®buf, rx.c_str(), flags);
00042 if (errcode)
00043 {
00044
00045 std::string error;
00046 char* errbuf;
00047 size_t sz = regerror(errcode, ®buf, NULL, 0);
00048 errbuf = new char[sz + 1];
00049 memset(errbuf, 0, sz + 1);
00050 regerror(errcode, ®buf, errbuf, sz + 1);
00051 error = errbuf;
00052 delete[] errbuf;
00053 regfree(®buf);
00054 throw POSIXRegexException(rx, error);
00055 }
00056 }
00057
00058 virtual ~POSIXRegex()
00059 {
00060 regfree(®buf);
00061 }
00062
00063 virtual bool Matches(const std::string& text)
00064 {
00065 if (regexec(®buf, text.c_str(), 0, NULL, 0) == 0)
00066 {
00067
00068 return true;
00069 }
00070 return false;
00071 }
00072 };
00073
00074 class ModuleRegexPOSIX : public Module
00075 {
00076 private:
00077 bool extended;
00078 public:
00079 ModuleRegexPOSIX(InspIRCd* Me) : Module(Me)
00080 {
00081 Me->Modules->PublishInterface("RegularExpression", this);
00082 Implementation eventlist[] = { I_OnRequest, I_OnRehash };
00083 Me->Modules->Attach(eventlist, this, 2);
00084 OnRehash(NULL, "");
00085 }
00086
00087 virtual Version GetVersion()
00088 {
00089 return Version("$Id: m_regex_posix.cpp 10593 2008-09-22 15:22:15Z brain $", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
00090 }
00091
00092 virtual ~ModuleRegexPOSIX()
00093 {
00094 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
00095 }
00096
00097 virtual void OnRehash(User* u, const std::string& parameter)
00098 {
00099 ConfigReader Conf(ServerInstance);
00100 extended = Conf.ReadFlag("posix", "extended", 0);
00101 }
00102
00103 virtual const char* OnRequest(Request* request)
00104 {
00105 if (strcmp("REGEX-NAME", request->GetId()) == 0)
00106 {
00107 return "posix";
00108 }
00109 else if (strcmp("REGEX", request->GetId()) == 0)
00110 {
00111 RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
00112 std::string rx = rfr->GetRegex();
00113 rfr->result = new POSIXRegex(rx, ServerInstance, extended);
00114 return "OK";
00115 }
00116 return NULL;
00117 }
00118 };
00119
00120 MODULE_INIT(ModuleRegexPOSIX)