2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2016-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 ERR_UNAVAILRESOURCE = 437
34 // The number of seconds the channel will be closed for.
35 static unsigned int duration;
37 /** Holds settings and state associated with channel mode +j
39 class joinfloodsettings
48 joinfloodsettings(unsigned int b, unsigned int c)
49 : secs(b), joins(c), unlocktime(0), counter(0)
51 reset = ServerInstance->Time() + secs;
56 if (ServerInstance->Time() > reset)
59 reset = ServerInstance->Time() + secs;
67 return (counter >= this->joins);
77 if (ServerInstance->Time() > unlocktime)
80 return (unlocktime != 0);
85 unlocktime = ServerInstance->Time() + duration;
88 bool operator==(const joinfloodsettings& other) const
90 return ((this->secs == other.secs) && (this->joins == other.joins));
94 /** Handles channel mode +j
96 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
99 JoinFlood(Module* Creator)
100 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
102 syntax = "<joins>:<seconds>";
105 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
107 std::string::size_type colon = parameter.find(':');
108 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
110 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
111 return MODEACTION_DENY;
114 /* Set up the flood parameters for this channel */
115 unsigned int njoins = ConvToNum<unsigned int>(parameter.substr(0, colon));
116 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
117 if ((njoins<1) || (nsecs<1))
119 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
120 return MODEACTION_DENY;
123 ext.set(channel, new joinfloodsettings(nsecs, njoins));
124 return MODEACTION_ALLOW;
127 void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
129 out.append(ConvToStr(jfs->joins)).push_back(':');
130 out.append(ConvToStr(jfs->secs));
134 class ModuleJoinFlood : public Module
144 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
146 ConfigTag* tag = ServerInstance->Config->ConfValue("joinflood");
147 duration = tag->getDuration("duration", 60, 10, 600);
150 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
154 joinfloodsettings *f = jf.ext.get(chan);
155 if (f && f->islocked())
157 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, "This channel is temporarily unavailable (+j is set). Please try again later.");
161 return MOD_RES_PASSTHRU;
164 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
166 /* We arent interested in JOIN events caused by a network burst */
170 joinfloodsettings *f = jf.ext.get(memb->chan);
172 /* But all others are OK */
173 if ((f) && (!f->islocked()))
180 memb->chan->WriteNotice(InspIRCd::Format("This channel has been closed to new users for %u seconds because there have been more than %d joins in %d seconds.", duration, f->joins, f->secs));
185 Version GetVersion() CXX11_OVERRIDE
187 return Version("Provides channel mode +j, join flood protection", VF_VENDOR);
191 MODULE_INIT(ModuleJoinFlood)