From 74d2f929455e316ab69fcf620b7f9bc792f5e716 Mon Sep 17 00:00:00 2001 From: brain Date: Fri, 10 Nov 2006 16:34:57 +0000 Subject: Inherit m_filter.cpp and m_filter_pcre.cpp from a base set of classes, so that changes for /filter command and gline actions etc only need to be made in one place (the base classes header) git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5665 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/modules/extra/m_filter_pcre.cpp | 114 +++++++++--------------------------- src/modules/m_filter.cpp | 76 ++++-------------------- src/modules/m_filter.h | 102 ++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+), 152 deletions(-) create mode 100644 src/modules/m_filter.h diff --git a/src/modules/extra/m_filter_pcre.cpp b/src/modules/extra/m_filter_pcre.cpp index 96956e3d8..5df48cb05 100644 --- a/src/modules/extra/m_filter_pcre.cpp +++ b/src/modules/extra/m_filter_pcre.cpp @@ -25,113 +25,61 @@ #include "channels.h" #include "modules.h" #include "inspircd.h" +#include "m_filter.h" /* $ModDesc: m_filter with regexps */ /* $CompileFlags: `pcre-config --cflags` */ /* $LinkerFlags: `pcre-config --libs` `perl extra/pcre_rpath.pl` -lpcre */ +/* $ModDep: ../m_filter.h */ -class ModuleFilterPCRE : public Module +class PCREFilter : public FilterResult { - class Filter - { - public: - pcre* regexp; - std::string reason; - std::string action; - - Filter(pcre* r, const std::string &rea, const std::string &act) - : regexp(r), reason(rea), action(act) - { - } - }; - - InspIRCd* Srv; - std::vector filters; + public: + pcre* regexp; + + PCREFilter(pcre* r, const std::string &rea, const std::string &act) + : FilterResult::FilterResult(rea, act), regexp(r) + { + } +}; + +class ModuleFilterPCRE : public FilterBase +{ + std::vector filters; pcre *re; const char *error; int erroffset; - + public: ModuleFilterPCRE(InspIRCd* Me) - : Module::Module(Me), Srv(Me) + : FilterBase::FilterBase(Me) { OnRehash(""); } - - virtual ~ModuleFilterPCRE() - { - } - void Implements(char* List) + virtual ~ModuleFilterPCRE() { - List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1; } - // format of a config entry is - - virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status) - { - return OnUserPreNotice(user,dest,target_type,text,status); - } - - virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) + virtual FilterResult* FilterMatch(const std::string &text) { for (unsigned int index = 0; index < filters.size(); index++) { - Filter& filt = filters[index]; + PCREFilter& filt = filters[index]; if (pcre_exec(filt.regexp,NULL,text.c_str(),text.length(),0,0,NULL,0) > -1) { - const char* target; - - if(filt.action.empty()) - filt.action = "none"; - - if (target_type == TYPE_USER) - { - userrec* t = (userrec*)dest; - target = t->nick; - } - else if (target_type == TYPE_CHANNEL) - { - chanrec* t = (chanrec*)dest; - target = t->name; - } - else - { - target = ""; - } - - ServerInstance->Log(DEFAULT, "Filter: %s had their notice filtered, target was %s: %s Action: %s", user->nick, target, filt.reason.c_str(), filt.action.c_str()); - - if (filt.action == "block") - { - Srv->WriteOpers("Filter: %s had their notice filtered, target was %s: %s", user->nick, target, filt.reason.c_str()); - user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+filt.reason); - } - else if (filt.action == "kill") - { - userrec::QuitUser(Srv, user, filt.reason); - } - - return 1; + return &filt; } } - return 0; + return NULL; } virtual void OnRehash(const std::string ¶meter) - { - /* Read the configuration file on startup and rehash. - * It is perfectly valid to set to the value of the - * main config file, then append your tags to the bottom - * of the main config... but rather messy. That's why the capability - * of using a seperate config file is provided. - */ - - ConfigReader MyConf(Srv); + { + ConfigReader MyConf(ServerInstance); - for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) + for (std::vector::iterator i = filters.begin(); i != filters.end(); i++) pcre_free((*i).regexp); filters.clear(); @@ -151,21 +99,13 @@ class ModuleFilterPCRE : public Module } else { - filters.push_back(Filter(re, reason, action)); + filters.push_back(PCREFilter(re, reason, action)); ServerInstance->Log(DEFAULT,"Regular expression %s loaded.", pattern.c_str()); } } } - - virtual Version GetVersion() - { - /* Version 1.x is the unreleased unrealircd module */ - return Version(3,2,1,0,VF_VENDOR,API_VERSION); - } - }; - -// stuff down here is the module-factory stuff. For basic modules you can ignore this. + class ModuleFilterPCREFactory : public ModuleFactory { diff --git a/src/modules/m_filter.cpp b/src/modules/m_filter.cpp index 38cd32ecd..38b2ee7e1 100644 --- a/src/modules/m_filter.cpp +++ b/src/modules/m_filter.cpp @@ -26,37 +26,27 @@ using namespace std; #include "channels.h" #include "modules.h" #include "inspircd.h" +#include "m_filter.h" -/* $ModDesc: An enhanced version of the unreal m_filter.so used by chatspike.net */ +/* $ModDesc: An advanced spam filtering module */ +/* $ModDep: m_filter.h */ +typedef std::map filter_t; - -/** Holds a filter pattern and reason - */ -class Filter : public classbase -{ - public: - std::string reason; - std::string action; -}; - -typedef std::map filter_t; - -class ModuleFilter : public Module +class ModuleFilter : public FilterBase { filter_t filters; - + public: ModuleFilter(InspIRCd* Me) - : Module::Module(Me) + : FilterBase::FilterBase(Me) { // read the configuration file on startup. // it is perfectly valid to set to the value of the // main config file, then append your tags to the bottom // of the main config... but rather messy. That's why the capability // of using a seperate config file is provided. - OnRehash(""); } @@ -64,54 +54,17 @@ class ModuleFilter : public Module { } - void Implements(char* List) - { - List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1; - } - - // format of a config entry is - - virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status) - { - return OnUserPreNotice(user,dest,target_type,text,status); - } - - virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) + virtual FilterResult* FilterMatch(const std::string &text) { std::string text2 = text+" "; for (filter_t::iterator index = filters.begin(); index != filters.end(); index++) { if ((ServerInstance->MatchText(text2,index->first)) || (ServerInstance->MatchText(text,index->first))) { - Filter* f = (Filter*)index->second; - std::string target = ""; - - if (target_type == TYPE_USER) - { - userrec* t = (userrec*)dest; - target = std::string(t->nick); - } - else if (target_type == TYPE_CHANNEL) - { - chanrec* t = (chanrec*)dest; - target = std::string(t->name); - } - - if (f->action == "block") - { - ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason); - user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason); - } - ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action); - - if (f->action == "kill") - { - userrec::QuitUser(ServerInstance,user,f->reason); - } - return 1; + return index->second; } } - return 0; + return NULL; } virtual void OnRehash(const std::string ¶meter) @@ -130,20 +83,13 @@ class ModuleFilter : public Module std::string do_action = MyConf->ReadValue("keyword","action",index); if (do_action == "") do_action = "none"; - Filter* x = new Filter; + FilterResult* x = new FilterResult; x->reason = reason; x->action = do_action; filters[pattern] = x; } DELETE(MyConf); } - - virtual Version GetVersion() - { - // This is version 2 because version 1.x is the unreleased unrealircd module - return Version(2,1,0,2,VF_VENDOR,API_VERSION); - } - }; // stuff down here is the module-factory stuff. For basic modules you can ignore this. diff --git a/src/modules/m_filter.h b/src/modules/m_filter.h new file mode 100644 index 000000000..34e3d2513 --- /dev/null +++ b/src/modules/m_filter.h @@ -0,0 +1,102 @@ +/* +------------------------------------+ + * | Inspire Internet Relay Chat Daemon | + * +------------------------------------+ + * + * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. + * E-mail: + * + * + * + * Written by Craig Edwards, Craig McLure, and others. + * This program is free but copyrighted software; see + * the file COPYING for details. + * + * --------------------------------------------------- + */ + +class FilterResult : public classbase +{ + public: + std::string reason; + std::string action; + + FilterResult(const std::string &rea, const std::string &act) : reason(rea), action(act) + { + } + + FilterResult() + { + } + + virtual ~FilterResult() + { + } +}; + +class FilterBase : public Module +{ + public: + FilterBase(InspIRCd* Me) + : Module::Module(Me) + { + } + + virtual ~FilterBase() + { + } + + virtual void Implements(char* List) + { + List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1; + } + + virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status) + { + return OnUserPreNotice(user,dest,target_type,text,status); + } + + /* This must be implemented by the module which uses the header */ + virtual FilterResult* FilterMatch(const std::string &text) = 0; + + virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status) + { + FilterResult* f = this->FilterMatch(text); + if (f) + { + std::string target = ""; + if (target_type == TYPE_USER) + { + userrec* t = (userrec*)dest; + target = std::string(t->nick); + } + else if (target_type == TYPE_CHANNEL) + { + chanrec* t = (chanrec*)dest; + target = std::string(t->name); + } + if (f->action == "block") + { + ServerInstance->WriteOpers(std::string("FILTER: ")+user->nick+" had their notice filtered, target was "+target+": "+f->reason); + user->WriteServ("NOTICE "+std::string(user->nick)+" :Your notice has been filtered and opers notified: "+f->reason); + } + ServerInstance->Log(DEFAULT,"FILTER: "+std::string(user->nick)+std::string(" had their notice filtered, target was ")+target+": "+f->reason+" Action: "+f->action); + + if (f->action == "kill") + { + userrec::QuitUser(ServerInstance,user,f->reason); + } + return 1; + } + return 0; + } + + virtual void OnRehash(const std::string ¶meter) + { + } + + virtual Version GetVersion() + { + // This is version 2 because version 1.x is the unreleased unrealircd module + return Version(1,1,0,2,VF_VENDOR,API_VERSION); + } +}; -- cgit v1.2.3