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