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 return (counter >= this->nicks);
63 if (ServerInstance->Time() > unlocktime)
79 unlocktime = ServerInstance->Time() + 60;
84 /** Handles channel mode +j
86 class NickFlood : public ModeHandler
89 NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
91 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
94 if (channel->GetExt("nickflood",x))
95 return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
97 return std::make_pair(false, parameter);
100 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
102 /* When TS is equal, the alphabetically later one wins */
103 return (their_param < our_param);
106 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
108 nickfloodsettings* dummy;
114 strlcpy(ndata,parameter.c_str(),MAXBUF);
131 /* Set up the flood parameters for this channel */
132 int nnicks = atoi(nicks);
133 int nsecs = atoi(secs);
134 if ((nnicks<1) || (nsecs<1))
136 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
138 return MODEACTION_DENY;
142 if (!channel->GetExt("nickflood", dummy))
144 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
145 nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
146 channel->Extend("nickflood", f);
147 channel->SetMode('F', true);
148 channel->SetModeParam('F', parameter.c_str(), true);
149 return MODEACTION_ALLOW;
153 std::string cur_param = channel->GetModeParameter('F');
154 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
155 if (cur_param == parameter)
158 return MODEACTION_DENY;
162 // new mode param, replace old with new
163 if ((nsecs > 0) && (nnicks > 0))
165 nickfloodsettings* f;
166 channel->GetExt("nickflood", f);
168 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
169 channel->Shrink("nickflood");
170 channel->Extend("nickflood", f);
171 channel->SetModeParam('F', cur_param.c_str(), false);
172 channel->SetModeParam('F', parameter.c_str(), true);
173 return MODEACTION_ALLOW;
177 return MODEACTION_DENY;
185 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
186 return MODEACTION_DENY;
191 if (channel->GetExt("nickflood", dummy))
193 nickfloodsettings *f;
194 channel->GetExt("nickflood", f);
196 channel->Shrink("nickflood");
197 channel->SetMode('F', false);
198 return MODEACTION_ALLOW;
201 return MODEACTION_DENY;
205 class ModuleNickFlood : public Module
211 ModuleNickFlood(InspIRCd* Me)
215 jf = new NickFlood(ServerInstance);
216 if (!ServerInstance->Modes->AddMode(jf))
217 throw ModuleException("Could not add new modes!");
218 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
219 ServerInstance->Modules->Attach(eventlist, this, 2);
222 virtual int OnUserPreNick(User* user, const std::string &newnick)
224 if (isdigit(newnick[0])) /* allow switches to UID */
227 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
229 Channel *channel = i->first;
231 nickfloodsettings *f;
232 if (channel->GetExt("nickflood", f))
234 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
239 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);
248 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);
257 void OnChannelDelete(Channel* chan)
259 nickfloodsettings *f;
260 if (chan->GetExt("nickflood",f))
263 chan->Shrink("nickflood");
268 virtual ~ModuleNickFlood()
270 ServerInstance->Modes->DelMode(jf);
274 virtual Version GetVersion()
276 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
280 MODULE_INIT(ModuleNickFlood)