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 "configreader.h"
27 /* $ModDesc: Provides channel mode +j (join flood protection) */
31 class joinfloodsettings : public classbase
42 joinfloodsettings() : secs(0), joins(0) {};
43 joinfloodsettings(int b, int c) : secs(b), joins(c)
45 reset = time(NULL) + secs;
53 if (time(NULL) > reset)
56 reset = time(NULL) + secs;
62 return (counter >= this->joins);
74 if (time(NULL) > unlocktime)
90 unlocktime = time(NULL) + 60;
95 class JoinFlood : public ModeHandler
98 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
100 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
102 joinfloodsettings* x;
103 if (channel->GetExt("joinflood",x))
104 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
106 return std::make_pair(false, parameter);
109 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
111 /* When TS is equal, the alphabetically later one wins */
112 return (their_param < our_param);
115 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
117 joinfloodsettings* dummy;
123 strlcpy(ndata,parameter.c_str(),MAXBUF);
139 /* Set up the flood parameters for this channel */
140 int njoins = atoi(joins);
141 int nsecs = atoi(secs);
142 if ((njoins<1) || (nsecs<1))
144 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
146 return MODEACTION_DENY;
150 if (!channel->GetExt("joinflood", dummy))
152 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
153 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
154 channel->Extend("joinflood", f);
155 channel->SetMode('j', true);
156 channel->SetModeParam('j', parameter.c_str(), true);
157 return MODEACTION_ALLOW;
163 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
164 return MODEACTION_DENY;
169 if (channel->GetExt("joinflood", dummy))
171 joinfloodsettings *f;
172 channel->GetExt("joinflood", f);
174 channel->Shrink("joinflood");
175 channel->SetMode('j', false);
176 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
183 class ModuleJoinFlood : public Module
190 ModuleJoinFlood(InspIRCd* Me)
194 jf = new JoinFlood(ServerInstance);
195 ServerInstance->AddMode(jf, 'j');
198 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
202 joinfloodsettings *f;
203 if (chan->GetExt("joinflood", f))
207 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
215 virtual void OnUserJoin(userrec* user, chanrec* channel)
217 joinfloodsettings *f;
218 if (channel->GetExt("joinflood",f))
225 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);
230 void OnChannelDelete(chanrec* chan)
232 joinfloodsettings *f;
233 if (chan->GetExt("joinflood",f))
236 chan->Shrink("joinflood");
240 void Implements(char* List)
242 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
245 virtual ~ModuleJoinFlood()
250 virtual Version GetVersion()
252 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
257 class ModuleJoinFloodFactory : public ModuleFactory
260 ModuleJoinFloodFactory()
264 ~ModuleJoinFloodFactory()
268 virtual Module * CreateModule(InspIRCd* Me)
270 return new ModuleJoinFlood(Me);
276 extern "C" void * init_module( void )
278 return new ModuleJoinFloodFactory;