1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +F (nick flood protection) */
18 /** Holds settings and state associated with channel mode +F
20 class nickfloodsettings
30 nickfloodsettings(int b, int c) : secs(b), nicks(c)
32 reset = ServerInstance->Time() + secs;
40 if (ServerInstance->Time() > reset)
43 reset = ServerInstance->Time() + secs;
49 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
50 * on successful nick changes; this will be checked before the counter is
53 return (counter + 1 >= this->nicks);
65 if (ServerInstance->Time() > unlocktime)
81 unlocktime = ServerInstance->Time() + 60;
86 /** Handles channel mode +j
88 class NickFlood : public ModeHandler
91 SimpleExtItem<nickfloodsettings> ext;
92 NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
93 ext("nickflood", Creator) { }
95 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
97 nickfloodsettings* x = ext.get(channel);
99 return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
101 return std::make_pair(false, parameter);
104 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
106 nickfloodsettings *f = ext.get(channel);
111 strlcpy(ndata,parameter.c_str(),MAXBUF);
127 /* Set up the flood parameters for this channel */
128 int nnicks = atoi(nicks);
129 int nsecs = atoi(secs);
130 if ((nnicks<1) || (nsecs<1))
132 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
134 return MODEACTION_DENY;
140 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
141 f = new nickfloodsettings(nsecs, nnicks);
143 channel->SetModeParam('F', parameter);
144 return MODEACTION_ALLOW;
148 std::string cur_param = channel->GetModeParameter('F');
149 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
150 if (cur_param == parameter)
153 return MODEACTION_DENY;
157 // new mode param, replace old with new
158 if ((nsecs > 0) && (nnicks > 0))
160 f = new nickfloodsettings(nsecs, nnicks);
162 channel->SetModeParam('F', parameter);
163 return MODEACTION_ALLOW;
167 return MODEACTION_DENY;
175 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
176 return MODEACTION_DENY;
184 channel->SetModeParam('F', "");
185 return MODEACTION_ALLOW;
188 return MODEACTION_DENY;
192 class ModuleNickFlood : public Module
201 if (!ServerInstance->Modes->AddMode(&nf))
202 throw ModuleException("Could not add new modes!");
203 ServerInstance->Extensions.Register(&nf.ext);
204 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
205 ServerInstance->Modules->Attach(eventlist, this, 2);
208 ModResult OnUserPreNick(User* user, const std::string &newnick)
210 if (isdigit(newnick[0])) /* allow switches to UID */
211 return MOD_RES_PASSTHRU;
213 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
215 Channel *channel = *i;
218 nickfloodsettings *f = nf.ext.get(channel);
221 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,channel,"nickflood"));
222 if (res == MOD_RES_ALLOW)
227 user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
235 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name.c_str(), f->nicks, f->secs);
241 return MOD_RES_PASSTHRU;
245 * 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.
247 void OnUserPostNick(User* user, const std::string &oldnick)
249 if (isdigit(user->nick[0])) /* allow switches to UID */
252 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
254 Channel *channel = *i;
257 nickfloodsettings *f = nf.ext.get(channel);
260 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,channel,"nickflood"));
261 if (res == MOD_RES_ALLOW)
264 /* moved this here to avoid incrementing the counter for nick
265 * changes that are denied for some other reason (bans, +N, etc.)
280 return Version("Channel mode F - nick flood protection", VF_COMMON | VF_VENDOR);
284 MODULE_INIT(ModuleNickFlood)