]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
e35094ac0ecc701581297b65cacf30731bb7a5b8
[user/henk/code/inspircd.git] / src / modules / extra / m_filter_pcre.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 <stdio.h>
15 #include <string>
16 #include <pcre.h>
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "inspircd.h"
21 #include "m_filter.h"
22
23 /* $ModDesc: m_filter with regexps */
24 /* $CompileFlags: exec("pcre-config --cflags") */
25 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
26 /* $ModDep: m_filter.h */
27
28 class PCREFilter : public FilterResult
29 {
30  public:
31          pcre* regexp;
32
33          PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat)
34                  : FilterResult::FilterResult(pat, rea, act, gline_time), regexp(r)
35          {
36          }
37 };
38
39 class ModuleFilterPCRE : public FilterBase
40 {
41         std::vector<PCREFilter> filters;
42         pcre *re;
43         const char *error;
44         int erroffset;
45
46  public:
47         ModuleFilterPCRE(InspIRCd* Me)
48         : FilterBase::FilterBase(Me, "m_filter_pcre.so")
49         {
50                 OnRehash(NULL,"");
51         }
52
53         virtual ~ModuleFilterPCRE()
54         {
55         }
56
57         virtual FilterResult* FilterMatch(const std::string &text)
58         {
59                 for (std::vector<PCREFilter>::iterator index = filters.begin(); index != filters.end(); index++)
60                 {
61                         if (pcre_exec(index->regexp, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
62                         {
63                                 PCREFilter* fr = &(*index);
64                                 if (index != filters.begin())
65                                 {
66                                         filters.erase(index);
67                                         filters.insert(filters.begin(), *fr);
68                                         index = filters.begin();
69                                 }
70                                 return &(*index);
71                         }
72                 }
73                 return NULL;
74         }
75
76         virtual bool DeleteFilter(const std::string &freeform)
77         {
78                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
79                 {
80                         if (i->freeform == freeform)
81                         {
82                                 pcre_free((*i).regexp);
83                                 filters.erase(i);
84                                 return true;
85                         }
86                 }
87                 return false;
88         }
89
90         virtual void SyncFilters(Module* proto, void* opaque)
91         {
92                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
93                 {
94                         this->SendFilter(proto, opaque, &(*i));
95                 }
96         }
97
98         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
99         {
100                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
101                 {
102                         if (i->freeform == freeform)
103                         {
104                                 return std::make_pair(false, "Filter already exists");
105                         }
106                 }
107
108                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
109
110                 if (!re)
111                 {
112                         ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
113                         ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
114                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
115                 }
116                 else
117                 {
118                         filters.push_back(PCREFilter(re, reason, type, duration, freeform));
119                         return std::make_pair(true, "");
120                 }
121         }
122
123         virtual void OnRehash(userrec* user, const std::string &parameter)
124         {               
125                 ConfigReader MyConf(ServerInstance);
126
127                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
128                 {
129                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
130
131                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
132                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
133                         std::string action = MyConf.ReadValue("keyword", "action", index);
134                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
135
136                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
137
138                         if (!re)
139                         {
140                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
141                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
142                         }
143                         else
144                         {
145                                 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern));
146                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
147                         }
148                 }
149         }
150
151         virtual int OnStats(char symbol, userrec* user, string_list &results)
152         {
153                 if (symbol == 's')
154                 {
155                         std::string sn = ServerInstance->Config->ServerName;
156                         for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
157                         {
158                                 results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
159                         }
160                 }
161                 return 0;
162         }
163 };
164         
165
166 class ModuleFilterPCREFactory : public ModuleFactory
167 {
168  public:
169         ModuleFilterPCREFactory()
170         {
171         }
172         
173         ~ModuleFilterPCREFactory()
174         {
175         }
176         
177         virtual Module * CreateModule(InspIRCd* Me)
178         {
179                 return new ModuleFilterPCRE(Me);
180         }
181         
182 };
183
184
185 extern "C" void * init_module( void )
186 {
187         return new ModuleFilterPCREFactory;
188 }