summaryrefslogtreecommitdiff
path: root/src/modules/m_kicknorejoin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/m_kicknorejoin.cpp')
-rw-r--r--src/modules/m_kicknorejoin.cpp173
1 files changed, 87 insertions, 86 deletions
diff --git a/src/modules/m_kicknorejoin.cpp b/src/modules/m_kicknorejoin.cpp
index a914f3869..b8a776667 100644
--- a/src/modules/m_kicknorejoin.cpp
+++ b/src/modules/m_kicknorejoin.cpp
@@ -25,49 +25,94 @@
#include "inspircd.h"
-/* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
+class KickRejoinData
+{
+ struct KickedUser
+ {
+ std::string uuid;
+ time_t expire;
+
+ KickedUser(User* user, unsigned int Delay)
+ : uuid(user->uuid)
+ , expire(ServerInstance->Time() + Delay)
+ {
+ }
+ };
+
+ typedef std::vector<KickedUser> KickedList;
+
+ mutable KickedList kicked;
+
+ public:
+ const unsigned int delay;
+
+ KickRejoinData(unsigned int Delay) : delay(Delay) { }
+
+ bool canjoin(LocalUser* user) const
+ {
+ for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
+ {
+ KickedUser& rec = *i;
+ if (rec.expire > ServerInstance->Time())
+ {
+ if (rec.uuid == user->uuid)
+ return false;
+ ++i;
+ }
+ else
+ {
+ // Expired record, remove.
+ stdalgo::vector::swaperase(kicked, i);
+ if (kicked.empty())
+ break;
+ }
+ }
+ return true;
+ }
-typedef std::map<std::string, time_t> delaylist;
+ void add(User* user)
+ {
+ // One user can be in the list multiple times if the user gets kicked, force joins
+ // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
+ // works correctly in this case as well
+ kicked.push_back(KickedUser(user, delay));
+ }
+};
/** Handles channel mode +J
*/
-class KickRejoin : public ModeHandler
+class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
{
+ const unsigned int max;
public:
- unsigned int max;
- SimpleExtItem<delaylist> ext;
KickRejoin(Module* Creator)
- : ModeHandler(Creator, "kicknorejoin", 'J', PARAM_SETONLY, MODETYPE_CHANNEL)
- , ext("norejoinusers", Creator)
+ : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
+ , max(60)
{
}
- ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding)
+ ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
{
- if (adding)
- {
- int v = ConvToInt(parameter);
- if (v <= 0)
- return MODEACTION_DENY;
- if (parameter == channel->GetModeParameter(this))
- return MODEACTION_DENY;
+ int v = ConvToInt(parameter);
+ if (v <= 0)
+ return MODEACTION_DENY;
- if ((IS_LOCAL(source) && ((unsigned int)v > max)))
- v = max;
+ if ((IS_LOCAL(source) && ((unsigned int)v > max)))
+ v = max;
- parameter = ConvToStr(v);
- channel->SetModeParam(this, parameter);
- }
- else
- {
- if (!channel->IsModeSet(this))
- return MODEACTION_DENY;
-
- ext.unset(channel);
- channel->SetModeParam(this, "");
- }
+ ext.set(channel, new KickRejoinData(v));
return MODEACTION_ALLOW;
}
+
+ void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
+ {
+ out.append(ConvToStr(krd->delay));
+ }
+
+ std::string GetModuleSettings() const
+ {
+ return ConvToStr(max);
+ }
};
class ModuleKickNoRejoin : public Module
@@ -75,85 +120,41 @@ class ModuleKickNoRejoin : public Module
KickRejoin kr;
public:
-
ModuleKickNoRejoin()
: kr(this)
{
}
- void init()
- {
- ServerInstance->Modules->AddService(kr);
- ServerInstance->Modules->AddService(kr.ext);
- Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick, I_OnRehash };
- ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
- OnRehash(NULL);
- }
-
- void OnRehash(User* user)
- {
- kr.max = ServerInstance->Duration(ServerInstance->Config->ConfValue("kicknorejoin")->getString("maxtime"));
- if (!kr.max)
- kr.max = 30*60;
- }
-
- ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
+ ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
{
if (chan)
{
- delaylist* dl = kr.ext.get(chan);
- if (dl)
+ const KickRejoinData* data = kr.ext.get(chan);
+ if ((data) && (!data->canjoin(user)))
{
- for (delaylist::iterator iter = dl->begin(); iter != dl->end(); )
- {
- if (iter->second > ServerInstance->Time())
- {
- if (iter->first == user->uuid)
- {
- std::string modeparam = chan->GetModeParameter(&kr);
- user->WriteNumeric(ERR_DELAYREJOIN, "%s %s :You must wait %s seconds after being kicked to rejoin (+J)",
- user->nick.c_str(), chan->name.c_str(), modeparam.c_str());
- return MOD_RES_DENY;
- }
- ++iter;
- }
- else
- {
- // Expired record, remove.
- dl->erase(iter++);
- }
- }
-
- if (dl->empty())
- kr.ext.unset(chan);
+ user->WriteNumeric(ERR_DELAYREJOIN, "%s :You must wait %u seconds after being kicked to rejoin (+J)", chan->name.c_str(), data->delay);
+ return MOD_RES_DENY;
}
}
return MOD_RES_PASSTHRU;
}
- void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts)
+ void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
{
- if (memb->chan->IsModeSet(&kr) && (IS_LOCAL(memb->user)) && (source != memb->user))
+ if ((!IS_LOCAL(memb->user)) || (source == memb->user))
+ return;
+
+ KickRejoinData* data = kr.ext.get(memb->chan);
+ if (data)
{
- delaylist* dl = kr.ext.get(memb->chan);
- if (!dl)
- {
- dl = new delaylist;
- kr.ext.set(memb->chan, dl);
- }
- (*dl)[memb->user->uuid] = ServerInstance->Time() + ConvToInt(memb->chan->GetModeParameter(&kr));
+ data->add(memb->user);
}
}
- ~ModuleKickNoRejoin()
+ Version GetVersion() CXX11_OVERRIDE
{
- }
-
- Version GetVersion()
- {
- return Version("Channel mode to delay rejoin after kick", VF_VENDOR);
+ return Version("Channel mode to delay rejoin after kick", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
}
};
-
MODULE_INIT(ModuleKickNoRejoin)