]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
Make regex providers load correctly. Move m_rline to use the regex providers, and...
[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 Version GetVersion()
88         {
89                 return Version("$Id: m_regex_posix.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
90         }
91
92         virtual ~ModuleRegexPOSIX()
93         {
94                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
95         }
96
97         virtual void OnRehash(User* u, const std::string& parameter)
98         {
99                 ConfigReader Conf(ServerInstance);
100                 extended = Conf.ReadFlag("posix", "extended", 0);
101         }
102
103         virtual const char* OnRequest(Request* request)
104         {
105                 if (strcmp("REGEX-NAME", request->GetId()) == 0)
106                 {
107                         return "posix";
108                 }
109                 else if (strcmp("REGEX", request->GetId()) == 0)
110                 {
111                         RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
112                         std::string rx = rfr->GetRegex();
113                         rfr->result = new POSIXRegex(rx, ServerInstance, extended);
114                         return "OK";
115                 }
116                 return NULL;
117         }
118 };
119
120 MODULE_INIT(ModuleRegexPOSIX)