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