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