]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_joinflood.cpp
Add config <options:disablehmac> to support disabling of HMAC, and tidy up to detect...
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
index 3adbb5332e2c96e0baf0a77ad61db480ffe5fc7b..ed67a880a8d6e915fecca2e07ae9958106536f18 100644 (file)
@@ -2,33 +2,27 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 
-using namespace std;
-
 #include <stdio.h>
 #include <map>
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
-#include "helperfuncs.h"
 #include "configreader.h"
 #include "inspircd.h"
 
 /* $ModDesc: Provides channel mode +j (join flood protection) */
 
-extern InspIRCd* ServerInstance;
-
+/** Holds settings and state associated with channel mode +j
+ */
 class joinfloodsettings : public classbase
 {
  public:
@@ -39,23 +33,22 @@ class joinfloodsettings : public classbase
        time_t unlocktime;
        int counter;
        bool locked;
-
+    InspIRCd* ServerInstance;
+    
        joinfloodsettings() : secs(0), joins(0) {};
+
        joinfloodsettings(int b, int c) : secs(b), joins(c)
        {
                reset = time(NULL) + secs;
                counter = 0;
                locked = false;
-               log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
        };
 
        void addjoin()
        {
                counter++;
-               log(DEBUG,"joinflood counter is %d",counter);
                if (time(NULL) > reset)
                {
-                       log(DEBUG,"joinflood counter reset");
                        counter = 0;
                        reset = time(NULL) + secs;
                }
@@ -68,7 +61,6 @@ class joinfloodsettings : public classbase
 
        void clear()
        {
-               log(DEBUG,"joinflood counter clear");
                counter = 0;
        }
 
@@ -91,13 +83,14 @@ class joinfloodsettings : public classbase
 
        void lock()
        {
-               log(DEBUG,"joinflood lock");
                locked = true;
                unlocktime = time(NULL) + 60;
        }
 
 };
 
+/** Handles channel mode +j
+ */
 class JoinFlood : public ModeHandler
 {
  public:
@@ -141,6 +134,7 @@ class JoinFlood : public ModeHandler
                                else data++;
                        }
                        if (secs)
+
                        {
                                /* Set up the flood parameters for this channel */
                                int njoins = atoi(joins);
@@ -162,6 +156,36 @@ class JoinFlood : public ModeHandler
                                                channel->SetModeParam('j', parameter.c_str(), true);
                                                return MODEACTION_ALLOW;
                                        }
+                                       else
+                                       {
+                                               std::string cur_param = channel->GetModeParameter('j');
+                                               parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
+                                               if (cur_param == parameter)
+                                               {
+                                                       // mode params match
+                                                       return MODEACTION_DENY;
+                                               }
+                                               else
+                                               {
+                                                       // new mode param, replace old with new
+                                                       if ((nsecs > 0) && (njoins > 0))
+                                                       {
+                                                               joinfloodsettings* f;
+                                                               channel->GetExt("joinflood", f);
+                                                               delete f;
+                                                               f = new joinfloodsettings(nsecs,njoins);
+                                                               channel->Shrink("joinflood");
+                                                               channel->Extend("joinflood", f);
+                                                               channel->SetModeParam('j', cur_param.c_str(), false);
+                                                               channel->SetModeParam('j', parameter.c_str(), true);
+                                                               return MODEACTION_ALLOW;
+                                                       }
+                                                       else
+                                                       {
+                                                               return MODEACTION_DENY;
+                                                       }
+                                               }
+                                       }
                                }
                        }
                        else
@@ -188,20 +212,21 @@ class JoinFlood : public ModeHandler
 
 class ModuleJoinFlood : public Module
 {
-       Server *Srv;
+       
        JoinFlood* jf;
        
  public:
  
-       ModuleJoinFlood(Server* Me)
+       ModuleJoinFlood(InspIRCd* Me)
                : Module::Module(Me)
        {
-               Srv = Me;
+               
                jf = new JoinFlood(ServerInstance);
-               Srv->AddMode(jf, 'j');
+               if (!ServerInstance->AddMode(jf, 'j'))
+                       throw ModuleException("Could not add new modes!");
        }
        
-       virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
+       virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
        {
                if (chan)
                {
@@ -245,22 +270,18 @@ class ModuleJoinFlood : public Module
 
        void Implements(char* List)
        {
-               List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
-       }
-
-       virtual void On005Numeric(std::string &output)
-       {
-               ServerInstance->ModeGrok->InsertMode(output, "j", 3);
+               List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
        }
 
        virtual ~ModuleJoinFlood()
        {
+               ServerInstance->Modes->DelMode(jf);
                DELETE(jf);
        }
        
        virtual Version GetVersion()
        {
-               return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
+               return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
        }
 };
 
@@ -276,7 +297,7 @@ class ModuleJoinFloodFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule(Server* Me)
+       virtual Module * CreateModule(InspIRCd* Me)
        {
                return new ModuleJoinFlood(Me);
        }