X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_nickflood.cpp;h=cade0b1db751ace5a5dd7829510b0d58275094e5;hb=df4afba6c23c939af1013582c94138832017af4e;hp=ec9d9a26140d91f9b12e945f8ac02f76d73095ab;hpb=16fad76febd1e35a5e289a0890df212c1c4e3c81;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_nickflood.cpp b/src/modules/m_nickflood.cpp index ec9d9a261..cade0b1db 100644 --- a/src/modules/m_nickflood.cpp +++ b/src/modules/m_nickflood.cpp @@ -20,8 +20,6 @@ #include "inspircd.h" -/* $ModDesc: Provides channel mode +F (nick flood protection) */ - /** Holds settings and state associated with channel mode +F */ class nickfloodsettings @@ -80,53 +78,41 @@ class nickfloodsettings /** Handles channel mode +F */ -class NickFlood : public ModeHandler +class NickFlood : public ParamMode > { public: - SimpleExtItem ext; - NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL), - ext("nickflood", Creator) { } + NickFlood(Module* Creator) + : ParamMode >(Creator, "nickflood", 'F') + { + } - ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) + ModeAction OnSet(User* source, Channel* channel, std::string& parameter) { - if (adding) + std::string::size_type colon = parameter.find(':'); + if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos)) { - std::string::size_type colon = parameter.find(':'); - if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos)) - { - source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str()); - return MODEACTION_DENY; - } - - /* Set up the flood parameters for this channel */ - unsigned int nnicks = ConvToInt(parameter.substr(0, colon)); - unsigned int nsecs = ConvToInt(parameter.substr(colon+1)); - - if ((nnicks<1) || (nsecs<1)) - { - source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str()); - return MODEACTION_DENY; - } + source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str()); + return MODEACTION_DENY; + } - nickfloodsettings* f = ext.get(channel); - if ((f) && (nnicks == f->nicks) && (nsecs == f->secs)) - // mode params match - return MODEACTION_DENY; + /* Set up the flood parameters for this channel */ + unsigned int nnicks = ConvToInt(parameter.substr(0, colon)); + unsigned int nsecs = ConvToInt(parameter.substr(colon+1)); - ext.set(channel, new nickfloodsettings(nsecs, nnicks)); - parameter = ConvToStr(nnicks) + ":" + ConvToStr(nsecs); - channel->SetModeParam('F', parameter); - return MODEACTION_ALLOW; - } - else + if ((nnicks<1) || (nsecs<1)) { - if (!channel->IsModeSet('F')) - return MODEACTION_DENY; - - ext.unset(channel); - channel->SetModeParam('F', ""); - return MODEACTION_ALLOW; + source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str()); + return MODEACTION_DENY; } + + ext.set(channel, new nickfloodsettings(nsecs, nnicks)); + return MODEACTION_ALLOW; + } + + void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out) + { + out.append(ConvToStr(nfs->nicks)).push_back(':'); + out.append(ConvToStr(nfs->secs)); } }; @@ -135,25 +121,16 @@ class ModuleNickFlood : public Module NickFlood nf; public: - ModuleNickFlood() : nf(this) { - if (!ServerInstance->Modes->AddMode(&nf)) - throw ModuleException("Could not add new modes!"); - ServerInstance->Extensions.Register(&nf.ext); - Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick }; - ServerInstance->Modules->Attach(eventlist, this, 2); } - ModResult OnUserPreNick(User* user, const std::string &newnick) + ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE { - if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */ - return MOD_RES_PASSTHRU; - - for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++) + for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++) { - Channel *channel = *i; + Channel* channel = (*i)->chan; ModResult res; nickfloodsettings *f = nf.ext.get(channel); @@ -165,7 +142,7 @@ class ModuleNickFlood : public Module if (f->islocked()) { - user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs); + user->WriteNumeric(ERR_CANTCHANGENICK, ":%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), f->nicks, f->secs); return MOD_RES_DENY; } @@ -185,14 +162,14 @@ class ModuleNickFlood : public Module /* * XXX: HACK: We do the increment on the *POST* event here (instead of all together) because we have no way of knowing whether other modules would block a nickchange. */ - void OnUserPostNick(User* user, const std::string &oldnick) + void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE { if (isdigit(user->nick[0])) /* allow switches to UID */ return; - for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i) + for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i) { - Channel *channel = *i; + Channel* channel = (*i)->chan; ModResult res; nickfloodsettings *f = nf.ext.get(channel); @@ -211,11 +188,7 @@ class ModuleNickFlood : public Module } } - ~ModuleNickFlood() - { - } - - Version GetVersion() + Version GetVersion() CXX11_OVERRIDE { return Version("Channel mode F - nick flood protection", VF_VENDOR); }