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 * ---------------------------------------------------
25 #include "configreader.h"
28 /* $ModDesc: Provides channel mode +j (join flood protection) */
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;
54 if (time(NULL) > reset)
57 reset = time(NULL) + secs;
63 return (counter >= this->joins);
75 if (time(NULL) > unlocktime)
91 unlocktime = time(NULL) + 60;
96 class JoinFlood : public ModeHandler
99 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
101 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
103 joinfloodsettings* x;
104 if (channel->GetExt("joinflood",x))
105 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
107 return std::make_pair(false, parameter);
110 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
112 /* When TS is equal, the alphabetically later one wins */
113 return (their_param < our_param);
116 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
118 joinfloodsettings* dummy;
124 strlcpy(ndata,parameter.c_str(),MAXBUF);
140 /* Set up the flood parameters for this channel */
141 int njoins = atoi(joins);
142 int nsecs = atoi(secs);
143 if ((njoins<1) || (nsecs<1))
145 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
147 return MODEACTION_DENY;
151 if (!channel->GetExt("joinflood", dummy))
153 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
154 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
155 channel->Extend("joinflood", f);
156 channel->SetMode('j', true);
157 channel->SetModeParam('j', parameter.c_str(), true);
158 return MODEACTION_ALLOW;
164 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
165 return MODEACTION_DENY;
170 if (channel->GetExt("joinflood", dummy))
172 joinfloodsettings *f;
173 channel->GetExt("joinflood", f);
175 channel->Shrink("joinflood");
176 channel->SetMode('j', false);
177 return MODEACTION_ALLOW;
180 return MODEACTION_DENY;
184 class ModuleJoinFlood : public Module
191 ModuleJoinFlood(InspIRCd* Me)
195 jf = new JoinFlood(ServerInstance);
196 ServerInstance->AddMode(jf, 'j');
199 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
203 joinfloodsettings *f;
204 if (chan->GetExt("joinflood", f))
208 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
216 virtual void OnUserJoin(userrec* user, chanrec* channel)
218 joinfloodsettings *f;
219 if (channel->GetExt("joinflood",f))
226 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);
231 void OnChannelDelete(chanrec* chan)
233 joinfloodsettings *f;
234 if (chan->GetExt("joinflood",f))
237 chan->Shrink("joinflood");
241 void Implements(char* List)
243 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
246 virtual void On005Numeric(std::string &output)
248 ServerInstance->Modes->InsertMode(output, "j", 3);
251 virtual ~ModuleJoinFlood()
256 virtual Version GetVersion()
258 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
263 class ModuleJoinFloodFactory : public ModuleFactory
266 ModuleJoinFloodFactory()
270 ~ModuleJoinFloodFactory()
274 virtual Module * CreateModule(InspIRCd* Me)
276 return new ModuleJoinFlood(Me);
282 extern "C" void * init_module( void )
284 return new ModuleJoinFloodFactory;