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;
178 , public ServerProtocol::SyncEventListener
179 , public Stats::EventListener
181 typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
186 RegexFactory* factory;
190 CommandFilter filtcommand;
191 dynamic_reference<RegexFactory> RegexEngine;
193 std::vector<FilterResult> filters;
196 // List of channel names excluded from filtering.
197 ExemptTargetSet exemptedchans;
199 // List of target nicknames excluded from filtering.
200 ExemptTargetSet exemptednicks;
203 void init() CXX11_OVERRIDE;
204 CullResult cull() CXX11_OVERRIDE;
205 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
206 FilterResult* FilterMatch(User* user, const std::string &text, int flags);
207 bool DeleteFilter(const std::string& freeform, std::string& reason);
208 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);
209 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
210 Version GetVersion() CXX11_OVERRIDE;
211 std::string EncodeFilter(FilterResult* filter);
212 FilterResult DecodeFilter(const std::string &data);
213 void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
214 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
215 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
216 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
217 void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
218 bool AppliesToMe(User* user, FilterResult* filter, int flags);
220 static bool StringToFilterAction(const std::string& str, FilterAction& fa);
221 static std::string FilterActionToString(FilterAction fa);
224 CmdResult CommandFilter::Handle(User* user, const Params& parameters)
226 if (parameters.size() == 1)
228 /* Deleting a filter */
229 Module* me = creator;
232 if (static_cast<ModuleFilter*>(me)->DeleteFilter(parameters[0], reason))
234 user->WriteNotice("*** Removed filter '" + parameters[0] + "': " + reason);
235 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "%s removed filter '%s': %s",
236 user->nick.c_str(), parameters[0].c_str(), reason.c_str());
241 user->WriteNotice("*** Filter '" + parameters[0] + "' not found on the list.");
247 /* Adding a filter */
248 if (parameters.size() >= 4)
250 const std::string& freeform = parameters[0];
252 const std::string& flags = parameters[2];
253 unsigned int reasonindex;
254 unsigned long duration = 0;
256 if (!ModuleFilter::StringToFilterAction(parameters[1], type))
258 if (ServerInstance->XLines->GetFactory("SHUN"))
259 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', 'kill', and 'shun'.");
261 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'zline', 'none', 'warn', 'block', 'silent', and 'kill'.");
265 if (type == FA_GLINE || type == FA_ZLINE || type == FA_SHUN)
267 if (parameters.size() >= 5)
269 if (!InspIRCd::Duration(parameters[3], duration))
271 user->WriteNotice("*** Invalid duration for filter");
278 user->WriteNotice("*** Not enough parameters: When setting a '" + parameters[1] + "' type filter, a duration must be specified as the third parameter.");
287 Module* me = creator;
288 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
291 const std::string message = InspIRCd::Format("'%s', type '%s'%s, flags '%s', reason: %s",
292 freeform.c_str(), parameters[1].c_str(),
293 (duration ? InspIRCd::Format(", duration '%s'",
294 InspIRCd::DurationString(duration).c_str()).c_str()
295 : ""), flags.c_str(), parameters[reasonindex].c_str());
297 user->WriteNotice("*** Added filter " + message);
298 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F',
299 "%s added filter %s", user->nick.c_str(), message.c_str());
305 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
311 user->WriteNotice("*** Not enough parameters.");
318 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
320 const AccountExtItem* accountext = GetAccountExtItem();
322 if ((filter->flag_no_opers) && user->IsOper())
324 if ((filter->flag_no_registered) && accountext && accountext->get(user))
326 if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
328 if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
330 if ((iflags & FLAG_QUIT) && (!filter->flag_quit_message))
332 if ((iflags & FLAG_PART) && (!filter->flag_part_message))
337 ModuleFilter::ModuleFilter()
338 : ServerProtocol::SyncEventListener(this)
339 , Stats::EventListener(this)
342 , RegexEngine(this, "regex")
346 void ModuleFilter::init()
348 ServerInstance->SNO->EnableSnomask('f', "FILTER");
351 CullResult ModuleFilter::cull()
354 return Module::cull();
357 void ModuleFilter::FreeFilters()
359 for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
365 ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details)
367 // Leave remote users and servers alone
369 return MOD_RES_PASSTHRU;
371 flags = (details.type == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
373 FilterResult* f = this->FilterMatch(user, details.text, flags);
376 bool is_selfmsg = false;
378 switch (msgtarget.type)
380 case MessageTarget::TYPE_USER:
382 User* t = msgtarget.Get<User>();
383 // Check if the target nick is exempted, if yes, ignore this message
384 if (exemptednicks.count(t->nick))
385 return MOD_RES_PASSTHRU;
393 case MessageTarget::TYPE_CHANNEL:
395 Channel* t = msgtarget.Get<Channel>();
396 if (exemptedchans.count(t->name))
397 return MOD_RES_PASSTHRU;
402 case MessageTarget::TYPE_SERVER:
406 if (is_selfmsg && warnonselfmsg)
408 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's self message matched %s (%s)",
409 user->nick.c_str(), f->freeform.c_str(), f->reason.c_str()));
410 return MOD_RES_PASSTHRU;
412 else if (f->action == FA_WARN)
414 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's message to %s matched %s (%s)",
415 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
416 return MOD_RES_PASSTHRU;
418 else if (f->action == FA_BLOCK)
420 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
421 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
424 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
425 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
427 user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
430 details.echo_original = true;
432 else if (f->action == FA_SILENT)
436 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
437 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
439 user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
442 details.echo_original = true;
444 else if (f->action == FA_KILL)
446 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
447 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
448 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
450 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
452 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
453 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their message to %s matched %s (%s)",
454 user->nick.c_str(), sh->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
455 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
456 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
457 if (ServerInstance->XLines->AddLine(sh, NULL))
459 ServerInstance->XLines->ApplyLines();
464 else if (f->action == FA_GLINE)
466 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
467 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their message to %s matched %s (%s)",
468 user->nick.c_str(), gl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
469 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
470 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
471 if (ServerInstance->XLines->AddLine(gl,NULL))
473 ServerInstance->XLines->ApplyLines();
478 else if (f->action == FA_ZLINE)
480 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
481 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their message to %s matched %s (%s)",
482 user->nick.c_str(), zl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
483 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
484 target.c_str(), f->freeform.c_str(), f->reason.c_str()));
485 if (ServerInstance->XLines->AddLine(zl,NULL))
487 ServerInstance->XLines->ApplyLines();
493 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
496 return MOD_RES_PASSTHRU;
499 ModResult ModuleFilter::OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated)
506 if (command == "QUIT")
508 /* QUIT with no reason: nothing to do */
509 if (parameters.size() < 1)
510 return MOD_RES_PASSTHRU;
515 else if (command == "PART")
517 /* PART with no reason: nothing to do */
518 if (parameters.size() < 2)
519 return MOD_RES_PASSTHRU;
521 if (exemptedchans.count(parameters[0]))
522 return MOD_RES_PASSTHRU;
528 /* We're only messing with PART and QUIT */
529 return MOD_RES_PASSTHRU;
531 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
533 /* PART or QUIT reason doesnt match a filter */
534 return MOD_RES_PASSTHRU;
536 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
537 parameters[parting ? 1 : 0] = "Reason filtered";
539 /* We're warning or blocking, OR theyre quitting and its a KILL action
540 * (we cant kill someone whos already quitting, so filter them anyway)
542 if ((f->action == FA_WARN) || (f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
544 return MOD_RES_PASSTHRU;
548 /* Are they parting, if so, kill is applicable */
549 if ((parting) && (f->action == FA_KILL))
551 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
552 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
554 if (f->action == FA_GLINE)
556 /* Note: We G-line *@IP so that if their host doesn't resolve the G-line still applies. */
557 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
558 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their %s message matched %s (%s)",
559 user->nick.c_str(), gl->Displayable().c_str(),
560 InspIRCd::DurationString(f->duration).c_str(),
561 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
562 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
564 if (ServerInstance->XLines->AddLine(gl,NULL))
566 ServerInstance->XLines->ApplyLines();
571 if (f->action == FA_ZLINE)
573 ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
574 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their %s message matched %s (%s)",
575 user->nick.c_str(), zl->Displayable().c_str(),
576 InspIRCd::DurationString(f->duration).c_str(),
577 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
578 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
580 if (ServerInstance->XLines->AddLine(zl,NULL))
582 ServerInstance->XLines->ApplyLines();
587 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
589 /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
590 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
591 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their %s message matched %s (%s)",
592 user->nick.c_str(), sh->Displayable().c_str(),
593 InspIRCd::DurationString(f->duration).c_str(),
594 InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
595 command.c_str(), f->freeform.c_str(), f->reason.c_str()));
597 if (ServerInstance->XLines->AddLine(sh, NULL))
599 ServerInstance->XLines->ApplyLines();
607 return MOD_RES_PASSTHRU;
610 void ModuleFilter::ReadConfig(ConfigStatus& status)
612 ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
613 exemptedchans.clear();
614 exemptednicks.clear();
616 for (ConfigIter i = tags.first; i != tags.second; ++i)
618 ConfigTag* tag = i->second;
620 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
621 const std::string target = tag->getString("target", tag->getString("channel"));
624 if (target[0] == '#')
625 exemptedchans.insert(target);
627 exemptednicks.insert(target);
631 ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
632 std::string newrxengine = tag->getString("engine");
633 notifyuser = tag->getBool("notifyuser", true);
634 warnonselfmsg = tag->getBool("warnonselfmsg");
636 factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
638 if (newrxengine.empty())
639 RegexEngine.SetProvider("regex");
641 RegexEngine.SetProvider("regex/" + newrxengine);
645 if (newrxengine.empty())
646 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
648 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
655 if ((!initing) && (RegexEngine.operator->() != factory))
657 ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
665 Version ModuleFilter::GetVersion()
667 return Version("Provides text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
670 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
672 std::ostringstream stream;
673 std::string x = filter->freeform;
675 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
676 for (std::string::iterator n = x.begin(); n != x.end(); n++)
680 stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
684 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
686 std::string filteraction;
688 irc::tokenstream tokens(data);
689 tokens.GetMiddle(res.freeform);
690 tokens.GetMiddle(filteraction);
691 if (!StringToFilterAction(filteraction, res.action))
692 throw ModuleException("Invalid action: " + filteraction);
694 std::string filterflags;
695 tokens.GetMiddle(filterflags);
696 char c = res.FillFlags(filterflags);
698 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
700 std::string duration;
701 tokens.GetMiddle(duration);
702 res.duration = ConvToNum<unsigned long>(duration);
704 tokens.GetTrailing(res.reason);
706 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
707 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
714 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
716 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
718 FilterResult& filter = *i;
719 if (filter.from_config)
722 server.SendMetaData("filter", EncodeFilter(&filter));
726 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
728 if ((target == NULL) && (extname == "filter"))
732 FilterResult data = DecodeFilter(extdata);
733 this->AddFilter(data.freeform, data.action, data.reason, data.duration, data.GetFlags());
735 catch (ModuleException& e)
737 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
742 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
744 static std::string stripped_text;
745 stripped_text.clear();
747 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
749 FilterResult* filter = &*i;
751 /* Skip ones that dont apply to us */
752 if (!AppliesToMe(user, filter, flgs))
755 if ((filter->flag_strip_color) && (stripped_text.empty()))
757 stripped_text = text;
758 InspIRCd::StripColor(stripped_text);
761 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
767 bool ModuleFilter::DeleteFilter(const std::string& freeform, std::string& reason)
769 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
771 if (i->freeform == freeform)
773 reason.assign(i->reason);
782 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)
784 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
786 if (i->freeform == freeform)
788 return std::make_pair(false, "Filter already exists");
794 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs, config));
796 catch (ModuleException &e)
798 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
799 return std::make_pair(false, e.GetReason());
801 return std::make_pair(true, "");
804 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
806 if (stdalgo::string::equalsci(str, "gline"))
808 else if (stdalgo::string::equalsci(str, "zline"))
810 else if (stdalgo::string::equalsci(str, "warn"))
812 else if (stdalgo::string::equalsci(str, "block"))
814 else if (stdalgo::string::equalsci(str, "silent"))
816 else if (stdalgo::string::equalsci(str, "kill"))
818 else if (stdalgo::string::equalsci(str, "shun") && (ServerInstance->XLines->GetFactory("SHUN")))
820 else if (stdalgo::string::equalsci(str, "none"))
828 std::string ModuleFilter::FilterActionToString(FilterAction fa)
832 case FA_GLINE: return "gline";
833 case FA_ZLINE: return "zline";
834 case FA_WARN: return "warn";
835 case FA_BLOCK: return "block";
836 case FA_SILENT: return "silent";
837 case FA_KILL: return "kill";
838 case FA_SHUN: return "shun";
839 default: return "none";
843 void ModuleFilter::ReadFilters()
845 insp::flat_set<std::string> removedfilters;
847 for (std::vector<FilterResult>::iterator filter = filters.begin(); filter != filters.end(); )
849 if (filter->from_config)
851 removedfilters.insert(filter->freeform);
852 delete filter->regex;
853 filter = filters.erase(filter);
857 // The filter is not from the config.
861 ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
862 for (ConfigIter i = tags.first; i != tags.second; ++i)
864 std::string pattern = i->second->getString("pattern");
865 std::string reason = i->second->getString("reason");
866 std::string action = i->second->getString("action");
867 std::string flgs = i->second->getString("flags");
868 unsigned long duration = i->second->getDuration("duration", 10*60, 1);
873 if (!StringToFilterAction(action, fa))
876 std::pair<bool, std::string> result = static_cast<ModuleFilter*>(this)->AddFilter(pattern, fa, reason, duration, flgs, true);
878 removedfilters.erase(pattern);
880 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Filter '%s' could not be added: %s", pattern.c_str(), result.second.c_str());
883 if (!removedfilters.empty())
885 for (insp::flat_set<std::string>::const_iterator it = removedfilters.begin(); it != removedfilters.end(); ++it)
886 ServerInstance->SNO->WriteGlobalSno('f', "Removing filter '" + *(it) + "' due to config rehash.");
890 ModResult ModuleFilter::OnStats(Stats::Context& stats)
892 if (stats.GetSymbol() == 's')
894 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
896 stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->duration)+" :"+i->reason);
898 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
900 stats.AddRow(223, "EXEMPT "+(*i));
902 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
904 stats.AddRow(223, "EXEMPT "+(*i));
907 return MOD_RES_PASSTHRU;
910 void ModuleFilter::OnUnloadModule(Module* mod)
912 // If the regex engine became unavailable or has changed, remove all filters
917 else if (RegexEngine.operator->() != factory)
919 factory = RegexEngine.operator->();
924 MODULE_INIT(ModuleFilter)