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
41 InspIRCd* ServerInstance;
43 joinfloodsettings() : secs(0), joins(0) {};
45 joinfloodsettings(int b, int c) : secs(b), joins(c)
47 reset = ServerInstance->Time() + secs;
55 if (ServerInstance->Time() > reset)
58 reset = ServerInstance->Time() + secs;
64 return (counter >= this->joins);
76 if (ServerInstance->Time() > unlocktime)
92 unlocktime = ServerInstance->Time() + 60;
97 /** Handles channel mode +j
99 class JoinFlood : public ModeHandler
102 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
104 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
106 joinfloodsettings* x;
107 if (channel->GetExt("joinflood",x))
108 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
110 return std::make_pair(false, parameter);
113 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
115 /* When TS is equal, the alphabetically later one wins */
116 return (their_param < our_param);
119 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
121 joinfloodsettings* dummy;
125 ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
128 strlcpy(ndata,parameter.c_str(),MAXBUF);
144 /* Set up the flood parameters for this channel */
145 int njoins = atoi(joins);
146 int nsecs = atoi(secs);
147 if ((njoins<1) || (nsecs<1))
149 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
151 return MODEACTION_DENY;
155 if (!channel->GetExt("joinflood", dummy))
157 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
158 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
159 channel->Extend("joinflood", f);
160 channel->SetMode('j', true);
161 channel->SetModeParam('j', parameter.c_str(), true);
162 return MODEACTION_ALLOW;
166 std::string cur_param = channel->GetModeParameter('j');
167 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
168 if (cur_param == parameter)
171 return MODEACTION_DENY;
175 // new mode param, replace old with new
176 if ((nsecs > 0) && (njoins > 0))
178 joinfloodsettings* f;
179 channel->GetExt("joinflood", f);
181 f = new joinfloodsettings(nsecs,njoins);
182 channel->Shrink("joinflood");
183 channel->Extend("joinflood", f);
184 channel->SetModeParam('j', cur_param.c_str(), false);
185 channel->SetModeParam('j', parameter.c_str(), true);
186 return MODEACTION_ALLOW;
190 return MODEACTION_DENY;
198 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
199 return MODEACTION_DENY;
204 if (channel->GetExt("joinflood", dummy))
206 joinfloodsettings *f;
207 channel->GetExt("joinflood", f);
209 channel->Shrink("joinflood");
210 channel->SetMode('j', false);
211 return MODEACTION_ALLOW;
214 return MODEACTION_DENY;
218 class ModuleJoinFlood : public Module
225 ModuleJoinFlood(InspIRCd* Me)
229 jf = new JoinFlood(ServerInstance);
230 ServerInstance->AddMode(jf, 'j');
233 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
237 joinfloodsettings *f;
238 if (chan->GetExt("joinflood", f))
242 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
250 virtual void OnUserJoin(userrec* user, chanrec* channel)
252 joinfloodsettings *f;
253 if (channel->GetExt("joinflood",f))
260 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);
265 void OnChannelDelete(chanrec* chan)
267 joinfloodsettings *f;
268 if (chan->GetExt("joinflood",f))
271 chan->Shrink("joinflood");
275 void Implements(char* List)
277 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
280 virtual ~ModuleJoinFlood()
282 ServerInstance->Modes->DelMode(jf);
286 virtual Version GetVersion()
288 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
293 class ModuleJoinFloodFactory : public ModuleFactory
296 ModuleJoinFloodFactory()
300 ~ModuleJoinFloodFactory()
304 virtual Module * CreateModule(InspIRCd* Me)
306 return new ModuleJoinFlood(Me);
312 extern "C" void * init_module( void )
314 return new ModuleJoinFloodFactory;