]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_kicknorejoin.cpp
None of the modules use an extern InspIRCd* any more
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
index e134ee3176c79ad17aa7c13cee6823c96745e631..9ad952f11ff30db1e968c2520d4a51d4918683a3 100644 (file)
@@ -6,9 +6,12 @@
 #include "channels.h"
 #include "modules.h"
 #include "helperfuncs.h"
+#include "inspircd.h"
 
 /* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
 
+
+
 inline int strtoint(const std::string &str)
 {
        std::istringstream ss(str);
@@ -19,50 +22,73 @@ inline int strtoint(const std::string &str)
 
 typedef std::map<userrec*, time_t> delaylist;
 
-class ModuleKickNoRejoin : public Module
+class KickRejoin : public ModeHandler
 {
-       Server *Srv;
-       
-public:
-       ModuleKickNoRejoin(Server* Me)
-               : Module::Module(Me)
+ public:
+       KickRejoin(InspIRCd* Instance) : ModeHandler(Instance, 'J', 1, 0, false, MODETYPE_CHANNEL, false) { }
+
+        ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
+        {
+                if (channel->IsModeSet('J'))
+                        return std::make_pair(true, channel->GetModeParameter('J'));
+                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, chanrec* channel)
        {
-               Srv = Me;
-               
-               Srv->AddExtendedMode('J', MT_CHANNEL, false, 1, 0);
+               /* When TS is equal, the alphabetically later one wins */
+               return (their_param < our_param);
        }
        
-       virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
+       ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
        {
-               if ((modechar == 'J') && (type == MT_CHANNEL))
+               if (!adding)
                {
-                       if (!mode_on)
+                       // Taking the mode off, we need to clean up.
+                       delaylist* dl;
+
+                       if (channel->GetExt("norejoinusers", dl))
                        {
-                               // 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");
-                               }
+                               DELETE(dl);
+                               channel->Shrink("norejoinusers");
                        }
-                       return 1;
                }
-               return 0;
+               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
+{
+       
+       KickRejoin* kr;
+       
+public:
+       ModuleKickNoRejoin(InspIRCd* Me)
+               : Module::Module(Me)
+       {
+               
+               kr = new KickRejoin(ServerInstance);
+               ServerInstance->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;
@@ -76,7 +102,7 @@ public:
                                                if (iter->first == user)                                        
                                                {
                                                        log(DEBUG, "m_kicknorejoin.so: and we have the right user");
-                                                       WriteServ(user->fd, "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
+                                                       user->WriteServ( "495 %s %s :You cannot rejoin this channel yet after being kicked (+J)", user->nick, chan->name);
                                                        return 1;
                                                }
                                        }
@@ -94,24 +120,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')));
@@ -121,11 +146,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");
                }
        }
@@ -138,16 +163,17 @@ 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)
        {
-               InsertMode(output, "J", 3);
+               ServerInstance->ModeGrok->InsertMode(output, "J", 3);
        }
 
        virtual ~ModuleKickNoRejoin()
        {
+               DELETE(kr);
        }
        
        virtual Version GetVersion()
@@ -168,7 +194,7 @@ class ModuleKickNoRejoinFactory : public ModuleFactory
        {
        }
        
-       virtual Module * CreateModule(Server* Me)
+       virtual Module * CreateModule(InspIRCd* Me)
        {
                return new ModuleKickNoRejoin(Me);
        }