]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_regex_pcre.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / modules / extra / m_regex_pcre.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 <pcre.h>
16 #include "m_regex.h"
17
18 /* $ModDesc: Regex Provider Module for PCRE */
19 /* $ModDep: m_regex.h */
20 /* $CompileFlags: exec("pcre-config --cflags") */
21 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
22
23 #ifdef WINDOWS
24 #pragma comment(lib, "pcre.lib")
25 #endif
26
27 class PCREException : public ModuleException
28 {
29 public:
30         PCREException(const std::string& rx, const std::string& error, int erroffset)
31                 : ModuleException(std::string("Error in regex ") + rx + " at offset " + ConvToStr(erroffset) + ": " + error)
32         {
33         }
34 };
35
36 class PCRERegex : public Regex
37 {
38 private:
39         pcre* regex;
40
41 public:
42         PCRERegex(const std::string& rx, ) : Regex(rx, Me)
43         {
44                 const char* error;
45                 int erroffset;
46                 regex = pcre_compile(rx.c_str(), 0, &error, &erroffset, NULL);
47                 if (!regex)
48                 {
49                         ServerInstance->Logs->Log("REGEX", DEBUG, "pcre_compile failed: /%s/ [%d] %s", rx.c_str(), erroffset, error);
50                         throw PCREException(rx, error, erroffset);
51                 }
52         }
53
54         virtual ~PCRERegex()
55         {
56                 pcre_free(regex);
57         }
58
59         virtual bool Matches(const std::string& text)
60         {
61                 if (pcre_exec(regex, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
62                 {
63                         // Bang. :D
64                         return true;
65                 }
66                 return false;
67         }
68 };
69
70 class ModuleRegexPCRE : public Module
71 {
72 public:
73         ModuleRegexPCRE()       {
74                 ServerInstance->Modules->PublishInterface("RegularExpression", this);
75                 Implementation eventlist[] = { I_OnRequest };
76                 ServerInstance->Modules->Attach(eventlist, this, 1);
77         }
78
79         virtual Version GetVersion()
80         {
81                 return Version("Regex Provider Module for PCRE", VF_COMMON | VF_VENDOR | VF_SERVICEPROVIDER, API_VERSION);
82         }
83
84         virtual ~ModuleRegexPCRE()
85         {
86                 ServerInstance->Modules->UnpublishInterface("RegularExpression", this);
87         }
88
89         virtual const char* OnRequest(Request* request)
90         {
91                 if (strcmp("REGEX-NAME", request->GetId()) == 0)
92                 {
93                         return "pcre";
94                 }
95                 else if (strcmp("REGEX", request->GetId()) == 0)
96                 {
97                         RegexFactoryRequest* rfr = (RegexFactoryRequest*)request;
98                         std::string rx = rfr->GetRegex();
99                         rfr->result = new PCRERegex(rx, ServerInstance);
100                         return "OK";
101                 }
102                 return NULL;
103         }
104 };
105
106 MODULE_INIT(ModuleRegexPCRE)