]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_tre.cpp
c50429a5ef009123fcebd6fd6b5165940ccad332
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_tre.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "m_regex.h"
16 #include <sys/types.h>
17 #include <tre/regex.h>
18
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 */
23
24 class TRERegexException : public ModuleException
25 {
26 public:
27         TRERegexException(const std::string& rx, const std::string& error)
28                 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
29         {
30         }
31 };
32
33 class TRERegex : public Regex
34 {
35 private:
36         regex_t regbuf;
37
38 public:
39         TRERegex(const std::string& rx, InspIRCd* Me) : Regex(rx, Me)
40         {
41                 int flags = REG_EXTENDED | REG_NOSUB;
42                 int errcode;
43                 errcode = regcomp(&regbuf, rx.c_str(), flags);
44                 if (errcode)
45                 {
46                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
47                         std::string error;
48                         char* errbuf;
49                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
50                         errbuf = new char[sz + 1];
51                         memset(errbuf, 0, sz + 1);
52                         regerror(errcode, &regbuf, errbuf, sz + 1);
53                         error = errbuf;
54                         delete[] errbuf;
55                         regfree(&regbuf);
56                         throw TRERegexException(rx, error);
57                 }
58         }
59
60         virtual ~TRERegex()
61         {
62                 regfree(&regbuf);
63         }
64
65         virtual bool Matches(const std::string& text)
66         {
67                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
68                 {
69                         // Bang. :D
70                         return true;
71                 }
72                 return false;
73         }
74 };
75
76 class ModuleRegexTRE : public Module
77 {
78 public:
79         ModuleRegexTRE(InspIRCd* Me) : Module(Me)
80         {
81                 Me->Modules->PublishInterface("RegularExpression", this);
82                 Implementation eventlist[] = { I_OnRequest };
83                 Me->Modules->Attach(eventlist, this, 1);
84         }
85
86         virtual Version GetVersion()
87         {
88                 return Version("Regex Provider Module for TRE Regular Expressions", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
89         }
90
91         virtual ~ModuleRegexTRE()
92         {
93                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
94         }
95
96         virtual const char* OnRequest(Request* request)
97         {
98                 if (strcmp("REGEX-NAME", request->GetId()) == 0)
99                 {
100                         return "tre";
101                 }
102                 else if (strcmp("REGEX", request->GetId()) == 0)
103                 {
104                         RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
105                         std::string rx = rfr->GetRegex();
106                         rfr->result = new TRERegex(rx, ServerInstance);
107                         return "OK";
108                 }
109                 return NULL;
110         }
111 };
112
113 MODULE_INIT(ModuleRegexTRE)