]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_filter.cpp
Merge pull request #92 from Robby-/insp20-headers
[user/henk/code/inspircd.git] / src / modules / m_filter.cpp
index 94dc2f134e755480db417e45f8911a2d7677c109..d2cffdb5dd8e712ae56a68846d0e424210b62704 100644 (file)
@@ -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 <danieldg@inspircd.org>
+ *   Copyright (C) 2004, 2008 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
  *
- * 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 <http://www.gnu.org/licenses/>.
  */
 
+
 #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,137 +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 = "<filter-definition> <action> <flags> [<gline-duration>] :<reason>";
        }
+       CmdResult Handle(const std::vector<std::string>&, 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<std::string>& parameters)
+       {
+               return ROUTE_BROADCAST;
+       }
+};
+
+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 CommandFilter;
 
-class FilterBase : public Module
+class ModuleFilter : public Module
 {
-       CommandFilter* filtcommand;
+ public:
+       CommandFilter filtcommand;
+       dynamic_reference<RegexFactory> RegexEngine;
+
+       std::vector<ImplFilter> filters;
+       const char *error;
+       int erroffset;
        int flags;
-protected:
+
        std::vector<std::string> 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<bool, std::string> 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 &parameter);
-       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<bool, std::string> 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<std::string> &parameters, 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<std::string> &parameters, 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<std::string> &parameters, User *user)
 {
-       FilterBase* Base;
- public:
-       CommandFilter(FilterBase* f, InspIRCd* Me, const std::string &ssource) : Command(Me, "FILTER", "o", 1, 5), Base(f)
+       if (parameters.size() == 1)
        {
-               this->source = ssource;
-               this->syntax = "<filter-definition> <action> <flags> [<gline-duration>] :<reason>";
-       }
-
-       CmdResult Handle(const std::vector<std::string> &parameters, User *user)
-       {
-               if (parameters.size() == 1)
+               /* Deleting a filter */
+               Module *me = creator;
+               if (static_cast<ModuleFilter *>(me)->DeleteFilter(parameters[0]))
                {
-                       /* Deleting a filter */
-                       if (Base->DeleteFilter(parameters[0]))
-                       {
-                               user->WriteServ("NOTICE %s :*** Removed filter '%s'", user->nick.c_str(), parameters[0].c_str());
-                               ServerInstance->SNO->WriteGlobalSno('a', std::string("FILTER: ")+user->nick+" removed filter '"+parameters[0]+"'");
-                               return CMD_SUCCESS;
-                       }
-                       else
-                       {
-                               user->WriteServ("NOTICE %s :*** Filter '%s' not found in list, try /stats s.", 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(), type.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 (type == "gline")
+                       {
+                               if (parameters.size() >= 5)
                                {
-                                       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;
-                                       }
+                                       duration = ServerInstance->Duration(parameters[3]);
+                                       reason = parameters[4];
                                }
                                else
                                {
-                                       reason = parameters[3];
+                                       this->TooFewParams(user, ": When setting a gline type filter, a gline duration must be specified as the third parameter.");
+                                       return CMD_FAILURE;
                                }
-                               std::pair<bool, std::string> result = Base->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());
+                       }
+                       else
+                       {
+                               reason = parameters[3];
+                       }
+                       
+                       Module *me = creator;
+                       std::pair<bool, std::string> result = static_cast<ModuleFilter *>(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->WriteGlobalSno('a', std::string("FILTER: ")+user->nick+" added filter '"+freeform+"', type '"+type+"', "+(duration ? "duration "+parameters[3]+", " : "")+"flags '"+flags+"', reason: "+reason);
+                               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;
-                               }
+                               return CMD_SUCCESS;
                        }
                        else
                        {
-                               this->TooFewParams(user, ".");
+                               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;
@@ -235,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)
@@ -278,7 +313,7 @@ int FilterBase::OnUserPreNotice(User* user,void* dest,int target_type, std::stri
                        Channel* t = (Channel*)dest;
                        target = std::string(t->name);
                        std::vector<std::string>::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")
                {
@@ -301,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();
@@ -311,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<std::string> &parameters, User *user, bool validated, const std::string &original_line)
+ModResult ModuleFilter::OnPreCommand(std::string &command, std::vector<std::string> &parameters, LocalUser *user, bool validated, const std::string &original_line)
 {
        flags = 0;
        if (validated && IS_LOCAL(user))
@@ -329,7 +364,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &par
                {
                        /* QUIT with no reason: nothing to do */
                        if (parameters.size() < 1)
-                               return 0;
+                               return MOD_RES_PASSTHRU;
 
                        checkline = parameters[0];
                        replacepoint = 0;
@@ -340,10 +375,10 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &par
                {
                        /* PART with no reason: nothing to do */
                        if (parameters.size() < 2)
-                               return 0;
+                               return MOD_RES_PASSTHRU;
 
                        std::vector<std::string>::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;
@@ -351,7 +386,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &par
                }
                else
                        /* We're only messing with PART and QUIT */
-                       return 0;
+                       return MOD_RES_PASSTHRU;
 
                FilterResult* f = NULL;
 
@@ -360,7 +395,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &par
 
                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);
@@ -377,7 +412,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &par
                        if ((f->action == "block") || (((!parting) && (f->action == "kill"))) || (f->action == "silent"))
                        {
                                c->Handle(params, user);
-                               return 1;
+                               return MOD_RES_DENY;
                        }
                        else
                        {
@@ -390,7 +425,7 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &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();
@@ -398,17 +433,17 @@ int FilterBase::OnPreCommand(std::string &command, std::vector<std::string> &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 &parameter)
+void ModuleFilter::OnRehash(User* user)
 {
-       ConfigReader MyConf(ServerInstance);
+       ConfigReader MyConf;
        std::vector<std::string>().swap(exemptfromfilter);
        for (int index = 0; index < MyConf.Enumerate("exemptfromfilter"); ++index)
        {
@@ -417,62 +452,30 @@ void FilterBase::OnRehash(User* user, const std::string &parameter)
                        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).Send() == 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());
+               ServerInstance->SNO->WriteGlobalSno('a', "WARNING: Regex engine '%s' is not loaded - Filter functionality disabled until this is corrected.", newrxengine.c_str());
        }
+       ReadFilters(MyConf);
 }
 
-void FilterBase::OnLoadModule(Module* mod, const std::string& name)
-{
-       if (ServerInstance->Modules->ModuleHasInterface(mod, "RegularExpression"))
-       {
-               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->WriteGlobalSno('a', "Found and activated regex module '%s' for m_filter.so.", RegexEngine.c_str());
-                       ReadFilters(Config);
-               }
-       }
-}
-
-
-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;
@@ -486,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);
@@ -507,183 +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<ImplFilter>::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<FilterResult*>(&(*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<ImplFilter> filters;
-       const char *error;
-       int erroffset;
-       ImplFilter fr;
-
- public:
-       ModuleFilter(InspIRCd* Me)
-       : FilterBase(Me, "m_filter.so")
+       for (std::vector<ImplFilter>::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<ImplFilter>::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<bool, std::string> ModuleFilter::AddFilter(const std::string &freeform, const std::string &type, const std::string &reason, long duration, const std::string &flgs)
+{
+       for (std::vector<ImplFilter>::iterator i = filters.begin(); i != filters.end(); i++)
        {
-               for (std::vector<ImplFilter>::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<FilterResult*>(&(*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<ImplFilter>::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<ImplFilter>::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<bool, std::string> 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<ImplFilter>::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 &parameter)
-       {
-               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<ImplFilter>::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<std::string>::iterator i = exemptfromfilter.begin(); i != exemptfromfilter.end(); ++i)
                {
-                       std::string sn = ServerInstance->Config->ServerName;
-                       for (std::vector<ImplFilter>::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<std::string>::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)