]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
...because every now and again, i have to do a massive commit.
[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-2010 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 PosixFactory : public RegexFactory
75 {
76  public:
77         bool extended;
78         PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
79         Regex* Create(const std::string& expr)
80         {
81                 return new POSIXRegex(expr, extended);
82         }
83 };
84
85 class ModuleRegexPOSIX : public Module
86 {
87         PosixFactory ref;
88 public:
89         ModuleRegexPOSIX() : ref(this) {
90                 ServerInstance->Modules->AddService(ref);
91                 Implementation eventlist[] = { I_OnRehash };
92                 ServerInstance->Modules->Attach(eventlist, this, 1);
93                 OnRehash(NULL);
94         }
95
96         Version GetVersion()
97         {
98                 return Version("Regex Provider Module for POSIX Regular Expressions", VF_COMMON | VF_VENDOR);
99         }
100
101         void OnRehash(User* u)
102         {
103                 ConfigReader Conf;
104                 ref.extended = Conf.ReadFlag("posix", "extended", 0);
105         }
106 };
107
108 MODULE_INIT(ModuleRegexPOSIX)