]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/extra/m_filter_pcre.cpp
The rest of the server protocol interface and fix a warning in m_rline
[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-2008 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 glinetime, const std::string &pat, const std::string &flgs)
36                  : FilterResult(pat, rea, act, glinetime, flgs), 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
61         virtual ~ModuleFilterPCRE()
62         {
63         }
64
65         virtual FilterResult* FilterMatch(User* user, const std::string &text, int flgs)
66         {
67                 for (std::vector<PCREFilter>::iterator index = filters.begin(); index != filters.end(); index++)
68                 {
69                         /* Skip ones that dont apply to us */
70
71                         if (!FilterBase::AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flgs))
72                                 continue;
73
74                         if (pcre_exec(index->regexp, NULL, text.c_str(), text.length(), 0, 0, NULL, 0) > -1)
75                         {
76                                 fr = *index;
77                                 if (index != filters.begin())
78                                 {
79                                         filters.erase(index);
80                                         filters.insert(filters.begin(), fr);
81                                 }
82                                 return &fr;
83                         }
84                 }
85                 return NULL;
86         }
87
88         virtual bool DeleteFilter(const std::string &freeform)
89         {
90                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
91                 {
92                         if (i->freeform == freeform)
93                         {
94                                 pcre_free((*i).regexp);
95                                 filters.erase(i);
96                                 return true;
97                         }
98                 }
99                 return false;
100         }
101
102         virtual void SyncFilters(Module* proto, void* opaque)
103         {
104                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
105                 {
106                         this->SendFilter(proto, opaque, &(*i));
107                 }
108         }
109
110         virtual std::pair<bool, std::string> AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flgs)
111         {
112                 for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
113                 {
114                         if (i->freeform == freeform)
115                         {
116                                 return std::make_pair(false, "Filter already exists");
117                         }
118                 }
119
120                 re = pcre_compile(freeform.c_str(),0,&error,&erroffset,NULL);
121
122                 if (!re)
123                 {
124                         ServerInstance->Logs->Log("m_filter_pcre", DEFAULT,"Error in regular expression: %s at offset %d: %s\n", freeform.c_str(), erroffset, error);
125                         ServerInstance->Logs->Log("m_filter_pcre", DEFAULT,"Regular expression %s not loaded.", freeform.c_str());
126                         return std::make_pair(false, "Error in regular expression at offset " + ConvToStr(erroffset) + ": "+error);
127                 }
128                 else
129                 {
130                         filters.push_back(PCREFilter(re, reason, type, duration, freeform, flgs));
131                         return std::make_pair(true, "");
132                 }
133         }
134
135         virtual void OnRehash(User* user, const std::string &parameter)
136         {               
137                 ConfigReader MyConf(ServerInstance);
138
139                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
140                 {
141                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
142
143                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
144                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
145                         std::string action = MyConf.ReadValue("keyword", "action", index);
146                         std::string flgs = MyConf.ReadValue("keyword", "flags", index);
147                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index));
148                         if (action.empty())
149                                 action = "none";
150                         if (flgs.empty())
151                                 flgs = "*";
152
153                         re = pcre_compile(pattern.c_str(),0,&error,&erroffset,NULL);
154
155                         if (!re)
156                         {
157                                 ServerInstance->Logs->Log("CONFIG",DEFAULT,"Error in regular expression: %s at offset %d: %s\n", pattern.c_str(), erroffset, error);
158                                 ServerInstance->Logs->Log("CONFIG",DEFAULT,"Regular expression %s not loaded.", pattern.c_str());
159                         }
160                         else
161                         {
162                                 filters.push_back(PCREFilter(re, reason, action, gline_time, pattern, flgs));
163                                 ServerInstance->Logs->Log("CONFIG",DEFAULT,"Regular expression %s loaded.", pattern.c_str());
164                         }
165                 }
166                 FilterBase::OnRehash(user, parameter);
167         }
168
169         virtual int OnStats(char symbol, User* user, string_list &results)
170         {
171                 if (symbol == 's')
172                 {
173                         std::string sn = ServerInstance->Config->ServerName;
174                         for (std::vector<PCREFilter>::iterator i = filters.begin(); i != filters.end(); i++)
175                         {
176                                 results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
177                         }
178                         for (std::vector<std::string>::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i)
179                         {
180                                 results.push_back(sn+" 223 "+user->nick+" :EXEMPT "+(*i));
181                         }
182                 }
183                 return 0;
184         }
185 };
186
187 MODULE_INIT(ModuleFilterPCRE)
188