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 : public classbase
23 InspIRCd* ServerInstance;
32 nickfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), nicks(c)
34 reset = Instance->Time() + secs;
42 if (ServerInstance->Time() > reset)
45 reset = ServerInstance->Time() + secs;
51 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
52 * on successful nick changes; this will be checked before the counter is
55 return (counter + 1 >= this->nicks);
67 if (ServerInstance->Time() > unlocktime)
83 unlocktime = ServerInstance->Time() + 60;
88 /** Handles channel mode +j
90 class NickFlood : public ModeHandler
93 NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
95 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
98 if (channel->GetExt("nickflood",x))
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, bool)
106 nickfloodsettings* dummy;
112 strlcpy(ndata,parameter.c_str(),MAXBUF);
129 /* Set up the flood parameters for this channel */
130 int nnicks = atoi(nicks);
131 int nsecs = atoi(secs);
132 if ((nnicks<1) || (nsecs<1))
134 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
136 return MODEACTION_DENY;
140 if (!channel->GetExt("nickflood", dummy))
142 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
143 nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
144 channel->Extend("nickflood", f);
145 channel->SetModeParam('F', parameter);
146 return MODEACTION_ALLOW;
150 std::string cur_param = channel->GetModeParameter('F');
151 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
152 if (cur_param == parameter)
155 return MODEACTION_DENY;
159 // new mode param, replace old with new
160 if ((nsecs > 0) && (nnicks > 0))
162 nickfloodsettings* f;
163 channel->GetExt("nickflood", f);
165 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
166 channel->Shrink("nickflood");
167 channel->Extend("nickflood", f);
168 channel->SetModeParam('F', parameter);
169 return MODEACTION_ALLOW;
173 return MODEACTION_DENY;
181 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
182 return MODEACTION_DENY;
187 if (channel->GetExt("nickflood", dummy))
189 nickfloodsettings *f;
190 channel->GetExt("nickflood", f);
192 channel->Shrink("nickflood");
193 channel->SetModeParam('F', "");
194 return MODEACTION_ALLOW;
197 return MODEACTION_DENY;
201 class ModuleNickFlood : public Module
207 ModuleNickFlood(InspIRCd* Me)
210 if (!ServerInstance->Modes->AddMode(&jf))
211 throw ModuleException("Could not add new modes!");
212 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick, I_OnUserPostNick };
213 ServerInstance->Modules->Attach(eventlist, this, 3);
216 virtual int OnUserPreNick(User* user, const std::string &newnick)
218 if (isdigit(newnick[0])) /* allow switches to UID */
221 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
223 Channel *channel = i->first;
225 nickfloodsettings *f;
226 if (channel->GetExt("nickflood", f))
228 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
233 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);
241 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "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);
251 * 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.
253 virtual void OnUserPostNick(User* user, const std::string &oldnick)
255 if (isdigit(user->nick[0])) /* allow switches to UID */
258 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
260 Channel *channel = i->first;
262 nickfloodsettings *f;
263 if (channel->GetExt("nickflood", f))
265 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
268 /* moved this here to avoid incrementing the counter for nick
269 * changes that are denied for some other reason (bans, +N, etc.)
278 void OnChannelDelete(Channel* chan)
280 nickfloodsettings *f;
281 if (chan->GetExt("nickflood",f))
284 chan->Shrink("nickflood");
289 virtual ~ModuleNickFlood()
291 ServerInstance->Modes->DelMode(&jf);
294 virtual Version GetVersion()
296 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
300 MODULE_INIT(ModuleNickFlood)