2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007, 2009 Robin Burchell <robin+git@viroteck.net>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "modules/exemption.h"
24 // The number of seconds nickname changing will be blocked for.
25 static unsigned int duration;
27 /** Holds settings and state associated with channel mode +F
29 class nickfloodsettings
38 nickfloodsettings(unsigned int b, unsigned int c)
39 : secs(b), nicks(c), unlocktime(0), counter(0)
41 reset = ServerInstance->Time() + secs;
46 if (ServerInstance->Time() > reset)
49 reset = ServerInstance->Time() + secs;
57 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
58 * on successful nick changes; this will be checked before the counter is
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')
93 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
95 std::string::size_type colon = parameter.find(':');
96 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
98 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
99 return MODEACTION_DENY;
102 /* Set up the flood parameters for this channel */
103 unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
104 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
106 if ((nnicks<1) || (nsecs<1))
108 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
109 return MODEACTION_DENY;
112 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
113 return MODEACTION_ALLOW;
116 void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
118 out.append(ConvToStr(nfs->nicks)).push_back(':');
119 out.append(ConvToStr(nfs->secs));
123 class ModuleNickFlood : public Module
125 CheckExemption::EventProvider exemptionprov;
130 : exemptionprov(this)
135 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
137 ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
138 duration = tag->getDuration("duration", 60, 10, 600);
141 ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
143 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
145 Channel* channel = (*i)->chan;
148 nickfloodsettings *f = nf.ext.get(channel);
151 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
152 if (res == MOD_RES_ALLOW)
157 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));
165 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));
171 return MOD_RES_PASSTHRU;
175 * 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.
177 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
179 if (isdigit(user->nick[0])) /* allow switches to UID */
182 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
184 Channel* channel = (*i)->chan;
187 nickfloodsettings *f = nf.ext.get(channel);
190 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
191 if (res == MOD_RES_ALLOW)
194 /* moved this here to avoid incrementing the counter for nick
195 * changes that are denied for some other reason (bans, +N, etc.)
203 Version GetVersion() CXX11_OVERRIDE
205 return Version("Channel mode F - nick flood protection", VF_VENDOR);
209 MODULE_INIT(ModuleNickFlood)