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