]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
3e2d10b6cea1a0760469a32a018c083aa6fcbad5
[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                         if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
93                         {
94                                 std::string target = "";
95                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
96                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
97
98                                 if (do_action == "")
99                                         do_action = "none";
100
101                                 if (target_type == TYPE_USER)
102                                 {
103                                         userrec* t = (userrec*)dest;
104                                         target = std::string(t->nick);
105                                 }
106                                 else if (target_type == TYPE_CHANNEL)
107                                 {
108                                         chanrec* t = (chanrec*)dest;
109                                         target = std::string(t->name);
110                                 }
111                                 if (do_action == "block")
112                                 {       
113                                         Srv->SendOpers(std::string("FilterPCRE: ")+std::string(user->nick)+
114                                                         std::string(" had their message filtered, target was ")+
115                                                         target+": "+reason);
116                                         // this form of SendTo (with the source as NuLL) sends a server notice
117                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
118                                                         " :Your message has been filtered and opers notified: "+reason);
119                                 }
120
121                                 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
122                                                 std::string(" had their message filtered, target was ")+
123                                                 target+": "+reason+" Action: "+do_action);
124
125                                 if (do_action == "kill")
126                                 {
127                                         Srv->QuitUser(user,reason);
128                                 }
129                                 return 1;
130                         }
131                 }
132                 return 0;
133         }
134         
135         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
136         {
137                 for (int index = 0; index < filters.size(); index++)
138                 {
139                         if (pcre_exec(filters[index],NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
140                         {
141                                 std::string target = "";
142                                 std::string reason = MyConf->ReadValue("keyword","reason",index);
143                                 std::string do_action = MyConf->ReadValue("keyword","action",index);
144
145                                 if (do_action == "")
146                                         do_action = "none";
147                                         
148                                 if (target_type == TYPE_USER)
149                                 {
150                                         userrec* t = (userrec*)dest;
151                                         target = std::string(t->nick);
152                                 }
153                                 else if (target_type == TYPE_CHANNEL)
154                                 {
155                                         chanrec* t = (chanrec*)dest;
156                                         target = std::string(t->name);
157                                 }
158                                 if (do_action == "block")
159                                 {       
160                                         Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
161                                                         std::string(" had their notice filtered, target was ")+
162                                                         target+": "+reason);
163                                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+
164                                                         " :Your notice has been filtered and opers notified: "+reason);
165                                 }
166                                 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
167                                                 std::string(" had their notice filtered, target was ")+
168                                                 target+": "+reason+" Action: "+do_action);
169
170                                 if (do_action == "kill")
171                                 {
172                                         Srv->QuitUser(user,reason);
173                                 }
174                                 return 1;
175                         }
176                 }
177                 return 0;
178         }
179         
180         virtual void OnRehash()
181         {
182                 // reload our config file on rehash - we must destroy and re-allocate the classes
183                 // to call the constructor again and re-read our data.
184                 delete Conf;
185                 delete MyConf;
186                 Conf = new ConfigReader;
187                 std::string filterfile = Conf->ReadValue("filter","file",0);
188                 // this automatically re-reads the configuration file into the class
189                 MyConf = new ConfigReader(filterfile);
190                 if ((filterfile == "") || (!MyConf->Verify()))
191                 {
192                         // bail if the user forgot to create a config file
193                         printf("Error, could not find <filter file=\"\"> definition in your config file!");
194                         log(DEFAULT,"Error, could not find <filter file=\"\"> definition in your config file!");
195                         return;
196                 }
197                 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
198
199                 filters.clear();
200                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
201                 {
202                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
203                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
204                         if (!re)
205                         {
206                                 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
207                                 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
208                         }
209                         else
210                         {
211                                 filters.push_back(re);
212                                 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
213                         }
214                 }
215
216         }
217         
218         virtual Version GetVersion()
219         {
220                 // This is version 2 because version 1.x is the unreleased unrealircd module
221                 return Version(3,0,0,0,VF_VENDOR);
222         }
223         
224 };
225
226 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
227
228 class ModuleFilterPCREFactory : public ModuleFactory
229 {
230  public:
231         ModuleFilterPCREFactory()
232         {
233         }
234         
235         ~ModuleFilterPCREFactory()
236         {
237         }
238         
239         virtual Module * CreateModule()
240         {
241                 return new ModuleFilterPCRE;
242         }
243         
244 };
245
246
247 extern "C" void * init_module( void )
248 {
249         return new ModuleFilterPCREFactory;
250 }
251