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 "helperfuncs.h"
27 /* $ModDesc: Provides channel mode +j (join flood protection) */
29 class joinfloodsettings : public classbase
40 joinfloodsettings() : secs(0), joins(0) {};
41 joinfloodsettings(int b, int c) : secs(b), joins(c)
43 reset = time(NULL) + secs;
46 log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
52 log(DEBUG,"joinflood counter is %d",counter);
53 if (time(NULL) > reset)
55 log(DEBUG,"joinflood counter reset");
57 reset = time(NULL) + secs;
63 return (counter >= this->joins);
68 log(DEBUG,"joinflood counter clear");
76 if (time(NULL) > unlocktime)
91 log(DEBUG,"joinflood lock");
93 unlocktime = time(NULL) + 60;
98 class JoinFlood : public ModeHandler
101 JoinFlood() : ModeHandler('j', 1, 0, false, MODETYPE_CHANNEL, false) { }
103 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
105 joinfloodsettings* x;
106 if (channel->GetExt("joinflood",x))
107 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
109 return std::make_pair(false, parameter);
112 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
114 /* When TS is equal, the alphabetically later one wins */
115 return (their_param < our_param);
118 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
120 joinfloodsettings* dummy;
126 strlcpy(ndata,parameter.c_str(),MAXBUF);
142 /* Set up the flood parameters for this channel */
143 int njoins = atoi(joins);
144 int nsecs = atoi(secs);
145 if ((njoins<1) || (nsecs<1))
147 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
149 return MODEACTION_DENY;
153 if (!channel->GetExt("joinflood", dummy))
155 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
156 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
157 channel->Extend("joinflood", f);
158 channel->SetMode('j', true);
159 channel->SetModeParam('j', parameter.c_str(), true);
160 return MODEACTION_ALLOW;
166 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
167 return MODEACTION_DENY;
172 if (channel->GetExt("joinflood", dummy))
174 joinfloodsettings *f;
175 channel->GetExt("joinflood", f);
177 channel->Shrink("joinflood");
178 channel->SetMode('j', false);
179 return MODEACTION_ALLOW;
182 return MODEACTION_DENY;
186 class ModuleJoinFlood : public Module
193 ModuleJoinFlood(Server* Me)
197 jf = new JoinFlood();
198 Srv->AddMode(jf, 'j');
201 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
205 joinfloodsettings *f;
206 if (chan->GetExt("joinflood", f))
210 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
218 virtual void OnUserJoin(userrec* user, chanrec* channel)
220 joinfloodsettings *f;
221 if (channel->GetExt("joinflood",f))
228 channel->WriteChannelWithServ((char*)Srv->GetServerName().c_str(), "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);
233 void OnChannelDelete(chanrec* chan)
235 joinfloodsettings *f;
236 if (chan->GetExt("joinflood",f))
239 chan->Shrink("joinflood");
243 void Implements(char* List)
245 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
248 virtual void On005Numeric(std::string &output)
250 InsertMode(output, "j", 3);
253 virtual ~ModuleJoinFlood()
258 virtual Version GetVersion()
260 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
265 class ModuleJoinFloodFactory : public ModuleFactory
268 ModuleJoinFloodFactory()
272 ~ModuleJoinFloodFactory()
276 virtual Module * CreateModule(Server* Me)
278 return new ModuleJoinFlood(Me);
284 extern "C" void * init_module( void )
286 return new ModuleJoinFloodFactory;