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