2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
10 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "modules/ctctags.h"
28 #include "modules/exemption.h"
30 /** Holds flood settings and state for mode +f
39 insp::flat_map<User*, double> counters;
41 floodsettings(bool a, unsigned int b, unsigned int c)
46 reset = ServerInstance->Time() + secs;
49 bool addmessage(User* who, double weight)
51 if (ServerInstance->Time() > reset)
54 reset = ServerInstance->Time() + secs;
57 counters[who] += weight;
58 return (counters[who] >= this->lines);
67 /** Handles channel mode +f
69 class MsgFlood : public ParamMode<MsgFlood, SimpleExtItem<floodsettings> >
72 MsgFlood(Module* Creator)
73 : ParamMode<MsgFlood, SimpleExtItem<floodsettings> >(Creator, "flood", 'f')
75 syntax = "[*]<messages>:<seconds>";
78 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
80 std::string::size_type colon = parameter.find(':');
81 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
83 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
84 return MODEACTION_DENY;
87 /* Set up the flood parameters for this channel */
88 bool ban = (parameter[0] == '*');
89 unsigned int nlines = ConvToNum<unsigned int>(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
90 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
92 if ((nlines<2) || (nsecs<1))
94 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
95 return MODEACTION_DENY;
98 ext.set(channel, new floodsettings(ban, nsecs, nlines));
99 return MODEACTION_ALLOW;
102 void SerializeParam(Channel* chan, const floodsettings* fs, std::string& out)
106 out.append(ConvToStr(fs->lines)).push_back(':');
107 out.append(ConvToStr(fs->secs));
113 , public CTCTags::EventListener
116 CheckExemption::EventProvider exemptionprov;
124 : CTCTags::EventListener(this)
125 , exemptionprov(this)
130 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
132 ConfigTag* tag = ServerInstance->Config->ConfValue("messageflood");
133 notice = tag->getFloat("notice", 1.0);
134 privmsg = tag->getFloat("privmsg", 1.0);
135 tagmsg = tag->getFloat("tagmsg", 0.2);
138 ModResult HandleMessage(User* user, const MessageTarget& target, double weight)
140 if (target.type != MessageTarget::TYPE_CHANNEL)
141 return MOD_RES_PASSTHRU;
143 Channel* dest = target.Get<Channel>();
144 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
145 return MOD_RES_PASSTHRU;
147 ModResult res = CheckExemption::Call(exemptionprov, user, dest, "flood");
148 if (res == MOD_RES_ALLOW)
149 return MOD_RES_PASSTHRU;
151 floodsettings *f = mf.ext.get(dest);
154 if (f->addmessage(user, weight))
156 /* Youre outttta here! */
160 Modes::ChangeList changelist;
161 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost());
162 ServerInstance->Modes->Process(ServerInstance->FakeClient, dest, NULL, changelist);
165 const std::string kickMessage = "Channel flood triggered (trigger is " + ConvToStr(f->lines) +
166 " lines in " + ConvToStr(f->secs) + " secs)";
168 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
174 return MOD_RES_PASSTHRU;
177 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
179 return HandleMessage(user, target, (details.type == MSG_PRIVMSG ? privmsg : notice));
182 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
184 return HandleMessage(user, target, tagmsg);
187 void Prioritize() CXX11_OVERRIDE
189 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
190 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
193 Version GetVersion() CXX11_OVERRIDE
195 return Version("Provides channel mode +f, message flood protection", VF_VENDOR);
199 MODULE_INIT(ModuleMsgFlood)