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"
59 bool flag_part_message;
60 bool flag_quit_message;
63 bool flag_strip_color;
65 FilterResult(dynamic_reference<RegexFactory>& RegexEngine, const std::string& free, const std::string& rea, FilterAction act, long gt, const std::string& fla, bool cfg)
73 throw ModuleException("Regex module implementing '"+RegexEngine.GetProvider()+"' is not loaded!");
74 regex = RegexEngine->Create(free);
78 char FillFlags(const std::string &fl)
80 flag_no_opers = flag_part_message = flag_quit_message = flag_privmsg =
81 flag_notice = flag_strip_color = false;
83 for (std::string::const_iterator n = fl.begin(); n != fl.end(); ++n)
91 flag_part_message = true;
94 flag_quit_message = true;
103 flag_strip_color = true;
106 flag_no_opers = flag_part_message = flag_quit_message =
107 flag_privmsg = flag_notice = flag_strip_color = true;
117 std::string GetFlags()
121 flags.push_back('o');
122 if (flag_part_message)
123 flags.push_back('P');
124 if (flag_quit_message)
125 flags.push_back('q');
127 flags.push_back('p');
129 flags.push_back('n');
131 /* Order is important here, 'c' must be the last char in the string as it is unsupported
132 * on < 2.0.10, and the logic in FillFlags() stops parsing when it ecounters an unknown
135 if (flag_strip_color)
136 flags.push_back('c');
139 flags.push_back('-');
149 class CommandFilter : public Command
152 CommandFilter(Module* f)
153 : Command(f, "FILTER", 1, 5)
156 this->syntax = "<filter-definition> <action> <flags> [<duration>] :<reason>";
158 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
160 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
162 return ROUTE_BROADCAST;
166 class ModuleFilter : public Module, public ServerEventListener, public Stats::EventListener
168 typedef insp::flat_set<std::string, irc::insensitive_swo> ExemptTargetSet;
172 RegexFactory* factory;
176 CommandFilter filtcommand;
177 dynamic_reference<RegexFactory> RegexEngine;
179 std::vector<FilterResult> filters;
182 // List of channel names excluded from filtering.
183 ExemptTargetSet exemptedchans;
185 // List of target nicknames excluded from filtering.
186 ExemptTargetSet exemptednicks;
189 void init() CXX11_OVERRIDE;
190 CullResult cull() CXX11_OVERRIDE;
191 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE;
192 FilterResult* FilterMatch(User* user, const std::string &text, int flags);
193 bool DeleteFilter(const std::string &freeform);
194 std::pair<bool, std::string> AddFilter(const std::string &freeform, FilterAction type, const std::string &reason, long duration, const std::string &flags);
195 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE;
196 Version GetVersion() CXX11_OVERRIDE;
197 std::string EncodeFilter(FilterResult* filter);
198 FilterResult DecodeFilter(const std::string &data);
199 void OnSyncNetwork(ProtocolInterface::Server& server) CXX11_OVERRIDE;
200 void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) CXX11_OVERRIDE;
201 ModResult OnStats(Stats::Context& stats) CXX11_OVERRIDE;
202 ModResult OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated) CXX11_OVERRIDE;
203 void OnUnloadModule(Module* mod) CXX11_OVERRIDE;
204 bool AppliesToMe(User* user, FilterResult* filter, int flags);
206 static bool StringToFilterAction(const std::string& str, FilterAction& fa);
207 static std::string FilterActionToString(FilterAction fa);
210 CmdResult CommandFilter::Handle(User* user, const Params& parameters)
212 if (parameters.size() == 1)
214 /* Deleting a filter */
215 Module *me = creator;
216 if (static_cast<ModuleFilter *>(me)->DeleteFilter(parameters[0]))
218 user->WriteNotice("*** Removed filter '" + parameters[0] + "'");
219 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "FILTER: "+user->nick+" removed filter '"+parameters[0]+"'");
224 user->WriteNotice("*** Filter '" + parameters[0] + "' not found in list, try /stats s.");
230 /* Adding a filter */
231 if (parameters.size() >= 4)
233 const std::string& freeform = parameters[0];
235 const std::string& flags = parameters[2];
236 unsigned int reasonindex;
239 if (!ModuleFilter::StringToFilterAction(parameters[1], type))
241 if (ServerInstance->XLines->GetFactory("SHUN"))
242 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'none', 'block', 'silent', 'kill', and 'shun'.");
244 user->WriteNotice("*** Invalid filter type '" + parameters[1] + "'. Supported types are 'gline', 'none', 'block', 'silent', and 'kill'.");
248 if (type == FA_GLINE || type == FA_SHUN)
250 if (parameters.size() >= 5)
252 duration = InspIRCd::Duration(parameters[3]);
257 user->WriteNotice("*** Not enough parameters: When setting a gline or shun type filter, a duration must be specified as the third parameter.");
266 Module *me = creator;
267 std::pair<bool, std::string> result = static_cast<ModuleFilter *>(me)->AddFilter(freeform, type, parameters[reasonindex], duration, flags);
270 user->WriteNotice("*** Added filter '" + freeform + "', type '" + parameters[1] + "'" +
271 (duration ? ", duration " + parameters[3] : "") + ", flags '" + flags + "', reason: '" +
272 parameters[reasonindex] + "'");
274 ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'f' : 'F', "FILTER: "+user->nick+" added filter '"+freeform+"', type '"+parameters[1]+"', "+(duration ? "duration "+parameters[3]+", " : "")+"flags '"+flags+"', reason: "+parameters[reasonindex]);
280 user->WriteNotice("*** Filter '" + freeform + "' could not be added: " + result.second);
286 user->WriteNotice("*** Not enough parameters.");
293 bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags)
295 if ((filter->flag_no_opers) && user->IsOper())
297 if ((iflags & FLAG_PRIVMSG) && (!filter->flag_privmsg))
299 if ((iflags & FLAG_NOTICE) && (!filter->flag_notice))
301 if ((iflags & FLAG_QUIT) && (!filter->flag_quit_message))
303 if ((iflags & FLAG_PART) && (!filter->flag_part_message))
308 ModuleFilter::ModuleFilter()
309 : ServerEventListener(this)
310 , Stats::EventListener(this)
313 , RegexEngine(this, "regex")
317 void ModuleFilter::init()
319 ServerInstance->SNO->EnableSnomask('f', "FILTER");
322 CullResult ModuleFilter::cull()
325 return Module::cull();
328 void ModuleFilter::FreeFilters()
330 for (std::vector<FilterResult>::const_iterator i = filters.begin(); i != filters.end(); ++i)
336 ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtarget, MessageDetails& details)
338 // Leave remote users and servers alone
340 return MOD_RES_PASSTHRU;
342 flags = (details.type == MSG_PRIVMSG) ? FLAG_PRIVMSG : FLAG_NOTICE;
344 FilterResult* f = this->FilterMatch(user, details.text, flags);
348 if (msgtarget.type == MessageTarget::TYPE_USER)
350 User* t = msgtarget.Get<User>();
351 // Check if the target nick is exempted, if yes, ignore this message
352 if (exemptednicks.count(t->nick))
353 return MOD_RES_PASSTHRU;
357 else if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
359 Channel* t = msgtarget.Get<Channel>();
360 if (exemptedchans.count(t->name))
361 return MOD_RES_PASSTHRU;
365 if (f->action == FA_BLOCK)
367 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
368 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
371 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
372 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
374 user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
377 details.echo_original = true;
379 else if (f->action == FA_SILENT)
383 if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
384 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
386 user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
389 details.echo_original = true;
391 else if (f->action == FA_KILL)
393 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
394 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
395 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
397 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
399 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
400 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was shunned because their message to %s matched %s (%s)",
401 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
402 if (ServerInstance->XLines->AddLine(sh, NULL))
404 ServerInstance->XLines->ApplyLines();
409 else if (f->action == FA_GLINE)
411 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
412 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was glined because their message to %s matched %s (%s)",
413 user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
414 if (ServerInstance->XLines->AddLine(gl,NULL))
416 ServerInstance->XLines->ApplyLines();
422 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
425 return MOD_RES_PASSTHRU;
428 ModResult ModuleFilter::OnPreCommand(std::string& command, CommandBase::Params& parameters, LocalUser* user, bool validated)
435 if (command == "QUIT")
437 /* QUIT with no reason: nothing to do */
438 if (parameters.size() < 1)
439 return MOD_RES_PASSTHRU;
444 else if (command == "PART")
446 /* PART with no reason: nothing to do */
447 if (parameters.size() < 2)
448 return MOD_RES_PASSTHRU;
450 if (exemptedchans.count(parameters[0]))
451 return MOD_RES_PASSTHRU;
457 /* We're only messing with PART and QUIT */
458 return MOD_RES_PASSTHRU;
460 FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
462 /* PART or QUIT reason doesnt match a filter */
463 return MOD_RES_PASSTHRU;
465 /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
466 parameters[parting ? 1 : 0] = "Reason filtered";
468 /* We're blocking, OR theyre quitting and its a KILL action
469 * (we cant kill someone whos already quitting, so filter them anyway)
471 if ((f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
473 return MOD_RES_PASSTHRU;
477 /* Are they parting, if so, kill is applicable */
478 if ((parting) && (f->action == FA_KILL))
480 user->WriteNotice("*** Your PART message was filtered: " + f->reason);
481 ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
483 if (f->action == FA_GLINE)
485 /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */
486 GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
487 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was glined because their %s message matched %s (%s)",
488 user->nick.c_str(), command.c_str(), f->freeform.c_str(), f->reason.c_str()));
490 if (ServerInstance->XLines->AddLine(gl,NULL))
492 ServerInstance->XLines->ApplyLines();
497 else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
499 /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
500 Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
501 ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was shunned because their %s message matched %s (%s)",
502 user->nick.c_str(), command.c_str(), f->freeform.c_str(), f->reason.c_str()));
503 if (ServerInstance->XLines->AddLine(sh, NULL))
505 ServerInstance->XLines->ApplyLines();
513 return MOD_RES_PASSTHRU;
516 void ModuleFilter::ReadConfig(ConfigStatus& status)
518 ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
519 exemptedchans.clear();
520 exemptednicks.clear();
522 for (ConfigIter i = tags.first; i != tags.second; ++i)
524 ConfigTag* tag = i->second;
526 // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
527 const std::string target = tag->getString("target", tag->getString("channel"));
530 if (target[0] == '#')
531 exemptedchans.insert(target);
533 exemptednicks.insert(target);
537 ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
538 std::string newrxengine = tag->getString("engine");
539 notifyuser = tag->getBool("notifyuser", true);
541 factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
543 if (newrxengine.empty())
544 RegexEngine.SetProvider("regex");
546 RegexEngine.SetProvider("regex/" + newrxengine);
550 if (newrxengine.empty())
551 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
553 ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
560 if ((!initing) && (RegexEngine.operator->() != factory))
562 ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
570 Version ModuleFilter::GetVersion()
572 return Version("Text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
575 std::string ModuleFilter::EncodeFilter(FilterResult* filter)
577 std::ostringstream stream;
578 std::string x = filter->freeform;
580 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
581 for (std::string::iterator n = x.begin(); n != x.end(); n++)
585 stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
589 FilterResult ModuleFilter::DecodeFilter(const std::string &data)
591 std::string filteraction;
593 irc::tokenstream tokens(data);
594 tokens.GetMiddle(res.freeform);
595 tokens.GetMiddle(filteraction);
596 if (!StringToFilterAction(filteraction, res.action))
597 throw ModuleException("Invalid action: " + filteraction);
599 std::string filterflags;
600 tokens.GetMiddle(filterflags);
601 char c = res.FillFlags(filterflags);
603 throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
605 std::string duration;
606 tokens.GetMiddle(duration);
607 res.duration = ConvToInt(duration);
609 tokens.GetTrailing(res.reason);
611 /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
612 for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
619 void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
621 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
623 FilterResult& filter = *i;
624 if (filter.from_config)
627 server.SendMetaData("filter", EncodeFilter(&filter));
631 void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
633 if ((target == NULL) && (extname == "filter"))
637 FilterResult data = DecodeFilter(extdata);
638 this->AddFilter(data.freeform, data.action, data.reason, data.duration, data.GetFlags());
640 catch (ModuleException& e)
642 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Error when unserializing filter: " + e.GetReason());
647 FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs)
649 static std::string stripped_text;
650 stripped_text.clear();
652 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
654 FilterResult* filter = &*i;
656 /* Skip ones that dont apply to us */
657 if (!AppliesToMe(user, filter, flgs))
660 if ((filter->flag_strip_color) && (stripped_text.empty()))
662 stripped_text = text;
663 InspIRCd::StripColor(stripped_text);
666 if (filter->regex->Matches(filter->flag_strip_color ? stripped_text : text))
672 bool ModuleFilter::DeleteFilter(const std::string &freeform)
674 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
676 if (i->freeform == freeform)
686 std::pair<bool, std::string> ModuleFilter::AddFilter(const std::string &freeform, FilterAction type, const std::string &reason, long duration, const std::string &flgs)
688 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
690 if (i->freeform == freeform)
692 return std::make_pair(false, "Filter already exists");
698 filters.push_back(FilterResult(RegexEngine, freeform, reason, type, duration, flgs, false));
700 catch (ModuleException &e)
702 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason().c_str());
703 return std::make_pair(false, e.GetReason());
705 return std::make_pair(true, "");
708 bool ModuleFilter::StringToFilterAction(const std::string& str, FilterAction& fa)
710 if (stdalgo::string::equalsci(str, "gline"))
712 else if (stdalgo::string::equalsci(str, "block"))
714 else if (stdalgo::string::equalsci(str, "silent"))
716 else if (stdalgo::string::equalsci(str, "kill"))
718 else if (stdalgo::string::equalsci(str, "shun") && (ServerInstance->XLines->GetFactory("SHUN")))
720 else if (stdalgo::string::equalsci(str, "none"))
728 std::string ModuleFilter::FilterActionToString(FilterAction fa)
732 case FA_GLINE: return "gline";
733 case FA_BLOCK: return "block";
734 case FA_SILENT: return "silent";
735 case FA_KILL: return "kill";
736 case FA_SHUN: return "shun";
737 default: return "none";
741 void ModuleFilter::ReadFilters()
743 for (std::vector<FilterResult>::iterator filter = filters.begin(); filter != filters.end(); )
745 if (filter->from_config)
747 ServerInstance->SNO->WriteGlobalSno('f', "FILTER: removing filter '" + filter->freeform + "' due to config rehash.");
748 delete filter->regex;
749 filter = filters.erase(filter);
753 // The filter is not from the config.
757 ConfigTagList tags = ServerInstance->Config->ConfTags("keyword");
758 for (ConfigIter i = tags.first; i != tags.second; ++i)
760 std::string pattern = i->second->getString("pattern");
761 std::string reason = i->second->getString("reason");
762 std::string action = i->second->getString("action");
763 std::string flgs = i->second->getString("flags");
764 unsigned long duration = i->second->getDuration("duration", 10*60, 1);
769 if (!StringToFilterAction(action, fa))
774 filters.push_back(FilterResult(RegexEngine, pattern, reason, fa, duration, flgs, true));
775 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Regular expression %s loaded.", pattern.c_str());
777 catch (ModuleException &e)
779 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason().c_str());
784 ModResult ModuleFilter::OnStats(Stats::Context& stats)
786 if (stats.GetSymbol() == 's')
788 for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); i++)
790 stats.AddRow(223, RegexEngine.GetProvider()+":"+i->freeform+" "+i->GetFlags()+" "+FilterActionToString(i->action)+" "+ConvToStr(i->duration)+" :"+i->reason);
792 for (ExemptTargetSet::const_iterator i = exemptedchans.begin(); i != exemptedchans.end(); ++i)
794 stats.AddRow(223, "EXEMPT "+(*i));
796 for (ExemptTargetSet::const_iterator i = exemptednicks.begin(); i != exemptednicks.end(); ++i)
798 stats.AddRow(223, "EXEMPT "+(*i));
801 return MOD_RES_PASSTHRU;
804 void ModuleFilter::OnUnloadModule(Module* mod)
806 // If the regex engine became unavailable or has changed, remove all filters
811 else if (RegexEngine.operator->() != factory)
813 factory = RegexEngine.operator->();
818 MODULE_INIT(ModuleFilter)