]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Add gline type, and <filter:duration> value to say how long to ban for.
[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)
41                  : FilterResult::FilterResult(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)
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 void OnRehash(const std::string &parameter)
79         {               
80                 ConfigReader MyConf(ServerInstance);
81
82                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
83                         pcre_free((*i).regexp);
84
85                 filters.clear();
86
87                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
88                 {
89                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
90                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
91                         std::string action = MyConf.ReadValue("keyword", "action", index);
92                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
93
94                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
95
96                         if (!re)
97                         {
98                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
99                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
100                         }
101                         else
102                         {
103                                 filters.push_back(PCREFilter(re, reason, action, gline_time));
104                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
105                         }
106                 }
107         }
108 };
109         
110
111 class ModuleFilterPCREFactory : public ModuleFactory
112 {
113  public:
114         ModuleFilterPCREFactory()
115         {
116         }
117         
118         ~ModuleFilterPCREFactory()
119         {
120         }
121         
122         virtual Module * CreateModule(InspIRCd* Me)
123         {
124                 return new ModuleFilterPCRE(Me);
125         }
126         
127 };
128
129
130 extern "C" void * init_module( void )
131 {
132         return new ModuleFilterPCREFactory;
133 }