X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fmodules%2Fm_messageflood.cpp;h=329d77aac69ffc594c81a038faac2188ae294ff1;hb=d38595e7e14e7509e744d33df657d50d00cc201f;hp=660c2128b895fd5b6496c7707fb7747d74edfcc4;hpb=537c7d617b9f840c66bef9a9f68b510bc312f997;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/modules/m_messageflood.cpp b/src/modules/m_messageflood.cpp index 660c2128b..329d77aac 100644 --- a/src/modules/m_messageflood.cpp +++ b/src/modules/m_messageflood.cpp @@ -1,287 +1,200 @@ -/* +------------------------------------+ - * | Inspire Internet Relay Chat Daemon | - * +------------------------------------+ +/* + * InspIRCd -- Internet Relay Chat Daemon * - * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev. - * E-mail: - * - * - * - * Written by Craig Edwards, Craig McLure, and others. - * This program is free but copyrighted software; see - * the file COPYING for details. + * Copyright (C) 2013, 2017-2019 Sadie Powell + * Copyright (C) 2012-2014, 2016 Attila Molnar + * Copyright (C) 2012, 2019 Robby + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2007-2008 Robin Burchell + * Copyright (C) 2007 John Brooks + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006, 2008, 2010 Craig Edwards * - * --------------------------------------------------- + * 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 . */ -using namespace std; - -#include -#include -#include "users.h" -#include "channels.h" -#include "modules.h" -#include "helperfuncs.h" -/* $ModDesc: Provides channel mode +f (message flood protection) */ +#include "inspircd.h" +#include "modules/ctctags.h" +#include "modules/exemption.h" +/** Holds flood settings and state for mode +f + */ class floodsettings { public: bool ban; - int secs; - int lines; + unsigned int secs; + unsigned int lines; time_t reset; - std::map counters; + insp::flat_map counters; - floodsettings() : ban(0), secs(0), lines(0) {}; - floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c) + floodsettings(bool a, unsigned int b, unsigned int c) + : ban(a) + , secs(b) + , lines(c) { - reset = time(NULL) + secs; - log(DEBUG,"Create new floodsettings: %lu %lu",time(NULL),reset); - }; + reset = ServerInstance->Time() + secs; + } - void addmessage(userrec* who) + bool addmessage(User* who, double weight) { - std::map::iterator iter = counters.find(who); - if (iter != counters.end()) + if (ServerInstance->Time() > reset) { - iter->second++; - log(DEBUG,"Count for %s is now %d",who->nick,iter->second); - } - else - { - counters[who] = 1; - log(DEBUG,"Count for %s is now *1*",who->nick); - } - if (time(NULL) > reset) - { - log(DEBUG,"floodsettings timer Resetting."); counters.clear(); - reset = time(NULL) + secs; + reset = ServerInstance->Time() + secs; } + + counters[who] += weight; + return (counters[who] >= this->lines); } - bool shouldkick(userrec* who) + void clear(User* who) { - std::map::iterator iter = counters.find(who); - if (iter != counters.end()) - { - log(DEBUG,"should kick? %d, %d",iter->second,this->lines); - return (iter->second >= this->lines); - } - else return false; + counters.erase(who); + } +}; + +/** Handles channel mode +f + */ +class MsgFlood : public ParamMode > +{ + public: + MsgFlood(Module* Creator) + : ParamMode >(Creator, "flood", 'f') + { + syntax = "[*]:"; } - void clear(userrec* who) + ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE { - std::map::iterator iter = counters.find(who); - if (iter != counters.end()) + std::string::size_type colon = parameter.find(':'); + if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos)) { - counters.erase(iter); + source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter)); + return MODEACTION_DENY; } + + /* Set up the flood parameters for this channel */ + bool ban = (parameter[0] == '*'); + unsigned int nlines = ConvToNum(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon)); + unsigned int nsecs = ConvToNum(parameter.substr(colon+1)); + + if ((nlines<2) || (nsecs<1)) + { + source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter)); + return MODEACTION_DENY; + } + + ext.set(channel, new floodsettings(ban, nsecs, nlines)); + return MODEACTION_ALLOW; + } + + void SerializeParam(Channel* chan, const floodsettings* fs, std::string& out) + { + if (fs->ban) + out.push_back('*'); + out.append(ConvToStr(fs->lines)).push_back(':'); + out.append(ConvToStr(fs->secs)); } }; -class ModuleMsgFlood : public Module +class ModuleMsgFlood + : public Module + , public CTCTags::EventListener { - Server *Srv; - +private: + CheckExemption::EventProvider exemptionprov; + MsgFlood mf; + double notice; + double privmsg; + double tagmsg; + public: - - ModuleMsgFlood(Server* Me) - : Module::Module(Me) + ModuleMsgFlood() + : CTCTags::EventListener(this) + , exemptionprov(this) + , mf(this) { - Srv = Me; - Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0); } - - virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms) + + void ReadConfig(ConfigStatus&) CXX11_OVERRIDE { - if ((modechar == 'f') && (type == MT_CHANNEL)) - { - if (mode_on) - { - std::string FloodParams = params[0]; - chanrec* c = (chanrec*)target; - char ndata[MAXBUF]; - char* data = ndata; - strlcpy(ndata,FloodParams.c_str(),MAXBUF); - char* lines = data; - char* secs = NULL; - bool ban = false; - if (*data == '*') - { - ban = true; - lines++; - } - else - { - ban = false; - } - while (*data) - { - if (*data == ':') - { - *data = 0; - data++; - secs = data; - break; - } - else data++; - } - if (secs) - { - /* Set up the flood parameters for this channel */ - int nlines = atoi(lines); - int nsecs = atoi(secs); - if ((nlines<1) || (nsecs<1)) - { - WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name); - return 0; - } - else - { - if (!c->GetExt("flood")) - { - floodsettings *f = new floodsettings(ban,nlines,nsecs); - c->Extend("flood",(char*)f); - } - } - return 1; - } - else - { - WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name); - return 0; - } - - } - else - { - chanrec* c = (chanrec*)target; - if (c->GetExt("flood")) - { - floodsettings *f = (floodsettings*)c->GetExt("flood"); - delete f; - c->Shrink("flood"); - } - } - return 1; - } - return 0; + ConfigTag* tag = ServerInstance->Config->ConfValue("messageflood"); + notice = tag->getFloat("notice", 1.0); + privmsg = tag->getFloat("privmsg", 1.0); + tagmsg = tag->getFloat("tagmsg", 0.2); } - int ProcessMessages(userrec* user,chanrec* dest,std::string &text) + ModResult HandleMessage(User* user, const MessageTarget& target, double weight) { - floodsettings *f = (floodsettings*)dest->GetExt("flood"); + if (target.type != MessageTarget::TYPE_CHANNEL) + return MOD_RES_PASSTHRU; + + Channel* dest = target.Get(); + if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf)) + return MOD_RES_PASSTHRU; + + ModResult res = CheckExemption::Call(exemptionprov, user, dest, "flood"); + if (res == MOD_RES_ALLOW) + return MOD_RES_PASSTHRU; + + floodsettings *f = mf.ext.get(dest); if (f) { - f->addmessage(user); - if (f->shouldkick(user)) + if (f->addmessage(user, weight)) { /* Youre outttta here! */ f->clear(user); if (f->ban) { - char* parameters[3]; - parameters[0] = dest->name; - parameters[1] = "+b"; - parameters[2] = user->MakeWildHost(); - Srv->SendMode(parameters,3,user); + Modes::ChangeList changelist; + changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost()); + ServerInstance->Modes->Process(ServerInstance->FakeClient, dest, NULL, changelist); } - Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)"); - return 1; - } - } - return 0; - } - virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) - { - if (target_type == TYPE_CHANNEL) - { - return ProcessMessages(user,(chanrec*)dest,text); - } - else return 0; - } + const std::string kickMessage = "Channel flood triggered (trigger is " + ConvToStr(f->lines) + + " lines in " + ConvToStr(f->secs) + " secs)"; - virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) - { - if (target_type == TYPE_CHANNEL) - { - return ProcessMessages(user,(chanrec*)dest,text); - } - else return 0; - } + dest->KickUser(ServerInstance->FakeClient, user, kickMessage); - void OnChannelDelete(chanrec* chan) - { - if (chan->GetExt("flood")) - { - floodsettings *f = (floodsettings*)chan->GetExt("flood"); - delete f; - chan->Shrink("flood"); + return MOD_RES_DENY; + } } - } - void Implements(char* List) - { - List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = 1; + return MOD_RES_PASSTHRU; } - 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,"f"); - } - temp2 = temp2 + temp1 + " "; - } - if (temp2.length()) - output = temp2.substr(0,temp2.length()-1); - } - - virtual ~ModuleMsgFlood() - { - } - - virtual Version GetVersion() + ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE { - return Version(1,0,0,0,VF_STATIC|VF_VENDOR); + return HandleMessage(user, target, (details.type == MSG_PRIVMSG ? privmsg : notice)); } -}; - -class ModuleMsgFloodFactory : public ModuleFactory -{ - public: - ModuleMsgFloodFactory() + ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE { + return HandleMessage(user, target, tagmsg); } - - ~ModuleMsgFloodFactory() + + void Prioritize() CXX11_OVERRIDE { + // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.) + ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST); } - - virtual Module * CreateModule(Server* Me) + + Version GetVersion() CXX11_OVERRIDE { - return new ModuleMsgFlood(Me); + return Version("Adds channel mode f (flood) which helps protect against spammers which mass-message channels.", VF_VENDOR); } - }; - -extern "C" void * init_module( void ) -{ - return new ModuleMsgFloodFactory; -} - +MODULE_INIT(ModuleMsgFlood)