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 std::pair<bool,std::string> ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
104 joinfloodsettings* x;
105 if (channel->GetExt("joinflood",x))
106 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
108 return std::make_pair(false, parameter);
111 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
113 joinfloodsettings* dummy;
119 strlcpy(ndata,parameter.c_str(),MAXBUF);
135 /* Set up the flood parameters for this channel */
136 int njoins = atoi(joins);
137 int nsecs = atoi(secs);
138 if ((njoins<1) || (nsecs<1))
140 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
142 return MODEACTION_DENY;
146 if (!channel->GetExt("joinflood", dummy))
148 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
149 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
150 channel->Extend("joinflood", f);
151 channel->SetMode('j', true);
152 channel->SetModeParam('j', parameter.c_str(), true);
153 return MODEACTION_ALLOW;
159 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
160 return MODEACTION_DENY;
165 if (channel->GetExt("joinflood", dummy))
167 joinfloodsettings *f;
168 channel->GetExt("joinflood", f);
170 channel->Shrink("joinflood");
171 channel->SetMode('j', false);
172 return MODEACTION_ALLOW;
175 return MODEACTION_DENY;
179 class ModuleJoinFlood : public Module
186 ModuleJoinFlood(Server* Me)
190 jf = new JoinFlood();
191 Srv->AddMode(jf, 'j');
194 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
198 joinfloodsettings *f;
199 if (chan->GetExt("joinflood", f))
203 WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
211 virtual void OnUserJoin(userrec* user, chanrec* channel)
213 joinfloodsettings *f;
214 if (channel->GetExt("joinflood",f))
221 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);
226 void OnChannelDelete(chanrec* chan)
228 joinfloodsettings *f;
229 if (chan->GetExt("joinflood",f))
232 chan->Shrink("joinflood");
236 void Implements(char* List)
238 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
241 virtual void On005Numeric(std::string &output)
243 InsertMode(output, "j", 3);
246 virtual ~ModuleJoinFlood()
251 virtual Version GetVersion()
253 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
258 class ModuleJoinFloodFactory : public ModuleFactory
261 ModuleJoinFloodFactory()
265 ~ModuleJoinFloodFactory()
269 virtual Module * CreateModule(Server* Me)
271 return new ModuleJoinFlood(Me);
277 extern "C" void * init_module( void )
279 return new ModuleJoinFloodFactory;