X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_kicknorejoin.cpp;h=87e8646ec47e2eb1dc1db77f23bb1dd91504f857;hb=91e0af0fc4889f20d2f63426f8fe379674fc0393;hp=e96e421850f44e9de9257afeafe0b8bcbdc5957b;hpb=034368624cbe5923a1df069825fe1e65fbf00895;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_kicknorejoin.cpp b/src/modules/m_kicknorejoin.cpp index e96e42185..1bc11948c 100644 --- a/src/modules/m_kicknorejoin.cpp +++ b/src/modules/m_kicknorejoin.cpp @@ -1,196 +1,160 @@ -#include -#include -#include -#include -#include "users.h" -#include "channels.h" -#include "modules.h" -#include "helperfuncs.h" -#include "inspircd.h" - -/* $ModDesc: Provides channel mode +J (delay rejoin after kick) */ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2008 Pippijn van Steenhoven + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006-2007 Robin Burchell + * Copyright (C) 2006 John Brooks + * Copyright (C) 2006 Craig Edwards + * Copyright (C) 2006 Oliver Lupton + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ -inline int strtoint(const std::string &str) -{ - std::istringstream ss(str); - int result; - ss >> result; - return result; -} -typedef std::map delaylist; +#include "inspircd.h" -class KickRejoin : public ModeHandler +class KickRejoinData { - public: - KickRejoin() : ModeHandler('J', 1, 0, false, MODETYPE_CHANNEL, false) { } - - ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding) + struct KickedUser { - if (!adding) - { - // Taking the mode off, we need to clean up. - delaylist* dl = (delaylist*)channel->GetExt("norejoinusers"); + std::string uuid; + time_t expire; - if (dl) - { - DELETE(dl); - channel->Shrink("norejoinusers"); - } - } - if ((!adding) || (atoi(parameter.c_str()) > 0)) + KickedUser(User* user, unsigned int Delay) + : uuid(user->uuid) + , expire(ServerInstance->Time() + Delay) { - 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: - - ModuleKickNoRejoin(Server* Me) - : Module::Module(Me) - { - Srv = Me; - kr = new KickRejoin(); - Srv->AddMode(kr, 'J'); - } + typedef std::vector KickedList; + + mutable KickedList kicked; + + public: + const unsigned int delay; - virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) + KickRejoinData(unsigned int Delay) : delay(Delay) { } + + bool canjoin(LocalUser* user) const { - if (chan) + for (KickedList::iterator i = kicked.begin(); i != kicked.end(); ) { - delaylist* dl = (delaylist*)chan->GetExt("norejoinusers"); - log(DEBUG, "m_kicknorejoin.so: tried to grab delay list"); - - if (dl) + KickedUser& rec = *i; + if (rec.expire > ServerInstance->Time()) { - log(DEBUG, "m_kicknorejoin.so: got delay list, iterating over it"); - std::vector itemstoremove; - - for (delaylist::iterator iter = dl->begin(); iter != dl->end(); iter++) - { - log(DEBUG, "m_kicknorejoin.so:\t[%s] => %d", iter->first->nick, iter->second); - if (iter->second > time(NULL)) - { - log(DEBUG, "m_kicknorejoin.so: still inside time slot"); - 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); - return 1; - } - } - else - { - // Expired record, remove. - log(DEBUG, "m_kicknorejoin.so: record expired"); - itemstoremove.push_back(iter->first); - } - } - - for (unsigned int i = 0; i < itemstoremove.size(); i++) - dl->erase(itemstoremove[i]); - - if (!dl->size()) - { - // Now it's empty.. - DELETE(dl); - chan->Shrink("norejoinusers"); - } - } + if (rec.uuid == user->uuid) + return false; + ++i; } - return 0; - } - - virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) - { - if (chan->IsModeSet('J') && (source != user)) + else { - delaylist* dl = (delaylist*)chan->GetExt("norejoinusers"); - - if (!dl) - { - dl = new delaylist; - chan->Extend("norejoinusers", (char*)dl); - } - - log(DEBUG, "m_kicknorejoin.so: setting record for %s, %d second delay", user->nick, strtoint(chan->GetModeParameter('J'))); - (*dl)[user] = time(NULL) + strtoint(chan->GetModeParameter('J')); + // Expired record, remove. + stdalgo::vector::swaperase(kicked, i); + if (kicked.empty()) + break; } } - - virtual void OnChannelDelete(chanrec* chan) - { - delaylist* dl = (delaylist*)chan->GetExt("norejoinusers"); - - if (dl) - { - DELETE(dl); - chan->Shrink("norejoinusers"); - } + return true; } - - virtual void OnCleanup(int target_type, void* item) + + void add(User* user) { - if(target_type == TYPE_CHANNEL) - OnChannelDelete((chanrec*)item); + // 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)); } +}; - virtual void Implements(char* List) +/** Handles channel mode +J + */ +class KickRejoin : public ParamMode > +{ + const unsigned int max; + public: + KickRejoin(Module* Creator) + : ParamMode >(Creator, "kicknorejoin", 'J') + , max(60) { - List[I_OnCleanup] = List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserKick] = 1; } - virtual void On005Numeric(std::string &output) + ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE { - InsertMode(output, "J", 3); + int v = ConvToInt(parameter); + if (v <= 0) + return MODEACTION_DENY; + + if ((IS_LOCAL(source) && ((unsigned int)v > max))) + v = max; + + ext.set(channel, new KickRejoinData(v)); + return MODEACTION_ALLOW; } - virtual ~ModuleKickNoRejoin() + void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out) { - DELETE(kr); + out.append(ConvToStr(krd->delay)); } - - virtual Version GetVersion() + + std::string GetModuleSettings() const { - return Version(1, 0, 0, 0, VF_STATIC | VF_VENDOR); + return ConvToStr(max); } }; - -class ModuleKickNoRejoinFactory : public ModuleFactory +class ModuleKickNoRejoin : public Module { - public: - ModuleKickNoRejoinFactory() + KickRejoin kr; + +public: + ModuleKickNoRejoin() + : kr(this) { } - - ~ModuleKickNoRejoinFactory() + + ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE { + if (chan) + { + const KickRejoinData* data = kr.ext.get(chan); + if ((data) && (!data->canjoin(user))) + { + user->WriteNumeric(ERR_DELAYREJOIN, chan, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J)", data->delay)); + return MOD_RES_DENY; + } + } + return MOD_RES_PASSTHRU; } - - virtual Module * CreateModule(Server* Me) + + void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE { - return new ModuleKickNoRejoin(Me); - } - -}; + if ((!IS_LOCAL(memb->user)) || (source == memb->user)) + return; + KickRejoinData* data = kr.ext.get(memb->chan); + if (data) + { + data->add(memb->user); + } + } -extern "C" void * init_module( void ) -{ - return new ModuleKickNoRejoinFactory; -} + Version GetVersion() CXX11_OVERRIDE + { + return Version("Channel mode to delay rejoin after kick", VF_VENDOR | VF_COMMON, kr.GetModuleSettings()); + } +}; +MODULE_INIT(ModuleKickNoRejoin)