]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Merge the latest changes from insp20 into master.
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2004, 2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "modules/regex.h"
26
27 enum FilterFlags
28 {
29         FLAG_PART = 2,
30         FLAG_QUIT = 4,
31         FLAG_PRIVMSG = 8,
32         FLAG_NOTICE = 16
33 };
34
35 enum FilterAction
36 {
37         FA_GLINE,
38         FA_BLOCK,
39         FA_SILENT,
40         FA_KILL,
41         FA_NONE
42 };
43
44 class FilterResult
45 {
46  public:
47         Regex* regex;
48         std::string freeform;
49         std::string reason;
50         FilterAction action;
51         long gline_time;
52
53         bool flag_no_opers;
54         bool flag_part_message;
55         bool flag_quit_message;
56         bool flag_privmsg;
57         bool flag_notice;
58         bool flag_strip_color;
59
60         FilterResult(dynamic_reference<RegexFactory>& RegexEngine, const std::string& free, const std::string& rea, FilterAction act, long gt, const std::string& fla)
61                 : freeform(free), reason(rea), action(act), gline_time(gt)
62         {
63                 if (!RegexEngine)
64                         throw ModuleException("Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!");
65                 regex = RegexEngine->Create(free);
66                 this->FillFlags(fla);
67         }
68
69         char FillFlags(const std::string &fl)
70         {
71                 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg =
72                         flag_notice = flag_strip_color = false;
73
74                 for (std::string::const_iterator n = fl.begin(); n != fl.end(); ++n)
75                 {
76                         switch (*n)
77                         {
78                                 case 'o':
79                                         flag_no_opers = true;
80                                 break;
81                                 case 'P':
82                                         flag_part_message = true;
83                                 break;
84                                 case 'q':
85                                         flag_quit_message = true;
86                                 break;
87                                 case 'p':
88                                         flag_privmsg = true;
89                                 break;
90                                 case 'n':
91                                         flag_notice = true;
92                                 break;
93                                 case 'c':
94                                         flag_strip_color = true;
95                                 break;
96                                 case '*':
97                                         flag_no_opers = flag_part_message = flag_quit_message =
98                                                 flag_privmsg = flag_notice = flag_strip_color = true;
99                                 break;
100                                 default:
101                                         return *n;
102                                 break;
103                         }
104                 }
105                 return 0;
106         }
107
108         std::string GetFlags()
109         {
110                 std::string flags;
111                 if (flag_no_opers)
112                         flags.push_back('o');
113                 if (flag_part_message)
114                         flags.push_back('P');
115                 if (flag_quit_message)
116                         flags.push_back('q');
117                 if (flag_privmsg)
118                         flags.push_back('p');
119                 if (flag_notice)
120                         flags.push_back('n');
121
122                 /* Order is important here, 'c' must be the last char in the string as it is unsupported
123                  * on < 2.0.10, and the logic in FillFlags() stops parsing when it ecounters an unknown
124                  * character.
125                  */
126                 if (flag_strip_color)
127                         flags.push_back('c');
128
129                 if (flags.empty())
130                         flags.push_back('-');
131
132                 return flags;
133         }
134
135         FilterResult()
136         {
137         }
138 };
139
140 class CommandFilter : public Command
141 {
142  public:
143         CommandFilter(Module* f)
144                 : Command(f, "FILTER", 1, 5)
145         {
146                 flags_needed = 'o';
147                 this->syntax = "<filter-definition> <action> <flags> [<gline-duration>] :<reason>";
148         }
149         CmdResult Handle(const std::vector<std::string>&, User*);
150
151         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
152         {
153                 return ROUTE_BROADCAST;
154         }
155 };
156
157 class ModuleFilter : public Module
158 {
159         typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
160
161         bool initing;
162         RegexFactory* factory;
163         void FreeFilters();
164
165  public:
166         CommandFilter filtcommand;
167         dynamic_reference<RegexFactory> RegexEngine;
168
169         std::vector<FilterResult> filters;
170         int flags;
171
172         // List of channel names excluded from filtering.
173         ExemptTargetSet exemptedchans;
174
175         // List of target nicknames excluded from filtering.
176         ExemptTargetSet exemptednicks;
177
178         ModuleFilter();
179         CullResult cull() CXX11_OVERRIDE;
180         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE;
181         FilterResult* FilterMatch(User* user, const std::string &text, int flags);
182         bool DeleteFilter(const std::string &freeform);
183         std::pair<bool, std::string> AddFilter(const std::string &freeform, FilterAction type, const std::string &reason, long duration, const std::string &flags);
184         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
185         Version GetVersion() CXX11_OVERRIDE;
186         std::string EncodeFilter(FilterResult* filter);
187         FilterResult DecodeFilter(const std::string &data);
188         void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
189         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
190         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
191         ModResult OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line) CXX11_OVERRIDE;
192         void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
193         bool AppliesToMe(User* user, FilterResult* filter, int flags);
194         void ReadFilters();
195         static bool StringToFilterAction(const std::string& str, FilterAction& fa);
196         static std::string FilterActionToString(FilterAction fa);
197 };
198
199 CmdResult CommandFilter::Handle(const std::vector<std::string> &parameters, User *user)
200 {
201         if (parameters.size() == 1)
202         {
203                 /* Deleting a filter */
204                 Module *me = creator;
205                 if (static_cast<ModuleFilter *>(me)->DeleteFilter(parameters[0]))
206                 {
207                         user->WriteNotice("*** Removed filter '" + parameters[0] + "'");
208                         ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', "FILTER: "+user->nick+" removed filter '"+parameters[0]+"'");
209                         return CMD_SUCCESS;
210                 }
211                 else
212                 {
213                         user->WriteNotice("*** Filter '" + parameters[0] + "' not found in list, try /stats s.");
214                         return CMD_FAILURE;
215                 }
216         }
217         else
218         {
219                 /* Adding a filter */
220                 if (parameters.size() >= 4)
221                 {
222                         const std::string& freeform = parameters[0];
223                         FilterAction type;
224                         const std::string& flags = parameters[2];
225                         unsigned int reasonindex;
226                         long duration = 0;
227
228                         if (!ModuleFilter::StringToFilterAction(parameters[1], type))
229                         {
230                                 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.");
231                                 return CMD_FAILURE;
232                         }
233
234                         if (type == FA_GLINE)
235                         {
236                                 if (parameters.size() >= 5)
237                                 {
238                                         duration = InspIRCd::Duration(parameters[3]);
239                                         reasonindex = 4;
240                                 }
241                                 else
242                                 {
243                                         user->WriteNotice("*** Not enough parameters: When setting a gline type filter, a gline duration must be specified as the third parameter.");
244                                         return CMD_FAILURE;
245                                 }
246                         }
247                         else
248                         {
249                                 reasonindex = 3;
250                         }
251
252                         Module *me = creator;
253                         std::pair<bool, std::string> result = static_cast<ModuleFilter *>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
254                         if (result.first)
255                         {
256                                 user->WriteNotice("*** Added filter '" + freeform + "', type '" + parameters[1] + "'" +
257                                         (duration ? ", duration " +  parameters[3] : "") + ", flags '" + flags + "', reason: '" +
258                                         parameters[reasonindex] + "'");
259
260                                 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', "FILTER: "+user->nick+" added filter '"+freeform+"', type '"+parameters[1]+"', "+(duration ? "duration "+parameters[3]+", " : "")+"flags '"+flags+"', reason: "+parameters[reasonindex]);
261
262                                 return CMD_SUCCESS;
263                         }
264                         else
265                         {
266                                 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
267                                 return CMD_FAILURE;
268                         }
269                 }
270                 else
271                 {
272                         user->WriteNotice("*** Not enough parameters.");
273                         return CMD_FAILURE;
274                 }
275
276         }
277 }
278
279 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
280 {
281         if ((filter->flag_no_opers) && user->IsOper())
282                 return false;
283         if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
284                 return false;
285         if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
286                 return false;
287         if ((iflags & FLAG_QUIT)   && (!filter->flag_quit_message))
288                 return false;
289         if ((iflags & FLAG_PART)   && (!filter->flag_part_message))
290                 return false;
291         return true;
292 }
293
294 ModuleFilter::ModuleFilter()
295         : initing(true), filtcommand(this), RegexEngine(this, "regex")
296 {
297 }
298
299 CullResult ModuleFilter::cull()
300 {
301         FreeFilters();
302         return Module::cull();
303 }
304
305 void ModuleFilter::FreeFilters()
306 {
307         for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
308                 delete i->regex;
309
310         filters.clear();
311 }
312
313 ModResult ModuleFilter::OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype)
314 {
315         // Leave remote users and servers alone
316         if (!IS_LOCAL(user))
317                 return MOD_RES_PASSTHRU;
318
319         flags = (msgtype == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
320
321         FilterResult* f = this->FilterMatch(user, text, flags);
322         if (f)
323         {
324                 std::string target;
325                 if (target_type == TYPE_USER)
326                 {
327                         User* t = (User*)dest;
328                         // Check if the target nick is exempted, if yes, ignore this message
329                         if (exemptednicks.count(t->nick))
330                                 return MOD_RES_PASSTHRU;
331
332                         target = t->nick;
333                 }
334                 else if (target_type == TYPE_CHANNEL)
335                 {
336                         Channel* t = (Channel*)dest;
337                         if (exemptedchans.count(t->name))
338                                 return MOD_RES_PASSTHRU;
339
340                         target = t->name;
341                 }
342                 if (f->action == FA_BLOCK)
343                 {
344                         ServerInstance->SNO->WriteGlobalSno('a', "FILTER: "+user->nick+" had their message filtered, target was "+target+": "+f->reason);
345                         if (target_type == TYPE_CHANNEL)
346                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
347                         else
348                                 user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
349                 }
350                 else if (f->action == FA_SILENT)
351                 {
352                         if (target_type == TYPE_CHANNEL)
353                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
354                         else
355                                 user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
356                 }
357                 else if (f->action == FA_KILL)
358                 {
359                         ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
360                 }
361                 else if (f->action == FA_GLINE)
362                 {
363                         GLine* gl = new GLine(ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
364                         if (ServerInstance->XLines->AddLine(gl,NULL))
365                         {
366                                 ServerInstance->XLines->ApplyLines();
367                         }
368                         else
369                                 delete gl;
370                 }
371
372                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
373                 return MOD_RES_DENY;
374         }
375         return MOD_RES_PASSTHRU;
376 }
377
378 ModResult ModuleFilter::OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
379 {
380         if (validated)
381         {
382                 flags = 0;
383                 bool parting;
384
385                 if (command == "QUIT")
386                 {
387                         /* QUIT with no reason: nothing to do */
388                         if (parameters.size() < 1)
389                                 return MOD_RES_PASSTHRU;
390
391                         parting = false;
392                         flags = FLAG_QUIT;
393                 }
394                 else if (command == "PART")
395                 {
396                         /* PART with no reason: nothing to do */
397                         if (parameters.size() < 2)
398                                 return MOD_RES_PASSTHRU;
399
400                         if (exemptedchans.count(parameters[0]))
401                                 return MOD_RES_PASSTHRU;
402
403                         parting = true;
404                         flags = FLAG_PART;
405                 }
406                 else
407                         /* We're only messing with PART and QUIT */
408                         return MOD_RES_PASSTHRU;
409
410                 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
411                 if (!f)
412                         /* PART or QUIT reason doesnt match a filter */
413                         return MOD_RES_PASSTHRU;
414
415                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
416                 parameters[parting ? 1 : 0] = "Reason filtered";
417
418                 /* We're blocking, OR theyre quitting and its a KILL action
419                  * (we cant kill someone whos already quitting, so filter them anyway)
420                  */
421                 if ((f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
422                 {
423                         return MOD_RES_PASSTHRU;
424                 }
425                 else
426                 {
427                         /* Are they parting, if so, kill is applicable */
428                         if ((parting) && (f->action == FA_KILL))
429                         {
430                                 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
431                                 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
432                         }
433                         if (f->action == FA_GLINE)
434                         {
435                                 /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
436                                 GLine* gl = new GLine(ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
437                                 if (ServerInstance->XLines->AddLine(gl,NULL))
438                                 {
439                                         ServerInstance->XLines->ApplyLines();
440                                 }
441                                 else
442                                         delete gl;
443                         }
444                         return MOD_RES_DENY;
445                 }
446         }
447         return MOD_RES_PASSTHRU;
448 }
449
450 void ModuleFilter::ReadConfig(ConfigStatus& status)
451 {
452         ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
453         exemptedchans.clear();
454         exemptednicks.clear();
455
456         for (ConfigIter i = tags.first; i != tags.second; ++i)
457         {
458                 ConfigTag* tag = i->second;
459
460                 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
461                 const std::string target = tag->getString("target", tag->getString("channel"));
462                 if (!target.empty())
463                 {
464                         if (target[0] == '#')
465                                 exemptedchans.insert(target);
466                         else
467                                 exemptednicks.insert(target);
468                 }
469         }
470
471         std::string newrxengine = ServerInstance->Config->ConfValue("filteropts")->getString("engine");
472
473         factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
474
475         if (newrxengine.empty())
476                 RegexEngine.SetProvider("regex");
477         else
478                 RegexEngine.SetProvider("regex/" + newrxengine);
479
480         if (!RegexEngine)
481         {
482                 if (newrxengine.empty())
483                         ServerInstance->SNO->WriteGlobalSno('a', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
484                 else
485                         ServerInstance->SNO->WriteGlobalSno('a', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
486
487                 initing = false;
488                 FreeFilters();
489                 return;
490         }
491
492         if ((!initing) && (RegexEngine.operator->() != factory))
493         {
494                 ServerInstance->SNO->WriteGlobalSno('a', "Dumping all filters due to regex engine change");
495                 FreeFilters();
496         }
497
498         initing = false;
499         ReadFilters();
500 }
501
502 Version ModuleFilter::GetVersion()
503 {
504         return Version("Text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
505 }
506
507 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
508 {
509         std::ostringstream stream;
510         std::string x = filter->freeform;
511
512         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
513         for (std::string::iterator n = x.begin(); n != x.end(); n++)
514                 if (*n == ' ')
515                         *n = '\7';
516
517         stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->gline_time << " :" << filter->reason;
518         return stream.str();
519 }
520
521 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
522 {
523         std::string filteraction;
524         FilterResult res;
525         irc::tokenstream tokens(data);
526         tokens.GetToken(res.freeform);
527         tokens.GetToken(filteraction);
528         if (!StringToFilterAction(filteraction, res.action))
529                 throw ModuleException("Invalid action: " + filteraction);
530
531         std::string filterflags;
532         tokens.GetToken(filterflags);
533         char c = res.FillFlags(filterflags);
534         if (c != 0)
535                 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
536
537         tokens.GetToken(res.gline_time);
538         tokens.GetToken(res.reason);
539
540         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
541         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
542                 if (*n == '\7')
543                         *n = ' ';
544
545         return res;
546 }
547
548 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
549 {
550         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
551         {
552                 server.SendMetaData("filter", EncodeFilter(&(*i)));
553         }
554 }
555
556 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
557 {
558         if ((target == NULL) && (extname == "filter"))
559         {
560                 try
561                 {
562                         FilterResult data = DecodeFilter(extdata);
563                         this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.GetFlags());
564                 }
565                 catch (ModuleException& e)
566                 {
567                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
568                 }
569         }
570 }
571
572 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
573 {
574         static std::string stripped_text;
575         stripped_text.clear();
576
577         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
578         {
579                 FilterResult* filter = &*i;
580
581                 /* Skip ones that dont apply to us */
582                 if (!AppliesToMe(user, filter, flgs))
583                         continue;
584
585                 if ((filter->flag_strip_color) && (stripped_text.empty()))
586                 {
587                         stripped_text = text;
588                         InspIRCd::StripColor(stripped_text);
589                 }
590
591                 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
592                         return filter;
593         }
594         return NULL;
595 }
596
597 bool ModuleFilter::DeleteFilter(const std::string &freeform)
598 {
599         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
600         {
601                 if (i->freeform == freeform)
602                 {
603                         delete i->regex;
604                         filters.erase(i);
605                         return true;
606                 }
607         }
608         return false;
609 }
610
611 std::pair<bool, std::string> ModuleFilter::AddFilter(const std::string &freeform, FilterAction type, const std::string &reason, long duration, const std::string &flgs)
612 {
613         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
614         {
615                 if (i->freeform == freeform)
616                 {
617                         return std::make_pair(false, "Filter already exists");
618                 }
619         }
620
621         try
622         {
623                 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs));
624         }
625         catch (ModuleException &e)
626         {
627                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
628                 return std::make_pair(false, e.GetReason());
629         }
630         return std::make_pair(true, "");
631 }
632
633 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
634 {
635         if (stdalgo::string::equalsci(str, "gline"))
636                 fa = FA_GLINE;
637         else if (stdalgo::string::equalsci(str, "block"))
638                 fa = FA_BLOCK;
639         else if (stdalgo::string::equalsci(str, "silent"))
640                 fa = FA_SILENT;
641         else if (stdalgo::string::equalsci(str, "kill"))
642                 fa = FA_KILL;
643         else if (stdalgo::string::equalsci(str, "none"))
644                 fa = FA_NONE;
645         else
646                 return false;
647
648         return true;
649 }
650
651 std::string ModuleFilter::FilterActionToString(FilterAction fa)
652 {
653         switch (fa)
654         {
655                 case FA_GLINE:  return "gline";
656                 case FA_BLOCK:  return "block";
657                 case FA_SILENT: return "silent";
658                 case FA_KILL:   return "kill";
659                 default:                return "none";
660         }
661 }
662
663 void ModuleFilter::ReadFilters()
664 {
665         ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
666         for (ConfigIter i = tags.first; i != tags.second; ++i)
667         {
668                 std::string pattern = i->second->getString("pattern");
669                 this->DeleteFilter(pattern);
670
671                 std::string reason = i->second->getString("reason");
672                 std::string action = i->second->getString("action");
673                 std::string flgs = i->second->getString("flags");
674                 unsigned long gline_time = i->second->getDuration("duration", 10*60, 1);
675                 if (flgs.empty())
676                         flgs = "*";
677
678                 FilterAction fa;
679                 if (!StringToFilterAction(action, fa))
680                         fa = FA_NONE;
681
682                 try
683                 {
684                         filters.push_back(FilterResult(RegexEngine, pattern, reason, fa, gline_time, flgs));
685                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Regular expression %s loaded.", pattern.c_str());
686                 }
687                 catch (ModuleException &e)
688                 {
689                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason().c_str());
690                 }
691         }
692 }
693
694 ModResult ModuleFilter::OnStats(Stats::Context& stats)
695 {
696         if (stats.GetSymbol() == 's')
697         {
698                 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
699                 {
700                         stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->gline_time)+" :"+i->reason);
701                 }
702                 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
703                 {
704                         stats.AddRow(223, "EXEMPT "+(*i));
705                 }
706                 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
707                 {
708                         stats.AddRow(223, "EXEMPT "+(*i));
709                 }
710         }
711         return MOD_RES_PASSTHRU;
712 }
713
714 void ModuleFilter::OnUnloadModule(Module* mod)
715 {
716         // If the regex engine became unavailable or has changed, remove all filters
717         if (!RegexEngine)
718         {
719                 FreeFilters();
720         }
721         else if (RegexEngine.operator->() != factory)
722         {
723                 factory = RegexEngine.operator->();
724                 FreeFilters();
725         }
726 }
727
728 MODULE_INIT(ModuleFilter)