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 ModeHandler
84 SimpleExtItem<nickfloodsettings> ext;
85 NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
86 ext("nickflood", Creator) { }
88 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
92 std::string::size_type colon = parameter.find(':');
93 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
95 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
96 return MODEACTION_DENY;
99 /* Set up the flood parameters for this channel */
100 unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
101 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
103 if ((nnicks<1) || (nsecs<1))
105 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
106 return MODEACTION_DENY;
109 nickfloodsettings* f = ext.get(channel);
110 if ((f) && (nnicks == f->nicks) && (nsecs == f->secs))
112 return MODEACTION_DENY;
114 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
115 parameter = ConvToStr(nnicks) + ":" + ConvToStr(nsecs);
116 return MODEACTION_ALLOW;
120 if (!channel->IsModeSet(this))
121 return MODEACTION_DENY;
124 return MODEACTION_ALLOW;
129 class ModuleNickFlood : public Module
139 ModResult OnUserPreNick(User* user, const std::string &newnick) CXX11_OVERRIDE
141 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
143 Channel *channel = *i;
146 nickfloodsettings *f = nf.ext.get(channel);
149 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
150 if (res == MOD_RES_ALLOW)
155 user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
163 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %u nick changes in %u seconds.", channel->name.c_str(), f->nicks, f->secs);
169 return MOD_RES_PASSTHRU;
173 * 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.
175 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
177 if (isdigit(user->nick[0])) /* allow switches to UID */
180 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
182 Channel *channel = *i;
185 nickfloodsettings *f = nf.ext.get(channel);
188 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
189 if (res == MOD_RES_ALLOW)
192 /* moved this here to avoid incrementing the counter for nick
193 * changes that are denied for some other reason (bans, +N, etc.)
201 Version GetVersion() CXX11_OVERRIDE
203 return Version("Channel mode F - nick flood protection", VF_VENDOR);
207 MODULE_INIT(ModuleNickFlood)