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