]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
FindNick, FindChan, ChanModes, UserList, CountInvisible, PurgeEmptyChannels, GetClass...
[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(Server* 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                 Srv = Me;
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                 Srv->Log(DEFAULT,std::string("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                                 std::string 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 = std::string(t->nick);
130                                 }
131                                 else if (target_type == TYPE_CHANNEL)
132                                 {
133                                         chanrec* t = (chanrec*)dest;
134                                         target = std::string(t->name);
135                                 }
136                                 if (do_action == "block")
137                                 {       
138                                         Srv->SendOpers(std::string("Filter: ")+std::string(user->nick)+
139                                                         std::string(" had their notice filtered, target was ")+
140                                                         target+": "+reason);
141                                         user->WriteServ("NOTICE "+std::string(user->nick)+
142                                                         " :Your notice has been filtered and opers notified: "+reason);
143                                 }
144                                 Srv->Log(DEFAULT,std::string("Filter: ")+std::string(user->nick)+
145                                                 std::string(" had their notice filtered, target was ")+
146                                                 target+": "+reason+" Action: "+do_action);
147
148                                 if (do_action == "kill")
149                                 {
150                                         userrec::QuitUser(ServerInstance,user,reason);
151                                 }
152                                 return 1;
153                         }
154                 }
155                 return 0;
156         }
157         
158         virtual void OnRehash(const std::string &parameter)
159         {
160                 // reload our config file on rehash - we must destroy and re-allocate the classes
161                 // to call the constructor again and re-read our data.
162                 DELETE(Conf);
163                 DELETE(MyConf);
164                 Conf = new ConfigReader;
165                 std::string filterfile = Conf->ReadValue("filter","file",0);
166                 // this automatically re-reads the configuration file into the class
167                 MyConf = new ConfigReader(filterfile);
168                 if ((filterfile == "") || (!MyConf->Verify()))
169                 {
170                         FilterPCREException e;
171                         // bail if the user forgot to create a config file
172                         throw(e);
173                 }
174                 Srv->Log(DEFAULT,std::string("m_filter_pcre: read configuration from ")+filterfile);
175
176                 filters.clear();
177                 for (int index = 0; index < MyConf->Enumerate("keyword"); index++)
178                 {
179                         std::string pattern = MyConf->ReadValue("keyword","pattern",index);
180                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
181                         if (!re)
182                         {
183                                 log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
184                                 log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
185                         }
186                         else
187                         {
188                                 filters.push_back(re);
189                                 log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
190                         }
191                 }
192
193         }
194         
195         virtual Version GetVersion()
196         {
197                 // This is version 2 because version 1.x is the unreleased unrealircd module
198                 return Version(3,0,0,0,VF_VENDOR);
199         }
200         
201 };
202
203 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
204
205 class ModuleFilterPCREFactory : public ModuleFactory
206 {
207  public:
208         ModuleFilterPCREFactory()
209         {
210         }
211         
212         ~ModuleFilterPCREFactory()
213         {
214         }
215         
216         virtual Module * CreateModule(Server* Me)
217         {
218                 return new ModuleFilterPCRE(Me);
219         }
220         
221 };
222
223
224 extern "C" void * init_module( void )
225 {
226         return new ModuleFilterPCREFactory;
227 }
228