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