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 ModePair 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 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
113 /* When TS is equal, the alphabetically later one wins */
114 return (their_param < our_param);
117 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
119 joinfloodsettings* dummy;
125 strlcpy(ndata,parameter.c_str(),MAXBUF);
141 /* Set up the flood parameters for this channel */
142 int njoins = atoi(joins);
143 int nsecs = atoi(secs);
144 if ((njoins<1) || (nsecs<1))
146 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
148 return MODEACTION_DENY;
152 if (!channel->GetExt("joinflood", dummy))
154 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
155 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
156 channel->Extend("joinflood", f);
157 channel->SetMode('j', true);
158 channel->SetModeParam('j', parameter.c_str(), true);
159 return MODEACTION_ALLOW;
165 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
166 return MODEACTION_DENY;
171 if (channel->GetExt("joinflood", dummy))
173 joinfloodsettings *f;
174 channel->GetExt("joinflood", f);
176 channel->Shrink("joinflood");
177 channel->SetMode('j', false);
178 return MODEACTION_ALLOW;
181 return MODEACTION_DENY;
185 class ModuleJoinFlood : public Module
192 ModuleJoinFlood(Server* Me)
196 jf = new JoinFlood();
197 Srv->AddMode(jf, 'j');
200 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
204 joinfloodsettings *f;
205 if (chan->GetExt("joinflood", f))
209 WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
217 virtual void OnUserJoin(userrec* user, chanrec* channel)
219 joinfloodsettings *f;
220 if (channel->GetExt("joinflood",f))
227 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);
232 void OnChannelDelete(chanrec* chan)
234 joinfloodsettings *f;
235 if (chan->GetExt("joinflood",f))
238 chan->Shrink("joinflood");
242 void Implements(char* List)
244 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
247 virtual void On005Numeric(std::string &output)
249 InsertMode(output, "j", 3);
252 virtual ~ModuleJoinFlood()
257 virtual Version GetVersion()
259 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
264 class ModuleJoinFloodFactory : public ModuleFactory
267 ModuleJoinFloodFactory()
271 ~ModuleJoinFloodFactory()
275 virtual Module * CreateModule(Server* Me)
277 return new ModuleJoinFlood(Me);
283 extern "C" void * init_module( void )
285 return new ModuleJoinFloodFactory;