2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2016-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
6 * Copyright (C) 2012, 2014 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
8 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "modules/exemption.h"
28 // The number of seconds nickname changing will be blocked for.
29 static unsigned int duration;
31 /** Holds settings and state associated with channel mode +F
33 class nickfloodsettings
42 nickfloodsettings(unsigned int b, unsigned int c)
43 : secs(b), nicks(c), unlocktime(0), counter(0)
45 reset = ServerInstance->Time() + secs;
50 if (ServerInstance->Time() > reset)
53 reset = ServerInstance->Time() + secs;
61 return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
71 if (ServerInstance->Time() > unlocktime)
74 return (unlocktime != 0);
79 unlocktime = ServerInstance->Time() + duration;
83 /** Handles channel mode +F
85 class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
88 NickFlood(Module* Creator)
89 : ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >(Creator, "nickflood", 'F')
91 syntax = "<nick-changes>:<seconds>";
94 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
96 std::string::size_type colon = parameter.find(':');
97 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
99 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
100 return MODEACTION_DENY;
103 /* Set up the flood parameters for this channel */
104 unsigned int nnicks = ConvToNum<unsigned int>(parameter.substr(0, colon));
105 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
107 if ((nnicks<1) || (nsecs<1))
109 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
110 return MODEACTION_DENY;
113 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
114 return MODEACTION_ALLOW;
117 void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
119 out.append(ConvToStr(nfs->nicks)).push_back(':');
120 out.append(ConvToStr(nfs->secs));
124 class ModuleNickFlood : public Module
126 CheckExemption::EventProvider exemptionprov;
131 : exemptionprov(this)
136 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
138 ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
139 duration = tag->getDuration("duration", 60, 10, 600);
142 ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
144 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
146 Channel* channel = (*i)->chan;
149 nickfloodsettings *f = nf.ext.get(channel);
152 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
153 if (res == MOD_RES_ALLOW)
158 user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("%s has been locked for nickchanges for %u seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), duration, f->nicks, f->secs));
166 channel->WriteNotice(InspIRCd::Format("No nick changes are allowed for %u seconds because there have been more than %u nick changes in %u seconds.", duration, f->nicks, f->secs));
172 return MOD_RES_PASSTHRU;
176 * XXX: HACK: We do the increment on the *POST* event here (instead of all together) because we have no way of knowing whether other modules would block a nickchange.
178 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
180 if (isdigit(user->nick[0])) /* allow switches to UID */
183 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
185 Channel* channel = (*i)->chan;
188 nickfloodsettings *f = nf.ext.get(channel);
191 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
192 if (res == MOD_RES_ALLOW)
195 /* moved this here to avoid incrementing the counter for nick
196 * changes that are denied for some other reason (bans, +N, etc.)
204 Version GetVersion() CXX11_OVERRIDE
206 return Version("Adds channel mode F (nickflood) which helps protect against spammers which mass-change nicknames.", VF_VENDOR);
210 MODULE_INIT(ModuleNickFlood)