]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_rline.cpp
Release v2.0.25
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
index 7f5eafd694590c3cb7448c38e7e5fc2a9ddf392f..d1ab5d9ba42cc35645ca77b54170a315f4319046 100644 (file)
@@ -1,24 +1,33 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
+ *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ *   Copyright (C) 2008 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/>.
  */
 
+
+/* $ModDesc: RLINE: Regexp user banning. */
+
 #include "inspircd.h"
 #include "m_regex.h"
 #include "xline.h"
 
 static bool ZlineOnMatch = false;
-static std::vector<ZLine *> background_zlines;
-
-/* $ModDesc: RLINE: Regexp user banning. */
+static bool added_zline = false;
 
 class RLine : public XLine
 {
@@ -32,11 +41,10 @@ class RLine : public XLine
         * @param regex Pattern to match with
         * @
         */
-       RLine(time_t s_time, long d, std::string src, std::string re, std::string regexs, dynamic_reference<RegexFactory>& rxfactory)
+       RLine(time_t s_time, long d, const std::string& src, const std::string& re, const std::string& regexs, dynamic_reference<RegexFactory>& rxfactory)
                : XLine(s_time, d, src, re, "R")
+               , matchtext(regexs)
        {
-               matchtext = regexs;
-
                /* This can throw on failure, but if it does we DONT catch it here, we catch it and display it
                 * where the object is created, we might not ALWAYS want it to output stuff to snomask x all the time
                 */
@@ -66,8 +74,18 @@ class RLine : public XLine
 
        void Apply(User* u)
        {
-               if (ZlineOnMatch) {
-                       background_zlines.push_back(new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString()));
+               if (ZlineOnMatch)
+               {
+                       ZLine* zl = new ZLine(ServerInstance->Time(), duration ? expiry - ServerInstance->Time() : 0, ServerInstance->Config->ServerName.c_str(), reason.c_str(), u->GetIPString());
+                       if (ServerInstance->XLines->AddLine(zl, NULL))
+                       {
+                               std::string timestr = ServerInstance->TimeString(zl->expiry);
+                               ServerInstance->SNO->WriteToSnoMask('x', "Z-line added due to R-line match on *@%s%s%s: %s",
+                                       zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? timestr.c_str() : "", zl->reason.c_str());
+                               added_zline = true;
+                       }
+                       else
+                               delete zl;
                }
                DefaultApply(u, "R", false);
        }
@@ -161,7 +179,8 @@ class CommandRLine : public Command
                                        else
                                        {
                                                time_t c_requires_crap = duration + ServerInstance->Time();
-                                               ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), ServerInstance->TimeString(c_requires_crap).c_str(), parameters[2].c_str());
+                                               std::string timestr = ServerInstance->TimeString(c_requires_crap);
+                                               ServerInstance->SNO->WriteToSnoMask('x', "%s added timed R-line for %s to expire on %s: %s", user->nick.c_str(), parameters[0].c_str(), timestr.c_str(), parameters[2].c_str());
                                        }
 
                                        ServerInstance->XLines->ApplyLines();
@@ -190,6 +209,9 @@ class CommandRLine : public Command
 
        RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
        {
+               if (IS_LOCAL(user))
+                       return ROUTE_LOCALONLY; // spanningtree will send ADDLINE
+
                return ROUTE_BROADCAST;
        }
 };
@@ -201,18 +223,25 @@ class ModuleRLine : public Module
        RLineFactory f;
        CommandRLine r;
        bool MatchOnNickChange;
-       std::string RegexEngine;
+       bool initing;
+       RegexFactory* factory;
 
  public:
-       ModuleRLine() : rxfactory(this, "regex"), f(rxfactory), r(this, f)
+       ModuleRLine()
+               : rxfactory(this, "regex"), f(rxfactory), r(this, f)
+               , initing(true)
+       {
+       }
+
+       void init()
        {
                OnRehash(NULL);
 
-               ServerInstance->AddCommand(&r);
+               ServerInstance->Modules->AddService(r);
                ServerInstance->XLines->RegisterFactory(&f);
 
-               Implementation eventlist[] = { I_OnUserConnect, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer };
-               ServerInstance->Modules->Attach(eventlist, this, 5);
+               Implementation eventlist[] = { I_OnUserRegister, I_OnRehash, I_OnUserPostNick, I_OnStats, I_OnBackgroundTimer, I_OnUnloadModule };
+               ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
        }
 
        virtual ~ModuleRLine()
@@ -223,10 +252,10 @@ class ModuleRLine : public Module
 
        virtual Version GetVersion()
        {
-               return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory.GetProvider());
+               return Version("RLINE: Regexp user banning.", VF_COMMON | VF_VENDOR, rxfactory ? rxfactory->name : "");
        }
 
-       virtual void OnUserConnect(LocalUser* user)
+       ModResult OnUserRegister(LocalUser* user)
        {
                // Apply lines on user connect
                XLine *rl = ServerInstance->XLines->MatchesLine("R", user);
@@ -235,25 +264,42 @@ class ModuleRLine : public Module
                {
                        // Bang. :P
                        rl->Apply(user);
+                       return MOD_RES_DENY;
                }
+               return MOD_RES_PASSTHRU;
        }
 
        virtual void OnRehash(User *user)
        {
-               ConfigReader Conf;
+               ConfigTag* tag = ServerInstance->Config->ConfValue("rline");
+
+               MatchOnNickChange = tag->getBool("matchonnickchange");
+               ZlineOnMatch = tag->getBool("zlineonmatch");
+               std::string newrxengine = tag->getString("engine");
 
-               if (!Conf.ReadFlag("rline", "zlineonmatch", 0) && ZlineOnMatch)
-                       background_zlines.clear();
+               factory = rxfactory ? (rxfactory.operator->()) : NULL;
 
-               MatchOnNickChange = Conf.ReadFlag("rline", "matchonnickchange", 0);
-               ZlineOnMatch = Conf.ReadFlag("rline", "zlineonmatch", 0);
-               std::string newrxengine = Conf.ReadValue("rline", "engine", 0);
+               if (newrxengine.empty())
+                       rxfactory.SetProvider("regex");
+               else
+                       rxfactory.SetProvider("regex/" + newrxengine);
 
-               rxfactory.SetProvider("regex/" + newrxengine);
                if (!rxfactory)
                {
-                       ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", RegexEngine.c_str());
+                       if (newrxengine.empty())
+                               ServerInstance->SNO->WriteToSnoMask('a', "WARNING: No regex engine loaded - R-Line functionality disabled until this is corrected.");
+                       else
+                               ServerInstance->SNO->WriteToSnoMask('a', "WARNING: Regex engine '%s' is not loaded - R-Line functionality disabled until this is corrected.", newrxengine.c_str());
+
+                       ServerInstance->XLines->DelAll(f.GetType());
+               }
+               else if ((!initing) && (rxfactory.operator->() != factory))
+               {
+                       ServerInstance->SNO->WriteToSnoMask('a', "Regex engine has changed, removing all R-Lines");
+                       ServerInstance->XLines->DelAll(f.GetType());
                }
+
+               initing = false;
        }
 
        virtual ModResult OnStats(char symbol, User* user, string_list &results)
@@ -284,21 +330,32 @@ class ModuleRLine : public Module
 
        virtual void OnBackgroundTimer(time_t curtime)
        {
-               if (!ZlineOnMatch) return;
-               for (std::vector<ZLine *>::iterator i = background_zlines.begin(); i != background_zlines.end(); i++)
+               if (added_zline)
                {
-                       ZLine *zl = *i;
-                       if (ServerInstance->XLines->AddLine(zl,NULL))
-                       {
-                               ServerInstance->SNO->WriteToSnoMask('x',"Z-line added due to R-line match on *@%s%s%s: %s", 
-                                       zl->ipaddr.c_str(), zl->duration ? " to expire on " : "", zl->duration ? ServerInstance->TimeString(zl->expiry).c_str() : "", zl->reason.c_str());
-                               ServerInstance->XLines->ApplyLines();
-                       }
+                       added_zline = false;
+                       ServerInstance->XLines->ApplyLines();
                }
-               background_zlines.clear();
        }
 
+       void OnUnloadModule(Module* mod)
+       {
+               // If the regex engine became unavailable or has changed, remove all rlines
+               if (!rxfactory)
+               {
+                       ServerInstance->XLines->DelAll(f.GetType());
+               }
+               else if (rxfactory.operator->() != factory)
+               {
+                       factory = rxfactory.operator->();
+                       ServerInstance->XLines->DelAll(f.GetType());
+               }
+       }
+
+       void Prioritize()
+       {
+               Module* mod = ServerInstance->Modules->Find("m_cgiirc.so");
+               ServerInstance->Modules->SetPriority(this, I_OnUserRegister, PRIORITY_AFTER, mod);
+       }
 };
 
 MODULE_INIT(ModuleRLine)
-