]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
This is better now.
[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 #include "m_filter.h"
29
30 /* $ModDesc: m_filter with regexps */
31 /* $CompileFlags: `pcre-config --cflags` */
32 /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */
33 /* $ModDep: m_filter.h */
34
35 class PCREFilter : public FilterResult
36 {
37  public:
38          pcre* regexp;
39
40          PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat)
41                  : FilterResult::FilterResult(pat, rea, act, gline_time), regexp(r)
42          {
43          }
44 };
45
46 class ModuleFilterPCRE : public FilterBase
47 {
48         std::vector<PCREFilter> filters;
49         pcre *re;
50         const char *error;
51         int erroffset;
52
53  public:
54         ModuleFilterPCRE(InspIRCd* Me)
55         : FilterBase::FilterBase(Me, "m_filter_pcre.so")
56         {
57                 OnRehash("");
58         }
59
60         virtual ~ModuleFilterPCRE()
61         {
62         }
63
64         virtual FilterResult* FilterMatch(const std::string &text)
65         {
66                 for (unsigned int index = 0; index < filters.size(); index++)
67                 {
68                         PCREFilter& filt = filters[index];
69                         
70                         if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
71                         {
72                                 return &filt;
73                         }
74                 }
75                 return NULL;
76         }
77
78         virtual bool DeleteFilter(const std::string &freeform)
79         {
80                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
81                 {
82                         if (i->freeform == freeform)
83                         {
84                                 pcre_free((*i).regexp);
85                                 filters.erase(i);
86                                 return true;
87                         }
88                 }
89                 return false;
90         }
91
92         virtual void SyncFilters(Module* proto, void* opaque)
93         {
94                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
95                 {
96                         this->SendFilter(proto, opaque, &(*i));
97                 }
98         }
99
100         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
101         {
102                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
103                 {
104                         if (i->freeform == freeform)
105                         {
106                                 return std::make_pair(false, "Filter already exists");
107                         }
108                 }
109
110                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
111
112                 if (!re)
113                 {
114                         ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
115                         ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
116                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
117                 }
118                 else
119                 {
120                         filters.push_back(PCREFilter(re, reason, type, duration, freeform));
121                         return std::make_pair(true, "");
122                 }
123         }
124
125         virtual void OnRehash(const std::string &parameter)
126         {               
127                 ConfigReader MyConf(ServerInstance);
128
129                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
130                 {
131                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
132
133                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
134                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
135                         std::string action = MyConf.ReadValue("keyword", "action", index);
136                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
137
138                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
139
140                         if (!re)
141                         {
142                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
143                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
144                         }
145                         else
146                         {
147                                 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern));
148                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
149                         }
150                 }
151         }
152
153         virtual int OnStats(char symbol, userrec* user, string_list &results)
154         {
155                 if (symbol == 's')
156                 {
157                         std::string sn = ServerInstance->Config->ServerName;
158                         for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
159                         {
160                                 results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
161                         }
162                 }
163                 return 0;
164         }
165 };
166         
167
168 class ModuleFilterPCREFactory : public ModuleFactory
169 {
170  public:
171         ModuleFilterPCREFactory()
172         {
173         }
174         
175         ~ModuleFilterPCREFactory()
176         {
177         }
178         
179         virtual Module * CreateModule(InspIRCd* Me)
180         {
181                 return new ModuleFilterPCRE(Me);
182         }
183         
184 };
185
186
187 extern "C" void * init_module( void )
188 {
189         return new ModuleFilterPCREFactory;
190 }