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