]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Make the flags work
[user/henk/code/inspircd.git] / src / modules / extra / m_filter_pcre.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <pcre.h>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "m_filter.h"
20
21 /* $ModDesc: m_filter with regexps */
22 /* $CompileFlags: exec("pcre-config --cflags") */
23 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
24 /* $ModDep: m_filter.h */
25
26 class PCREFilter : public FilterResult
27 {
28  public:
29          pcre* regexp;
30
31          PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat, const std::string &flags)
32                  : FilterResult::FilterResult(pat, rea, act, gline_time, flags), regexp(r)
33          {
34          }
35
36          PCREFilter()
37          {
38          }
39 };
40
41 class ModuleFilterPCRE : public FilterBase
42 {
43         std::vector<PCREFilter> filters;
44         pcre *re;
45         const char *error;
46         int erroffset;
47         PCREFilter fr;
48
49  public:
50         ModuleFilterPCRE(InspIRCd* Me)
51         : FilterBase::FilterBase(Me, "m_filter_pcre.so")
52         {
53                 OnRehash(NULL,"");
54         }
55
56         virtual ~ModuleFilterPCRE()
57         {
58         }
59
60         virtual FilterResult* FilterMatch(userrec* user, const std::string &text, int flags)
61         {
62                 for (std::vector<PCREFilter>::iterator index = filters.begin(); index != filters.end(); index++)
63                 {
64                         /* Skip ones that dont apply to us */
65
66                         if (!FilterBase::AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flags))
67                                 continue;
68
69                         if (pcre_exec(index->regexp, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
70                         {
71                                 fr = *index;
72                                 if (index != filters.begin())
73                                 {
74                                         filters.erase(index);
75                                         filters.insert(filters.begin(), fr);
76                                 }
77                                 return &fr;
78                         }
79                 }
80                 return NULL;
81         }
82
83         virtual bool DeleteFilter(const std::string &freeform)
84         {
85                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
86                 {
87                         if (i->freeform == freeform)
88                         {
89                                 pcre_free((*i).regexp);
90                                 filters.erase(i);
91                                 return true;
92                         }
93                 }
94                 return false;
95         }
96
97         virtual void SyncFilters(Module* proto, void* opaque)
98         {
99                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
100                 {
101                         this->SendFilter(proto, opaque, &(*i));
102                 }
103         }
104
105         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flags)
106         {
107                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
108                 {
109                         if (i->freeform == freeform)
110                         {
111                                 return std::make_pair(false, "Filter already exists");
112                         }
113                 }
114
115                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
116
117                 if (!re)
118                 {
119                         ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
120                         ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
121                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
122                 }
123                 else
124                 {
125                         filters.push_back(PCREFilter(re, reason, type, duration, freeform, flags));
126                         return std::make_pair(true, "");
127                 }
128         }
129
130         virtual void OnRehash(userrec* user, const std::string &parameter)
131         {               
132                 ConfigReader MyConf(ServerInstance);
133
134                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
135                 {
136                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
137
138                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
139                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
140                         std::string action = MyConf.ReadValue("keyword", "action", index);
141                         std::string flags = MyConf.ReadValue("keyword", "flags", index);
142                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
143
144                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
145
146                         if (!re)
147                         {
148                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
149                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
150                         }
151                         else
152                         {
153                                 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern, flags));
154                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
155                         }
156                 }
157         }
158
159         virtual int OnStats(char symbol, userrec* user, string_list &results)
160         {
161                 if (symbol == 's')
162                 {
163                         std::string sn = ServerInstance->Config->ServerName;
164                         for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
165                         {
166                                 results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
167                         }
168                 }
169                 return 0;
170         }
171 };
172         
173
174 class ModuleFilterPCREFactory : public ModuleFactory
175 {
176  public:
177         ModuleFilterPCREFactory()
178         {
179         }
180         
181         ~ModuleFilterPCREFactory()
182         {
183         }
184         
185         virtual Module * CreateModule(InspIRCd* Me)
186         {
187                 return new ModuleFilterPCRE(Me);
188         }
189         
190 };
191
192
193 extern "C" void * init_module( void )
194 {
195         return new ModuleFilterPCREFactory;
196 }