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"
26 /* $ModDesc: Provides channel mode +j (join flood protection) */
28 class joinfloodsettings : public classbase
39 joinfloodsettings() : secs(0), joins(0) {};
40 joinfloodsettings(int b, int c) : secs(b), joins(c)
42 reset = time(NULL) + secs;
45 log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
51 log(DEBUG,"joinflood counter is %d",counter);
52 if (time(NULL) > reset)
54 log(DEBUG,"joinflood counter reset");
56 reset = time(NULL) + secs;
62 return (counter >= this->joins);
67 log(DEBUG,"joinflood counter clear");
75 if (time(NULL) > unlocktime)
90 log(DEBUG,"joinflood lock");
92 unlocktime = time(NULL) + 60;
97 class JoinFlood : public ModeHandler
100 JoinFlood() : ModeHandler('j', 1, 0, false, MODETYPE_CHANNEL, false) { }
102 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
104 joinfloodsettings* dummy;
110 strlcpy(ndata,parameter.c_str(),MAXBUF);
126 /* Set up the flood parameters for this channel */
127 int njoins = atoi(joins);
128 int nsecs = atoi(secs);
129 if ((njoins<1) || (nsecs<1))
131 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
133 return MODEACTION_DENY;
137 if (!channel->GetExt("joinflood", dummy))
139 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
140 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
141 channel->Extend("joinflood", f);
142 channel->SetMode('j', true);
143 channel->SetModeParam('j', parameter.c_str(), true);
144 return MODEACTION_ALLOW;
150 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
151 return MODEACTION_DENY;
156 if (channel->GetExt("joinflood", dummy))
158 joinfloodsettings *f;
159 channel->GetExt("joinflood", f);
161 channel->Shrink("joinflood");
162 channel->SetMode('j', false);
163 return MODEACTION_ALLOW;
166 return MODEACTION_DENY;
170 class ModuleJoinFlood : public Module
177 ModuleJoinFlood(Server* Me)
181 jf = new JoinFlood();
182 Srv->AddMode(jf, 'j');
185 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
189 joinfloodsettings *f;
190 if (chan->GetExt("joinflood", f))
194 WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
202 virtual void OnUserJoin(userrec* user, chanrec* channel)
204 joinfloodsettings *f;
205 if (channel->GetExt("joinflood",f))
212 WriteChannelWithServ((char*)Srv->GetServerName().c_str(), channel, "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);
217 void OnChannelDelete(chanrec* chan)
219 joinfloodsettings *f;
220 if (chan->GetExt("joinflood",f))
223 chan->Shrink("joinflood");
227 void Implements(char* List)
229 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
232 virtual void On005Numeric(std::string &output)
234 InsertMode(output, "j", 3);
237 virtual ~ModuleJoinFlood()
242 virtual Version GetVersion()
244 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
249 class ModuleJoinFloodFactory : public ModuleFactory
252 ModuleJoinFloodFactory()
256 ~ModuleJoinFloodFactory()
260 virtual Module * CreateModule(Server* Me)
262 return new ModuleJoinFlood(Me);
268 extern "C" void * init_module( void )
270 return new ModuleJoinFloodFactory;