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 SimpleExtItem<nickfloodsettings> ext;
94 NickFlood(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'F', 1, 0, false, MODETYPE_CHANNEL, false),
95 ext("nickflood", Creator) { }
97 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
99 nickfloodsettings* x = ext.get(channel);
101 return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
103 return std::make_pair(false, parameter);
106 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
108 nickfloodsettings *f = ext.get(channel);
113 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;
142 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
143 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
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 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
164 channel->SetModeParam('F', parameter);
165 return MODEACTION_ALLOW;
169 return MODEACTION_DENY;
177 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
178 return MODEACTION_DENY;
186 channel->SetModeParam('F', "");
187 return MODEACTION_ALLOW;
190 return MODEACTION_DENY;
194 class ModuleNickFlood : public Module
200 ModuleNickFlood(InspIRCd* Me)
201 : Module(Me), nf(Me, this)
203 if (!ServerInstance->Modes->AddMode(&nf))
204 throw ModuleException("Could not add new modes!");
205 Extensible::Register(&nf.ext);
206 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
207 ServerInstance->Modules->Attach(eventlist, this, 2);
210 ModResult OnUserPreNick(User* user, const std::string &newnick)
212 if (isdigit(newnick[0])) /* allow switches to UID */
213 return MOD_RES_PASSTHRU;
215 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
217 Channel *channel = i->first;
219 nickfloodsettings *f = nf.ext.get(channel);
222 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
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, "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->first;
256 nickfloodsettings *f = nf.ext.get(channel);
259 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
262 /* moved this here to avoid incrementing the counter for nick
263 * changes that are denied for some other reason (bans, +N, etc.)
274 ServerInstance->Modes->DelMode(&nf);
279 return Version("Channel mode F - nick flood protection", VF_COMMON | VF_VENDOR);
283 MODULE_INIT(ModuleNickFlood)