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"
25 #include "configreader.h"
28 /* $ModDesc: Provides channel mode +j (join flood protection) */
30 extern InspIRCd* ServerInstance;
32 class joinfloodsettings : public classbase
43 joinfloodsettings() : secs(0), joins(0) {};
44 joinfloodsettings(int b, int c) : secs(b), joins(c)
46 reset = time(NULL) + secs;
49 log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
55 log(DEBUG,"joinflood counter is %d",counter);
56 if (time(NULL) > reset)
58 log(DEBUG,"joinflood counter reset");
60 reset = time(NULL) + secs;
66 return (counter >= this->joins);
71 log(DEBUG,"joinflood counter clear");
79 if (time(NULL) > unlocktime)
94 log(DEBUG,"joinflood lock");
96 unlocktime = time(NULL) + 60;
101 class JoinFlood : public ModeHandler
104 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
106 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
108 joinfloodsettings* x;
109 if (channel->GetExt("joinflood",x))
110 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
112 return std::make_pair(false, parameter);
115 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
117 /* When TS is equal, the alphabetically later one wins */
118 return (their_param < our_param);
121 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
123 joinfloodsettings* dummy;
129 strlcpy(ndata,parameter.c_str(),MAXBUF);
145 /* Set up the flood parameters for this channel */
146 int njoins = atoi(joins);
147 int nsecs = atoi(secs);
148 if ((njoins<1) || (nsecs<1))
150 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
152 return MODEACTION_DENY;
156 if (!channel->GetExt("joinflood", dummy))
158 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
159 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
160 channel->Extend("joinflood", f);
161 channel->SetMode('j', true);
162 channel->SetModeParam('j', parameter.c_str(), true);
163 return MODEACTION_ALLOW;
169 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
170 return MODEACTION_DENY;
175 if (channel->GetExt("joinflood", dummy))
177 joinfloodsettings *f;
178 channel->GetExt("joinflood", f);
180 channel->Shrink("joinflood");
181 channel->SetMode('j', false);
182 return MODEACTION_ALLOW;
185 return MODEACTION_DENY;
189 class ModuleJoinFlood : public Module
196 ModuleJoinFlood(Server* Me)
200 jf = new JoinFlood(ServerInstance);
201 Srv->AddMode(jf, 'j');
204 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
208 joinfloodsettings *f;
209 if (chan->GetExt("joinflood", f))
213 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
221 virtual void OnUserJoin(userrec* user, chanrec* channel)
223 joinfloodsettings *f;
224 if (channel->GetExt("joinflood",f))
231 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);
236 void OnChannelDelete(chanrec* chan)
238 joinfloodsettings *f;
239 if (chan->GetExt("joinflood",f))
242 chan->Shrink("joinflood");
246 void Implements(char* List)
248 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
251 virtual void On005Numeric(std::string &output)
253 ServerInstance->ModeGrok->InsertMode(output, "j", 3);
256 virtual ~ModuleJoinFlood()
261 virtual Version GetVersion()
263 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
268 class ModuleJoinFloodFactory : public ModuleFactory
271 ModuleJoinFloodFactory()
275 ~ModuleJoinFloodFactory()
279 virtual Module * CreateModule(Server* Me)
281 return new ModuleJoinFlood(Me);
287 extern "C" void * init_module( void )
289 return new ModuleJoinFloodFactory;