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 /* $ModDesc: Provides channel mode +j (join flood protection) */
21 /** Holds settings and state associated with channel mode +j
23 class joinfloodsettings : public classbase
33 InspIRCd* ServerInstance;
35 joinfloodsettings() : secs(0), joins(0) {};
37 joinfloodsettings(int b, int c) : secs(b), joins(c)
39 reset = time(NULL) + secs;
47 if (time(NULL) > reset)
50 reset = time(NULL) + secs;
56 return (counter >= this->joins);
68 if (time(NULL) > unlocktime)
84 unlocktime = time(NULL) + 60;
89 /** Handles channel mode +j
91 class JoinFlood : public ModeHandler
94 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
96 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
99 if (channel->GetExt("joinflood",x))
100 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
102 return std::make_pair(false, parameter);
105 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
107 /* When TS is equal, the alphabetically later one wins */
108 return (their_param < our_param);
111 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
113 joinfloodsettings* dummy;
119 strlcpy(ndata,parameter.c_str(),MAXBUF);
136 /* Set up the flood parameters for this channel */
137 int njoins = atoi(joins);
138 int nsecs = atoi(secs);
139 if ((njoins<1) || (nsecs<1))
141 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
143 return MODEACTION_DENY;
147 if (!channel->GetExt("joinflood", dummy))
149 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
150 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
151 channel->Extend("joinflood", f);
152 channel->SetMode('j', true);
153 channel->SetModeParam('j', parameter.c_str(), true);
154 return MODEACTION_ALLOW;
158 std::string cur_param = channel->GetModeParameter('j');
159 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
160 if (cur_param == parameter)
163 return MODEACTION_DENY;
167 // new mode param, replace old with new
168 if ((nsecs > 0) && (njoins > 0))
170 joinfloodsettings* f;
171 channel->GetExt("joinflood", f);
173 f = new joinfloodsettings(nsecs,njoins);
174 channel->Shrink("joinflood");
175 channel->Extend("joinflood", f);
176 channel->SetModeParam('j', cur_param.c_str(), false);
177 channel->SetModeParam('j', parameter.c_str(), true);
178 return MODEACTION_ALLOW;
182 return MODEACTION_DENY;
190 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
191 return MODEACTION_DENY;
196 if (channel->GetExt("joinflood", dummy))
198 joinfloodsettings *f;
199 channel->GetExt("joinflood", f);
201 channel->Shrink("joinflood");
202 channel->SetMode('j', false);
203 return MODEACTION_ALLOW;
206 return MODEACTION_DENY;
210 class ModuleJoinFlood : public Module
217 ModuleJoinFlood(InspIRCd* Me)
221 jf = new JoinFlood(ServerInstance);
222 if (!ServerInstance->AddMode(jf, 'j'))
223 throw ModuleException("Could not add new modes!");
226 virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
230 joinfloodsettings *f;
231 if (chan->GetExt("joinflood", f))
235 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
243 virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
245 joinfloodsettings *f;
246 if (channel->GetExt("joinflood",f))
253 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);
258 void OnChannelDelete(chanrec* chan)
260 joinfloodsettings *f;
261 if (chan->GetExt("joinflood",f))
264 chan->Shrink("joinflood");
268 void Implements(char* List)
270 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
273 virtual ~ModuleJoinFlood()
275 ServerInstance->Modes->DelMode(jf);
279 virtual Version GetVersion()
281 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
285 MODULE_INIT(ModuleJoinFlood);