2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /* $ModDesc: Provides channel mode +j (join flood protection) */
28 /** Holds settings and state associated with channel mode +j
30 class joinfloodsettings
40 joinfloodsettings(int b, int c) : secs(b), joins(c)
42 reset = ServerInstance->Time() + secs;
50 if (ServerInstance->Time() > reset)
53 reset = ServerInstance->Time() + secs;
59 return (counter >= this->joins);
71 if (ServerInstance->Time() > unlocktime)
87 unlocktime = ServerInstance->Time() + 60;
91 /** Handles channel mode +j
93 class JoinFlood : public ModeHandler
96 SimpleExtItem<joinfloodsettings> ext;
97 JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
98 ext("joinflood", Creator) { }
100 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
106 strlcpy(ndata,parameter.c_str(),MAXBUF);
123 /* Set up the flood parameters for this channel */
124 int njoins = atoi(joins);
125 int nsecs = atoi(secs);
126 if ((njoins<1) || (nsecs<1))
128 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
130 return MODEACTION_DENY;
134 joinfloodsettings* f = ext.get(channel);
137 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138 f = new joinfloodsettings(nsecs, njoins);
140 channel->SetModeParam('j', parameter);
141 return MODEACTION_ALLOW;
145 std::string cur_param = channel->GetModeParameter('j');
146 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
147 if (cur_param == parameter)
150 return MODEACTION_DENY;
154 // new mode param, replace old with new
155 if ((nsecs > 0) && (njoins > 0))
157 f = new joinfloodsettings(nsecs, njoins);
159 channel->SetModeParam('j', parameter);
160 return MODEACTION_ALLOW;
164 return MODEACTION_DENY;
172 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
173 return MODEACTION_DENY;
178 joinfloodsettings* f = ext.get(channel);
182 channel->SetModeParam('j', "");
183 return MODEACTION_ALLOW;
186 return MODEACTION_DENY;
190 class ModuleJoinFlood : public Module
202 ServerInstance->Modules->AddService(jf);
203 ServerInstance->Modules->AddService(jf.ext);
204 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
205 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
208 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven)
212 joinfloodsettings *f = jf.ext.get(chan);
213 if (f && f->islocked())
215 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
219 return MOD_RES_PASSTHRU;
222 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
224 /* We arent interested in JOIN events caused by a network burst */
228 joinfloodsettings *f = jf.ext.get(memb->chan);
230 /* But all others are OK */
238 memb->chan->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", memb->chan->name.c_str(), f->joins, f->secs);
245 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
249 MODULE_INIT(ModuleJoinFlood)