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;
122 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;
161 std::string cur_param = channel->GetModeParameter('j');
162 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
163 if (cur_param == parameter)
166 return MODEACTION_DENY;
170 // new mode param, replace old with new
171 if ((nsecs > 0) && (njoins > 0))
173 joinfloodsettings* f;
174 channel->GetExt("joinflood", f);
176 f = new joinfloodsettings(nsecs,njoins);
177 channel->Shrink("joinflood");
178 channel->Extend("joinflood", f);
179 channel->SetModeParam('j', cur_param.c_str(), false);
180 channel->SetModeParam('j', parameter.c_str(), true);
181 return MODEACTION_ALLOW;
185 return MODEACTION_DENY;
193 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
194 return MODEACTION_DENY;
199 if (channel->GetExt("joinflood", dummy))
201 joinfloodsettings *f;
202 channel->GetExt("joinflood", f);
204 channel->Shrink("joinflood");
205 channel->SetMode('j', false);
206 return MODEACTION_ALLOW;
209 return MODEACTION_DENY;
213 class ModuleJoinFlood : public Module
220 ModuleJoinFlood(InspIRCd* Me)
224 jf = new JoinFlood(ServerInstance);
225 if (!ServerInstance->AddMode(jf, 'j'))
226 throw ModuleException("Could not add new modes!");
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;