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