]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Add m_taxonomy and api minor tweak to make it work, enable some modules for it.
[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          PCREFilter()
39          {
40          }
41 };
42
43 class ModuleFilterPCRE : public FilterBase
44 {
45         std::vector<PCREFilter> filters;
46         pcre *re;
47         const char *error;
48         int erroffset;
49         PCREFilter fr;
50
51  public:
52         ModuleFilterPCRE(InspIRCd* Me)
53         : FilterBase::FilterBase(Me, "m_filter_pcre.so")
54         {
55                 OnRehash(NULL,"");
56         }
57
58         virtual ~ModuleFilterPCRE()
59         {
60         }
61
62         virtual FilterResult* FilterMatch(const std::string &text)
63         {
64                 for (std::vector<PCREFilter>::iterator index = filters.begin(); index != filters.end(); index++)
65                 {
66                         if (pcre_exec(index->regexp, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
67                         {
68                                 fr = *index;
69                                 if (index != filters.begin())
70                                 {
71                                         filters.erase(index);
72                                         filters.insert(filters.begin(), fr);
73                                 }
74                                 return &fr;
75                         }
76                 }
77                 return NULL;
78         }
79
80         virtual bool DeleteFilter(const std::string &freeform)
81         {
82                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
83                 {
84                         if (i->freeform == freeform)
85                         {
86                                 pcre_free((*i).regexp);
87                                 filters.erase(i);
88                                 return true;
89                         }
90                 }
91                 return false;
92         }
93
94         virtual void SyncFilters(Module* proto, void* opaque)
95         {
96                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
97                 {
98                         this->SendFilter(proto, opaque, &(*i));
99                 }
100         }
101
102         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
103         {
104                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
105                 {
106                         if (i->freeform == freeform)
107                         {
108                                 return std::make_pair(false, "Filter already exists");
109                         }
110                 }
111
112                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
113
114                 if (!re)
115                 {
116                         ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
117                         ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
118                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
119                 }
120                 else
121                 {
122                         filters.push_back(PCREFilter(re, reason, type, duration, freeform));
123                         return std::make_pair(true, "");
124                 }
125         }
126
127         virtual void OnRehash(userrec* user, const std::string &parameter)
128         {               
129                 ConfigReader MyConf(ServerInstance);
130
131                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
132                 {
133                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
134
135                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
136                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
137                         std::string action = MyConf.ReadValue("keyword", "action", index);
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));
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 }