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
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)
108 strlcpy(ndata,parameter.c_str(),MAXBUF);
124 /* Set up the flood parameters for this channel */
125 int njoins = atoi(joins);
126 int nsecs = atoi(secs);
127 if ((njoins<1) || (nsecs<1))
129 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
131 return MODEACTION_DENY;
135 if (!channel->GetExt("joinflood"))
137 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
139 channel->Extend("joinflood",(char*)f);
140 channel->SetMode('j', true);
141 channel->SetModeParam('j', parameter.c_str(), true);
142 return MODEACTION_ALLOW;
148 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
149 return MODEACTION_DENY;
154 if (channel->GetExt("joinflood"))
156 joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood");
158 channel->Shrink("joinflood");
159 channel->SetMode('j', false);
160 return MODEACTION_ALLOW;
163 return MODEACTION_DENY;
167 class ModuleJoinFlood : public Module
174 ModuleJoinFlood(Server* Me)
178 jf = new JoinFlood();
179 Srv->AddMode(jf, 'j');
182 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
186 joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
191 WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
199 virtual void OnUserJoin(userrec* user, chanrec* channel)
201 joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood");
209 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);
214 void OnChannelDelete(chanrec* chan)
216 if (chan->GetExt("joinflood"))
218 joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
220 chan->Shrink("joinflood");
224 void Implements(char* List)
226 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
229 virtual void On005Numeric(std::string &output)
231 InsertMode(output, "j", 3);
234 virtual ~ModuleJoinFlood()
239 virtual Version GetVersion()
241 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
246 class ModuleJoinFloodFactory : public ModuleFactory
249 ModuleJoinFloodFactory()
253 ~ModuleJoinFloodFactory()
257 virtual Module * CreateModule(Server* Me)
259 return new ModuleJoinFlood(Me);
265 extern "C" void * init_module( void )
267 return new ModuleJoinFloodFactory;