X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_filter.cpp;h=d2cffdb5dd8e712ae56a68846d0e424210b62704;hb=553a8da754c8cd308bad2008018849714e70f9b7;hp=5beb46fc44c96d34f634af155149444f4906a13a;hpb=7e843c22e16c81054bad18073d24fe1a07026431;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 5beb46fc4..d2cffdb5d 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -1,24 +1,32 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team - * See: http://wiki.inspircd.org/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 "xline.h" #include "m_regex.h" /* $ModDesc: Text (spam) filtering */ -static std::string RegexEngine = ""; -static Module* rxengine = NULL; +class ModuleFilter; enum FilterFlags { @@ -28,7 +36,7 @@ enum FilterFlags FLAG_NOTICE = 16 }; -class FilterResult : public classbase +class FilterResult { public: std::string freeform; @@ -90,19 +98,16 @@ class FilterResult : public classbase { } - virtual ~FilterResult() + ~FilterResult() { } }; -class FilterBase; - class CommandFilter : public Command { - FilterBase* Base; public: - CommandFilter(FilterBase* f) - : Command(reinterpret_cast(f), "FILTER", 1, 5), Base(f) + CommandFilter(Module* f) + : Command(f, "FILTER", 1, 5) { flags_needed = 'o'; this->syntax = " [] :"; @@ -120,33 +125,48 @@ class CommandFilter : public Command } }; -class FilterBase : public Module +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 ModuleFilter : public Module { + 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(); - virtual ~FilterBase(); - virtual ModResult 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 ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list); - virtual void OnRehash(User* user); - 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 OnSyncNetwork(Module* proto, void* opaque); - virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata); - virtual ModResult OnStats(char symbol, User* user, string_list &results) = 0; - virtual ModResult 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); }; CmdResult CommandFilter::Handle(const std::vector ¶meters, User *user) @@ -154,7 +174,8 @@ CmdResult CommandFilter::Handle(const std::vector ¶meters, User if (parameters.size() == 1) { /* Deleting a filter */ - if (Base->DeleteFilter(parameters[0])) + Module *me = creator; + if (static_cast(me)->DeleteFilter(parameters[0])) { 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]+"'"); @@ -201,7 +222,9 @@ CmdResult CommandFilter::Handle(const std::vector ¶meters, User { reason = parameters[3]; } - std::pair result = Base->AddFilter(freeform, type, reason, duration, flags); + + 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(), @@ -227,7 +250,7 @@ CmdResult CommandFilter::Handle(const std::vector ¶meters, User } } -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; @@ -242,20 +265,23 @@ bool FilterBase::AppliesToMe(User* user, FilterResult* filter, int iflags) return true; } -FilterBase::FilterBase() : filtcommand(this) +ModuleFilter::ModuleFilter() : filtcommand(this), RegexEngine(this, "regex") +{ +} + +void ModuleFilter::init() { - ServerInstance->Modules->UseInterface("RegularExpression"); ServerInstance->AddCommand(&filtcommand); - Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncNetwork, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnLoadModule }; - ServerInstance->Modules->Attach(eventlist, this, 8); + Implementation eventlist[] = { I_OnPreCommand, I_OnStats, I_OnSyncNetwork, I_OnDecodeMetaData, I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash }; + ServerInstance->Modules->Attach(eventlist, this, 7); + OnRehash(NULL); } -FilterBase::~FilterBase() +ModuleFilter::~ModuleFilter() { - ServerInstance->Modules->DoneWithInterface("RegularExpression"); } -ModResult FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list) +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; @@ -264,7 +290,7 @@ ModResult FilterBase::OnUserPreMessage(User* user,void* dest,int target_type, st return OnUserPreNotice(user,dest,target_type,text,status,exempt_list); } -ModResult 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) { /* Leave ulines alone */ if ((ServerInstance->ULine(user->server)) || (!IS_LOCAL(user))) @@ -325,7 +351,7 @@ ModResult FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std return MOD_RES_PASSTHRU; } -ModResult 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)) @@ -415,7 +441,7 @@ ModResult FilterBase::OnPreCommand(std::string &command, std::vector().swap(exemptfromfilter); @@ -426,62 +452,30 @@ void FilterBase::OnRehash(User* user) exemptfromfilter.push_back(chan); } } - std::string newrxengine = MyConf.ReadValue("filteropts", "engine", 0); - if (!RegexEngine.empty()) - { - if (RegexEngine == newrxengine) - return; + std::string newrxengine = "regex/" + MyConf.ReadValue("filteropts", "engine", 0); + if (newrxengine == "regex/") + newrxengine = "regex"; + if (RegexEngine.GetProvider() == newrxengine) + return; - ServerInstance->SNO->WriteGlobalSno('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; + //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"); - RegexEngine = newrxengine; - modulelist* ml = ServerInstance->Modules->FindInterface("RegularExpression"); - if (ml) + RegexEngine.SetProvider(newrxengine); + if (!RegexEngine) { - for (modulelist::iterator i = ml->begin(); i != ml->end(); ++i) - { - if (RegexNameRequest(this, *i).result == newrxengine) - { - ServerInstance->SNO->WriteGlobalSno('a', "Filter now using engine '%s'", RegexEngine.c_str()); - rxengine = *i; - } - } - } - if (!rxengine) - { - ServerInstance->SNO->WriteGlobalSno('a', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", RegexEngine.c_str()); - } -} - -void FilterBase::OnLoadModule(Module* mod, const std::string& name) -{ - if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression")) - { - std::string rxname = RegexNameRequest(this, mod).result; - 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->SNO->WriteGlobalSno('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("Text (spam) filtering", 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; @@ -495,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); @@ -516,17 +510,17 @@ FilterResult FilterBase::DecodeFilter(const std::string &data) return res; } -void FilterBase::OnSyncNetwork(Module* proto, void* opaque) +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, NULL, "filter", EncodeFilter(iter)); } -void FilterBase::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) +void ModuleFilter::OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata) { if ((target == NULL) && (extname == "filter")) { @@ -535,163 +529,127 @@ void FilterBase::OnDecodeMetaData(Extensible* target, const std::string &extname } } -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!"); + /* Skip ones that dont apply to us */ + if (!AppliesToMe(user, dynamic_cast(&(*index)), flgs)) + continue; - regex = RegexFactoryRequest(mymodule, rxengine, pat).Create(); - } - - 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() + 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; - - //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"); - fr = *index; - if (index != filters.begin()) - { - /* Move to head of list for efficiency */ - filters.erase(index); - filters.insert(filters.begin(), fr); - } - return &fr; - } - //ServerInstance->Logs->Log("m_filter", DEBUG, "NO MATCH"); + 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) - { - delete i->regex; - 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) - { - ConfigReader MyConf; - FilterBase::OnRehash(user); - 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 ModResult 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+" :"+RegexEngine+":"+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 MOD_RES_PASSTHRU; } -}; + return MOD_RES_PASSTHRU; +} MODULE_INIT(ModuleFilter)