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