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