2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2007 John Brooks <special@inspircd.org>
10 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11 * Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
13 * This file is part of InspIRCd. InspIRCd is free software: you can
14 * redistribute it and/or modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation, version 2.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "modules/ctctags.h"
29 #include "modules/exemption.h"
31 /** Holds flood settings and state for mode +f
40 insp::flat_map<User*, double> counters;
42 floodsettings(bool a, unsigned int b, unsigned int c)
47 reset = ServerInstance->Time() + secs;
50 bool addmessage(User* who, double weight)
52 if (ServerInstance->Time() > reset)
55 reset = ServerInstance->Time() + secs;
58 counters[who] += weight;
59 return (counters[who] >= this->lines);
68 /** Handles channel mode +f
70 class MsgFlood : public ParamMode<MsgFlood, SimpleExtItem<floodsettings> >
73 MsgFlood(Module* Creator)
74 : ParamMode<MsgFlood, SimpleExtItem<floodsettings> >(Creator, "flood", 'f')
76 syntax = "[*]<messages>:<seconds>";
79 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
81 std::string::size_type colon = parameter.find(':');
82 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
84 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
85 return MODEACTION_DENY;
88 /* Set up the flood parameters for this channel */
89 bool ban = (parameter[0] == '*');
90 unsigned int nlines = ConvToNum<unsigned int>(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
91 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
93 if ((nlines<2) || (nsecs<1))
95 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
96 return MODEACTION_DENY;
99 ext.set(channel, new floodsettings(ban, nsecs, nlines));
100 return MODEACTION_ALLOW;
103 void SerializeParam(Channel* chan, const floodsettings* fs, std::string& out)
107 out.append(ConvToStr(fs->lines)).push_back(':');
108 out.append(ConvToStr(fs->secs));
114 , public CTCTags::EventListener
117 CheckExemption::EventProvider exemptionprov;
125 : CTCTags::EventListener(this)
126 , exemptionprov(this)
131 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
133 ConfigTag* tag = ServerInstance->Config->ConfValue("messageflood");
134 notice = tag->getFloat("notice", 1.0);
135 privmsg = tag->getFloat("privmsg", 1.0);
136 tagmsg = tag->getFloat("tagmsg", 0.2);
139 ModResult HandleMessage(User* user, const MessageTarget& target, double weight)
141 if (target.type != MessageTarget::TYPE_CHANNEL)
142 return MOD_RES_PASSTHRU;
144 Channel* dest = target.Get<Channel>();
145 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
146 return MOD_RES_PASSTHRU;
148 ModResult res = CheckExemption::Call(exemptionprov, user, dest, "flood");
149 if (res == MOD_RES_ALLOW)
150 return MOD_RES_PASSTHRU;
152 floodsettings *f = mf.ext.get(dest);
155 if (f->addmessage(user, weight))
157 /* Youre outttta here! */
161 Modes::ChangeList changelist;
162 changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost());
163 ServerInstance->Modes->Process(ServerInstance->FakeClient, dest, NULL, changelist);
166 const std::string kickMessage = "Channel flood triggered (trigger is " + ConvToStr(f->lines) +
167 " lines in " + ConvToStr(f->secs) + " secs)";
169 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
175 return MOD_RES_PASSTHRU;
178 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
180 return HandleMessage(user, target, (details.type == MSG_PRIVMSG ? privmsg : notice));
183 ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
185 return HandleMessage(user, target, tagmsg);
188 void Prioritize() CXX11_OVERRIDE
190 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
191 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
194 Version GetVersion() CXX11_OVERRIDE
196 return Version("Adds channel mode f (flood) which helps protect against spammers which mass-message channels.", VF_VENDOR);
200 MODULE_INIT(ModuleMsgFlood)