]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Replace OnAccessCheck with OnPreMode to remove a number of redundant checks
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 "xline.h"
16 #include "m_regex.h"
17
18 /* $ModDesc: Text (spam) filtering */
19
20 static std::string RegexEngine = "";
21 static Module* rxengine = NULL;
22
23 enum FilterFlags
24 {
25         FLAG_PART = 2,
26         FLAG_QUIT = 4,
27         FLAG_PRIVMSG = 8,
28         FLAG_NOTICE = 16
29 };
30
31 class FilterResult : public classbase
32 {
33  public:
34         std::string freeform;
35         std::string reason;
36         std::string action;
37         long gline_time;
38         std::string flags;
39
40         bool flag_no_opers;
41         bool flag_part_message;
42         bool flag_quit_message;
43         bool flag_privmsg;
44         bool flag_notice;
45
46         FilterResult(const std::string free, const std::string &rea, const std::string &act, long gt, const std::string &fla) :
47                         freeform(free), reason(rea), action(act), gline_time(gt), flags(fla)
48         {
49                 this->FillFlags(fla);
50         }
51
52         int FillFlags(const std::string &fl)
53         {
54                 flags = fl;
55                 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg = flag_notice = false;
56                 size_t x = 0;
57
58                 for (std::string::const_iterator n = flags.begin(); n != flags.end(); ++n, ++x)
59                 {
60                         switch (*n)
61                         {
62                                 case 'o':
63                                         flag_no_opers = true;
64                                 break;
65                                 case 'P':
66                                         flag_part_message = true;
67                                 break;
68                                 case 'q':
69                                         flag_quit_message = true;
70                                 break;
71                                 case 'p':
72                                         flag_privmsg = true;
73                                 break;
74                                 case 'n':
75                                         flag_notice = true;
76                                 break;
77                                 case '*':
78                                         flag_no_opers = flag_part_message = flag_quit_message =
79                                                 flag_privmsg = flag_notice = true;
80                                 break;
81                                 default:
82                                         return x;
83                                 break;
84                         }
85                 }
86                 return 0;
87         }
88
89         FilterResult()
90         {
91         }
92
93         virtual ~FilterResult()
94         {
95         }
96 };
97
98 class FilterBase;
99
100 class CommandFilter : public Command
101 {
102         FilterBase* Base;
103  public:
104         CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &ssource)
105                 : Command(Me, reinterpret_cast<Module*>(f), "FILTER", "o", 1, 5), Base(f)
106         {
107                 this->syntax = "<filter-definition> <action> <flags> [<gline-duration>] :<reason>";
108         }
109         CmdResult Handle(const std::vector<std::string>&, User*);
110
111         void TooFewParams(User* user, const std::string &extra_text)
112         {
113                 user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick.c_str(), extra_text.c_str());
114         }
115
116         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
117         {
118                 return ROUTE_BROADCAST;
119         }
120 };
121
122 class FilterBase : public Module
123 {
124         CommandFilter filtcommand;
125         int flags;
126 protected:
127         std::vector<std::string> exemptfromfilter; // List of channel names excluded from filtering.
128  public:
129         FilterBase(InspIRCd* Me, const std::string &source);
130         virtual ~FilterBase();
131         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
132         virtual FilterResult* FilterMatch(User* user, const std::string &text, int flags) = 0;
133         virtual bool DeleteFilter(const std::string &freeform) = 0;
134         virtual void SyncFilters(Module* proto, void* opaque) = 0;
135         virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter);
136         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) = 0;
137         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list);
138         virtual void OnRehash(User* user);
139         virtual Version GetVersion();
140         std::string EncodeFilter(FilterResult* filter);
141         FilterResult DecodeFilter(const std::string &data);
142         virtual void OnSyncNetwork(Module* proto, void* opaque);
143         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata);
144         virtual ModResult OnStats(char symbol, User* user, string_list &results) = 0;
145         virtual ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line);
146         bool AppliesToMe(User* user, FilterResult* filter, int flags);
147         void OnLoadModule(Module* mod, const std::string& name);
148         virtual void ReadFilters(ConfigReader &MyConf) = 0;
149 };
150
151 CmdResult CommandFilter::Handle(const std::vector<std::string> &parameters, User *user)
152 {
153         if (parameters.size() == 1)
154         {
155                 /* Deleting a filter */
156                 if (Base->DeleteFilter(parameters[0]))
157                 {
158                         user->WriteServ("NOTICE %s :*** Removed filter '%s'", user->nick.c_str(), parameters[0].c_str());
159                         ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', std::string("FILTER: ")+user->nick+" removed filter '"+parameters[0]+"'");
160                         return CMD_SUCCESS;
161                 }
162                 else
163                 {
164                         user->WriteServ("NOTICE %s :*** Filter '%s' not found in list, try /stats s.", user->nick.c_str(), parameters[0].c_str());
165                         return CMD_FAILURE;
166                 }
167         }
168         else
169         {
170                 /* Adding a filter */
171                 if (parameters.size() >= 4)
172                 {
173                         std::string freeform = parameters[0];
174                         std::string type = parameters[1];
175                         std::string flags = parameters[2];
176                         std::string reason;
177                         long duration = 0;
178
179
180                         if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent"))
181                         {
182                                 user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick.c_str(), type.c_str());
183                                 return CMD_FAILURE;
184                         }
185
186                         if (type == "gline")
187                         {
188                                 if (parameters.size() >= 5)
189                                 {
190                                         duration = ServerInstance->Duration(parameters[3]);
191                                         reason = parameters[4];
192                                 }
193                                 else
194                                 {
195                                         this->TooFewParams(user, ": When setting a gline type filter, a gline duration must be specified as the third parameter.");
196                                         return CMD_FAILURE;
197                                 }
198                         }
199                         else
200                         {
201                                 reason = parameters[3];
202                         }
203                         std::pair<bool, std::string> result = Base->AddFilter(freeform, type, reason, duration, flags);
204                         if (result.first)
205                         {
206                                 user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick.c_str(), freeform.c_str(),
207                                                 type.c_str(), (duration ? ", duration " : ""), (duration ? parameters[3].c_str() : ""),
208                                                 flags.c_str(), reason.c_str());
209
210                                 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', std::string("FILTER: ")+user->nick+" added filter '"+freeform+"', type '"+type+"', "+(duration ? "duration "+parameters[3]+", " : "")+"flags '"+flags+"', reason: "+reason);
211
212                                 return CMD_SUCCESS;
213                         }
214                         else
215                         {
216                                 user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick.c_str(), freeform.c_str(), result.second.c_str());
217                                 return CMD_FAILURE;
218                         }
219                 }
220                 else
221                 {
222                         this->TooFewParams(user, ".");
223                         return CMD_FAILURE;
224                 }
225
226         }
227 }
228
229 bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int iflags)
230 {
231         if ((filter->flag_no_opers) && IS_OPER(user))
232                 return false;
233         if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
234                 return false;
235         if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
236                 return false;
237         if ((iflags & FLAG_QUIT)   && (!filter->flag_quit_message))
238                 return false;
239         if ((iflags & FLAG_PART)   && (!filter->flag_part_message))
240                 return false;
241         return true;
242 }
243
244 FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me), filtcommand(this, Me, source)
245 {
246         Me->Modules->UseInterface("RegularExpression");
247         ServerInstance->AddCommand(&filtcommand);
248         Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncNetwork, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnLoadModule };
249         ServerInstance->Modules->Attach(eventlist, this, 8);
250 }
251
252 FilterBase::~FilterBase()
253 {
254         ServerInstance->Modules->DoneWithInterface("RegularExpression");
255 }
256
257 ModResult FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
258 {
259         if (!IS_LOCAL(user))
260                 return MOD_RES_PASSTHRU;
261
262         flags = FLAG_PRIVMSG;
263         return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
264 }
265
266 ModResult FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
267 {
268         /* Leave ulines alone */
269         if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user)))
270                 return MOD_RES_PASSTHRU;
271
272         if (!flags)
273                 flags = FLAG_NOTICE;
274
275         FilterResult* f = this->FilterMatch(user, text, flags);
276         if (f)
277         {
278                 std::string target = "";
279                 if (target_type == TYPE_USER)
280                 {
281                         User* t = (User*)dest;
282                         target = std::string(t->nick);
283                 }
284                 else if (target_type == TYPE_CHANNEL)
285                 {
286                         Channel* t = (Channel*)dest;
287                         target = std::string(t->name);
288                         std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), target);
289                         if (i != exemptfromfilter.end()) return MOD_RES_PASSTHRU;
290                 }
291                 if (f->action == "block")
292                 {
293                         ServerInstance->SNO->WriteGlobalSno('a', std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason);
294                         if (target_type == TYPE_CHANNEL)
295                                 user->WriteNumeric(404, "%s %s :Message to channel blocked and opers notified (%s)",user->nick.c_str(), target.c_str(), f->reason.c_str());
296                         else
297                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message to "+target+" was blocked and opers notified: "+f->reason);
298                 }
299                 if (f->action == "silent")
300                 {
301                         if (target_type == TYPE_CHANNEL)
302                                 user->WriteNumeric(404, "%s %s :Message to channel blocked (%s)",user->nick.c_str(), target.c_str(), f->reason.c_str());
303                         else
304                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message to "+target+" was blocked: "+f->reason);
305                 }
306                 if (f->action == "kill")
307                 {
308                         ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
309                 }
310                 if (f->action == "gline")
311                 {
312                         GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
313                         if (ServerInstance->XLines->AddLine(gl,NULL))
314                         {
315                                 ServerInstance->XLines->ApplyLines();
316                         }
317                         else
318                                 delete gl;
319                 }
320
321                 ServerInstance->Logs->Log("FILTER",DEFAULT,"FILTER: "+ user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + f->action);
322                 return MOD_RES_DENY;
323         }
324         return MOD_RES_PASSTHRU;
325 }
326
327 ModResult FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &parameters, User *user, bool validated, const std::string &original_line)
328 {
329         flags = 0;
330         if (validated && IS_LOCAL(user))
331         {
332                 std::string checkline;
333                 int replacepoint = 0;
334                 bool parting = false;
335
336                 if (command == "QUIT")
337                 {
338                         /* QUIT with no reason: nothing to do */
339                         if (parameters.size() < 1)
340                                 return MOD_RES_PASSTHRU;
341
342                         checkline = parameters[0];
343                         replacepoint = 0;
344                         parting = false;
345                         flags = FLAG_QUIT;
346                 }
347                 else if (command == "PART")
348                 {
349                         /* PART with no reason: nothing to do */
350                         if (parameters.size() < 2)
351                                 return MOD_RES_PASSTHRU;
352
353                         std::vector<std::string>::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), parameters[0]);
354                         if (i != exemptfromfilter.end()) return MOD_RES_PASSTHRU;
355                         checkline = parameters[1];
356                         replacepoint = 1;
357                         parting = true;
358                         flags = FLAG_PART;
359                 }
360                 else
361                         /* We're only messing with PART and QUIT */
362                         return MOD_RES_PASSTHRU;
363
364                 FilterResult* f = NULL;
365
366                 if (flags)
367                         f = this->FilterMatch(user, checkline, flags);
368
369                 if (!f)
370                         /* PART or QUIT reason doesnt match a filter */
371                         return MOD_RES_PASSTHRU;
372
373                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
374                 Command* c = ServerInstance->Parser->GetHandler(command);
375                 if (c)
376                 {
377                         std::vector<std::string> params;
378                         for (int item = 0; item < (int)parameters.size(); item++)
379                                 params.push_back(parameters[item]);
380                         params[replacepoint] = "Reason filtered";
381
382                         /* We're blocking, OR theyre quitting and its a KILL action
383                          * (we cant kill someone whos already quitting, so filter them anyway)
384                          */
385                         if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
386                         {
387                                 c->Handle(params, user);
388                                 return MOD_RES_DENY;
389                         }
390                         else
391                         {
392                                 /* Are they parting, if so, kill is applicable */
393                                 if ((parting) && (f->action == "kill"))
394                                 {
395                                         user->WriteServ("NOTICE %s :*** Your PART message was filtered: %s", user->nick.c_str(), f->reason.c_str());
396                                         ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
397                                 }
398                                 if (f->action == "gline")
399                                 {
400                                         /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
401                                         GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString());
402                                         if (ServerInstance->XLines->AddLine(gl,NULL))
403                                         {
404                                                 ServerInstance->XLines->ApplyLines();
405                                         }
406                                         else
407                                                 delete gl;
408                                 }
409                                 return MOD_RES_DENY;
410                         }
411                 }
412                 return MOD_RES_PASSTHRU;
413         }
414         return MOD_RES_PASSTHRU;
415 }
416
417 void FilterBase::OnRehash(User* user)
418 {
419         ConfigReader MyConf(ServerInstance);
420         std::vector<std::string>().swap(exemptfromfilter);
421         for (int index = 0; index < MyConf.Enumerate("exemptfromfilter"); ++index)
422         {
423                 std::string chan = MyConf.ReadValue("exemptfromfilter", "channel", index);
424                 if (!chan.empty()) {
425                         exemptfromfilter.push_back(chan);
426                 }
427         }
428         std::string newrxengine = MyConf.ReadValue("filteropts", "engine", 0);
429         if (!RegexEngine.empty())
430         {
431                 if (RegexEngine == newrxengine)
432                         return;
433
434                 ServerInstance->SNO->WriteGlobalSno('a', "Dumping all filters due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str());
435                 //ServerInstance->XLines->DelAll("R");
436         }
437         rxengine = NULL;
438
439         RegexEngine = newrxengine;
440         modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression");
441         if (ml)
442         {
443                 for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i)
444                 {
445                         if (RegexNameRequest(this, *i).Send() == newrxengine)
446                         {
447                                 ServerInstance->SNO->WriteGlobalSno('a', "Filter now using engine '%s'", RegexEngine.c_str());
448                                 rxengine = *i;
449                         }
450                 }
451         }
452         if (!rxengine)
453         {
454                 ServerInstance->SNO->WriteGlobalSno('a', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", RegexEngine.c_str());
455         }
456 }
457
458 void FilterBase::OnLoadModule(Module* mod, const std::string& name)
459 {
460         if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
461         {
462                 std::string rxname = RegexNameRequest(this, mod).Send();
463                 if (rxname == RegexEngine)
464                 {
465                         rxengine = mod;
466                         /* Force a rehash to make sure that any filters that couldnt be applied from the conf
467                          * on startup or on load are applied right now.
468                          */
469                         ConfigReader Config(ServerInstance);
470                         ServerInstance->SNO->WriteGlobalSno('a', "Found and activated regex module '%s' for m_filter.so.", RegexEngine.c_str());
471                         ReadFilters(Config);
472                 }
473         }
474 }
475
476
477 Version FilterBase::GetVersion()
478 {
479         return Version("$Id$", VF_VENDOR | VF_COMMON, API_VERSION);
480 }
481
482
483 std::string FilterBase::EncodeFilter(FilterResult* filter)
484 {
485         std::ostringstream stream;
486         std::string x = filter->freeform;
487
488         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
489         for (std::string::iterator n = x.begin(); n != x.end(); n++)
490                 if (*n == ' ')
491                         *n = '\7';
492
493         stream << x << " " << filter->action << " " << (filter->flags.empty() ? "-" : filter->flags) << " " << filter->gline_time << " :" << filter->reason;
494         return stream.str();
495 }
496
497 FilterResult FilterBase::DecodeFilter(const std::string &data)
498 {
499         FilterResult res;
500         irc::tokenstream tokens(data);
501         tokens.GetToken(res.freeform);
502         tokens.GetToken(res.action);
503         tokens.GetToken(res.flags);
504         if (res.flags == "-")
505                 res.flags = "";
506         res.FillFlags(res.flags);
507         tokens.GetToken(res.gline_time);
508         tokens.GetToken(res.reason);
509
510         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
511         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
512                 if (*n == '\7')
513                         *n = ' ';
514
515         return res;
516 }
517
518 void FilterBase::OnSyncNetwork(Module* proto, void* opaque)
519 {
520         this->SyncFilters(proto, opaque);
521 }
522
523 void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter)
524 {
525         proto->ProtoSendMetaData(opaque, NULL, "filter", EncodeFilter(iter));
526 }
527
528 void FilterBase::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
529 {
530         if ((target == NULL) && (extname == "filter"))
531         {
532                 FilterResult data = DecodeFilter(extdata);
533                 this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags);
534         }
535 }
536
537 class ImplFilter : public FilterResult
538 {
539  public:
540         Regex* regex;
541
542         ImplFilter(Module* mymodule, const std::string &rea, const std::string &act, long glinetime, const std::string &pat, const std::string &flgs)
543                 : FilterResult(pat, rea, act, glinetime, flgs)
544         {
545                 if (!rxengine)
546                         throw ModuleException("Regex module implementing '"+RegexEngine+"' is not loaded!");
547
548                 regex = RegexFactoryRequest(mymodule, rxengine, pat).Create();
549         }
550
551         ImplFilter()
552         {
553         }
554 };
555
556 class ModuleFilter : public FilterBase
557 {
558         std::vector<ImplFilter> filters;
559         const char *error;
560         int erroffset;
561         ImplFilter fr;
562
563  public:
564         ModuleFilter(InspIRCd* Me)
565         : FilterBase(Me, "m_filter.so")
566         {
567                 OnRehash(NULL);
568         }
569
570         virtual ~ModuleFilter()
571         {
572         }
573
574         virtual FilterResult* FilterMatch(User* user, const std::string &text, int flgs)
575         {
576                 for (std::vector<ImplFilter>::iterator index = filters.begin(); index != filters.end(); index++)
577                 {
578                         /* Skip ones that dont apply to us */
579                         if (!FilterBase::AppliesToMe(user, dynamic_cast<FilterResult*>(&(*index)), flgs))
580                                 continue;
581
582                         //ServerInstance->Logs->Log("m_filter", DEBUG, "Match '%s' against '%s'", text.c_str(), index->freeform.c_str());
583                         if (index->regex->Matches(text))
584                         {
585                                 //ServerInstance->Logs->Log("m_filter", DEBUG, "MATCH");
586                                 fr = *index;
587                                 if (index != filters.begin())
588                                 {
589                                         /* Move to head of list for efficiency */
590                                         filters.erase(index);
591                                         filters.insert(filters.begin(), fr);
592                                 }
593                                 return &fr;
594                         }
595                         //ServerInstance->Logs->Log("m_filter", DEBUG, "NO MATCH");
596                 }
597                 return NULL;
598         }
599
600         virtual bool DeleteFilter(const std::string &freeform)
601         {
602                 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
603                 {
604                         if (i->freeform == freeform)
605                         {
606                                 delete i->regex;
607                                 filters.erase(i);
608                                 return true;
609                         }
610                 }
611                 return false;
612         }
613
614         virtual void SyncFilters(Module* proto, void* opaque)
615         {
616                 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
617                 {
618                         this->SendFilter(proto, opaque, &(*i));
619                 }
620         }
621
622         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)
623         {
624                 for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
625                 {
626                         if (i->freeform == freeform)
627                         {
628                                 return std::make_pair(false, "Filter already exists");
629                         }
630                 }
631
632                 try
633                 {
634                         filters.push_back(ImplFilter(this, reason, type, duration, freeform, flgs));
635                 }
636                 catch (ModuleException &e)
637                 {
638                         ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason());
639                         return std::make_pair(false, e.GetReason());
640                 }
641                 return std::make_pair(true, "");
642         }
643
644         virtual void OnRehash(User* user)
645         {
646                 ConfigReader MyConf(ServerInstance);
647                 FilterBase::OnRehash(user);
648                 ReadFilters(MyConf);
649         }
650
651         void ReadFilters(ConfigReader &MyConf)
652         {
653                 for (int index = 0; index < MyConf.Enumerate("keyword"); index++)
654                 {
655                         this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index));
656
657                         std::string pattern = MyConf.ReadValue("keyword", "pattern", index);
658                         std::string reason = MyConf.ReadValue("keyword", "reason", index);
659                         std::string action = MyConf.ReadValue("keyword", "action", index);
660                         std::string flgs = MyConf.ReadValue("keyword", "flags", index);
661                         long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index));
662                         if (action.empty())
663                                 action = "none";
664                         if (flgs.empty())
665                                 flgs = "*";
666
667                         try
668                         {
669                                 filters.push_back(ImplFilter(this, reason, action, gline_time, pattern, flgs));
670                                 ServerInstance->Logs->Log("m_filter", DEFAULT, "Regular expression %s loaded.", pattern.c_str());
671                         }
672                         catch (ModuleException &e)
673                         {
674                                 ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason());
675                         }
676                 }
677         }
678
679         virtual ModResult OnStats(char symbol, User* user, string_list &results)
680         {
681                 if (symbol == 's')
682                 {
683                         std::string sn = ServerInstance->Config->ServerName;
684                         for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
685                         {
686                                 results.push_back(sn+" 223 "+user->nick+" :"+RegexEngine+":"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason);
687                         }
688                         for (std::vector<std::string>::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i)
689                         {
690                                 results.push_back(sn+" 223 "+user->nick+" :EXEMPT "+(*i));
691                         }
692                 }
693                 return MOD_RES_PASSTHRU;
694         }
695 };
696
697 MODULE_INIT(ModuleFilter)