2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
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"
58 unsigned long duration;
62 bool flag_no_registered;
63 bool flag_part_message;
64 bool flag_quit_message;
67 bool flag_strip_color;
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)
77 throw ModuleException("Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!");
78 regex = RegexEngine->Create(free);
82 char FillFlags(const std::string &fl)
84 flag_no_opers = flag_no_registered = flag_part_message = flag_quit_message = flag_privmsg =
85 flag_notice = flag_strip_color = false;
87 for (std::string::const_iterator n = fl.begin(); n != fl.end(); ++n)
95 flag_part_message = true;
98 flag_quit_message = true;
107 flag_strip_color = true;
110 flag_no_registered = true;
113 flag_no_opers = flag_part_message = flag_quit_message =
114 flag_privmsg = flag_notice = flag_strip_color = true;
124 std::string GetFlags()
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');
134 flags.push_back('p');
136 flags.push_back('n');
137 /* Order is important here, 'c' must be the last char in the string as it is unsupported
138 * on < 2.0.10, and the logic in FillFlags() stops parsing when it ecounters an unknown
141 if (flag_strip_color)
142 flags.push_back('c');
143 if (flag_no_registered)
144 flags.push_back('r');
147 flags.push_back('-');
157 class CommandFilter : public Command
160 CommandFilter(Module* f)
161 : Command(f, "FILTER", 1, 5)
164 this->syntax = "<pattern> [<action> <flags> [<duration>] :<reason>]";
166 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
168 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
170 return ROUTE_BROADCAST;
174 class ModuleFilter : public Module, public ServerEventListener, public Stats::EventListener
176 typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
180 RegexFactory* factory;
184 CommandFilter filtcommand;
185 dynamic_reference<RegexFactory> RegexEngine;
187 std::vector<FilterResult> filters;
190 // List of channel names excluded from filtering.
191 ExemptTargetSet exemptedchans;
193 // List of target nicknames excluded from filtering.
194 ExemptTargetSet exemptednicks;
197 void init() CXX11_OVERRIDE;
198 CullResult cull() CXX11_OVERRIDE;
199 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
200 FilterResult* FilterMatch(User* user, const std::string &text, int flags);
201 bool DeleteFilter(const std::string& freeform, std::string& reason);
202 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);
203 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
204 Version GetVersion() CXX11_OVERRIDE;
205 std::string EncodeFilter(FilterResult* filter);
206 FilterResult DecodeFilter(const std::string &data);
207 void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
208 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
209 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
210 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
211 void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
212 bool AppliesToMe(User* user, FilterResult* filter, int flags);
214 static bool StringToFilterAction(const std::string& str, FilterAction& fa);
215 static std::string FilterActionToString(FilterAction fa);
218 CmdResult CommandFilter::Handle(User* user, const Params& parameters)
220 if (parameters.size() == 1)
222 /* Deleting a filter */
223 Module* me = creator;
226 if (static_cast<ModuleFilter*>(me)->DeleteFilter(parameters[0], reason))
228 user->WriteNotice("*** Removed filter '" + parameters[0] + "': " + reason);
229 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "%s removed filter '%s': %s",
230 user->nick.c_str(), parameters[0].c_str(), reason.c_str());
235 user->WriteNotice("*** Filter '" + parameters[0] + "' not found on the list.");
241 /* Adding a filter */
242 if (parameters.size() >= 4)
244 const std::string& freeform = parameters[0];
246 const std::string& flags = parameters[2];
247 unsigned int reasonindex;
248 unsigned long duration = 0;
250 if (!ModuleFilter::StringToFilterAction(parameters[1], type))
252 if (ServerInstance->XLines->GetFactory("SHUN"))
253 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', 'kill', and 'shun'.");
255 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', and 'kill'.");
259 if (type == FA_GLINE || type == FA_ZLINE || type == FA_SHUN)
261 if (parameters.size() >= 5)
263 if (!InspIRCd::Duration(parameters[3], duration))
265 user->WriteNotice("*** Invalid duration for filter");
272 user->WriteNotice("*** Not enough parameters: When setting a '" + parameters[1] + "' type filter, a duration must be specified as the third parameter.");
281 Module* me = creator;
282 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
285 const std::string message = InspIRCd::Format("'%s', type '%s'%s, flags '%s', reason: %s",
286 freeform.c_str(), parameters[1].c_str(),
287 (duration ? InspIRCd::Format(", duration '%s'",
288 InspIRCd::DurationString(duration).c_str()).c_str()
289 : ""), flags.c_str(), parameters[reasonindex].c_str());
291 user->WriteNotice("*** Added filter " + message);
292 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F',
293 "%s added filter %s", user->nick.c_str(), message.c_str());
299 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
305 user->WriteNotice("*** Not enough parameters.");
312 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
314 const AccountExtItem* accountext = GetAccountExtItem();
316 if ((filter->flag_no_opers) && user->IsOper())
318 if ((filter->flag_no_registered) && accountext && accountext->get(user))
320 if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
322 if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
324 if ((iflags & FLAG_QUIT) && (!filter->flag_quit_message))
326 if ((iflags & FLAG_PART) && (!filter->flag_part_message))
331 ModuleFilter::ModuleFilter()
332 : ServerEventListener(this)
333 , Stats::EventListener(this)
336 , RegexEngine(this, "regex")
340 void ModuleFilter::init()
342 ServerInstance->SNO->EnableSnomask('f', "FILTER");
345 CullResult ModuleFilter::cull()
348 return Module::cull();
351 void ModuleFilter::FreeFilters()
353 for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
359 ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details)
361 // Leave remote users and servers alone
363 return MOD_RES_PASSTHRU;
365 flags = (details.type == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
367 FilterResult* f = this->FilterMatch(user, details.text, flags);
371 if (msgtarget.type == MessageTarget::TYPE_USER)
373 User* t = msgtarget.Get<User>();
374 // Check if the target nick is exempted, if yes, ignore this message
375 if (exemptednicks.count(t->nick))
376 return MOD_RES_PASSTHRU;
380 else if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
382 Channel* t = msgtarget.Get<Channel>();
383 if (exemptedchans.count(t->name))
384 return MOD_RES_PASSTHRU;
388 if (f->action == FA_WARN)
390 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's message to %s matched %s (%s)",
391 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
392 return MOD_RES_PASSTHRU;
394 if (f->action == FA_BLOCK)
396 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
397 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
400 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
401 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
403 user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
406 details.echo_original = true;
408 else if (f->action == FA_SILENT)
412 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
413 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
415 user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
418 details.echo_original = true;
420 else if (f->action == FA_KILL)
422 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
423 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
424 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
426 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
428 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
429 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their message to %s matched %s (%s)",
430 user->nick.c_str(), sh->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
431 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
432 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
433 if (ServerInstance->XLines->AddLine(sh, NULL))
435 ServerInstance->XLines->ApplyLines();
440 else if (f->action == FA_GLINE)
442 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
443 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their message to %s matched %s (%s)",
444 user->nick.c_str(), gl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
445 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
446 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
447 if (ServerInstance->XLines->AddLine(gl,NULL))
449 ServerInstance->XLines->ApplyLines();
454 else if (f->action == FA_ZLINE)
456 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
457 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their message to %s matched %s (%s)",
458 user->nick.c_str(), zl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
459 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
460 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
461 if (ServerInstance->XLines->AddLine(zl,NULL))
463 ServerInstance->XLines->ApplyLines();
469 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
472 return MOD_RES_PASSTHRU;
475 ModResult ModuleFilter::OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated)
482 if (command == "QUIT")
484 /* QUIT with no reason: nothing to do */
485 if (parameters.size() < 1)
486 return MOD_RES_PASSTHRU;
491 else if (command == "PART")
493 /* PART with no reason: nothing to do */
494 if (parameters.size() < 2)
495 return MOD_RES_PASSTHRU;
497 if (exemptedchans.count(parameters[0]))
498 return MOD_RES_PASSTHRU;
504 /* We're only messing with PART and QUIT */
505 return MOD_RES_PASSTHRU;
507 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
509 /* PART or QUIT reason doesnt match a filter */
510 return MOD_RES_PASSTHRU;
512 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
513 parameters[parting ? 1 : 0] = "Reason filtered";
515 /* We're warning or blocking, OR theyre quitting and its a KILL action
516 * (we cant kill someone whos already quitting, so filter them anyway)
518 if ((f->action == FA_WARN) || (f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
520 return MOD_RES_PASSTHRU;
524 /* Are they parting, if so, kill is applicable */
525 if ((parting) && (f->action == FA_KILL))
527 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
528 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
530 if (f->action == FA_GLINE)
532 /* Note: We G-line *@IP so that if their host doesn't resolve the G-line still applies. */
533 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
534 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their %s message matched %s (%s)",
535 user->nick.c_str(), gl->Displayable().c_str(),
536 InspIRCd::DurationString(f->duration).c_str(),
537 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
538 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
540 if (ServerInstance->XLines->AddLine(gl,NULL))
542 ServerInstance->XLines->ApplyLines();
547 if (f->action == FA_ZLINE)
549 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
550 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their %s message matched %s (%s)",
551 user->nick.c_str(), zl->Displayable().c_str(),
552 InspIRCd::DurationString(f->duration).c_str(),
553 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
554 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
556 if (ServerInstance->XLines->AddLine(zl,NULL))
558 ServerInstance->XLines->ApplyLines();
563 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
565 /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
566 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
567 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their %s message matched %s (%s)",
568 user->nick.c_str(), sh->Displayable().c_str(),
569 InspIRCd::DurationString(f->duration).c_str(),
570 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
571 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
573 if (ServerInstance->XLines->AddLine(sh, NULL))
575 ServerInstance->XLines->ApplyLines();
583 return MOD_RES_PASSTHRU;
586 void ModuleFilter::ReadConfig(ConfigStatus& status)
588 ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
589 exemptedchans.clear();
590 exemptednicks.clear();
592 for (ConfigIter i = tags.first; i != tags.second; ++i)
594 ConfigTag* tag = i->second;
596 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
597 const std::string target = tag->getString("target", tag->getString("channel"));
600 if (target[0] == '#')
601 exemptedchans.insert(target);
603 exemptednicks.insert(target);
607 ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
608 std::string newrxengine = tag->getString("engine");
609 notifyuser = tag->getBool("notifyuser", true);
611 factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
613 if (newrxengine.empty())
614 RegexEngine.SetProvider("regex");
616 RegexEngine.SetProvider("regex/" + newrxengine);
620 if (newrxengine.empty())
621 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
623 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
630 if ((!initing) && (RegexEngine.operator->() != factory))
632 ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
640 Version ModuleFilter::GetVersion()
642 return Version("Provides text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
645 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
647 std::ostringstream stream;
648 std::string x = filter->freeform;
650 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
651 for (std::string::iterator n = x.begin(); n != x.end(); n++)
655 stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
659 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
661 std::string filteraction;
663 irc::tokenstream tokens(data);
664 tokens.GetMiddle(res.freeform);
665 tokens.GetMiddle(filteraction);
666 if (!StringToFilterAction(filteraction, res.action))
667 throw ModuleException("Invalid action: " + filteraction);
669 std::string filterflags;
670 tokens.GetMiddle(filterflags);
671 char c = res.FillFlags(filterflags);
673 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
675 std::string duration;
676 tokens.GetMiddle(duration);
677 res.duration = ConvToNum<unsigned long>(duration);
679 tokens.GetTrailing(res.reason);
681 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
682 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
689 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
691 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
693 FilterResult& filter = *i;
694 if (filter.from_config)
697 server.SendMetaData("filter", EncodeFilter(&filter));
701 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
703 if ((target == NULL) && (extname == "filter"))
707 FilterResult data = DecodeFilter(extdata);
708 this->AddFilter(data.freeform, data.action, data.reason, data.duration, data.GetFlags());
710 catch (ModuleException& e)
712 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
717 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
719 static std::string stripped_text;
720 stripped_text.clear();
722 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
724 FilterResult* filter = &*i;
726 /* Skip ones that dont apply to us */
727 if (!AppliesToMe(user, filter, flgs))
730 if ((filter->flag_strip_color) && (stripped_text.empty()))
732 stripped_text = text;
733 InspIRCd::StripColor(stripped_text);
736 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
742 bool ModuleFilter::DeleteFilter(const std::string& freeform, std::string& reason)
744 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
746 if (i->freeform == freeform)
748 reason.assign(i->reason);
757 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)
759 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
761 if (i->freeform == freeform)
763 return std::make_pair(false, "Filter already exists");
769 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs, config));
771 catch (ModuleException &e)
773 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
774 return std::make_pair(false, e.GetReason());
776 return std::make_pair(true, "");
779 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
781 if (stdalgo::string::equalsci(str, "gline"))
783 else if (stdalgo::string::equalsci(str, "zline"))
785 else if (stdalgo::string::equalsci(str, "warn"))
787 else if (stdalgo::string::equalsci(str, "block"))
789 else if (stdalgo::string::equalsci(str, "silent"))
791 else if (stdalgo::string::equalsci(str, "kill"))
793 else if (stdalgo::string::equalsci(str, "shun") && (ServerInstance->XLines->GetFactory("SHUN")))
795 else if (stdalgo::string::equalsci(str, "none"))
803 std::string ModuleFilter::FilterActionToString(FilterAction fa)
807 case FA_GLINE: return "gline";
808 case FA_ZLINE: return "zline";
809 case FA_WARN: return "warn";
810 case FA_BLOCK: return "block";
811 case FA_SILENT: return "silent";
812 case FA_KILL: return "kill";
813 case FA_SHUN: return "shun";
814 default: return "none";
818 void ModuleFilter::ReadFilters()
820 insp::flat_set<std::string> removedfilters;
822 for (std::vector<FilterResult>::iterator filter = filters.begin(); filter != filters.end(); )
824 if (filter->from_config)
826 removedfilters.insert(filter->freeform);
827 delete filter->regex;
828 filter = filters.erase(filter);
832 // The filter is not from the config.
836 ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
837 for (ConfigIter i = tags.first; i != tags.second; ++i)
839 std::string pattern = i->second->getString("pattern");
840 std::string reason = i->second->getString("reason");
841 std::string action = i->second->getString("action");
842 std::string flgs = i->second->getString("flags");
843 unsigned long duration = i->second->getDuration("duration", 10*60, 1);
848 if (!StringToFilterAction(action, fa))
851 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(this)->AddFilter(pattern, fa, reason, duration, flgs, true);
853 removedfilters.erase(pattern);
855 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Filter '%s' could not be added: %s", pattern.c_str(), result.second.c_str());
858 if (!removedfilters.empty())
860 for (insp::flat_set<std::string>::const_iterator it = removedfilters.begin(); it != removedfilters.end(); ++it)
861 ServerInstance->SNO->WriteGlobalSno('f', "Removing filter '" + *(it) + "' due to config rehash.");
865 ModResult ModuleFilter::OnStats(Stats::Context& stats)
867 if (stats.GetSymbol() == 's')
869 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
871 stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->duration)+" :"+i->reason);
873 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
875 stats.AddRow(223, "EXEMPT "+(*i));
877 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
879 stats.AddRow(223, "EXEMPT "+(*i));
882 return MOD_RES_PASSTHRU;
885 void ModuleFilter::OnUnloadModule(Module* mod)
887 // If the regex engine became unavailable or has changed, remove all filters
892 else if (RegexEngine.operator->() != factory)
894 factory = RegexEngine.operator->();
899 MODULE_INIT(ModuleFilter)