]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Inherit m_filter.cpp and m_filter_pcre.cpp from a base set of classes, so that change...
[user/henk/code/inspircd.git] / src / modules / extra / m_filter_pcre.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2004 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 // Message and notice filtering using regex patterns
18 // a module based on the original work done by Craig Edwards in 2003
19 // for the chatspike network.
20
21 #include <stdio.h>
22 #include <string>
23 #include <pcre.h>
24 #include "users.h"
25 #include "channels.h"
26 #include "modules.h"
27 #include "inspircd.h"
28 #include "m_filter.h"
29
30 /* $ModDesc: m_filter with regexps */
31 /* $CompileFlags: `pcre-config --cflags` */
32 /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
33 /* $ModDep: ../m_filter.h */
34
35 class PCREFilter : public FilterResult
36 {
37  public:
38          pcre* regexp;
39
40          PCREFilter(pcre* r, const std::string &rea, const std::string &act)
41                  : FilterResult::FilterResult(rea, act), regexp(r)
42          {
43          }
44 };
45
46 class ModuleFilterPCRE : public FilterBase
47 {
48         std::vector<PCREFilter> filters;
49         pcre *re;
50         const char *error;
51         int erroffset;
52
53  public:
54         ModuleFilterPCRE(InspIRCd* Me)
55         : FilterBase::FilterBase(Me)
56         {
57                 OnRehash("");
58         }
59
60         virtual ~ModuleFilterPCRE()
61         {
62         }
63
64         virtual FilterResult* FilterMatch(const std::string &text)
65         {
66                 for (unsigned int index = 0; index < filters.size(); index++)
67                 {
68                         PCREFilter& filt = filters[index];
69                         
70                         if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
71                         {
72                                 return &filt;
73                         }
74                 }
75                 return NULL;
76         }
77         
78         virtual void OnRehash(const std::string &parameter)
79         {               
80                 ConfigReader MyConf(ServerInstance);
81
82                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
83                         pcre_free((*i).regexp);
84
85                 filters.clear();
86
87                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
88                 {
89                         std::string pattern = MyConf.ReadValue("keyword","pattern",index);
90                         std::string reason = MyConf.ReadValue("keyword","reason",index);
91                         std::string action = MyConf.ReadValue("keyword","action",index);
92                         
93                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
94                         
95                         if (!re)
96                         {
97                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
98                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
99                         }
100                         else
101                         {
102                                 filters.push_back(PCREFilter(re, reason, action));
103                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
104                         }
105                 }
106         }
107 };
108         
109
110 class ModuleFilterPCREFactory : public ModuleFactory
111 {
112  public:
113         ModuleFilterPCREFactory()
114         {
115         }
116         
117         ~ModuleFilterPCREFactory()
118         {
119         }
120         
121         virtual Module * CreateModule(InspIRCd* Me)
122         {
123                 return new ModuleFilterPCRE(Me);
124         }
125         
126 };
127
128
129 extern "C" void * init_module( void )
130 {
131         return new ModuleFilterPCREFactory;
132 }