/* * InspIRCd -- Internet Relay Chat Daemon * * Copyright (C) 2009 Daniel De Graaf * Copyright (C) 2008 Pippijn van Steenhoven * Copyright (C) 2007 Robin Burchell * Copyright (C) 2007 John Brooks * Copyright (C) 2007 Dennis Friis * 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 . */ #include "inspircd.h" /** Holds flood settings and state for mode +f */ class floodsettings { public: bool ban; unsigned int secs; unsigned int lines; time_t reset; std::map counters; floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c) { reset = ServerInstance->Time() + secs; } bool addmessage(User* who) { if (ServerInstance->Time() > reset) { counters.clear(); reset = ServerInstance->Time() + secs; } return (++counters[who] >= this->lines); } void clear(User* who) { std::map::iterator iter = counters.find(who); if (iter != counters.end()) { counters.erase(iter); } } }; /** Handles channel mode +f */ class MsgFlood : public ModeHandler { public: SimpleExtItem ext; MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL), ext("messageflood", Creator) { } ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) { if (adding) { std::string::size_type colon = parameter.find(':'); if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos)) { source->WriteNumeric(608, "%s :Invalid flood parameter", channel->name.c_str()); return MODEACTION_DENY; } /* Set up the flood parameters for this channel */ bool ban = (parameter[0] == '*'); unsigned int nlines = ConvToInt(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon)); unsigned int nsecs = ConvToInt(parameter.substr(colon+1)); if ((nlines<2) || (nsecs<1)) { source->WriteNumeric(608, "%s :Invalid flood parameter", channel->name.c_str()); return MODEACTION_DENY; } floodsettings* f = ext.get(channel); if ((f) && (nlines == f->lines) && (nsecs == f->secs) && (ban == f->ban)) // mode params match return MODEACTION_DENY; ext.set(channel, new floodsettings(ban, nsecs, nlines)); parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" + ConvToStr(nsecs); return MODEACTION_ALLOW; } else { if (!channel->IsModeSet(this)) return MODEACTION_DENY; ext.unset(channel); return MODEACTION_ALLOW; } } }; class ModuleMsgFlood : public Module { MsgFlood mf; public: ModuleMsgFlood() : mf(this) { } ModResult OnUserPreMessage(User* user, void* voiddest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE { if (target_type != TYPE_CHANNEL) return MOD_RES_PASSTHRU; Channel* dest = static_cast(voiddest); if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf)) return MOD_RES_PASSTHRU; if (ServerInstance->OnCheckExemption(user,dest,"flood") == MOD_RES_ALLOW) return MOD_RES_PASSTHRU; floodsettings *f = mf.ext.get(dest); if (f) { if (f->addmessage(user)) { /* Youre outttta here! */ f->clear(user); if (f->ban) { std::vector parameters; parameters.push_back(dest->name); parameters.push_back("+b"); parameters.push_back("*!*@" + user->dhost); ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient); } const std::string kickMessage = "Channel flood triggered (limit is " + ConvToStr(f->lines) + " in " + ConvToStr(f->secs) + " secs)"; dest->KickUser(ServerInstance->FakeClient, user, kickMessage); return MOD_RES_DENY; } } return MOD_RES_PASSTHRU; } void Prioritize() { // 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); } Version GetVersion() CXX11_OVERRIDE { return Version("Provides channel mode +f (message flood protection)", VF_VENDOR); } }; MODULE_INIT(ModuleMsgFlood)