#include "inspircd.h"
+#include "modules/invite.h"
-/* $ModDesc: Provides channel mode +J (delay rejoin after kick) */
+enum
+{
+ // From RFC 2182.
+ ERR_UNAVAILRESOURCE = 437
+};
+
+
+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;
-typedef std::map<std::string, time_t> delaylist;
+ 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;
+ }
+
+ 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> >
{
- static const unsigned int max = 60;
+ const unsigned int max;
public:
- 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) CXX11_OVERRIDE
+ ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
{
- if (adding)
+ int v = ConvToInt(parameter);
+ if (v <= 0)
{
- int v = ConvToInt(parameter);
- if (v <= 0)
- return MODEACTION_DENY;
- if (parameter == channel->GetModeParameter(this))
- return MODEACTION_DENY;
-
- if ((IS_LOCAL(source) && ((unsigned int)v > max)))
- v = max;
-
- parameter = ConvToStr(v);
- channel->SetModeParam(this, parameter);
+ source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
+ return MODEACTION_DENY;
}
- else
- {
- if (!channel->IsModeSet(this))
- return MODEACTION_DENY;
- ext.unset(channel);
- channel->SetModeParam(this, "");
- }
+ if ((IS_LOCAL(source) && ((unsigned int)v > max)))
+ v = max;
+
+ 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
{
KickRejoin kr;
+ Invite::API invapi;
public:
ModuleKickNoRejoin()
: kr(this)
+ , invapi(this)
{
}
- void init() CXX11_OVERRIDE
- {
- ServerInstance->Modules->AddService(kr);
- ServerInstance->Modules->AddService(kr.ext);
- Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserKick };
- ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
- }
-
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) && !invapi->IsInvited(user, chan) && (!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_UNAVAILRESOURCE, chan, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J)", data->delay));
+ return MOD_RES_DENY;
}
}
return MOD_RES_PASSTHRU;
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);
}
}
Version GetVersion() CXX11_OVERRIDE
{
- 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());
}
};