]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Changed behaviour of module API to pass Server* to the constructor, rather than have...
[user/henk/code/inspircd.git] / src / modules / extra / m_filter_pcre.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire 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 using namespace std;
18
19 // Message and notice filtering using regex patterns
20 // a module based on the original work done by Craig Edwards in 2003
21 // for the chatspike network.
22
23 #include <stdio.h>
24 #include <string>
25 #include <pcre.h>
26 #include "users.h"
27 #include "channels.h"
28 #include "modules.h"
29 #include "helperfuncs.h"
30
31 /* $ModDesc: m_filter with regexps */
32 /* $CompileFlags: -I/usr/local/include -L/usr/local/lib -lpcre */
33
34 class ModuleFilterPCRE : public Module
35 {
36         Server *Srv;
37         ConfigReader *Conf, *MyConf;
38         std::vector<pcre*> filters;
39         pcre *re;
40         const char *error;
41         int erroffset;
42  
43  public:
44         ModuleFilterPCRE(Server* Me)
45                 : Module::Module(Me)
46         {
47                 // read the configuration file on startup.
48                 // it is perfectly valid to set <filter file> to the value of the
49                 // main config file, then append your <keyword> tags to the bottom
50                 // of the main config... but rather messy. That's why the capability
51                 // of using a seperate config file is provided.
52                 Srv = Me;
53                 Conf = new ConfigReader;
54                 std::string filterfile = Conf->ReadValue("filter","file",0);
55                 MyConf = new ConfigReader(filterfile);
56                 if ((filterfile == "") || (!MyConf->Verify()))
57                 {
58                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
59                         log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
60                         return;
61                 }
62                 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
63
64                 filters.clear();
65                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
66                 {
67                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
68                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
69                         if (!re)
70                         {
71                                 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
72                                 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
73                         }
74                         else
75                         {
76                                 filters.push_back(re);
77                                 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
78                         }
79                 }
80
81         }
82         
83         virtual ~ModuleFilterPCRE()
84         {
85                 delete MyConf;
86                 delete Conf;
87         }
88         
89         // format of a config entry is <keyword pattern="^regexp$" reason="Some reason here" action="kill/block">
90         
91         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
92         {
93                 for (unsigned int index = 0; index < filters.size(); index++)
94                 {
95                         if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
96                         {
97                                 std::string target = "";
98                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
99                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
100
101                                 if (do_action == "")
102                                         do_action = "none";
103
104                                 if (target_type == TYPE_USER)
105                                 {
106                                         userrec* t = (userrec*)dest;
107                                         target = std::string(t->nick);
108                                 }
109                                 else if (target_type == TYPE_CHANNEL)
110                                 {
111                                         chanrec* t = (chanrec*)dest;
112                                         target = std::string(t->name);
113                                 }
114                                 if (do_action == "block")
115                                 {       
116                                         Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
117                                                         std::string(" had their message filtered, target was ")+
118                                                         target+": "+reason);
119                                         // this form of SendTo (with the source as NuLL) sends a server notice
120                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
121                                                         " :Your message has been filtered and opers notified: "+reason);
122                                 }
123
124                                 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
125                                                 std::string(" had their message filtered, target was ")+
126                                                 target+": "+reason+" Action: "+do_action);
127
128                                 if (do_action == "kill")
129                                 {
130                                         Srv->QuitUser(user,reason);
131                                 }
132                                 return 1;
133                         }
134                 }
135                 return 0;
136         }
137         
138         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
139         {
140                 for (unsigned int index = 0; index < filters.size(); index++)
141                 {
142                         if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
143                         {
144                                 std::string target = "";
145                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
146                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
147
148                                 if (do_action == "")
149                                         do_action = "none";
150                                         
151                                 if (target_type == TYPE_USER)
152                                 {
153                                         userrec* t = (userrec*)dest;
154                                         target = std::string(t->nick);
155                                 }
156                                 else if (target_type == TYPE_CHANNEL)
157                                 {
158                                         chanrec* t = (chanrec*)dest;
159                                         target = std::string(t->name);
160                                 }
161                                 if (do_action == "block")
162                                 {       
163                                         Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
164                                                         std::string(" had their notice filtered, target was ")+
165                                                         target+": "+reason);
166                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
167                                                         " :Your notice has been filtered and opers notified: "+reason);
168                                 }
169                                 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
170                                                 std::string(" had their notice filtered, target was ")+
171                                                 target+": "+reason+" Action: "+do_action);
172
173                                 if (do_action == "kill")
174                                 {
175                                         Srv->QuitUser(user,reason);
176                                 }
177                                 return 1;
178                         }
179                 }
180                 return 0;
181         }
182         
183         virtual void OnRehash(std::string parameter)
184         {
185                 // reload our config file on rehash - we must destroy and re-allocate the classes
186                 // to call the constructor again and re-read our data.
187                 delete Conf;
188                 delete MyConf;
189                 Conf = new ConfigReader;
190                 std::string filterfile = Conf->ReadValue("filter","file",0);
191                 // this automatically re-reads the configuration file into the class
192                 MyConf = new ConfigReader(filterfile);
193                 if ((filterfile == "") || (!MyConf->Verify()))
194                 {
195                         // bail if the user forgot to create a config file
196                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
197                         log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
198                         return;
199                 }
200                 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
201
202                 filters.clear();
203                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
204                 {
205                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
206                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
207                         if (!re)
208                         {
209                                 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
210                                 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
211                         }
212                         else
213                         {
214                                 filters.push_back(re);
215                                 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
216                         }
217                 }
218
219         }
220         
221         virtual Version GetVersion()
222         {
223                 // This is version 2 because version 1.x is the unreleased unrealircd module
224                 return Version(3,0,0,0,VF_VENDOR);
225         }
226         
227 };
228
229 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
230
231 class ModuleFilterPCREFactory : public ModuleFactory
232 {
233  public:
234         ModuleFilterPCREFactory()
235         {
236         }
237         
238         ~ModuleFilterPCREFactory()
239         {
240         }
241         
242         virtual Module * CreateModule(Server* Me)
243         {
244                 return new ModuleFilterPCRE(Me);
245         }
246         
247 };
248
249
250 extern "C" void * init_module( void )
251 {
252         return new ModuleFilterPCREFactory;
253 }
254