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/>.
23 /** Holds settings and state associated with channel mode +F
25 class nickfloodsettings
34 nickfloodsettings(unsigned int b, unsigned int c)
35 : secs(b), nicks(c), unlocktime(0), counter(0)
37 reset = ServerInstance->Time() + secs;
42 if (ServerInstance->Time() > reset)
45 reset = ServerInstance->Time() + secs;
53 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
54 * on successful nick changes; this will be checked before the counter is
57 return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
67 if (ServerInstance->Time() > unlocktime)
70 return (unlocktime != 0);
75 unlocktime = ServerInstance->Time() + 60;
79 /** Handles channel mode +F
81 class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
84 NickFlood(Module* Creator)
85 : ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >(Creator, "nickflood", 'F')
89 ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
91 std::string::size_type colon = parameter.find(':');
92 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
94 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
95 return MODEACTION_DENY;
98 /* Set up the flood parameters for this channel */
99 unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
100 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
102 if ((nnicks<1) || (nsecs<1))
104 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
105 return MODEACTION_DENY;
108 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
109 return MODEACTION_ALLOW;
112 void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
114 out.append(ConvToStr(nfs->nicks)).push_back(':');
115 out.append(ConvToStr(nfs->secs));
119 class ModuleNickFlood : public Module
129 ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
131 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
133 Channel* channel = (*i)->chan;
136 nickfloodsettings *f = nf.ext.get(channel);
139 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
140 if (res == MOD_RES_ALLOW)
145 user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), f->nicks, f->secs));
153 channel->WriteNotice(InspIRCd::Format("No nick changes are allowed for 60 seconds because there have been more than %u nick changes in %u seconds.", f->nicks, f->secs));
159 return MOD_RES_PASSTHRU;
163 * 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.
165 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
167 if (isdigit(user->nick[0])) /* allow switches to UID */
170 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
172 Channel* channel = (*i)->chan;
175 nickfloodsettings *f = nf.ext.get(channel);
178 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
179 if (res == MOD_RES_ALLOW)
182 /* moved this here to avoid incrementing the counter for nick
183 * changes that are denied for some other reason (bans, +N, etc.)
191 Version GetVersion() CXX11_OVERRIDE
193 return Version("Channel mode F - nick flood protection", VF_VENDOR);
197 MODULE_INIT(ModuleNickFlood)