X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_joinflood.cpp;h=f818484e89be9673bcec221f88ea083c4e390ba1;hb=6ab1d0dffb8084bf6a2ad8a446a3836fa3760c8a;hp=a15c48bc9662e1491bffb304bd7defa40a5e1b00;hpb=59c81b058084260c201938672d69cc6245a25301;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_joinflood.cpp b/src/modules/m_joinflood.cpp index a15c48bc9..f818484e8 100644 --- a/src/modules/m_joinflood.cpp +++ b/src/modules/m_joinflood.cpp @@ -23,9 +23,9 @@ using namespace std; #include "modules.h" #include "helperfuncs.h" -/* $ModDesc: Provides channel mode +f (message flood protection) */ +/* $ModDesc: Provides channel mode +j (join flood protection) */ -class joinfloodsettings +class joinfloodsettings : public classbase { public: @@ -92,94 +92,117 @@ class joinfloodsettings unlocktime = time(NULL) + 60; } - }; -class ModuleJoinFlood : public Module +class JoinFlood : public ModeHandler { - Server *Srv; - public: - - ModuleJoinFlood(Server* Me) - : Module::Module(Me) + JoinFlood() : ModeHandler('j', 1, 0, false, MODETYPE_CHANNEL, false) { } + + ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter) + { + joinfloodsettings* x; + if (channel->GetExt("joinflood",x)) + return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs)); + 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 ¶ms) + + ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding) { - if ((modechar == 'j') && (type == MT_CHANNEL)) + joinfloodsettings* dummy; + + if (adding) { - if (mode_on) + char ndata[MAXBUF]; + char* data = ndata; + strlcpy(ndata,parameter.c_str(),MAXBUF); + char* joins = data; + char* secs = NULL; + while (*data) { - std::string FloodParams = params[0]; - chanrec* c = (chanrec*)target; - char ndata[MAXBUF]; - char* data = ndata; - strlcpy(ndata,FloodParams.c_str(),MAXBUF); - char* joins = data; - char* secs = NULL; - while (*data) + if (*data == ':') { - if (*data == ':') - { - *data = 0; - data++; - secs = data; - break; - } - else data++; + *data = 0; + data++; + secs = data; + break; } - if (secs) + else data++; + } + if (secs) + { + /* Set up the flood parameters for this channel */ + int njoins = atoi(joins); + int nsecs = atoi(secs); + if ((njoins<1) || (nsecs<1)) { - /* Set up the flood parameters for this channel */ - int njoins = atoi(joins); - int nsecs = atoi(secs); - if ((njoins<1) || (nsecs<1)) - { - WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name); - return 0; - } - else - { - if (!c->GetExt("joinflood")) - { - joinfloodsettings *f = new joinfloodsettings(nsecs,njoins); - c->Extend("joinflood",(char*)f); - } - } - return 1; + WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name); + parameter = ""; + return MODEACTION_DENY; } else { - WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name); - return 0; + if (!channel->GetExt("joinflood", dummy)) + { + parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs); + joinfloodsettings *f = new joinfloodsettings(nsecs,njoins); + channel->Extend("joinflood", f); + channel->SetMode('j', true); + channel->SetModeParam('j', parameter.c_str(), true); + return MODEACTION_ALLOW; + } } - } else { - chanrec* c = (chanrec*)target; - if (c->GetExt("joinflood")) - { - joinfloodsettings *f = (joinfloodsettings*)c->GetExt("joinflood"); - delete f; - c->Shrink("joinflood"); - } + WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name); + return MODEACTION_DENY; } - return 1; } - return 0; + else + { + if (channel->GetExt("joinflood", dummy)) + { + joinfloodsettings *f; + channel->GetExt("joinflood", f); + DELETE(f); + channel->Shrink("joinflood"); + channel->SetMode('j', false); + return MODEACTION_ALLOW; + } + } + return MODEACTION_DENY; } +}; +class ModuleJoinFlood : public Module +{ + Server *Srv; + JoinFlood* jf; + + public: + + ModuleJoinFlood(Server* Me) + : Module::Module(Me) + { + Srv = Me; + jf = new JoinFlood(); + Srv->AddMode(jf, 'j'); + } + virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { if (chan) { - joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood"); - if (f) + joinfloodsettings *f; + if (chan->GetExt("joinflood", f)) { if (f->islocked()) { @@ -193,56 +216,42 @@ class ModuleJoinFlood : public Module virtual void OnUserJoin(userrec* user, chanrec* channel) { - joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood"); - if (f) + joinfloodsettings *f; + if (channel->GetExt("joinflood",f)) { f->addjoin(); if (f->shouldlock()) { f->clear(); f->lock(); - WriteChannelWithServ((char*)Srv->GetServerName().c_str(), channel, "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.",channel->name,f->joins,f->secs); + channel->WriteChannelWithServ((char*)Srv->GetServerName().c_str(), "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", channel->name, f->joins, f->secs); } } } void OnChannelDelete(chanrec* chan) { - if (chan->GetExt("joinflood")) + joinfloodsettings *f; + if (chan->GetExt("joinflood",f)) { - joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood"); - delete f; + DELETE(f); chan->Shrink("joinflood"); } } void Implements(char* List) { - List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1; + List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1; } - virtual void On005Numeric(std::string &output) - { - std::stringstream line(output); - std::string temp1, temp2; - while (!line.eof()) - { - line >> temp1; - if (temp1.substr(0,10) == "CHANMODES=") - { - // By doing this we're *assuming* no other module has fucked up the CHANMODES= - // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory, - // with a honking great EXCEPTION :) - temp1.insert(temp1.find(",")+1,"j"); - } - temp2 = temp2 + temp1 + " "; - } - if (temp2.length()) - output = temp2.substr(0,temp2.length()-1); - } + virtual void On005Numeric(std::string &output) + { + InsertMode(output, "j", 3); + } virtual ~ModuleJoinFlood() { + DELETE(jf); } virtual Version GetVersion()