1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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
30 InspIRCd* ServerInstance;
32 nickfloodsettings() : secs(0), nicks(0) {};
34 nickfloodsettings(int b, int c) : secs(b), nicks(c)
36 reset = time(NULL) + secs;
44 if (time(NULL) > reset)
47 reset = time(NULL) + secs;
53 return (counter >= this->nicks);
65 if (time(NULL) > unlocktime)
81 unlocktime = time(NULL) + 60;
86 /** Handles channel mode +j
88 class NickFlood : public ModeHandler
91 NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
93 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
96 if (channel->GetExt("nickflood",x))
97 return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
99 return std::make_pair(false, parameter);
102 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
104 /* When TS is equal, the alphabetically later one wins */
105 return (their_param < our_param);
108 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
110 nickfloodsettings* dummy;
116 strlcpy(ndata,parameter.c_str(),MAXBUF);
133 /* Set up the flood parameters for this channel */
134 int nnicks = atoi(nicks);
135 int nsecs = atoi(secs);
136 if ((nnicks<1) || (nsecs<1))
138 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
140 return MODEACTION_DENY;
144 if (!channel->GetExt("nickflood", dummy))
146 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
147 nickfloodsettings *f = new nickfloodsettings(nsecs,nnicks);
148 channel->Extend("nickflood", f);
149 channel->SetMode('F', true);
150 channel->SetModeParam('F', parameter.c_str(), true);
151 return MODEACTION_ALLOW;
155 std::string cur_param = channel->GetModeParameter('F');
156 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
157 if (cur_param == parameter)
160 return MODEACTION_DENY;
164 // new mode param, replace old with new
165 if ((nsecs > 0) && (nnicks > 0))
167 nickfloodsettings* f;
168 channel->GetExt("nickflood", f);
170 f = new nickfloodsettings(nsecs, nnicks);
171 channel->Shrink("nickflood");
172 channel->Extend("nickflood", f);
173 channel->SetModeParam('F', cur_param.c_str(), false);
174 channel->SetModeParam('F', parameter.c_str(), true);
175 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
187 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
188 return MODEACTION_DENY;
193 if (channel->GetExt("nickflood", dummy))
195 nickfloodsettings *f;
196 channel->GetExt("nickflood", f);
198 channel->Shrink("nickflood");
199 channel->SetMode('F', false);
200 return MODEACTION_ALLOW;
203 return MODEACTION_DENY;
207 class ModuleNickFlood : public Module
213 ModuleNickFlood(InspIRCd* Me)
217 jf = new NickFlood(ServerInstance);
218 if (!ServerInstance->Modes->AddMode(jf))
219 throw ModuleException("Could not add new modes!");
220 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
221 ServerInstance->Modules->Attach(eventlist, this, 2);
224 virtual int OnUserPreNick(User* user, const std::string &newnick)
226 if (isdigit(newnick[0])) /* allow switches to UID */
229 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
231 Channel *channel = i->first;
233 nickfloodsettings *f;
234 if (channel->GetExt("nickflood", f))
236 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
241 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);
250 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);
259 void OnChannelDelete(Channel* chan)
261 nickfloodsettings *f;
262 if (chan->GetExt("nickflood",f))
265 chan->Shrink("nickflood");
270 virtual ~ModuleNickFlood()
272 ServerInstance->Modes->DelMode(jf);
276 virtual Version GetVersion()
278 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
282 MODULE_INIT(ModuleNickFlood)