]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_nickflood.cpp
Include explicit routing information in Command, will replace CMD_LOCALONLY return...
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
index 483cf2db1cd920c7cd6e0f67e5ffd93a4a401670..edb5aff3ecfc1191944324811f1fece72234b53b 100644 (file)
@@ -2,8 +2,8 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  */
 class nickfloodsettings : public classbase
 {
+ private:
+       InspIRCd* ServerInstance;
  public:
-
        int secs;
        int nicks;
        time_t reset;
        time_t unlocktime;
        int counter;
        bool locked;
-       InspIRCd* ServerInstance;
-
-       nickfloodsettings() : secs(0), nicks(0) {};
 
-       nickfloodsettings(int b, int c) : secs(b), nicks(c)
+       nickfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), nicks(c)
        {
-               reset = time(NULL) + secs;
+               reset = Instance->Time() + secs;
                counter = 0;
                locked = false;
        };
@@ -41,16 +39,20 @@ class nickfloodsettings : public classbase
        void addnick()
        {
                counter++;
-               if (time(NULL) > reset)
+               if (ServerInstance->Time() > reset)
                {
                        counter = 0;
-                       reset = time(NULL) + secs;
+                       reset = ServerInstance->Time() + secs;
                }
        }
 
        bool shouldlock()
        {
-               return (counter >= this->nicks);
+               /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
+                * on successful nick changes; this will be checked before the counter is
+                * incremented.
+                */
+               return (counter + 1 >= this->nicks);
        }
 
        void clear()
@@ -62,7 +64,7 @@ class nickfloodsettings : public classbase
        {
                if (locked)
                {
-                       if (time(NULL) > unlocktime)
+                       if (ServerInstance->Time() > unlocktime)
                        {
                                locked = false;
                                return false;
@@ -78,7 +80,7 @@ class nickfloodsettings : public classbase
        void lock()
        {
                locked = true;
-               unlocktime = time(NULL) + 60;
+               unlocktime = ServerInstance->Time() + 60;
        }
 
 };
@@ -97,15 +99,9 @@ class NickFlood : public ModeHandler
                        return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
                else
                        return std::make_pair(false, parameter);
-       } 
-
-       bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
-       {
-               /* When TS is equal, the alphabetically later one wins */
-               return (their_param < our_param);
        }
 
-       ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
+       ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
        {
                nickfloodsettings* dummy;
 
@@ -135,7 +131,7 @@ class NickFlood : public ModeHandler
                                int nsecs = atoi(secs);
                                if ((nnicks<1) || (nsecs<1))
                                {
-                                       source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
+                                       source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
                                        parameter.clear();
                                        return MODEACTION_DENY;
                                }
@@ -144,10 +140,9 @@ class NickFlood : public ModeHandler
                                        if (!channel->GetExt("nickflood", dummy))
                                        {
                                                parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
-                                               nickfloodsettings *f = new nickfloodsettings(nsecs,nnicks);
+                                               nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
                                                channel->Extend("nickflood", f);
-                                               channel->SetMode('F', true);
-                                               channel->SetModeParam('F', parameter.c_str(), true);
+                                               channel->SetModeParam('F', parameter);
                                                return MODEACTION_ALLOW;
                                        }
                                        else
@@ -167,11 +162,10 @@ class NickFlood : public ModeHandler
                                                                nickfloodsettings* f;
                                                                channel->GetExt("nickflood", f);
                                                                delete f;
-                                                               f = new nickfloodsettings(nsecs, nnicks);
+                                                               f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
                                                                channel->Shrink("nickflood");
                                                                channel->Extend("nickflood", f);
-                                                               channel->SetModeParam('F', cur_param.c_str(), false);
-                                                               channel->SetModeParam('F', parameter.c_str(), true);
+                                                               channel->SetModeParam('F', parameter);
                                                                return MODEACTION_ALLOW;
                                                        }
                                                        else
@@ -184,7 +178,7 @@ class NickFlood : public ModeHandler
                        }
                        else
                        {
-                               source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
+                               source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
                                return MODEACTION_DENY;
                        }
                }
@@ -196,7 +190,7 @@ class NickFlood : public ModeHandler
                                channel->GetExt("nickflood", f);
                                delete f;
                                channel->Shrink("nickflood");
-                               channel->SetMode('F', false);
+                               channel->SetModeParam('F', "");
                                return MODEACTION_ALLOW;
                        }
                }
@@ -206,19 +200,17 @@ class NickFlood : public ModeHandler
 
 class ModuleNickFlood : public Module
 {
-       NickFlood* jf;
-       
+       NickFlood jf;
+
  public:
+
        ModuleNickFlood(InspIRCd* Me)
-               : Module(Me)
+               : Module(Me), jf(Me)
        {
-               
-               jf = new NickFlood(ServerInstance);
-               if (!ServerInstance->Modes->AddMode(jf))
+               if (!ServerInstance->Modes->AddMode(&jf))
                        throw ModuleException("Could not add new modes!");
-               Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
-               ServerInstance->Modules->Attach(eventlist, this, 2);
+               Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick, I_OnUserPostNick };
+               ServerInstance->Modules->Attach(eventlist, this, 3);
        }
 
        virtual int OnUserPreNick(User* user, const std::string &newnick)
@@ -238,16 +230,15 @@ class ModuleNickFlood : public Module
 
                                if (f->islocked())
                                {
-                                       user->WriteServ("447 %s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick, channel->name, f->nicks, f->secs);
+                                       user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
                                        return 1;
                                }
 
-                               f->addnick();
                                if (f->shouldlock())
                                {
                                        f->clear();
                                        f->lock();
-                                       channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name, f->nicks, f->secs);
+                                       channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name.c_str(), f->nicks, f->secs);
                                        return 1;
                                }
                        }
@@ -255,7 +246,35 @@ class ModuleNickFlood : public Module
 
                return 0;
        }
-       
+
+       /*
+        * XXX: HACK: We do the increment on the *POST* event here (instead of all together) because we have no way of knowing whether other modules would block a nickchange.
+        */
+       virtual void OnUserPostNick(User* user, const std::string &oldnick)
+       {
+               if (isdigit(user->nick[0])) /* allow switches to UID */
+                       return;
+
+               for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
+               {
+                       Channel *channel = i->first;
+
+                       nickfloodsettings *f;
+                       if (channel->GetExt("nickflood", f))
+                       {
+                               if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
+                                       return;
+                               
+                               /* moved this here to avoid incrementing the counter for nick
+                                * changes that are denied for some other reason (bans, +N, etc.)
+                                * per bug #874.
+                                */
+                               f->addnick();
+                       }
+               }
+               return;
+       }
+
        void OnChannelDelete(Channel* chan)
        {
                nickfloodsettings *f;
@@ -269,13 +288,12 @@ class ModuleNickFlood : public Module
 
        virtual ~ModuleNickFlood()
        {
-               ServerInstance->Modes->DelMode(jf);
-               delete jf;
+               ServerInstance->Modes->DelMode(&jf);
        }
-       
+
        virtual Version GetVersion()
        {
-               return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
+               return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
        }
 };