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