The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_regex_tre.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 #include "m_regex.h"
00016 #include <sys/types.h>
00017 #include <tre/regex.h>
00018 
00019 /* $ModDesc: Regex Provider Module for TRE Regular Expressions */
00020 /* $CompileFlags: pkgconfincludes("tre","tre/regex.h","") */
00021 /* $LinkerFlags: pkgconflibs("tre","/libtre.so","-ltre") rpath("pkg-config --libs tre") */
00022 /* $ModDep: m_regex.h */
00023 
00024 class TRERegexException : public ModuleException
00025 {
00026 public:
00027         TRERegexException(const std::string& rx, const std::string& error)
00028                 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
00029         {
00030         }
00031 };
00032 
00033 class TRERegex : public Regex
00034 {
00035 private:
00036         regex_t regbuf;
00037 
00038 public:
00039         TRERegex(const std::string& rx, InspIRCd* Me) : Regex(rx, Me)
00040         {
00041                 int flags = REG_EXTENDED | REG_NOSUB;
00042                 int errcode;
00043                 errcode = regcomp(&regbuf, rx.c_str(), flags);
00044                 if (errcode)
00045                 {
00046                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
00047                         std::string error;
00048                         char* errbuf;
00049                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
00050                         errbuf = new char[sz + 1];
00051                         memset(errbuf, 0, sz + 1);
00052                         regerror(errcode, &regbuf, errbuf, sz + 1);
00053                         error = errbuf;
00054                         delete[] errbuf;
00055                         regfree(&regbuf);
00056                         throw TRERegexException(rx, error);
00057                 }
00058         }
00059 
00060         virtual ~TRERegex()
00061         {
00062                 regfree(&regbuf);
00063         }
00064 
00065         virtual bool Matches(const std::string& text)
00066         {
00067                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
00068                 {
00069                         // Bang. :D
00070                         return true;
00071                 }
00072                 return false;
00073         }
00074 };
00075 
00076 class ModuleRegexTRE : public Module
00077 {
00078 public:
00079         ModuleRegexTRE(InspIRCd* Me) : Module(Me)
00080         {
00081                 Me->Modules->PublishInterface("RegularExpression", this);
00082                 Implementation eventlist[] = { I_OnRequest };
00083                 Me->Modules->Attach(eventlist, this, 1);
00084         }
00085 
00086         virtual Version GetVersion()
00087         {
00088                 return Version("$Id: m_regex_tre.cpp 10593 2008-09-22 15:22:15Z brain $", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
00089         }
00090 
00091         virtual ~ModuleRegexTRE()
00092         {
00093                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
00094         }
00095 
00096         virtual const char* OnRequest(Request* request)
00097         {
00098                 if (strcmp("REGEX-NAME", request->GetId()) == 0)
00099                 {
00100                         return "tre";
00101                 }
00102                 else if (strcmp("REGEX", request->GetId()) == 0)
00103                 {
00104                         RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
00105                         std::string rx = rfr->GetRegex();
00106                         rfr->result = new TRERegex(rx, ServerInstance);
00107                         return "OK";
00108                 }
00109                 return NULL;
00110         }
00111 };
00112 
00113 MODULE_INIT(ModuleRegexTRE)