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
145 ServerInstance->Modules->AddService(nf);
146 ServerInstance->Modules->AddService(nf.ext);
147 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
148 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
151 ModResult OnUserPreNick(User* user, const std::string &newnick)
153 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
154 return MOD_RES_PASSTHRU;
156 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
158 Channel *channel = *i;
161 nickfloodsettings *f = nf.ext.get(channel);
164 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
165 if (res == MOD_RES_ALLOW)
170 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);
178 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);
184 return MOD_RES_PASSTHRU;
188 * 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.
190 void OnUserPostNick(User* user, const std::string &oldnick)
192 if (isdigit(user->nick[0])) /* allow switches to UID */
195 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
197 Channel *channel = *i;
200 nickfloodsettings *f = nf.ext.get(channel);
203 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
204 if (res == MOD_RES_ALLOW)
207 /* moved this here to avoid incrementing the counter for nick
208 * changes that are denied for some other reason (bans, +N, etc.)
218 return Version("Channel mode F - nick flood protection", VF_VENDOR);
222 MODULE_INIT(ModuleNickFlood)