]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_filter.cpp
Replace large if/else blocks for target.type with switches (#1668).
[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 #include "modules/server.h"
27 #include "modules/shun.h"
28 #include "modules/stats.h"
29 #include "modules/account.h"
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_ZLINE,
43         FA_WARN,
44         FA_BLOCK,
45         FA_SILENT,
46         FA_KILL,
47         FA_SHUN,
48         FA_NONE
49 };
50
51 class FilterResult
52 {
53  public:
54         Regex* regex;
55         std::string freeform;
56         std::string reason;
57         FilterAction action;
58         unsigned long duration;
59         bool from_config;
60
61         bool flag_no_opers;
62         bool flag_part_message;
63         bool flag_quit_message;
64         bool flag_privmsg;
65         bool flag_notice;
66         bool flag_strip_color;
67         bool flag_no_registered;
68
69         FilterResult(dynamic_reference<RegexFactory>& RegexEngine, const std::string& free, const std::string& rea, FilterAction act, unsigned long gt, const std::string& fla, bool cfg)
70                 : freeform(free)
71                 , reason(rea)
72                 , action(act)
73                 , duration(gt)
74                 , from_config(cfg)
75         {
76                 if (!RegexEngine)
77                         throw ModuleException("Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!");
78                 regex = RegexEngine->Create(free);
79                 this->FillFlags(fla);
80         }
81
82         char FillFlags(const std::string &fl)
83         {
84                 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg =
85                         flag_notice = flag_strip_color = flag_no_registered = false;
86
87                 for (std::string::const_iterator n = fl.begin(); n != fl.end(); ++n)
88                 {
89                         switch (*n)
90                         {
91                                 case 'o':
92                                         flag_no_opers = true;
93                                 break;
94                                 case 'P':
95                                         flag_part_message = true;
96                                 break;
97                                 case 'q':
98                                         flag_quit_message = true;
99                                 break;
100                                 case 'p':
101                                         flag_privmsg = true;
102                                 break;
103                                 case 'n':
104                                         flag_notice = true;
105                                 break;
106                                 case 'c':
107                                         flag_strip_color = true;
108                                 break;
109                                 case 'r':
110                                         flag_no_registered = true;
111                                 break;
112                                 case '*':
113                                         flag_no_opers = flag_part_message = flag_quit_message =
114                                                 flag_privmsg = flag_notice = flag_strip_color = true;
115                                 break;
116                                 default:
117                                         return *n;
118                                 break;
119                         }
120                 }
121                 return 0;
122         }
123
124         std::string GetFlags()
125         {
126                 std::string flags;
127                 if (flag_no_opers)
128                         flags.push_back('o');
129                 if (flag_part_message)
130                         flags.push_back('P');
131                 if (flag_quit_message)
132                         flags.push_back('q');
133                 if (flag_privmsg)
134                         flags.push_back('p');
135                 if (flag_notice)
136                         flags.push_back('n');
137
138                 /* Order is important here, as the logic in FillFlags() stops parsing when it encounters
139                  * an unknown character. So the following characters must be last in the string.
140                  * 'c' is unsupported on < 2.0.10
141                  * 'r' is unsupported on < 3.2.0
142                  */
143                 if (flag_strip_color)
144                         flags.push_back('c');
145                 if (flag_no_registered)
146                         flags.push_back('r');
147
148                 if (flags.empty())
149                         flags.push_back('-');
150
151                 return flags;
152         }
153
154         FilterResult()
155         {
156         }
157 };
158
159 class CommandFilter : public Command
160 {
161  public:
162         CommandFilter(Module* f)
163                 : Command(f, "FILTER", 1, 5)
164         {
165                 flags_needed = 'o';
166                 this->syntax = "<pattern> [<action> <flags> [<duration>] :<reason>]";
167         }
168         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
169
170         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
171         {
172                 return ROUTE_BROADCAST;
173         }
174 };
175
176 class ModuleFilter : public Module, public ServerEventListener, public Stats::EventListener
177 {
178         typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
179
180         bool initing;
181         bool notifyuser;
182         bool warnonselfmsg;
183         RegexFactory* factory;
184         void FreeFilters();
185
186  public:
187         CommandFilter filtcommand;
188         dynamic_reference<RegexFactory> RegexEngine;
189
190         std::vector<FilterResult> filters;
191         int flags;
192
193         // List of channel names excluded from filtering.
194         ExemptTargetSet exemptedchans;
195
196         // List of target nicknames excluded from filtering.
197         ExemptTargetSet exemptednicks;
198
199         ModuleFilter();
200         void init() CXX11_OVERRIDE;
201         CullResult cull() CXX11_OVERRIDE;
202         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
203         FilterResult* FilterMatch(User* user, const std::string &text, int flags);
204         bool DeleteFilter(const std::string& freeform, std::string& reason);
205         std::pair<bool, std::string> AddFilter(const std::string& freeform, FilterAction type, const std::string& reason, unsigned long duration, const std::string& flags, bool config = false);
206         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
207         Version GetVersion() CXX11_OVERRIDE;
208         std::string EncodeFilter(FilterResult* filter);
209         FilterResult DecodeFilter(const std::string &data);
210         void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
211         void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
212         ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
213         ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
214         void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
215         bool AppliesToMe(User* user, FilterResult* filter, int flags);
216         void ReadFilters();
217         static bool StringToFilterAction(const std::string& str, FilterAction& fa);
218         static std::string FilterActionToString(FilterAction fa);
219 };
220
221 CmdResult CommandFilter::Handle(User* user, const Params& parameters)
222 {
223         if (parameters.size() == 1)
224         {
225                 /* Deleting a filter */
226                 Module* me = creator;
227                 std::string reason;
228
229                 if (static_cast<ModuleFilter*>(me)->DeleteFilter(parameters[0], reason))
230                 {
231                         user->WriteNotice("*** Removed filter '" + parameters[0] + "': " + reason);
232                         ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "%s removed filter '%s': %s",
233                                 user->nick.c_str(), parameters[0].c_str(), reason.c_str());
234                         return CMD_SUCCESS;
235                 }
236                 else
237                 {
238                         user->WriteNotice("*** Filter '" + parameters[0] + "' not found on the list.");
239                         return CMD_FAILURE;
240                 }
241         }
242         else
243         {
244                 /* Adding a filter */
245                 if (parameters.size() >= 4)
246                 {
247                         const std::string& freeform = parameters[0];
248                         FilterAction type;
249                         const std::string& flags = parameters[2];
250                         unsigned int reasonindex;
251                         unsigned long duration = 0;
252
253                         if (!ModuleFilter::StringToFilterAction(parameters[1], type))
254                         {
255                                 if (ServerInstance->XLines->GetFactory("SHUN"))
256                                         user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', 'kill', and 'shun'.");
257                                 else
258                                         user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', and 'kill'.");
259                                 return CMD_FAILURE;
260                         }
261
262                         if (type == FA_GLINE || type == FA_ZLINE || type == FA_SHUN)
263                         {
264                                 if (parameters.size() >= 5)
265                                 {
266                                         if (!InspIRCd::Duration(parameters[3], duration))
267                                         {
268                                                 user->WriteNotice("*** Invalid duration for filter");
269                                                 return CMD_FAILURE;
270                                         }
271                                         reasonindex = 4;
272                                 }
273                                 else
274                                 {
275                                         user->WriteNotice("*** Not enough parameters: When setting a '" + parameters[1] + "' type filter, a duration must be specified as the third parameter.");
276                                         return CMD_FAILURE;
277                                 }
278                         }
279                         else
280                         {
281                                 reasonindex = 3;
282                         }
283
284                         Module* me = creator;
285                         std::pair<bool, std::string> result = static_cast<ModuleFilter*>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
286                         if (result.first)
287                         {
288                                 const std::string message = InspIRCd::Format("'%s', type '%s'%s, flags '%s', reason: %s",
289                                         freeform.c_str(), parameters[1].c_str(),
290                                         (duration ? InspIRCd::Format(", duration '%s'",
291                                                 InspIRCd::DurationString(duration).c_str()).c_str()
292                                         : ""), flags.c_str(), parameters[reasonindex].c_str());
293
294                                 user->WriteNotice("*** Added filter " + message);
295                                 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F',
296                                         "%s added filter %s", user->nick.c_str(), message.c_str());
297
298                                 return CMD_SUCCESS;
299                         }
300                         else
301                         {
302                                 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
303                                 return CMD_FAILURE;
304                         }
305                 }
306                 else
307                 {
308                         user->WriteNotice("*** Not enough parameters.");
309                         return CMD_FAILURE;
310                 }
311
312         }
313 }
314
315 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
316 {
317         const AccountExtItem* accountext = GetAccountExtItem();
318
319         if ((filter->flag_no_opers) && user->IsOper())
320                 return false;
321         if ((filter->flag_no_registered) && accountext && accountext->get(user))
322                 return false;
323         if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
324                 return false;
325         if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
326                 return false;
327         if ((iflags & FLAG_QUIT)   && (!filter->flag_quit_message))
328                 return false;
329         if ((iflags & FLAG_PART)   && (!filter->flag_part_message))
330                 return false;
331         return true;
332 }
333
334 ModuleFilter::ModuleFilter()
335         : ServerEventListener(this)
336         , Stats::EventListener(this)
337         , initing(true)
338         , filtcommand(this)
339         , RegexEngine(this, "regex")
340 {
341 }
342
343 void ModuleFilter::init()
344 {
345         ServerInstance->SNO->EnableSnomask('f', "FILTER");
346 }
347
348 CullResult ModuleFilter::cull()
349 {
350         FreeFilters();
351         return Module::cull();
352 }
353
354 void ModuleFilter::FreeFilters()
355 {
356         for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
357                 delete i->regex;
358
359         filters.clear();
360 }
361
362 ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details)
363 {
364         // Leave remote users and servers alone
365         if (!IS_LOCAL(user))
366                 return MOD_RES_PASSTHRU;
367
368         flags = (details.type == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
369
370         FilterResult* f = this->FilterMatch(user, details.text, flags);
371         if (f)
372         {
373                 bool is_selfmsg = false;
374                 std::string target;
375                 switch (msgtarget.type)
376                 {
377                         case MessageTarget::TYPE_USER:
378                         {
379                                 User* t = msgtarget.Get<User>();
380                                 // Check if the target nick is exempted, if yes, ignore this message
381                                 if (exemptednicks.count(t->nick))
382                                         return MOD_RES_PASSTHRU;
383
384                                 if (user == t)
385                                         is_selfmsg = true;
386
387                                 target = t->nick;
388                                 break;
389                         }
390                         case MessageTarget::TYPE_CHANNEL:
391                         {
392                                 Channel* t = msgtarget.Get<Channel>();
393                                 if (exemptedchans.count(t->name))
394                                         return MOD_RES_PASSTHRU;
395
396                                 target = t->name;
397                                 break;
398                         }
399                         case MessageTarget::TYPE_SERVER:
400                                 break;
401                 }
402
403                 if (is_selfmsg && warnonselfmsg)
404                 {
405                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's self message matched %s (%s)",
406                                 user->nick.c_str(), f->freeform.c_str(), f->reason.c_str()));
407                         return MOD_RES_PASSTHRU;
408                 }
409                 else if (f->action == FA_WARN)
410                 {
411                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's message to %s matched %s (%s)",
412                                 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
413                         return MOD_RES_PASSTHRU;
414                 }
415                 else if (f->action == FA_BLOCK)
416                 {
417                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
418                                 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
419                         if (notifyuser)
420                         {
421                                 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
422                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
423                                 else
424                                         user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
425                         }
426                         else
427                                 details.echo_original = true;
428                 }
429                 else if (f->action == FA_SILENT)
430                 {
431                         if (notifyuser)
432                         {
433                                 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
434                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
435                                 else
436                                         user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
437                         }
438                         else
439                                 details.echo_original = true;
440                 }
441                 else if (f->action == FA_KILL)
442                 {
443                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
444                                 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
445                         ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
446                 }
447                 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
448                 {
449                         Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
450                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their message to %s matched %s (%s)",
451                                 user->nick.c_str(), sh->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
452                                 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
453                                 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
454                         if (ServerInstance->XLines->AddLine(sh, NULL))
455                         {
456                                 ServerInstance->XLines->ApplyLines();
457                         }
458                         else
459                                 delete sh;
460                 }
461                 else if (f->action == FA_GLINE)
462                 {
463                         GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
464                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their message to %s matched %s (%s)",
465                                 user->nick.c_str(), gl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
466                                 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
467                                 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
468                         if (ServerInstance->XLines->AddLine(gl,NULL))
469                         {
470                                 ServerInstance->XLines->ApplyLines();
471                         }
472                         else
473                                 delete gl;
474                 }
475                 else if (f->action == FA_ZLINE)
476                 {
477                         ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
478                         ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their message to %s matched %s (%s)",
479                                 user->nick.c_str(), zl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
480                                 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
481                                 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
482                         if (ServerInstance->XLines->AddLine(zl,NULL))
483                         {
484                                 ServerInstance->XLines->ApplyLines();
485                         }
486                         else
487                                 delete zl;
488                 }
489
490                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
491                 return MOD_RES_DENY;
492         }
493         return MOD_RES_PASSTHRU;
494 }
495
496 ModResult ModuleFilter::OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated)
497 {
498         if (validated)
499         {
500                 flags = 0;
501                 bool parting;
502
503                 if (command == "QUIT")
504                 {
505                         /* QUIT with no reason: nothing to do */
506                         if (parameters.size() < 1)
507                                 return MOD_RES_PASSTHRU;
508
509                         parting = false;
510                         flags = FLAG_QUIT;
511                 }
512                 else if (command == "PART")
513                 {
514                         /* PART with no reason: nothing to do */
515                         if (parameters.size() < 2)
516                                 return MOD_RES_PASSTHRU;
517
518                         if (exemptedchans.count(parameters[0]))
519                                 return MOD_RES_PASSTHRU;
520
521                         parting = true;
522                         flags = FLAG_PART;
523                 }
524                 else
525                         /* We're only messing with PART and QUIT */
526                         return MOD_RES_PASSTHRU;
527
528                 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
529                 if (!f)
530                         /* PART or QUIT reason doesnt match a filter */
531                         return MOD_RES_PASSTHRU;
532
533                 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
534                 parameters[parting ? 1 : 0] = "Reason filtered";
535
536                 /* We're warning or blocking, OR theyre quitting and its a KILL action
537                  * (we cant kill someone whos already quitting, so filter them anyway)
538                  */
539                 if ((f->action == FA_WARN) || (f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
540                 {
541                         return MOD_RES_PASSTHRU;
542                 }
543                 else
544                 {
545                         /* Are they parting, if so, kill is applicable */
546                         if ((parting) && (f->action == FA_KILL))
547                         {
548                                 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
549                                 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
550                         }
551                         if (f->action == FA_GLINE)
552                         {
553                                 /* Note: We G-line *@IP so that if their host doesn't resolve the G-line still applies. */
554                                 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
555                                 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their %s message matched %s (%s)",
556                                         user->nick.c_str(), gl->Displayable().c_str(),
557                                         InspIRCd::DurationString(f->duration).c_str(),
558                                         InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
559                                         command.c_str(), f->freeform.c_str(), f->reason.c_str()));
560
561                                 if (ServerInstance->XLines->AddLine(gl,NULL))
562                                 {
563                                         ServerInstance->XLines->ApplyLines();
564                                 }
565                                 else
566                                         delete gl;
567                         }
568                         if (f->action == FA_ZLINE)
569                         {
570                                 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
571                                 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their %s message matched %s (%s)",
572                                         user->nick.c_str(), zl->Displayable().c_str(),
573                                         InspIRCd::DurationString(f->duration).c_str(),
574                                         InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
575                                         command.c_str(), f->freeform.c_str(), f->reason.c_str()));
576
577                                 if (ServerInstance->XLines->AddLine(zl,NULL))
578                                 {
579                                         ServerInstance->XLines->ApplyLines();
580                                 }
581                                 else
582                                         delete zl;
583                         }
584                         else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
585                         {
586                                 /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
587                                 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
588                                 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their %s message matched %s (%s)",
589                                         user->nick.c_str(), sh->Displayable().c_str(),
590                                         InspIRCd::DurationString(f->duration).c_str(),
591                                         InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
592                                         command.c_str(), f->freeform.c_str(), f->reason.c_str()));
593
594                                 if (ServerInstance->XLines->AddLine(sh, NULL))
595                                 {
596                                         ServerInstance->XLines->ApplyLines();
597                                 }
598                                 else
599                                         delete sh;
600                         }
601                         return MOD_RES_DENY;
602                 }
603         }
604         return MOD_RES_PASSTHRU;
605 }
606
607 void ModuleFilter::ReadConfig(ConfigStatus& status)
608 {
609         ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
610         exemptedchans.clear();
611         exemptednicks.clear();
612
613         for (ConfigIter i = tags.first; i != tags.second; ++i)
614         {
615                 ConfigTag* tag = i->second;
616
617                 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
618                 const std::string target = tag->getString("target", tag->getString("channel"));
619                 if (!target.empty())
620                 {
621                         if (target[0] == '#')
622                                 exemptedchans.insert(target);
623                         else
624                                 exemptednicks.insert(target);
625                 }
626         }
627
628         ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
629         std::string newrxengine = tag->getString("engine");
630         notifyuser = tag->getBool("notifyuser", true);
631         warnonselfmsg = tag->getBool("warnonselfmsg");
632
633         factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
634
635         if (newrxengine.empty())
636                 RegexEngine.SetProvider("regex");
637         else
638                 RegexEngine.SetProvider("regex/" + newrxengine);
639
640         if (!RegexEngine)
641         {
642                 if (newrxengine.empty())
643                         ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
644                 else
645                         ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
646
647                 initing = false;
648                 FreeFilters();
649                 return;
650         }
651
652         if ((!initing) && (RegexEngine.operator->() != factory))
653         {
654                 ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
655                 FreeFilters();
656         }
657
658         initing = false;
659         ReadFilters();
660 }
661
662 Version ModuleFilter::GetVersion()
663 {
664         return Version("Provides text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
665 }
666
667 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
668 {
669         std::ostringstream stream;
670         std::string x = filter->freeform;
671
672         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
673         for (std::string::iterator n = x.begin(); n != x.end(); n++)
674                 if (*n == ' ')
675                         *n = '\7';
676
677         stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
678         return stream.str();
679 }
680
681 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
682 {
683         std::string filteraction;
684         FilterResult res;
685         irc::tokenstream tokens(data);
686         tokens.GetMiddle(res.freeform);
687         tokens.GetMiddle(filteraction);
688         if (!StringToFilterAction(filteraction, res.action))
689                 throw ModuleException("Invalid action: " + filteraction);
690
691         std::string filterflags;
692         tokens.GetMiddle(filterflags);
693         char c = res.FillFlags(filterflags);
694         if (c != 0)
695                 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
696
697         std::string duration;
698         tokens.GetMiddle(duration);
699         res.duration = ConvToNum<unsigned long>(duration);
700
701         tokens.GetTrailing(res.reason);
702
703         /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
704         for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
705                 if (*n == '\7')
706                         *n = ' ';
707
708         return res;
709 }
710
711 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
712 {
713         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
714         {
715                 FilterResult& filter = *i;
716                 if (filter.from_config)
717                         continue;
718
719                 server.SendMetaData("filter", EncodeFilter(&filter));
720         }
721 }
722
723 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
724 {
725         if ((target == NULL) && (extname == "filter"))
726         {
727                 try
728                 {
729                         FilterResult data = DecodeFilter(extdata);
730                         this->AddFilter(data.freeform, data.action, data.reason, data.duration, data.GetFlags());
731                 }
732                 catch (ModuleException& e)
733                 {
734                         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
735                 }
736         }
737 }
738
739 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
740 {
741         static std::string stripped_text;
742         stripped_text.clear();
743
744         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
745         {
746                 FilterResult* filter = &*i;
747
748                 /* Skip ones that dont apply to us */
749                 if (!AppliesToMe(user, filter, flgs))
750                         continue;
751
752                 if ((filter->flag_strip_color) && (stripped_text.empty()))
753                 {
754                         stripped_text = text;
755                         InspIRCd::StripColor(stripped_text);
756                 }
757
758                 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
759                         return filter;
760         }
761         return NULL;
762 }
763
764 bool ModuleFilter::DeleteFilter(const std::string& freeform, std::string& reason)
765 {
766         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
767         {
768                 if (i->freeform == freeform)
769                 {
770                         reason.assign(i->reason);
771                         delete i->regex;
772                         filters.erase(i);
773                         return true;
774                 }
775         }
776         return false;
777 }
778
779 std::pair<bool, std::string> ModuleFilter::AddFilter(const std::string& freeform, FilterAction type, const std::string& reason, unsigned long duration, const std::string& flgs, bool config)
780 {
781         for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
782         {
783                 if (i->freeform == freeform)
784                 {
785                         return std::make_pair(false, "Filter already exists");
786                 }
787         }
788
789         try
790         {
791                 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs, config));
792         }
793         catch (ModuleException &e)
794         {
795                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
796                 return std::make_pair(false, e.GetReason());
797         }
798         return std::make_pair(true, "");
799 }
800
801 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
802 {
803         if (stdalgo::string::equalsci(str, "gline"))
804                 fa = FA_GLINE;
805         else if (stdalgo::string::equalsci(str, "zline"))
806                 fa = FA_ZLINE;
807         else if (stdalgo::string::equalsci(str, "warn"))
808                 fa = FA_WARN;
809         else if (stdalgo::string::equalsci(str, "block"))
810                 fa = FA_BLOCK;
811         else if (stdalgo::string::equalsci(str, "silent"))
812                 fa = FA_SILENT;
813         else if (stdalgo::string::equalsci(str, "kill"))
814                 fa = FA_KILL;
815         else if (stdalgo::string::equalsci(str, "shun") && (ServerInstance->XLines->GetFactory("SHUN")))
816                 fa = FA_SHUN;
817         else if (stdalgo::string::equalsci(str, "none"))
818                 fa = FA_NONE;
819         else
820                 return false;
821
822         return true;
823 }
824
825 std::string ModuleFilter::FilterActionToString(FilterAction fa)
826 {
827         switch (fa)
828         {
829                 case FA_GLINE:  return "gline";
830                 case FA_ZLINE:  return "zline";
831                 case FA_WARN:   return "warn";
832                 case FA_BLOCK:  return "block";
833                 case FA_SILENT: return "silent";
834                 case FA_KILL:   return "kill";
835                 case FA_SHUN:   return "shun";
836                 default:                return "none";
837         }
838 }
839
840 void ModuleFilter::ReadFilters()
841 {
842         insp::flat_set<std::string> removedfilters;
843
844         for (std::vector<FilterResult>::iterator filter = filters.begin(); filter != filters.end(); )
845         {
846                 if (filter->from_config)
847                 {
848                         removedfilters.insert(filter->freeform);
849                         delete filter->regex;
850                         filter = filters.erase(filter);
851                         continue;
852                 }
853
854                 // The filter is not from the config.
855                 filter++;
856         }
857
858         ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
859         for (ConfigIter i = tags.first; i != tags.second; ++i)
860         {
861                 std::string pattern = i->second->getString("pattern");
862                 std::string reason = i->second->getString("reason");
863                 std::string action = i->second->getString("action");
864                 std::string flgs = i->second->getString("flags");
865                 unsigned long duration = i->second->getDuration("duration", 10*60, 1);
866                 if (flgs.empty())
867                         flgs = "*";
868
869                 FilterAction fa;
870                 if (!StringToFilterAction(action, fa))
871                         fa = FA_NONE;
872
873                 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(this)->AddFilter(pattern, fa, reason, duration, flgs, true);
874                 if (result.first)
875                         removedfilters.erase(pattern);
876                 else
877                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Filter '%s' could not be added: %s", pattern.c_str(), result.second.c_str());
878         }
879
880         if (!removedfilters.empty())
881         {
882                 for (insp::flat_set<std::string>::const_iterator it = removedfilters.begin(); it != removedfilters.end(); ++it)
883                         ServerInstance->SNO->WriteGlobalSno('f', "Removing filter '" + *(it) + "' due to config rehash.");
884         }
885 }
886
887 ModResult ModuleFilter::OnStats(Stats::Context& stats)
888 {
889         if (stats.GetSymbol() == 's')
890         {
891                 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
892                 {
893                         stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->duration)+" :"+i->reason);
894                 }
895                 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
896                 {
897                         stats.AddRow(223, "EXEMPT "+(*i));
898                 }
899                 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
900                 {
901                         stats.AddRow(223, "EXEMPT "+(*i));
902                 }
903         }
904         return MOD_RES_PASSTHRU;
905 }
906
907 void ModuleFilter::OnUnloadModule(Module* mod)
908 {
909         // If the regex engine became unavailable or has changed, remove all filters
910         if (!RegexEngine)
911         {
912                 FreeFilters();
913         }
914         else if (RegexEngine.operator->() != factory)
915         {
916                 factory = RegexEngine.operator->();
917                 FreeFilters();
918         }
919 }
920
921 MODULE_INIT(ModuleFilter)