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) */
29 /** Holds settings and state associated with channel mode +j
31 class joinfloodsettings : public classbase
42 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 /** Handles channel mode +j
98 class JoinFlood : public ModeHandler
101 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
103 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
105 joinfloodsettings* x;
106 if (channel->GetExt("joinflood",x))
107 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
109 return std::make_pair(false, parameter);
112 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
114 /* When TS is equal, the alphabetically later one wins */
115 return (their_param < our_param);
118 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
120 joinfloodsettings* dummy;
124 ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
127 strlcpy(ndata,parameter.c_str(),MAXBUF);
143 /* Set up the flood parameters for this channel */
144 int njoins = atoi(joins);
145 int nsecs = atoi(secs);
146 if ((njoins<1) || (nsecs<1))
148 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
150 return MODEACTION_DENY;
154 if (!channel->GetExt("joinflood", dummy))
156 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
157 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
158 channel->Extend("joinflood", f);
159 channel->SetMode('j', true);
160 channel->SetModeParam('j', parameter.c_str(), true);
161 return MODEACTION_ALLOW;
165 std::string cur_param = channel->GetModeParameter('j');
166 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
167 if (cur_param == parameter)
170 return MODEACTION_DENY;
174 // new mode param, replace old with new
175 if ((nsecs > 0) && (njoins > 0))
177 joinfloodsettings* f;
178 channel->GetExt("joinflood", f);
180 f = new joinfloodsettings(nsecs,njoins);
181 channel->Shrink("joinflood");
182 channel->Extend("joinflood", f);
183 channel->SetModeParam('j', cur_param.c_str(), false);
184 channel->SetModeParam('j', parameter.c_str(), true);
185 return MODEACTION_ALLOW;
189 return MODEACTION_DENY;
197 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
198 return MODEACTION_DENY;
203 if (channel->GetExt("joinflood", dummy))
205 joinfloodsettings *f;
206 channel->GetExt("joinflood", f);
208 channel->Shrink("joinflood");
209 channel->SetMode('j', false);
210 return MODEACTION_ALLOW;
213 return MODEACTION_DENY;
217 class ModuleJoinFlood : public Module
224 ModuleJoinFlood(InspIRCd* Me)
228 jf = new JoinFlood(ServerInstance);
229 ServerInstance->AddMode(jf, 'j');
232 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
236 joinfloodsettings *f;
237 if (chan->GetExt("joinflood", f))
241 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
249 virtual void OnUserJoin(userrec* user, chanrec* channel)
251 joinfloodsettings *f;
252 if (channel->GetExt("joinflood",f))
259 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);
264 void OnChannelDelete(chanrec* chan)
266 joinfloodsettings *f;
267 if (chan->GetExt("joinflood",f))
270 chan->Shrink("joinflood");
274 void Implements(char* List)
276 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
279 virtual ~ModuleJoinFlood()
281 ServerInstance->Modes->DelMode(jf);
285 virtual Version GetVersion()
287 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
292 class ModuleJoinFloodFactory : public ModuleFactory
295 ModuleJoinFloodFactory()
299 ~ModuleJoinFloodFactory()
303 virtual Module * CreateModule(InspIRCd* Me)
305 return new ModuleJoinFlood(Me);
311 extern "C" void * init_module( void )
313 return new ModuleJoinFloodFactory;