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 /* $ModDesc: Provides channel mode +F (nick flood protection) */
25 /** Holds settings and state associated with channel mode +F
27 class nickfloodsettings
36 nickfloodsettings(unsigned int b, unsigned int c)
37 : secs(b), nicks(c), unlocktime(0), counter(0)
39 reset = ServerInstance->Time() + secs;
44 if (ServerInstance->Time() > reset)
47 reset = ServerInstance->Time() + secs;
55 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
56 * on successful nick changes; this will be checked before the counter is
59 return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
69 if (ServerInstance->Time() > unlocktime)
72 return (unlocktime != 0);
77 unlocktime = ServerInstance->Time() + 60;
81 /** Handles channel mode +F
83 class NickFlood : public ModeHandler
86 SimpleExtItem<nickfloodsettings> ext;
87 NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
88 ext("nickflood", Creator) { }
90 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
94 std::string::size_type colon = parameter.find(':');
95 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
97 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
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, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
108 return MODEACTION_DENY;
111 nickfloodsettings* f = ext.get(channel);
112 if ((f) && (nnicks == f->nicks) && (nsecs == f->secs))
114 return MODEACTION_DENY;
116 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
117 parameter = ConvToStr(nnicks) + ":" + ConvToStr(nsecs);
118 channel->SetModeParam('F', parameter);
119 return MODEACTION_ALLOW;
123 if (!channel->IsModeSet('F'))
124 return MODEACTION_DENY;
127 channel->SetModeParam('F', "");
128 return MODEACTION_ALLOW;
133 class ModuleNickFlood : public Module
142 if (!ServerInstance->Modes->AddMode(&nf))
143 throw ModuleException("Could not add new modes!");
144 ServerInstance->Extensions.Register(&nf.ext);
145 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
146 ServerInstance->Modules->Attach(eventlist, this, 2);
149 ModResult OnUserPreNick(User* user, const std::string &newnick)
151 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
152 return MOD_RES_PASSTHRU;
154 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
156 Channel *channel = *i;
159 nickfloodsettings *f = nf.ext.get(channel);
162 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
163 if (res == MOD_RES_ALLOW)
168 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);
176 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);
182 return MOD_RES_PASSTHRU;
186 * 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.
188 void OnUserPostNick(User* user, const std::string &oldnick)
190 if (isdigit(user->nick[0])) /* allow switches to UID */
193 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
195 Channel *channel = *i;
198 nickfloodsettings *f = nf.ext.get(channel);
201 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
202 if (res == MOD_RES_ALLOW)
205 /* moved this here to avoid incrementing the counter for nick
206 * changes that are denied for some other reason (bans, +N, etc.)
220 return Version("Channel mode F - nick flood protection", VF_VENDOR);
224 MODULE_INIT(ModuleNickFlood)