1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
97 nickfloodsettings *f = ext.get(channel);
102 strlcpy(ndata,parameter.c_str(),MAXBUF);
118 /* Set up the flood parameters for this channel */
119 int nnicks = atoi(nicks);
120 int nsecs = atoi(secs);
121 if ((nnicks<1) || (nsecs<1))
123 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
125 return MODEACTION_DENY;
131 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
132 f = new nickfloodsettings(nsecs, nnicks);
134 channel->SetModeParam('F', parameter);
135 return MODEACTION_ALLOW;
139 std::string cur_param = channel->GetModeParameter('F');
140 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
141 if (cur_param == parameter)
144 return MODEACTION_DENY;
148 // new mode param, replace old with new
149 if ((nsecs > 0) && (nnicks > 0))
151 f = new nickfloodsettings(nsecs, nnicks);
153 channel->SetModeParam('F', parameter);
154 return MODEACTION_ALLOW;
158 return MODEACTION_DENY;
166 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
167 return MODEACTION_DENY;
175 channel->SetModeParam('F', "");
176 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
183 class ModuleNickFlood : public Module
192 if (!ServerInstance->Modes->AddMode(&nf))
193 throw ModuleException("Could not add new modes!");
194 ServerInstance->Extensions.Register(&nf.ext);
195 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
196 ServerInstance->Modules->Attach(eventlist, this, 2);
199 ModResult OnUserPreNick(User* user, const std::string &newnick)
201 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
202 return MOD_RES_PASSTHRU;
204 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
206 Channel *channel = *i;
209 nickfloodsettings *f = nf.ext.get(channel);
212 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
213 if (res == MOD_RES_ALLOW)
218 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);
226 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);
232 return MOD_RES_PASSTHRU;
236 * 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.
238 void OnUserPostNick(User* user, const std::string &oldnick)
240 if (isdigit(user->nick[0])) /* allow switches to UID */
243 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
245 Channel *channel = *i;
248 nickfloodsettings *f = nf.ext.get(channel);
251 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
252 if (res == MOD_RES_ALLOW)
255 /* moved this here to avoid incrementing the counter for nick
256 * changes that are denied for some other reason (bans, +N, etc.)
271 return Version("Channel mode F - nick flood protection", VF_VENDOR);
275 MODULE_INIT(ModuleNickFlood)