1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +j (join flood protection) */
18 /** Holds settings and state associated with channel mode +j
20 class joinfloodsettings : public classbase
30 InspIRCd* ServerInstance;
32 joinfloodsettings() : secs(0), joins(0) {};
34 joinfloodsettings(int b, int c) : secs(b), joins(c)
36 reset = time(NULL) + secs;
44 if (time(NULL) > reset)
47 reset = time(NULL) + secs;
53 return (counter >= this->joins);
65 if (time(NULL) > unlocktime)
81 unlocktime = time(NULL) + 60;
86 /** Handles channel mode +j
88 class JoinFlood : public ModeHandler
91 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
93 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
96 if (channel->GetExt("joinflood",x))
97 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
99 return std::make_pair(false, parameter);
102 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
104 /* When TS is equal, the alphabetically later one wins */
105 return (their_param < our_param);
108 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
110 joinfloodsettings* dummy;
116 strlcpy(ndata,parameter.c_str(),MAXBUF);
133 /* Set up the flood parameters for this channel */
134 int njoins = atoi(joins);
135 int nsecs = atoi(secs);
136 if ((njoins<1) || (nsecs<1))
138 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
140 return MODEACTION_DENY;
144 if (!channel->GetExt("joinflood", dummy))
146 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
147 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
148 channel->Extend("joinflood", f);
149 channel->SetMode('j', true);
150 channel->SetModeParam('j', parameter.c_str(), true);
151 return MODEACTION_ALLOW;
155 std::string cur_param = channel->GetModeParameter('j');
156 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
157 if (cur_param == parameter)
160 return MODEACTION_DENY;
164 // new mode param, replace old with new
165 if ((nsecs > 0) && (njoins > 0))
167 joinfloodsettings* f;
168 channel->GetExt("joinflood", f);
170 f = new joinfloodsettings(nsecs,njoins);
171 channel->Shrink("joinflood");
172 channel->Extend("joinflood", f);
173 channel->SetModeParam('j', cur_param.c_str(), false);
174 channel->SetModeParam('j', parameter.c_str(), true);
175 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
187 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
188 return MODEACTION_DENY;
193 if (channel->GetExt("joinflood", dummy))
195 joinfloodsettings *f;
196 channel->GetExt("joinflood", f);
198 channel->Shrink("joinflood");
199 channel->SetMode('j', false);
200 return MODEACTION_ALLOW;
203 return MODEACTION_DENY;
207 class ModuleJoinFlood : public Module
214 ModuleJoinFlood(InspIRCd* Me)
218 jf = new JoinFlood(ServerInstance);
219 if (!ServerInstance->Modes->AddMode(jf))
220 throw ModuleException("Could not add new modes!");
221 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
222 ServerInstance->Modules->Attach(eventlist, this, 3);
225 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
229 joinfloodsettings *f;
230 if (chan->GetExt("joinflood", f))
234 user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
242 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
244 joinfloodsettings *f;
246 /* We arent interested in JOIN events caused by a network burst */
250 /* But all others are OK */
251 if (channel->GetExt("joinflood",f))
258 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);
263 void OnChannelDelete(Channel* chan)
265 joinfloodsettings *f;
266 if (chan->GetExt("joinflood",f))
269 chan->Shrink("joinflood");
274 virtual ~ModuleJoinFlood()
276 ServerInstance->Modes->DelMode(jf);
280 virtual Version GetVersion()
282 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
286 MODULE_INIT(ModuleJoinFlood)