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