X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_filter.cpp;h=d2cffdb5dd8e712ae56a68846d0e424210b62704;hb=553a8da754c8cd308bad2008018849714e70f9b7;hp=57e5d8fdbe25e6e52159214fd61258db196f8713;hpb=1b2dd8489c4105172f49ac94c5427f922c033b70;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 57e5d8fdb..d2cffdb5d 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -1,27 +1,32 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2008 InspIRCd Development Team - * See: http://www.inspircd.org/wiki/index.php/Credits + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2004, 2008 Craig Edwards + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2007 Robin Burchell * - * This program is free but copyrighted software; see - * the file COPYING for details. + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. * - * --------------------------------------------------- + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + #include "inspircd.h" -#include "users.h" -#include "channels.h" -#include "modules.h" #include "xline.h" #include "m_regex.h" /* $ModDesc: Text (spam) filtering */ -static std::string RegexEngine = ""; -static Module* rxengine = NULL; +class ModuleFilter; enum FilterFlags { @@ -31,7 +36,7 @@ enum FilterFlags FLAG_NOTICE = 16 }; -class FilterResult : public classbase +class FilterResult { public: std::string freeform; @@ -93,133 +98,159 @@ class FilterResult : public classbase { } - virtual ~FilterResult() + ~FilterResult() + { + } +}; + +class CommandFilter : public Command +{ + public: + CommandFilter(Module* f) + : Command(f, "FILTER", 1, 5) + { + flags_needed = 'o'; + this->syntax = " [] :"; + } + CmdResult Handle(const std::vector&, User*); + + void TooFewParams(User* user, const std::string &extra_text) + { + user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick.c_str(), extra_text.c_str()); + } + + RouteDescriptor GetRouting(User* user, const std::vector& parameters) { + return ROUTE_BROADCAST; } }; -class CommandFilter; +class ImplFilter : public FilterResult +{ + public: + Regex* regex; + + ImplFilter(ModuleFilter* mymodule, const std::string &rea, const std::string &act, long glinetime, const std::string &pat, const std::string &flgs); +}; + -class FilterBase : public Module +class ModuleFilter : public Module { - CommandFilter* filtcommand; + public: + CommandFilter filtcommand; + dynamic_reference RegexEngine; + + std::vector filters; + const char *error; + int erroffset; int flags; -protected: + std::vector exemptfromfilter; // List of channel names excluded from filtering. - public: - FilterBase(InspIRCd* Me, const std::string &source); - virtual ~FilterBase(); - virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list); - virtual FilterResult* FilterMatch(User* user, const std::string &text, int flags) = 0; - virtual bool DeleteFilter(const std::string &freeform) = 0; - virtual void SyncFilters(Module* proto, void* opaque) = 0; - virtual void SendFilter(Module* proto, void* opaque, FilterResult* iter); - virtual std::pair AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flags) = 0; - virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list); - virtual void OnRehash(User* user, const std::string ¶meter); - virtual Version GetVersion(); + + ModuleFilter(); + void init(); + ~ModuleFilter(); + ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list); + FilterResult* FilterMatch(User* user, const std::string &text, int flags); + bool DeleteFilter(const std::string &freeform); + void SyncFilters(Module* proto, void* opaque); + void SendFilter(Module* proto, void* opaque, FilterResult* iter); + std::pair AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flags); + ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list); + void OnRehash(User* user); + Version GetVersion(); std::string EncodeFilter(FilterResult* filter); FilterResult DecodeFilter(const std::string &data); - virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable = false); - virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata); - virtual int OnStats(char symbol, User* user, string_list &results) = 0; - virtual int OnPreCommand(std::string &command, std::vector ¶meters, User *user, bool validated, const std::string &original_line); + void OnSyncNetwork(Module* proto, void* opaque); + void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata); + ModResult OnStats(char symbol, User* user, string_list &results); + ModResult OnPreCommand(std::string &command, std::vector ¶meters, LocalUser *user, bool validated, const std::string &original_line); bool AppliesToMe(User* user, FilterResult* filter, int flags); - void OnLoadModule(Module* mod, const std::string& name); - virtual void ReadFilters(ConfigReader &MyConf) = 0; + void ReadFilters(ConfigReader &MyConf); }; -class CommandFilter : public Command +CmdResult CommandFilter::Handle(const std::vector ¶meters, User *user) { - FilterBase* Base; - public: - CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &ssource) : Command(Me, "FILTER", "o", 1), Base(f) - { - this->source = ssource; - this->syntax = " [] :"; - } - - CmdResult Handle(const std::vector ¶meters, User *user) + if (parameters.size() == 1) { - if (parameters.size() == 1) + /* Deleting a filter */ + Module *me = creator; + if (static_cast(me)->DeleteFilter(parameters[0])) { - /* Deleting a filter */ - if (Base->DeleteFilter(parameters[0])) - { - user->WriteServ("NOTICE %s :*** Deleted filter '%s'", user->nick.c_str(), parameters[0].c_str()); - return CMD_SUCCESS; - } - else - { - user->WriteServ("NOTICE %s :*** Filter '%s' not found on list.", user->nick.c_str(), parameters[0].c_str()); - return CMD_FAILURE; - } + user->WriteServ("NOTICE %s :*** Removed filter '%s'", user->nick.c_str(), parameters[0].c_str()); + ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', std::string("FILTER: ")+user->nick+" removed filter '"+parameters[0]+"'"); + return CMD_SUCCESS; } else { - /* Adding a filter */ - if (parameters.size() >= 4) - { - std::string freeform = parameters[0]; - std::string type = parameters[1]; - std::string flags = parameters[2]; - std::string reason; - long duration = 0; + user->WriteServ("NOTICE %s :*** Filter '%s' not found in list, try /stats s.", user->nick.c_str(), parameters[0].c_str()); + return CMD_FAILURE; + } + } + else + { + /* Adding a filter */ + if (parameters.size() >= 4) + { + std::string freeform = parameters[0]; + std::string type = parameters[1]; + std::string flags = parameters[2]; + std::string reason; + long duration = 0; - if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent")) - { - user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick.c_str(), freeform.c_str()); - return CMD_FAILURE; - } + if ((type != "gline") && (type != "none") && (type != "block") && (type != "kill") && (type != "silent")) + { + user->WriteServ("NOTICE %s :*** Invalid filter type '%s'. Supported types are 'gline', 'none', 'block', 'silent' and 'kill'.", user->nick.c_str(), type.c_str()); + return CMD_FAILURE; + } - if (type == "gline") - { - if (parameters.size() >= 5) - { - duration = ServerInstance->Duration(parameters[3]); - reason = parameters[4]; - } - else - { - this->TooFewParams(user, " When setting a gline type filter, a gline duration must be specified as the third parameter."); - return CMD_FAILURE; - } - } - else - { - reason = parameters[3]; - } - std::pair result = Base->AddFilter(freeform, type, reason, duration, flags); - if (result.first) + if (type == "gline") + { + if (parameters.size() >= 5) { - user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick.c_str(), freeform.c_str(), - type.c_str(), (duration ? " duration: " : ""), (duration ? parameters[3].c_str() : ""), - flags.c_str(), reason.c_str()); - return CMD_SUCCESS; + duration = ServerInstance->Duration(parameters[3]); + reason = parameters[4]; } else { - user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick.c_str(), freeform.c_str(), result.second.c_str()); + this->TooFewParams(user, ": When setting a gline type filter, a gline duration must be specified as the third parameter."); return CMD_FAILURE; } } else { - this->TooFewParams(user, "."); - return CMD_FAILURE; + reason = parameters[3]; } + + Module *me = creator; + std::pair result = static_cast(me)->AddFilter(freeform, type, reason, duration, flags); + if (result.first) + { + user->WriteServ("NOTICE %s :*** Added filter '%s', type '%s'%s%s, flags '%s', reason: '%s'", user->nick.c_str(), freeform.c_str(), + type.c_str(), (duration ? ", duration " : ""), (duration ? parameters[3].c_str() : ""), + flags.c_str(), reason.c_str()); + + ServerInstance->SNO->WriteToSnoMask(IS_LOCAL(user) ? 'a' : 'A', std::string("FILTER: ")+user->nick+" added filter '"+freeform+"', type '"+type+"', "+(duration ? "duration "+parameters[3]+", " : "")+"flags '"+flags+"', reason: "+reason); + return CMD_SUCCESS; + } + else + { + user->WriteServ("NOTICE %s :*** Filter '%s' could not be added: %s", user->nick.c_str(), freeform.c_str(), result.second.c_str()); + return CMD_FAILURE; + } + } + else + { + this->TooFewParams(user, "."); + return CMD_FAILURE; } - } - void TooFewParams(User* user, const std::string &extra_text) - { - user->WriteServ("NOTICE %s :*** Not enough parameters%s", user->nick.c_str(), extra_text.c_str()); } -}; +} -bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int iflags) +bool ModuleFilter::AppliesToMe(User* user, FilterResult* filter, int iflags) { if ((filter->flag_no_opers) && IS_OPER(user)) return false; @@ -234,34 +265,39 @@ bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int iflags) return true; } -FilterBase::FilterBase(InspIRCd* Me, const std::string &source) : Module(Me) +ModuleFilter::ModuleFilter() : filtcommand(this), RegexEngine(this, "regex") { - Me->Modules->UseInterface("RegularExpression"); - filtcommand = new CommandFilter(this, Me, source); - ServerInstance->AddCommand(filtcommand); - Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncOtherMetaData, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnLoadModule }; - ServerInstance->Modules->Attach(eventlist, this, 8); } -FilterBase::~FilterBase() +void ModuleFilter::init() { - ServerInstance->Modules->DoneWithInterface("RegularExpression"); + ServerInstance->AddCommand(&filtcommand); + Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncNetwork, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash }; + ServerInstance->Modules->Attach(eventlist, this, 7); + OnRehash(NULL); } -int FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) +ModuleFilter::~ModuleFilter() { +} + +ModResult ModuleFilter::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) +{ + if (!IS_LOCAL(user)) + return MOD_RES_PASSTHRU; + flags = FLAG_PRIVMSG; return OnUserPreNotice(user,dest,target_type,text,status,exempt_list); } -int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) +ModResult ModuleFilter::OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) { - if (!flags) - flags = FLAG_NOTICE; - /* Leave ulines alone */ if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user))) - return 0; + return MOD_RES_PASSTHRU; + + if (!flags) + flags = FLAG_NOTICE; FilterResult* f = this->FilterMatch(user, text, flags); if (f) @@ -277,16 +313,22 @@ int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::stri Channel* t = (Channel*)dest; target = std::string(t->name); std::vector::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), target); - if (i != exemptfromfilter.end()) return 0; + if (i != exemptfromfilter.end()) return MOD_RES_PASSTHRU; } if (f->action == "block") - { - ServerInstance->SNO->WriteToSnoMask('A', std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason); - user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered and opers notified: "+f->reason); + { + ServerInstance->SNO->WriteGlobalSno('a', std::string("FILTER: ")+user->nick+" had their message filtered, target was "+target+": "+f->reason); + if (target_type == TYPE_CHANNEL) + user->WriteNumeric(404, "%s %s :Message to channel blocked and opers notified (%s)",user->nick.c_str(), target.c_str(), f->reason.c_str()); + else + user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message to "+target+" was blocked and opers notified: "+f->reason); } if (f->action == "silent") { - user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message has been filtered: "+f->reason); + if (target_type == TYPE_CHANNEL) + user->WriteNumeric(404, "%s %s :Message to channel blocked (%s)",user->nick.c_str(), target.c_str(), f->reason.c_str()); + else + user->WriteServ("NOTICE "+std::string(user->nick)+" :Your message to "+target+" was blocked: "+f->reason); } if (f->action == "kill") { @@ -294,7 +336,7 @@ int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::stri } if (f->action == "gline") { - GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString()); + GLine* gl = new GLine(ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString()); if (ServerInstance->XLines->AddLine(gl,NULL)) { ServerInstance->XLines->ApplyLines(); @@ -304,12 +346,12 @@ int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::stri } ServerInstance->Logs->Log("FILTER",DEFAULT,"FILTER: "+ user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + f->action); - return 1; + return MOD_RES_DENY; } - return 0; + return MOD_RES_PASSTHRU; } -int FilterBase::OnPreCommand(std::string &command, std::vector ¶meters, User *user, bool validated, const std::string &original_line) +ModResult ModuleFilter::OnPreCommand(std::string &command, std::vector ¶meters, LocalUser *user, bool validated, const std::string &original_line) { flags = 0; if (validated && IS_LOCAL(user)) @@ -317,12 +359,12 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par std::string checkline; int replacepoint = 0; bool parting = false; - + if (command == "QUIT") { /* QUIT with no reason: nothing to do */ if (parameters.size() < 1) - return 0; + return MOD_RES_PASSTHRU; checkline = parameters[0]; replacepoint = 0; @@ -333,10 +375,10 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par { /* PART with no reason: nothing to do */ if (parameters.size() < 2) - return 0; + return MOD_RES_PASSTHRU; std::vector::iterator i = find(exemptfromfilter.begin(), exemptfromfilter.end(), parameters[0]); - if (i != exemptfromfilter.end()) return 0; + if (i != exemptfromfilter.end()) return MOD_RES_PASSTHRU; checkline = parameters[1]; replacepoint = 1; parting = true; @@ -344,16 +386,16 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par } else /* We're only messing with PART and QUIT */ - return 0; + return MOD_RES_PASSTHRU; FilterResult* f = NULL; - + if (flags) f = this->FilterMatch(user, checkline, flags); if (!f) /* PART or QUIT reason doesnt match a filter */ - return 0; + return MOD_RES_PASSTHRU; /* We cant block a part or quit, so instead we change the reason to 'Reason filtered' */ Command* c = ServerInstance->Parser->GetHandler(command); @@ -370,7 +412,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent")) { c->Handle(params, user); - return 1; + return MOD_RES_DENY; } else { @@ -383,7 +425,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par if (f->action == "gline") { /* Note: We gline *@IP so that if their host doesnt resolve the gline still applies. */ - GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName, f->reason.c_str(), "*", user->GetIPString()); + GLine* gl = new GLine(ServerInstance->Time(), f->gline_time, ServerInstance->Config->ServerName.c_str(), f->reason.c_str(), "*", user->GetIPString()); if (ServerInstance->XLines->AddLine(gl,NULL)) { ServerInstance->XLines->ApplyLines(); @@ -391,83 +433,49 @@ int FilterBase::OnPreCommand(std::string &command, std::vector &par else delete gl; } - return 1; + return MOD_RES_DENY; } } - return 0; + return MOD_RES_PASSTHRU; } - return 0; + return MOD_RES_PASSTHRU; } -void FilterBase::OnRehash(User* user, const std::string ¶meter) +void ModuleFilter::OnRehash(User* user) { - ConfigReader* MyConf = new ConfigReader(ServerInstance); + ConfigReader MyConf; std::vector().swap(exemptfromfilter); - for (int index = 0; index < MyConf->Enumerate("exemptfromfilter"); ++index) + for (int index = 0; index < MyConf.Enumerate("exemptfromfilter"); ++index) { - std::string chan = MyConf->ReadValue("exemptfromfilter", "channel", index); + std::string chan = MyConf.ReadValue("exemptfromfilter", "channel", index); if (!chan.empty()) { exemptfromfilter.push_back(chan); } } - std::string newrxengine = MyConf->ReadValue("filteropts", "engine", 0); - if (!RegexEngine.empty()) - { - if (RegexEngine == newrxengine) - return; - - ServerInstance->SNO->WriteToSnoMask('A', "Dumping all filters due to regex engine change (was '%s', now '%s')", RegexEngine.c_str(), newrxengine.c_str()); - //ServerInstance->XLines->DelAll("R"); - } - rxengine = NULL; - - RegexEngine = newrxengine; - modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression"); - if (ml) - { - for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i) - { - if (RegexNameRequest(this, *i).Send() == newrxengine) - { - ServerInstance->SNO->WriteToSnoMask('A', "Filter now using engine '%s'", RegexEngine.c_str()); - rxengine = *i; - } - } - } - if (!rxengine) - { - ServerInstance->SNO->WriteToSnoMask('A', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", RegexEngine.c_str()); - } + std::string newrxengine = "regex/" + MyConf.ReadValue("filteropts", "engine", 0); + if (newrxengine == "regex/") + newrxengine = "regex"; + if (RegexEngine.GetProvider() == newrxengine) + return; - delete MyConf; -} + //ServerInstance->SNO->WriteGlobalSno('a', "Dumping all filters due to regex engine change (was '%s', now '%s')", RegexEngine.GetProvider().c_str(), newrxengine.c_str()); + //ServerInstance->XLines->DelAll("R"); -void FilterBase::OnLoadModule(Module* mod, const std::string& name) -{ - if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression")) + RegexEngine.SetProvider(newrxengine); + if (!RegexEngine) { - std::string rxname = RegexNameRequest(this, mod).Send(); - if (rxname == RegexEngine) - { - rxengine = mod; - /* Force a rehash to make sure that any filters that couldnt be applied from the conf - * on startup or on load are applied right now. - */ - ConfigReader Config(ServerInstance); - ServerInstance->SNO->WriteToSnoMask('A', "Found and activated regex module '%s' for m_filter.so.", RegexEngine.c_str()); - ReadFilters(Config); - } + ServerInstance->SNO->WriteGlobalSno('a', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str()); } + ReadFilters(MyConf); } - -Version FilterBase::GetVersion() +Version ModuleFilter::GetVersion() { - return Version("$Id$", VF_VENDOR | VF_COMMON, API_VERSION); + return Version("Text (spam) filtering", VF_VENDOR | VF_COMMON, RegexEngine ? RegexEngine->name : ""); } -std::string FilterBase::EncodeFilter(FilterResult* filter) +std::string ModuleFilter::EncodeFilter(FilterResult* filter) { std::ostringstream stream; std::string x = filter->freeform; @@ -481,7 +489,7 @@ std::string FilterBase::EncodeFilter(FilterResult* filter) return stream.str(); } -FilterResult FilterBase::DecodeFilter(const std::string &data) +FilterResult ModuleFilter::DecodeFilter(const std::string &data) { FilterResult res; irc::tokenstream tokens(data); @@ -502,181 +510,146 @@ FilterResult FilterBase::DecodeFilter(const std::string &data) return res; } -void FilterBase::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable) +void ModuleFilter::OnSyncNetwork(Module* proto, void* opaque) { this->SyncFilters(proto, opaque); } -void FilterBase::SendFilter(Module* proto, void* opaque, FilterResult* iter) +void ModuleFilter::SendFilter(Module* proto, void* opaque, FilterResult* iter) { - proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "filter", EncodeFilter(iter)); + proto->ProtoSendMetaData(opaque, NULL, "filter", EncodeFilter(iter)); } -void FilterBase::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) +void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) { - if ((target_type == TYPE_OTHER) && (extname == "filter")) + if ((target == NULL) && (extname == "filter")) { FilterResult data = DecodeFilter(extdata); this->AddFilter(data.freeform, data.action, data.reason, data.gline_time, data.flags); } } -class ImplFilter : public FilterResult +ImplFilter::ImplFilter(ModuleFilter* mymodule, const std::string &rea, const std::string &act, long glinetime, const std::string &pat, const std::string &flgs) + : FilterResult(pat, rea, act, glinetime, flgs) { - public: - Regex* regex; + if (!mymodule->RegexEngine) + throw ModuleException("Regex module implementing '"+mymodule->RegexEngine.GetProvider()+"' is not loaded!"); + regex = mymodule->RegexEngine->Create(pat); +} - ImplFilter(Module* mymodule, const std::string &rea, const std::string &act, long glinetime, const std::string &pat, const std::string &flgs) - : FilterResult(pat, rea, act, glinetime, flgs) +FilterResult* ModuleFilter::FilterMatch(User* user, const std::string &text, int flgs) +{ + for (std::vector::iterator index = filters.begin(); index != filters.end(); index++) { - if (!rxengine) - throw ModuleException("Regex module implementing '"+RegexEngine+"' is not loaded!"); - - regex = RegexFactoryRequest(mymodule, rxengine, pat).Create(); - } + /* Skip ones that dont apply to us */ + if (!AppliesToMe(user, dynamic_cast(&(*index)), flgs)) + continue; - ImplFilter() - { + //ServerInstance->Logs->Log("m_filter", DEBUG, "Match '%s' against '%s'", text.c_str(), index->freeform.c_str()); + if (index->regex->Matches(text)) + { + //ServerInstance->Logs->Log("m_filter", DEBUG, "MATCH"); + ImplFilter fr = *index; + if (index != filters.begin()) + { + /* Move to head of list for efficiency */ + filters.erase(index); + filters.insert(filters.begin(), fr); + } + return &*filters.begin(); + } + //ServerInstance->Logs->Log("m_filter", DEBUG, "NO MATCH"); } -}; + return NULL; +} -class ModuleFilter : public FilterBase +bool ModuleFilter::DeleteFilter(const std::string &freeform) { - std::vector filters; - const char *error; - int erroffset; - ImplFilter fr; - - public: - ModuleFilter(InspIRCd* Me) - : FilterBase(Me, "m_filter.so") + for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) { - OnRehash(NULL,""); + if (i->freeform == freeform) + { + delete i->regex; + filters.erase(i); + return true; + } } + return false; +} - virtual ~ModuleFilter() +void ModuleFilter::SyncFilters(Module* proto, void* opaque) +{ + for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) { + this->SendFilter(proto, opaque, &(*i)); } +} - virtual FilterResult* FilterMatch(User* user, const std::string &text, int flgs) +std::pair ModuleFilter::AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flgs) +{ + for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) { - for (std::vector::iterator index = filters.begin(); index != filters.end(); index++) + if (i->freeform == freeform) { - /* Skip ones that dont apply to us */ - - if (!FilterBase::AppliesToMe(user, dynamic_cast(&(*index)), flgs)) - continue; - - if (index->regex->Matches(text)) - { - fr = *index; - if (index != filters.begin()) - { - /* Move to head of list for efficiency */ - filters.erase(index); - filters.insert(filters.begin(), fr); - } - return &fr; - } + return std::make_pair(false, "Filter already exists"); } - return NULL; } - virtual bool DeleteFilter(const std::string &freeform) + try { - for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) - { - if (i->freeform == freeform) - { - filters.erase(i); - return true; - } - } - return false; + filters.push_back(ImplFilter(this, reason, type, duration, freeform, flgs)); } - - virtual void SyncFilters(Module* proto, void* opaque) + catch (ModuleException &e) { - for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) - { - this->SendFilter(proto, opaque, &(*i)); - } + ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason()); + return std::make_pair(false, e.GetReason()); } + return std::make_pair(true, ""); +} - virtual std::pair AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flgs) +void ModuleFilter::ReadFilters(ConfigReader &MyConf) +{ + for (int index = 0; index < MyConf.Enumerate("keyword"); index++) { - for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) - { - if (i->freeform == freeform) - { - return std::make_pair(false, "Filter already exists"); - } - } + this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index)); + + std::string pattern = MyConf.ReadValue("keyword", "pattern", index); + std::string reason = MyConf.ReadValue("keyword", "reason", index); + std::string action = MyConf.ReadValue("keyword", "action", index); + std::string flgs = MyConf.ReadValue("keyword", "flags", index); + long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index)); + if (action.empty()) + action = "none"; + if (flgs.empty()) + flgs = "*"; try { - filters.push_back(ImplFilter(this, reason, type, duration, freeform, flgs)); + filters.push_back(ImplFilter(this, reason, action, gline_time, pattern, flgs)); + ServerInstance->Logs->Log("m_filter", DEFAULT, "Regular expression %s loaded.", pattern.c_str()); } catch (ModuleException &e) { - ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", freeform.c_str(), e.GetReason()); - return std::make_pair(false, e.GetReason()); + ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason()); } - return std::make_pair(true, ""); - } - - virtual void OnRehash(User* user, const std::string ¶meter) - { - ConfigReader MyConf(ServerInstance); - FilterBase::OnRehash(user, parameter); - ReadFilters(MyConf); } +} - void ReadFilters(ConfigReader &MyConf) +ModResult ModuleFilter::OnStats(char symbol, User* user, string_list &results) +{ + if (symbol == 's') { - for (int index = 0; index < MyConf.Enumerate("keyword"); index++) + std::string sn = ServerInstance->Config->ServerName; + for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) { - this->DeleteFilter(MyConf.ReadValue("keyword", "pattern", index)); - - std::string pattern = MyConf.ReadValue("keyword", "pattern", index); - std::string reason = MyConf.ReadValue("keyword", "reason", index); - std::string action = MyConf.ReadValue("keyword", "action", index); - std::string flgs = MyConf.ReadValue("keyword", "flags", index); - long gline_time = ServerInstance->Duration(MyConf.ReadValue("keyword", "duration", index)); - if (action.empty()) - action = "none"; - if (flgs.empty()) - flgs = "*"; - - try - { - filters.push_back(ImplFilter(this, reason, action, gline_time, pattern, flgs)); - ServerInstance->Logs->Log("m_filter", DEFAULT, "Regular expression %s loaded.", pattern.c_str()); - } - catch (ModuleException &e) - { - ServerInstance->Logs->Log("m_filter", DEFAULT, "Error in regular expression '%s': %s", pattern.c_str(), e.GetReason()); - } + results.push_back(sn+" 223 "+user->nick+" :"+RegexEngine.GetProvider()+":"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason); } - } - - virtual int OnStats(char symbol, User* user, string_list &results) - { - if (symbol == 's') + for (std::vector::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i) { - std::string sn = ServerInstance->Config->ServerName; - for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) - { - results.push_back(sn+" 223 "+user->nick+" :REGEXP:"+i->freeform+" "+i->flags+" "+i->action+" "+ConvToStr(i->gline_time)+" :"+i->reason); - } - for (std::vector::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i) - { - results.push_back(sn+" 223 "+user->nick+" :EXEMPT "+(*i)); - } + results.push_back(sn+" 223 "+user->nick+" :EXEMPT "+(*i)); } - return 0; } -}; + return MOD_RES_PASSTHRU; +} MODULE_INIT(ModuleFilter) -