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_part_message;
63 bool flag_quit_message;
66 bool flag_strip_color;
67 bool flag_no_registered;
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_part_message = flag_quit_message = flag_privmsg =
85 flag_notice = flag_strip_color = flag_no_registered = 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');
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
143 if (flag_strip_color)
144 flags.push_back('c');
145 if (flag_no_registered)
146 flags.push_back('r');
149 flags.push_back('-');
159 class CommandFilter : public Command
162 CommandFilter(Module* f)
163 : Command(f, "FILTER", 1, 5)
166 this->syntax = "<pattern> [<action> <flags> [<duration>] :<reason>]";
168 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
170 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
172 return ROUTE_BROADCAST;
176 class ModuleFilter : public Module, public ServerEventListener, public Stats::EventListener
178 typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
182 RegexFactory* factory;
186 CommandFilter filtcommand;
187 dynamic_reference<RegexFactory> RegexEngine;
189 std::vector<FilterResult> filters;
192 // List of channel names excluded from filtering.
193 ExemptTargetSet exemptedchans;
195 // List of target nicknames excluded from filtering.
196 ExemptTargetSet exemptednicks;
199 void init() CXX11_OVERRIDE;
200 CullResult cull() CXX11_OVERRIDE;
201 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
202 FilterResult* FilterMatch(User* user, const std::string &text, int flags);
203 bool DeleteFilter(const std::string& freeform, std::string& reason);
204 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);
205 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
206 Version GetVersion() CXX11_OVERRIDE;
207 std::string EncodeFilter(FilterResult* filter);
208 FilterResult DecodeFilter(const std::string &data);
209 void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
210 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
211 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
212 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
213 void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
214 bool AppliesToMe(User* user, FilterResult* filter, int flags);
216 static bool StringToFilterAction(const std::string& str, FilterAction& fa);
217 static std::string FilterActionToString(FilterAction fa);
220 CmdResult CommandFilter::Handle(User* user, const Params& parameters)
222 if (parameters.size() == 1)
224 /* Deleting a filter */
225 Module* me = creator;
228 if (static_cast<ModuleFilter*>(me)->DeleteFilter(parameters[0], reason))
230 user->WriteNotice("*** Removed filter '" + parameters[0] + "': " + reason);
231 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "%s removed filter '%s': %s",
232 user->nick.c_str(), parameters[0].c_str(), reason.c_str());
237 user->WriteNotice("*** Filter '" + parameters[0] + "' not found on the list.");
243 /* Adding a filter */
244 if (parameters.size() >= 4)
246 const std::string& freeform = parameters[0];
248 const std::string& flags = parameters[2];
249 unsigned int reasonindex;
250 unsigned long duration = 0;
252 if (!ModuleFilter::StringToFilterAction(parameters[1], type))
254 if (ServerInstance->XLines->GetFactory("SHUN"))
255 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', 'kill', and 'shun'.");
257 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', and 'kill'.");
261 if (type == FA_GLINE || type == FA_ZLINE || type == FA_SHUN)
263 if (parameters.size() >= 5)
265 if (!InspIRCd::Duration(parameters[3], duration))
267 user->WriteNotice("*** Invalid duration for filter");
274 user->WriteNotice("*** Not enough parameters: When setting a '" + parameters[1] + "' type filter, a duration must be specified as the third parameter.");
283 Module* me = creator;
284 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
287 const std::string message = InspIRCd::Format("'%s', type '%s'%s, flags '%s', reason: %s",
288 freeform.c_str(), parameters[1].c_str(),
289 (duration ? InspIRCd::Format(", duration '%s'",
290 InspIRCd::DurationString(duration).c_str()).c_str()
291 : ""), flags.c_str(), parameters[reasonindex].c_str());
293 user->WriteNotice("*** Added filter " + message);
294 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F',
295 "%s added filter %s", user->nick.c_str(), message.c_str());
301 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
307 user->WriteNotice("*** Not enough parameters.");
314 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
316 const AccountExtItem* accountext = GetAccountExtItem();
318 if ((filter->flag_no_opers) && user->IsOper())
320 if ((filter->flag_no_registered) && accountext && accountext->get(user))
322 if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
324 if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
326 if ((iflags & FLAG_QUIT) && (!filter->flag_quit_message))
328 if ((iflags & FLAG_PART) && (!filter->flag_part_message))
333 ModuleFilter::ModuleFilter()
334 : ServerEventListener(this)
335 , Stats::EventListener(this)
338 , RegexEngine(this, "regex")
342 void ModuleFilter::init()
344 ServerInstance->SNO->EnableSnomask('f', "FILTER");
347 CullResult ModuleFilter::cull()
350 return Module::cull();
353 void ModuleFilter::FreeFilters()
355 for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
361 ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details)
363 // Leave remote users and servers alone
365 return MOD_RES_PASSTHRU;
367 flags = (details.type == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
369 FilterResult* f = this->FilterMatch(user, details.text, flags);
373 if (msgtarget.type == MessageTarget::TYPE_USER)
375 User* t = msgtarget.Get<User>();
376 // Check if the target nick is exempted, if yes, ignore this message
377 if (exemptednicks.count(t->nick))
378 return MOD_RES_PASSTHRU;
382 else if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
384 Channel* t = msgtarget.Get<Channel>();
385 if (exemptedchans.count(t->name))
386 return MOD_RES_PASSTHRU;
390 if (f->action == FA_WARN)
392 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's message to %s matched %s (%s)",
393 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
394 return MOD_RES_PASSTHRU;
396 if (f->action == FA_BLOCK)
398 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
399 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
402 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
403 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
405 user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
408 details.echo_original = true;
410 else if (f->action == FA_SILENT)
414 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
415 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
417 user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
420 details.echo_original = true;
422 else if (f->action == FA_KILL)
424 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
425 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
426 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
428 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
430 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
431 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their message to %s matched %s (%s)",
432 user->nick.c_str(), sh->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
433 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
434 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
435 if (ServerInstance->XLines->AddLine(sh, NULL))
437 ServerInstance->XLines->ApplyLines();
442 else if (f->action == FA_GLINE)
444 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
445 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their message to %s matched %s (%s)",
446 user->nick.c_str(), gl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
447 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
448 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
449 if (ServerInstance->XLines->AddLine(gl,NULL))
451 ServerInstance->XLines->ApplyLines();
456 else if (f->action == FA_ZLINE)
458 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
459 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their message to %s matched %s (%s)",
460 user->nick.c_str(), zl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
461 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
462 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
463 if (ServerInstance->XLines->AddLine(zl,NULL))
465 ServerInstance->XLines->ApplyLines();
471 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
474 return MOD_RES_PASSTHRU;
477 ModResult ModuleFilter::OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated)
484 if (command == "QUIT")
486 /* QUIT with no reason: nothing to do */
487 if (parameters.size() < 1)
488 return MOD_RES_PASSTHRU;
493 else if (command == "PART")
495 /* PART with no reason: nothing to do */
496 if (parameters.size() < 2)
497 return MOD_RES_PASSTHRU;
499 if (exemptedchans.count(parameters[0]))
500 return MOD_RES_PASSTHRU;
506 /* We're only messing with PART and QUIT */
507 return MOD_RES_PASSTHRU;
509 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
511 /* PART or QUIT reason doesnt match a filter */
512 return MOD_RES_PASSTHRU;
514 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
515 parameters[parting ? 1 : 0] = "Reason filtered";
517 /* We're warning or blocking, OR theyre quitting and its a KILL action
518 * (we cant kill someone whos already quitting, so filter them anyway)
520 if ((f->action == FA_WARN) || (f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
522 return MOD_RES_PASSTHRU;
526 /* Are they parting, if so, kill is applicable */
527 if ((parting) && (f->action == FA_KILL))
529 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
530 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
532 if (f->action == FA_GLINE)
534 /* Note: We G-line *@IP so that if their host doesn't resolve the G-line still applies. */
535 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
536 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their %s message matched %s (%s)",
537 user->nick.c_str(), gl->Displayable().c_str(),
538 InspIRCd::DurationString(f->duration).c_str(),
539 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
540 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
542 if (ServerInstance->XLines->AddLine(gl,NULL))
544 ServerInstance->XLines->ApplyLines();
549 if (f->action == FA_ZLINE)
551 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
552 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their %s message matched %s (%s)",
553 user->nick.c_str(), zl->Displayable().c_str(),
554 InspIRCd::DurationString(f->duration).c_str(),
555 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
556 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
558 if (ServerInstance->XLines->AddLine(zl,NULL))
560 ServerInstance->XLines->ApplyLines();
565 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
567 /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
568 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
569 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their %s message matched %s (%s)",
570 user->nick.c_str(), sh->Displayable().c_str(),
571 InspIRCd::DurationString(f->duration).c_str(),
572 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
573 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
575 if (ServerInstance->XLines->AddLine(sh, NULL))
577 ServerInstance->XLines->ApplyLines();
585 return MOD_RES_PASSTHRU;
588 void ModuleFilter::ReadConfig(ConfigStatus& status)
590 ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
591 exemptedchans.clear();
592 exemptednicks.clear();
594 for (ConfigIter i = tags.first; i != tags.second; ++i)
596 ConfigTag* tag = i->second;
598 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
599 const std::string target = tag->getString("target", tag->getString("channel"));
602 if (target[0] == '#')
603 exemptedchans.insert(target);
605 exemptednicks.insert(target);
609 ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
610 std::string newrxengine = tag->getString("engine");
611 notifyuser = tag->getBool("notifyuser", true);
613 factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
615 if (newrxengine.empty())
616 RegexEngine.SetProvider("regex");
618 RegexEngine.SetProvider("regex/" + newrxengine);
622 if (newrxengine.empty())
623 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
625 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
632 if ((!initing) && (RegexEngine.operator->() != factory))
634 ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
642 Version ModuleFilter::GetVersion()
644 return Version("Provides text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
647 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
649 std::ostringstream stream;
650 std::string x = filter->freeform;
652 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
653 for (std::string::iterator n = x.begin(); n != x.end(); n++)
657 stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
661 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
663 std::string filteraction;
665 irc::tokenstream tokens(data);
666 tokens.GetMiddle(res.freeform);
667 tokens.GetMiddle(filteraction);
668 if (!StringToFilterAction(filteraction, res.action))
669 throw ModuleException("Invalid action: " + filteraction);
671 std::string filterflags;
672 tokens.GetMiddle(filterflags);
673 char c = res.FillFlags(filterflags);
675 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
677 std::string duration;
678 tokens.GetMiddle(duration);
679 res.duration = ConvToNum<unsigned long>(duration);
681 tokens.GetTrailing(res.reason);
683 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
684 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
691 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
693 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
695 FilterResult& filter = *i;
696 if (filter.from_config)
699 server.SendMetaData("filter", EncodeFilter(&filter));
703 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
705 if ((target == NULL) && (extname == "filter"))
709 FilterResult data = DecodeFilter(extdata);
710 this->AddFilter(data.freeform, data.action, data.reason, data.duration, data.GetFlags());
712 catch (ModuleException& e)
714 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
719 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
721 static std::string stripped_text;
722 stripped_text.clear();
724 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
726 FilterResult* filter = &*i;
728 /* Skip ones that dont apply to us */
729 if (!AppliesToMe(user, filter, flgs))
732 if ((filter->flag_strip_color) && (stripped_text.empty()))
734 stripped_text = text;
735 InspIRCd::StripColor(stripped_text);
738 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
744 bool ModuleFilter::DeleteFilter(const std::string& freeform, std::string& reason)
746 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
748 if (i->freeform == freeform)
750 reason.assign(i->reason);
759 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)
761 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
763 if (i->freeform == freeform)
765 return std::make_pair(false, "Filter already exists");
771 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs, config));
773 catch (ModuleException &e)
775 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
776 return std::make_pair(false, e.GetReason());
778 return std::make_pair(true, "");
781 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
783 if (stdalgo::string::equalsci(str, "gline"))
785 else if (stdalgo::string::equalsci(str, "zline"))
787 else if (stdalgo::string::equalsci(str, "warn"))
789 else if (stdalgo::string::equalsci(str, "block"))
791 else if (stdalgo::string::equalsci(str, "silent"))
793 else if (stdalgo::string::equalsci(str, "kill"))
795 else if (stdalgo::string::equalsci(str, "shun") && (ServerInstance->XLines->GetFactory("SHUN")))
797 else if (stdalgo::string::equalsci(str, "none"))
805 std::string ModuleFilter::FilterActionToString(FilterAction fa)
809 case FA_GLINE: return "gline";
810 case FA_ZLINE: return "zline";
811 case FA_WARN: return "warn";
812 case FA_BLOCK: return "block";
813 case FA_SILENT: return "silent";
814 case FA_KILL: return "kill";
815 case FA_SHUN: return "shun";
816 default: return "none";
820 void ModuleFilter::ReadFilters()
822 insp::flat_set<std::string> removedfilters;
824 for (std::vector<FilterResult>::iterator filter = filters.begin(); filter != filters.end(); )
826 if (filter->from_config)
828 removedfilters.insert(filter->freeform);
829 delete filter->regex;
830 filter = filters.erase(filter);
834 // The filter is not from the config.
838 ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
839 for (ConfigIter i = tags.first; i != tags.second; ++i)
841 std::string pattern = i->second->getString("pattern");
842 std::string reason = i->second->getString("reason");
843 std::string action = i->second->getString("action");
844 std::string flgs = i->second->getString("flags");
845 unsigned long duration = i->second->getDuration("duration", 10*60, 1);
850 if (!StringToFilterAction(action, fa))
853 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(this)->AddFilter(pattern, fa, reason, duration, flgs, true);
855 removedfilters.erase(pattern);
857 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Filter '%s' could not be added: %s", pattern.c_str(), result.second.c_str());
860 if (!removedfilters.empty())
862 for (insp::flat_set<std::string>::const_iterator it = removedfilters.begin(); it != removedfilters.end(); ++it)
863 ServerInstance->SNO->WriteGlobalSno('f', "Removing filter '" + *(it) + "' due to config rehash.");
867 ModResult ModuleFilter::OnStats(Stats::Context& stats)
869 if (stats.GetSymbol() == 's')
871 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
873 stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->duration)+" :"+i->reason);
875 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
877 stats.AddRow(223, "EXEMPT "+(*i));
879 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
881 stats.AddRow(223, "EXEMPT "+(*i));
884 return MOD_RES_PASSTHRU;
887 void ModuleFilter::OnUnloadModule(Module* mod)
889 // If the regex engine became unavailable or has changed, remove all filters
894 else if (RegexEngine.operator->() != factory)
896 factory = RegexEngine.operator->();
901 MODULE_INIT(ModuleFilter)