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', parameter);
148 return MODEACTION_ALLOW;
152 std::string cur_param = channel->GetModeParameter('F');
153 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
154 if (cur_param == parameter)
157 return MODEACTION_DENY;
161 // new mode param, replace old with new
162 if ((nsecs > 0) && (nnicks > 0))
164 nickfloodsettings* f;
165 channel->GetExt("nickflood", f);
167 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
168 channel->Shrink("nickflood");
169 channel->Extend("nickflood", f);
170 channel->SetMode('F', parameter);
171 return MODEACTION_ALLOW;
175 return MODEACTION_DENY;
183 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
184 return MODEACTION_DENY;
189 if (channel->GetExt("nickflood", dummy))
191 nickfloodsettings *f;
192 channel->GetExt("nickflood", f);
194 channel->Shrink("nickflood");
195 channel->SetMode('F', "");
196 return MODEACTION_ALLOW;
199 return MODEACTION_DENY;
203 class ModuleNickFlood : public Module
209 ModuleNickFlood(InspIRCd* Me)
213 jf = new NickFlood(ServerInstance);
214 if (!ServerInstance->Modes->AddMode(jf))
215 throw ModuleException("Could not add new modes!");
216 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
217 ServerInstance->Modules->Attach(eventlist, this, 2);
220 virtual int OnUserPreNick(User* user, const std::string &newnick)
222 if (isdigit(newnick[0])) /* allow switches to UID */
225 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
227 Channel *channel = i->first;
229 nickfloodsettings *f;
230 if (channel->GetExt("nickflood", f))
232 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
237 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);
246 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);
255 void OnChannelDelete(Channel* chan)
257 nickfloodsettings *f;
258 if (chan->GetExt("nickflood",f))
261 chan->Shrink("nickflood");
266 virtual ~ModuleNickFlood()
268 ServerInstance->Modes->DelMode(jf);
272 virtual Version GetVersion()
274 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
278 MODULE_INIT(ModuleNickFlood)