1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 * ---------------------------------------------------
19 /* $ModDesc: Provides channel mode +F (nick flood protection) */
21 /** Holds settings and state associated with channel mode +F
23 class nickfloodsettings : public classbase
33 InspIRCd* ServerInstance;
35 nickfloodsettings() : secs(0), nicks(0) {};
37 nickfloodsettings(int b, int c) : secs(b), nicks(c)
39 reset = time(NULL) + secs;
47 if (time(NULL) > reset)
50 reset = time(NULL) + secs;
56 return (counter >= this->nicks);
68 if (time(NULL) > unlocktime)
84 unlocktime = time(NULL) + 60;
89 /** Handles channel mode +j
91 class NickFlood : public ModeHandler
94 NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
96 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
99 if (channel->GetExt("nickflood",x))
100 return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
102 return std::make_pair(false, parameter);
105 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
107 /* When TS is equal, the alphabetically later one wins */
108 return (their_param < our_param);
111 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
113 nickfloodsettings* dummy;
119 strlcpy(ndata,parameter.c_str(),MAXBUF);
136 /* Set up the flood parameters for this channel */
137 int nnicks = atoi(nicks);
138 int nsecs = atoi(secs);
139 if ((nnicks<1) || (nsecs<1))
141 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
143 return MODEACTION_DENY;
147 if (!channel->GetExt("nickflood", dummy))
149 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
150 nickfloodsettings *f = new nickfloodsettings(nsecs,nnicks);
151 channel->Extend("nickflood", f);
152 channel->SetMode('F', true);
153 channel->SetModeParam('F', parameter.c_str(), true);
154 return MODEACTION_ALLOW;
158 std::string cur_param = channel->GetModeParameter('F');
159 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
160 if (cur_param == parameter)
163 return MODEACTION_DENY;
167 // new mode param, replace old with new
168 if ((nsecs > 0) && (nnicks > 0))
170 nickfloodsettings* f;
171 channel->GetExt("nickflood", f);
173 f = new nickfloodsettings(nsecs, nnicks);
174 channel->Shrink("nickflood");
175 channel->Extend("nickflood", f);
176 channel->SetModeParam('F', cur_param.c_str(), false);
177 channel->SetModeParam('F', parameter.c_str(), true);
178 return MODEACTION_ALLOW;
182 return MODEACTION_DENY;
190 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
191 return MODEACTION_DENY;
196 if (channel->GetExt("nickflood", dummy))
198 nickfloodsettings *f;
199 channel->GetExt("nickflood", f);
201 channel->Shrink("nickflood");
202 channel->SetMode('F', false);
203 return MODEACTION_ALLOW;
206 return MODEACTION_DENY;
210 class ModuleNickFlood : public Module
216 ModuleNickFlood(InspIRCd* Me)
220 jf = new NickFlood(ServerInstance);
221 if (!ServerInstance->AddMode(jf, 'F'))
222 throw ModuleException("Could not add new modes!");
225 virtual int OnUserPreNick(userrec* user, const std::string &newnick)
227 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
229 chanrec *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->WriteServ("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, channel->name, 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, f->nicks, f->secs);
257 void OnChannelDelete(chanrec* chan)
259 nickfloodsettings *f;
260 if (chan->GetExt("nickflood",f))
263 chan->Shrink("nickflood");
267 void Implements(char* List)
269 List[I_OnChannelDelete] = List[I_OnUserPreNick] = 1;
272 virtual ~ModuleNickFlood()
274 ServerInstance->Modes->DelMode(jf);
278 virtual Version GetVersion()
280 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
284 MODULE_INIT(ModuleNickFlood)