1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
24 #include "configreader.h"
27 /* $ModDesc: Provides channel mode +j (join flood protection) */
31 class joinfloodsettings : public classbase
42 joinfloodsettings() : secs(0), joins(0) {};
43 joinfloodsettings(int b, int c) : secs(b), joins(c)
45 reset = time(NULL) + secs;
53 if (time(NULL) > reset)
56 reset = time(NULL) + secs;
62 return (counter >= this->joins);
74 if (time(NULL) > unlocktime)
90 unlocktime = time(NULL) + 60;
95 class JoinFlood : public ModeHandler
98 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
100 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
102 joinfloodsettings* x;
103 if (channel->GetExt("joinflood",x))
104 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
106 return std::make_pair(false, parameter);
109 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
111 /* When TS is equal, the alphabetically later one wins */
112 return (their_param < our_param);
115 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
117 joinfloodsettings* dummy;
121 ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
124 strlcpy(ndata,parameter.c_str(),MAXBUF);
140 /* Set up the flood parameters for this channel */
141 int njoins = atoi(joins);
142 int nsecs = atoi(secs);
143 if ((njoins<1) || (nsecs<1))
145 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
147 return MODEACTION_DENY;
151 if (!channel->GetExt("joinflood", dummy))
153 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
154 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
155 channel->Extend("joinflood", f);
156 channel->SetMode('j', true);
157 channel->SetModeParam('j', parameter.c_str(), true);
158 return MODEACTION_ALLOW;
162 std::string cur_param = channel->GetModeParameter('j');
163 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
164 if (cur_param == parameter)
167 return MODEACTION_DENY;
171 // new mode param, replace old with new
172 if ((nsecs > 0) && (njoins > 0))
174 joinfloodsettings* f;
175 channel->GetExt("joinflood", f);
177 f = new joinfloodsettings(nsecs,njoins);
178 channel->Shrink("joinflood");
179 channel->Extend("joinflood", f);
180 channel->SetModeParam('j', cur_param.c_str(), false);
181 channel->SetModeParam('j', parameter.c_str(), true);
182 return MODEACTION_ALLOW;
186 return MODEACTION_DENY;
194 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
195 return MODEACTION_DENY;
200 if (channel->GetExt("joinflood", dummy))
202 joinfloodsettings *f;
203 channel->GetExt("joinflood", f);
205 channel->Shrink("joinflood");
206 channel->SetMode('j', false);
207 return MODEACTION_ALLOW;
210 return MODEACTION_DENY;
214 class ModuleJoinFlood : public Module
221 ModuleJoinFlood(InspIRCd* Me)
225 jf = new JoinFlood(ServerInstance);
226 ServerInstance->AddMode(jf, 'j');
229 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
233 joinfloodsettings *f;
234 if (chan->GetExt("joinflood", f))
238 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
246 virtual void OnUserJoin(userrec* user, chanrec* channel)
248 joinfloodsettings *f;
249 if (channel->GetExt("joinflood",f))
256 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", channel->name, f->joins, f->secs);
261 void OnChannelDelete(chanrec* chan)
263 joinfloodsettings *f;
264 if (chan->GetExt("joinflood",f))
267 chan->Shrink("joinflood");
271 void Implements(char* List)
273 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
276 virtual ~ModuleJoinFlood()
278 ServerInstance->Modes->DelMode(jf);
282 virtual Version GetVersion()
284 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
289 class ModuleJoinFloodFactory : public ModuleFactory
292 ModuleJoinFloodFactory()
296 ~ModuleJoinFloodFactory()
300 virtual Module * CreateModule(InspIRCd* Me)
302 return new ModuleJoinFlood(Me);
308 extern "C" void * init_module( void )
310 return new ModuleJoinFloodFactory;