]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_kicknorejoin.cpp
Works with the m_testclient test program/suite!
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
index 2387abb3efd281b068efd06d604fa6492f5945c9..80e1fb8d0ecd1f58f3e533f92c43f216fd03cb20 100644 (file)
@@ -6,6 +6,7 @@
 #include "channels.h"
 #include "modules.h"
 #include "helperfuncs.h"
+#include "inspircd.h"
 
 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
 
@@ -19,9 +20,42 @@ inline int strtoint(const std::string &str)
 
 typedef std::map<userrec*, time_t> delaylist;
 
+class KickRejoin : public ModeHandler
+{
+ public:
+       KickRejoin() : ModeHandler('J', 1, 0, false, MODETYPE_CHANNEL, false) { }
+
+       ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
+       {
+               if (!adding)
+               {
+                       // Taking the mode off, we need to clean up.
+                       delaylist* dl;
+
+                       if (channel->GetExt("norejoinusers", dl))
+                       {
+                               DELETE(dl);
+                               channel->Shrink("norejoinusers");
+                       }
+               }
+               if ((!adding) || (atoi(parameter.c_str()) > 0))
+               {
+                       parameter = ConvToStr(atoi(parameter.c_str()));
+                       channel->SetModeParam('J', parameter.c_str(), adding);
+                       channel->SetMode('J', adding);
+                       return MODEACTION_ALLOW;
+               }
+               else
+               {
+                       return MODEACTION_DENY;
+               }
+       }
+};
+
 class ModuleKickNoRejoin : public Module
 {
        Server *Srv;
+       KickRejoin* kr;
        
 public:
  
@@ -29,41 +63,16 @@ public:
                : Module::Module(Me)
        {
                Srv = Me;
-               
-               Srv->AddExtendedMode('J', MT_CHANNEL, false, 1, 0);
-       }
-       
-       virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
-       {
-               if ((modechar == 'J') && (type == MT_CHANNEL))
-               {
-                       if (!mode_on)
-                       {
-                               // Taking the mode off, we need to clean up.
-                               chanrec* c = (chanrec*)target;
-                               
-                               delaylist* dl = (delaylist*)c->GetExt("norejoinusers");
-                               
-                               if (dl)
-                               {
-                                       delete dl;
-                                       c->Shrink("norejoinusers");
-                               }
-                       }
-                       /* Don't allow negative or 0 +J value */
-                       return ((!mode_on) || (atoi(params[0].c_str()) > 0));
-               }
-               return 0;
+               kr = new KickRejoin();
+               Srv->AddMode(kr, 'J');
        }
 
        virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
        {
                if (chan)
                {
-                       delaylist* dl = (delaylist*)chan->GetExt("norejoinusers");
-                       log(DEBUG, "m_kicknorejoin.so: tried to grab delay list");
-                       
-                       if (dl)
+                       delaylist* dl;
+                       if (chan->GetExt("norejoinusers", dl))
                        {
                                log(DEBUG, "m_kicknorejoin.so: got delay list, iterating over it");
                                std::vector<userrec*> itemstoremove;
@@ -95,24 +104,23 @@ public:
                                if (!dl->size())
                                {
                                        // Now it's empty..
-                                       delete dl;
-                                       chan->Shrink("norejoinusers");
+                                               DELETE(dl);
+                                               chan->Shrink("norejoinusers");
                                }
                        }
                }
                return 0;
        }
-       
-       virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason)
+               
+       virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
        {
-               if (chan->IsCustomModeSet('J') && (source != user))
+               if (chan->IsModeSet('J') && (source != user))
                {
-                       delaylist* dl = (delaylist*)chan->GetExt("norejoinusers");
-                       
-                       if (!dl)
+                       delaylist* dl;
+                       if (!chan->GetExt("norejoinusers", dl))
                        {
                                dl = new delaylist;
-                               chan->Extend("norejoinusers", (char*)dl);
+                               chan->Extend("norejoinusers", dl);
                        }
                        
                        log(DEBUG, "m_kicknorejoin.so: setting record for %s, %d second delay", user->nick, strtoint(chan->GetModeParameter('J')));
@@ -122,11 +130,11 @@ public:
        
        virtual void OnChannelDelete(chanrec* chan)
        {
-               delaylist* dl = (delaylist*)chan->GetExt("norejoinusers");
-               
-               if (dl)
+               delaylist* dl;
+                       
+               if (chan->GetExt("norejoinusers", dl))
                {
-                       delete dl;
+                       DELETE(dl);
                        chan->Shrink("norejoinusers");
                }
        }
@@ -139,7 +147,7 @@ public:
 
        virtual void Implements(char* List)
        {
-               List[I_OnCleanup] = List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;
+               List[I_OnCleanup] = List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1;
        }
 
        virtual void On005Numeric(std::string &output)
@@ -149,6 +157,7 @@ public:
 
        virtual ~ModuleKickNoRejoin()
        {
+               DELETE(kr);
        }
        
        virtual Version GetVersion()