+ else if (command == "PART")
+ {
+ /* PART with no reason: nothing to do */
+ if (parameters.size() < 2)
+ return MOD_RES_PASSTHRU;
+
+ if (exemptedchans.count(parameters[0]))
+ return MOD_RES_PASSTHRU;
+
+ parting = true;
+ flags = FLAG_PART;
+ }
+ else
+ /* We're only messing with PART and QUIT */
+ return MOD_RES_PASSTHRU;
+
+ FilterResult* f = this->FilterMatch(user, parameters[parting ? 1 : 0], flags);
+ if (!f)
+ /* PART or QUIT reason doesnt match a filter */
+ return MOD_RES_PASSTHRU;
+
+ /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */
+ parameters[parting ? 1 : 0] = "Reason filtered";
+
+ /* We're warning or blocking, OR they're quitting and its a KILL action
+ * (we cant kill someone whos already quitting, so filter them anyway)
+ */
+ if ((f->action == FA_WARN) || (f->action == FA_BLOCK) || (((!parting) && (f->action == FA_KILL))) || (f->action == FA_SILENT))
+ {
+ return MOD_RES_PASSTHRU;
+ }
+ else
+ {
+ /* Are they parting, if so, kill is applicable */
+ if ((parting) && (f->action == FA_KILL))
+ {
+ user->WriteNotice("*** Your PART message was filtered: " + f->reason);
+ ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
+ }
+ if (f->action == FA_GLINE)
+ {
+ /* Note: We G-line *@IP so that if their host doesn't resolve the G-line still applies. */
+ GLine* gl = new GLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString());
+ ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their %s message matched %s (%s)",
+ user->nick.c_str(), gl->Displayable().c_str(),
+ InspIRCd::DurationString(f->duration).c_str(),
+ InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
+ command.c_str(), f->freeform.c_str(), f->reason.c_str()));
+
+ if (ServerInstance->XLines->AddLine(gl,NULL))
+ {
+ ServerInstance->XLines->ApplyLines();
+ }
+ else
+ delete gl;
+ }
+ if (f->action == FA_ZLINE)
+ {
+ ZLine* zl = new ZLine(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
+ ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their %s message matched %s (%s)",
+ user->nick.c_str(), zl->Displayable().c_str(),
+ InspIRCd::DurationString(f->duration).c_str(),
+ InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
+ command.c_str(), f->freeform.c_str(), f->reason.c_str()));
+
+ if (ServerInstance->XLines->AddLine(zl,NULL))
+ {
+ ServerInstance->XLines->ApplyLines();
+ }
+ else
+ delete zl;
+ }
+ else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
+ {
+ /* Note: We shun *!*@IP so that if their host doesnt resolve the shun still applies. */
+ Shun* sh = new Shun(ServerInstance->Time(), f->duration, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), user->GetIPString());
+ ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their %s message matched %s (%s)",
+ user->nick.c_str(), sh->Displayable().c_str(),
+ InspIRCd::DurationString(f->duration).c_str(),
+ InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
+ command.c_str(), f->freeform.c_str(), f->reason.c_str()));
+
+ if (ServerInstance->XLines->AddLine(sh, NULL))
+ {
+ ServerInstance->XLines->ApplyLines();
+ }
+ else
+ delete sh;
+ }
+ return MOD_RES_DENY;
+ }
+ }
+ return MOD_RES_PASSTHRU;
+}
+
+void ModuleFilter::ReadConfig(ConfigStatus& status)
+{
+ ConfigTagList tags = ServerInstance->Config->ConfTags("exemptfromfilter");
+ exemptedchans.clear();
+ exemptednicks.clear();
+
+ for (ConfigIter i = tags.first; i != tags.second; ++i)
+ {
+ ConfigTag* tag = i->second;
+
+ // If "target" is not found, try the old "channel" key to keep compatibility with 2.0 configs
+ const std::string target = tag->getString("target", tag->getString("channel"), 1);
+ if (!target.empty())
+ {
+ if (target[0] == '#')
+ exemptedchans.insert(target);
+ else
+ exemptednicks.insert(target);
+ }
+ }
+
+ ConfigTag* tag = ServerInstance->Config->ConfValue("filteropts");
+ std::string newrxengine = tag->getString("engine");
+ notifyuser = tag->getBool("notifyuser", true);
+ warnonselfmsg = tag->getBool("warnonselfmsg");
+
+ factory = RegexEngine ? (RegexEngine.operator->()) : NULL;
+
+ if (newrxengine.empty())
+ RegexEngine.SetProvider("regex");
+ else
+ RegexEngine.SetProvider("regex/" + newrxengine);
+
+ if (!RegexEngine)
+ {
+ if (newrxengine.empty())
+ ServerInstance->SNO->WriteGlobalSno('f', "WARNING: No regex engine loaded - Filter functionality disabled until this is corrected.");
+ else
+ ServerInstance->SNO->WriteGlobalSno('f', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
+
+ initing = false;
+ FreeFilters();
+ return;
+ }
+
+ if ((!initing) && (RegexEngine.operator->() != factory))
+ {
+ ServerInstance->SNO->WriteGlobalSno('f', "Dumping all filters due to regex engine change");
+ FreeFilters();
+ }
+
+ initing = false;
+ ReadFilters();
+}
+
+Version ModuleFilter::GetVersion()
+{
+ return Version("Adds the /FILTER command which allows server operators to define regex matches for inappropriate phrases that are not allowed to be used in channel messages, private messages, part messages, or quit messages.", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : "");
+}
+
+std::string ModuleFilter::EncodeFilter(FilterResult* filter)
+{
+ std::ostringstream stream;
+ std::string x = filter->freeform;
+
+ /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
+ for (std::string::iterator n = x.begin(); n != x.end(); n++)
+ if (*n == ' ')
+ *n = '\7';
+
+ stream << x << " " << FilterActionToString(filter->action) << " " << filter->GetFlags() << " " << filter->duration << " :" << filter->reason;
+ return stream.str();
+}
+
+FilterResult ModuleFilter::DecodeFilter(const std::string &data)
+{
+ std::string filteraction;
+ FilterResult res;
+ irc::tokenstream tokens(data);
+ tokens.GetMiddle(res.freeform);
+ tokens.GetMiddle(filteraction);
+ if (!StringToFilterAction(filteraction, res.action))
+ throw ModuleException("Invalid action: " + filteraction);
+
+ std::string filterflags;
+ tokens.GetMiddle(filterflags);
+ char c = res.FillFlags(filterflags);
+ if (c != 0)
+ throw ModuleException("Invalid flag: '" + std::string(1, c) + "'");
+
+ std::string duration;
+ tokens.GetMiddle(duration);
+ res.duration = ConvToNum<unsigned long>(duration);
+
+ tokens.GetTrailing(res.reason);
+
+ /* Hax to allow spaces in the freeform without changing the design of the irc protocol */
+ for (std::string::iterator n = res.freeform.begin(); n != res.freeform.end(); n++)
+ if (*n == '\7')
+ *n = ' ';
+
+ return res;
+}
+
+void ModuleFilter::OnSyncNetwork(ProtocolInterface::Server& server)
+{
+ for (std::vector<FilterResult>::iterator i = filters.begin(); i != filters.end(); ++i)
+ {
+ FilterResult& filter = *i;
+ if (filter.from_config)
+ continue;