]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_posix.cpp
Tidy up source files:
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_posix.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "modules/regex.h"
23 #include <sys/types.h>
24 #include <regex.h>
25
26 /* $ModDesc: Regex Provider Module for POSIX Regular Expressions */
27 /* $ModDep: modules/regex.h */
28
29 class POSIXRegexException : public ModuleException
30 {
31  public:
32         POSIXRegexException(const std::string& rx, const std::string& error)
33                 : ModuleException("Error in regex " + rx + ": " + error)
34         {
35         }
36 };
37
38 class POSIXRegex : public Regex
39 {
40         regex_t regbuf;
41
42  public:
43         POSIXRegex(const std::string& rx, bool extended) : Regex(rx)
44         {
45                 int flags = (extended ? REG_EXTENDED : 0) | REG_NOSUB;
46                 int errcode;
47                 errcode = regcomp(&regbuf, rx.c_str(), flags);
48                 if (errcode)
49                 {
50                         // Get the error string into a std::string. YUCK this involves at least 2 string copies.
51                         std::string error;
52                         char* errbuf;
53                         size_t sz = regerror(errcode, &regbuf, NULL, 0);
54                         errbuf = new char[sz + 1];
55                         memset(errbuf, 0, sz + 1);
56                         regerror(errcode, &regbuf, errbuf, sz + 1);
57                         error = errbuf;
58                         delete[] errbuf;
59                         regfree(&regbuf);
60                         throw POSIXRegexException(rx, error);
61                 }
62         }
63
64         virtual ~POSIXRegex()
65         {
66                 regfree(&regbuf);
67         }
68
69         virtual bool Matches(const std::string& text)
70         {
71                 if (regexec(&regbuf, text.c_str(), 0, NULL, 0) == 0)
72                 {
73                         // Bang. :D
74                         return true;
75                 }
76                 return false;
77         }
78 };
79
80 class PosixFactory : public RegexFactory
81 {
82  public:
83         bool extended;
84         PosixFactory(Module* m) : RegexFactory(m, "regex/posix") {}
85         Regex* Create(const std::string& expr)
86         {
87                 return new POSIXRegex(expr, extended);
88         }
89 };
90
91 class ModuleRegexPOSIX : public Module
92 {
93         PosixFactory ref;
94
95  public:
96         ModuleRegexPOSIX() : ref(this)
97         {
98                 ServerInstance->Modules->AddService(ref);
99                 Implementation eventlist[] = { I_OnRehash };
100                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
101                 OnRehash(NULL);
102         }
103
104         Version GetVersion()
105         {
106                 return Version("Regex Provider Module for POSIX Regular Expressions", VF_VENDOR);
107         }
108
109         void OnRehash(User* u)
110         {
111                 ref.extended = ServerInstance->Config->ConfValue("posix")->getBool("extended");
112         }
113 };
114
115 MODULE_INIT(ModuleRegexPOSIX)