]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_joinflood.cpp
Reasonably sized fix - when adding modes in modules, be sure to check the return...
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
index 83feb2e5962044ada836e74923850b6d7dedc5a7..1410c7d98982e728f3ff53f6632d183ccac4de8f 100644 (file)
@@ -2,20 +2,15 @@
  *       | 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"
@@ -26,8 +21,8 @@ using namespace std;
 
 /* $ModDesc: Provides channel mode +j (join flood protection) */
 
-
-
+/** Holds settings and state associated with channel mode +j
+ */
 class joinfloodsettings : public classbase
 {
  public:
@@ -38,8 +33,10 @@ 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;
@@ -92,6 +89,8 @@ class joinfloodsettings : public classbase
 
 };
 
+/** Handles channel mode +j
+ */
 class JoinFlood : public ModeHandler
 {
  public:
@@ -118,6 +117,7 @@ class JoinFlood : public ModeHandler
 
                if (adding)
                {
+                       ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
                        char ndata[MAXBUF];
                        char* data = ndata;
                        strlcpy(ndata,parameter.c_str(),MAXBUF);
@@ -135,6 +135,7 @@ class JoinFlood : public ModeHandler
                                else data++;
                        }
                        if (secs)
+
                        {
                                /* Set up the flood parameters for this channel */
                                int njoins = atoi(joins);
@@ -156,6 +157,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
@@ -192,10 +223,11 @@ class ModuleJoinFlood : public Module
        {
                
                jf = new JoinFlood(ServerInstance);
-               ServerInstance->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)
                {
@@ -244,12 +276,13 @@ class ModuleJoinFlood : public Module
 
        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);
        }
 };