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