1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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
23 InspIRCd* ServerInstance;
32 joinfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), joins(c)
34 reset = ServerInstance->Time() + secs;
42 if (ServerInstance->Time() > reset)
45 reset = ServerInstance->Time() + secs;
51 return (counter >= this->joins);
63 if (ServerInstance->Time() > unlocktime)
79 unlocktime = ServerInstance->Time() + 60;
84 /** Handles channel mode +j
86 class JoinFlood : public ModeHandler
89 JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
91 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
94 if (channel->GetExt("joinflood",x))
95 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
97 return std::make_pair(false, parameter);
100 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
102 /* When TS is equal, the alphabetically later one wins */
103 return (their_param < our_param);
106 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
108 joinfloodsettings* dummy;
114 strlcpy(ndata,parameter.c_str(),MAXBUF);
131 /* Set up the flood parameters for this channel */
132 int njoins = atoi(joins);
133 int nsecs = atoi(secs);
134 if ((njoins<1) || (nsecs<1))
136 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
138 return MODEACTION_DENY;
142 if (!channel->GetExt("joinflood", dummy))
144 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
145 joinfloodsettings *f = new joinfloodsettings(ServerInstance, nsecs, njoins);
146 channel->Extend("joinflood", f);
147 channel->SetModeParam('j', parameter);
148 return MODEACTION_ALLOW;
152 std::string cur_param = channel->GetModeParameter('j');
153 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
154 if (cur_param == parameter)
157 return MODEACTION_DENY;
161 // new mode param, replace old with new
162 if ((nsecs > 0) && (njoins > 0))
164 joinfloodsettings* f;
165 channel->GetExt("joinflood", f);
167 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
168 channel->Shrink("joinflood");
169 channel->Extend("joinflood", f);
170 channel->SetModeParam('j', parameter);
171 return MODEACTION_ALLOW;
175 return MODEACTION_DENY;
183 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
184 return MODEACTION_DENY;
189 if (channel->GetExt("joinflood", dummy))
191 joinfloodsettings *f;
192 channel->GetExt("joinflood", f);
194 channel->Shrink("joinflood");
195 channel->SetModeParam('j', "");
196 return MODEACTION_ALLOW;
199 return MODEACTION_DENY;
203 class ModuleJoinFlood : public Module
210 ModuleJoinFlood(InspIRCd* Me)
214 jf = new JoinFlood(ServerInstance);
215 if (!ServerInstance->Modes->AddMode(jf))
216 throw ModuleException("Could not add new modes!");
217 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
218 ServerInstance->Modules->Attach(eventlist, this, 3);
221 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
225 joinfloodsettings *f;
226 if (chan->GetExt("joinflood", f))
230 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
238 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
240 joinfloodsettings *f;
242 /* We arent interested in JOIN events caused by a network burst */
246 /* But all others are OK */
247 if (channel->GetExt("joinflood",f))
254 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.c_str(), f->joins, f->secs);
259 void OnChannelDelete(Channel* chan)
261 joinfloodsettings *f;
262 if (chan->GetExt("joinflood",f))
265 chan->Shrink("joinflood");
270 virtual ~ModuleJoinFlood()
272 ServerInstance->Modes->DelMode(jf);
276 virtual Version GetVersion()
278 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
282 MODULE_INIT(ModuleJoinFlood)