]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
Oops, forgot command source, these wouldnt be able to be unloaded properly
[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          std::string pattern;
40
41          PCREFilter(pcre* r, const std::string &rea, const std::string &act, long gline_time, const std::string &pat)
42                  : FilterResult::FilterResult(rea, act, gline_time), regexp(r), pattern(pat)
43          {
44          }
45 };
46
47 class ModuleFilterPCRE : public FilterBase
48 {
49         std::vector<PCREFilter> filters;
50         pcre *re;
51         const char *error;
52         int erroffset;
53
54  public:
55         ModuleFilterPCRE(InspIRCd* Me)
56         : FilterBase::FilterBase(Me, "m_filter_pcre.so")
57         {
58                 OnRehash("");
59         }
60
61         virtual ~ModuleFilterPCRE()
62         {
63         }
64
65         virtual FilterResult* FilterMatch(const std::string &text)
66         {
67                 for (unsigned int index = 0; index < filters.size(); index++)
68                 {
69                         PCREFilter& filt = filters[index];
70                         
71                         if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1)
72                         {
73                                 return &filt;
74                         }
75                 }
76                 return NULL;
77         }
78
79         virtual bool DeleteFilter(const std::string &freeform)
80         {
81                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
82                 {
83                         if (i->pattern == freeform)
84                         {
85                                 filters.erase(i);
86                                 return true;
87                         }
88                 }
89                 return false;
90         }
91
92         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration)
93         {
94                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
95                 {
96                         if (i->pattern == freeform)
97                         {
98                                 return std::make_pair(false, "Filter already exists");
99                         }
100                 }
101
102                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
103
104                 if (!re)
105                 {
106                         ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
107                         ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
108                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
109                 }
110                 else
111                 {
112                         filters.push_back(PCREFilter(re, reason, type, duration, freeform));
113                         return std::make_pair(true, "");
114                 }
115         }
116
117         virtual void OnRehash(const std::string &parameter)
118         {               
119                 ConfigReader MyConf(ServerInstance);
120
121                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
122                         pcre_free((*i).regexp);
123
124                 filters.clear();
125
126                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
127                 {
128                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
129                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
130                         std::string action = MyConf.ReadValue("keyword", "action", index);
131                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index).c_str());
132
133                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
134
135                         if (!re)
136                         {
137                                 ServerInstance->Log(DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
138                                 ServerInstance->Log(DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
139                         }
140                         else
141                         {
142                                 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern));
143                                 ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str());
144                         }
145                 }
146         }
147 };
148         
149
150 class ModuleFilterPCREFactory : public ModuleFactory
151 {
152  public:
153         ModuleFilterPCREFactory()
154         {
155         }
156         
157         ~ModuleFilterPCREFactory()
158         {
159         }
160         
161         virtual Module * CreateModule(InspIRCd* Me)
162         {
163                 return new ModuleFilterPCRE(Me);
164         }
165         
166 };
167
168
169 extern "C" void * init_module( void )
170 {
171         return new ModuleFilterPCREFactory;
172 }