1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 #include "configreader.h"
22 /* $ModDesc: Provides channel mode +j (join flood protection) */
24 /** Holds settings and state associated with channel mode +j
26 class joinfloodsettings : public classbase
36 InspIRCd* ServerInstance;
38 joinfloodsettings() : secs(0), joins(0) {};
40 joinfloodsettings(int b, int c) : secs(b), joins(c)
42 reset = time(NULL) + secs;
50 if (time(NULL) > reset)
53 reset = time(NULL) + secs;
59 return (counter >= this->joins);
71 if (time(NULL) > unlocktime)
87 unlocktime = time(NULL) + 60;
92 /** Handles channel mode +j
94 class JoinFlood : public ModeHandler
97 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
99 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
101 joinfloodsettings* x;
102 if (channel->GetExt("joinflood",x))
103 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
105 return std::make_pair(false, parameter);
108 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
110 /* When TS is equal, the alphabetically later one wins */
111 return (their_param < our_param);
114 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
116 joinfloodsettings* dummy;
120 ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
123 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;
162 std::string cur_param = channel->GetModeParameter('j');
163 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
164 if (cur_param == parameter)
167 return MODEACTION_DENY;
171 // new mode param, replace old with new
172 if ((nsecs > 0) && (njoins > 0))
174 joinfloodsettings* f;
175 channel->GetExt("joinflood", f);
177 f = new joinfloodsettings(nsecs,njoins);
178 channel->Shrink("joinflood");
179 channel->Extend("joinflood", f);
180 channel->SetModeParam('j', cur_param.c_str(), false);
181 channel->SetModeParam('j', parameter.c_str(), true);
182 return MODEACTION_ALLOW;
186 return MODEACTION_DENY;
194 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
195 return MODEACTION_DENY;
200 if (channel->GetExt("joinflood", dummy))
202 joinfloodsettings *f;
203 channel->GetExt("joinflood", f);
205 channel->Shrink("joinflood");
206 channel->SetMode('j', false);
207 return MODEACTION_ALLOW;
210 return MODEACTION_DENY;
214 class ModuleJoinFlood : public Module
221 ModuleJoinFlood(InspIRCd* Me)
225 jf = new JoinFlood(ServerInstance);
226 ServerInstance->AddMode(jf, 'j');
229 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
233 joinfloodsettings *f;
234 if (chan->GetExt("joinflood", f))
238 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
246 virtual void OnUserJoin(userrec* user, chanrec* channel)
248 joinfloodsettings *f;
249 if (channel->GetExt("joinflood",f))
256 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);
261 void OnChannelDelete(chanrec* chan)
263 joinfloodsettings *f;
264 if (chan->GetExt("joinflood",f))
267 chan->Shrink("joinflood");
271 void Implements(char* List)
273 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
276 virtual ~ModuleJoinFlood()
278 ServerInstance->Modes->DelMode(jf);
282 virtual Version GetVersion()
284 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
289 class ModuleJoinFloodFactory : public ModuleFactory
292 ModuleJoinFloodFactory()
296 ~ModuleJoinFloodFactory()
300 virtual Module * CreateModule(InspIRCd* Me)
302 return new ModuleJoinFlood(Me);
308 extern "C" void * init_module( void )
310 return new ModuleJoinFloodFactory;