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