]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_tre.cpp
01c6ebf4aa33ba624a6d7a060918769933debf55
[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) : Regex(rx)
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()        {
80                 ServerInstance->Modules->PublishInterface("RegularExpression", this);
81         }
82
83         virtual Version GetVersion()
84         {
85                 return Version("Regex Provider Module for TRE Regular Expressions", VF_COMMON | VF_VENDOR);
86         }
87
88         virtual ~ModuleRegexTRE()
89         {
90                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
91         }
92
93         void OnRequest(Request& request)
94         {
95                 if (strcmp("REGEX-NAME", request.id) == 0)
96                 {
97                         static_cast<RegexNameRequest&>(request).result = "tre";
98                 }
99                 else if (strcmp("REGEX", request.id) == 0)
100                 {
101                         RegexFactoryRequest& rfr = (RegexFactoryRequest&)request;
102                         std::string rx = rfr.GetRegex();
103                         rfr.result = new TRERegex(rx);
104                 }
105         }
106 };
107
108 MODULE_INIT(ModuleRegexTRE)