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