]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
TRE regex provider (the same engine used by Unreal 3.2)
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_posix.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 <regex.h>
18
19 /* $ModDesc: Regex Provider Module for POSIX Regular Expressions */
20 /* $ModDep: m_regex.h */
21
22 class POSIXRegexException : public ModuleException
23 {
24 public:
25         POSIXRegexException(const std::string& rx, const std::string& error)
26                 : ModuleException(std::string("Error in regex ") + rx + ": " + error)
27         {
28         }
29 };
30
31 class POSIXRegex : public Regex
32 {
33 private:
34         regex_t regbuf;
35
36 public:
37         POSIXRegex(const std::string& rx, InspIRCd* Me, bool extended) : Regex(rx, Me)
38         {
39                 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
40                 int errcode;
41                 errcode = regcomp(&regbuf, rx.c_str(), flags);
42                 if (errcode)
43                 {
44                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
45                         std::string error;
46                         char* errbuf;
47                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
48                         errbuf = new char[sz + 1];
49                         memset(errbuf, 0, sz + 1);
50                         regerror(errcode, &regbuf, errbuf, sz + 1);
51                         error = errbuf;
52                         delete[] errbuf;
53                         regfree(&regbuf);
54                         throw POSIXRegexException(rx, error);
55                 }
56         }
57
58         virtual ~POSIXRegex()
59         {
60                 regfree(&regbuf);
61         }
62
63         virtual bool Matches(const std::string& text)
64         {
65                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
66                 {
67                         // Bang. :D
68                         return true;
69                 }
70                 return false;
71         }
72 };
73
74 class ModuleRegexPOSIX : public Module
75 {
76 private:
77         bool extended;
78 public:
79         ModuleRegexPOSIX(InspIRCd* Me) : Module(Me)
80         {
81                 Me->Modules->PublishInterface("RegularExpression", this);
82                 Implementation eventlist[] = { I_OnRequest, I_OnRehash };
83                 Me->Modules->Attach(eventlist, this, 2);
84                 OnRehash(NULL, "");
85         }
86
87         virtual ~ModuleRegexPOSIX()
88         {
89                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
90         }
91
92         virtual void OnRehash(User* u, const std::string& parameter)
93         {
94                 ConfigReader Conf(ServerInstance);
95                 extended = Conf.ReadFlag("posix", "extended", 0);
96         }
97
98         virtual const char* OnRequest(Request* request)
99         {
100                 if (strcmp("REGEX-NAME", request->GetId()) == 0)
101                 {
102                         return "posix";
103                 }
104                 else if (strcmp("REGEX", request->GetId()) == 0)
105                 {
106                         RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
107                         std::string rx = rfr->GetRegex();
108                         rfr->result = new POSIXRegex(rx, ServerInstance, extended);
109                         return "OK";
110                 }
111                 return NULL;
112         }
113 };