]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
Remove dummy API_VERSION from Version constructor
[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-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 <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, bool extended) : Regex(rx)
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()      {
80                 ServerInstance->Modules->PublishInterface("RegularExpression", this);
81                 Implementation eventlist[] = { I_OnRehash };
82                 ServerInstance->Modules->Attach(eventlist, this, 1);
83                 OnRehash(NULL);
84         }
85
86         virtual Version GetVersion()
87         {
88                 return Version("Regex Provider Module for POSIX Regular Expressions", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER);
89         }
90
91         virtual ~ModuleRegexPOSIX()
92         {
93                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
94         }
95
96         virtual void OnRehash(User* u)
97         {
98                 ConfigReader Conf;
99                 extended = Conf.ReadFlag("posix", "extended", 0);
100         }
101
102         void OnRequest(Request& request)
103         {
104                 if (strcmp("REGEX-NAME", request.id) == 0)
105                 {
106                         static_cast<RegexNameRequest&>(request).result = "posix";
107                 }
108                 else if (strcmp("REGEX", request.id) == 0)
109                 {
110                         RegexFactoryRequest& rfr = (RegexFactoryRequest&)request;
111                         std::string rx = rfr.GetRegex();
112                         rfr.result = new POSIXRegex(rx, extended);
113                 }
114         }
115 };
116
117 MODULE_INIT(ModuleRegexPOSIX)