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 // The number of seconds nickname changing will be blocked for.
24 static unsigned int duration;
26 /** Holds settings and state associated with channel mode +F
28 class nickfloodsettings
37 nickfloodsettings(unsigned int b, unsigned int c)
38 : secs(b), nicks(c), unlocktime(0), counter(0)
40 reset = ServerInstance->Time() + secs;
45 if (ServerInstance->Time() > reset)
48 reset = ServerInstance->Time() + secs;
56 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
57 * on successful nick changes; this will be checked before the counter is
60 return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
70 if (ServerInstance->Time() > unlocktime)
73 return (unlocktime != 0);
78 unlocktime = ServerInstance->Time() + duration;
82 /** Handles channel mode +F
84 class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
87 NickFlood(Module* Creator)
88 : ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >(Creator, "nickflood", 'F')
92 ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
94 std::string::size_type colon = parameter.find(':');
95 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
97 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
98 return MODEACTION_DENY;
101 /* Set up the flood parameters for this channel */
102 unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
103 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
105 if ((nnicks<1) || (nsecs<1))
107 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
108 return MODEACTION_DENY;
111 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
112 return MODEACTION_ALLOW;
115 void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
117 out.append(ConvToStr(nfs->nicks)).push_back(':');
118 out.append(ConvToStr(nfs->secs));
122 class ModuleNickFlood : public Module
132 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
134 ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
135 duration = tag->getDuration("duration", 60, 10, 600);
138 ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
140 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
142 Channel* channel = (*i)->chan;
145 nickfloodsettings *f = nf.ext.get(channel);
148 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
149 if (res == MOD_RES_ALLOW)
154 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));
162 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));
168 return MOD_RES_PASSTHRU;
172 * 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.
174 void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
176 if (isdigit(user->nick[0])) /* allow switches to UID */
179 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
181 Channel* channel = (*i)->chan;
184 nickfloodsettings *f = nf.ext.get(channel);
187 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
188 if (res == MOD_RES_ALLOW)
191 /* moved this here to avoid incrementing the counter for nick
192 * changes that are denied for some other reason (bans, +N, etc.)
200 Version GetVersion() CXX11_OVERRIDE
202 return Version("Channel mode F - nick flood protection", VF_VENDOR);
206 MODULE_INIT(ModuleNickFlood)