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/>.
27 #include "modules/server.h"
32 ERR_UNAVAILRESOURCE = 437
35 // The number of seconds the channel will be closed for.
36 static unsigned int duration;
38 /** Holds settings and state associated with channel mode +j
40 class joinfloodsettings
49 joinfloodsettings(unsigned int b, unsigned int c)
50 : secs(b), joins(c), unlocktime(0), counter(0)
52 reset = ServerInstance->Time() + secs;
57 if (ServerInstance->Time() > reset)
60 reset = ServerInstance->Time() + secs;
68 return (counter >= this->joins);
78 if (ServerInstance->Time() > unlocktime)
81 return (unlocktime != 0);
86 unlocktime = ServerInstance->Time() + duration;
89 bool operator==(const joinfloodsettings& other) const
91 return ((this->secs == other.secs) && (this->joins == other.joins));
95 /** Handles channel mode +j
97 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
100 JoinFlood(Module* Creator)
101 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
103 syntax = "<joins>:<seconds>";
106 ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
108 std::string::size_type colon = parameter.find(':');
109 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
111 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
112 return MODEACTION_DENY;
115 /* Set up the flood parameters for this channel */
116 unsigned int njoins = ConvToNum<unsigned int>(parameter.substr(0, colon));
117 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
118 if ((njoins<1) || (nsecs<1))
120 source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
121 return MODEACTION_DENY;
124 ext.set(channel, new joinfloodsettings(nsecs, njoins));
125 return MODEACTION_ALLOW;
128 void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
130 out.append(ConvToStr(jfs->joins)).push_back(':');
131 out.append(ConvToStr(jfs->secs));
135 class ModuleJoinFlood
137 , public ServerProtocol::LinkEventListener
142 unsigned long bootwait;
143 unsigned long splitwait;
146 // Stop GCC warnings about the deprecated OnServerSplit event.
147 using ServerProtocol::LinkEventListener::OnServerSplit;
150 : ServerProtocol::LinkEventListener(this)
156 void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
158 ConfigTag* tag = ServerInstance->Config->ConfValue("joinflood");
159 duration = tag->getDuration("duration", 60, 10, 600);
160 bootwait = tag->getDuration("bootwait", 30);
161 splitwait = tag->getDuration("splitwait", 30);
163 ignoreuntil = ServerInstance->startup_time + bootwait;
166 void OnServerSplit(const Server* server, bool error) CXX11_OVERRIDE
169 ignoreuntil = ServerInstance->Time() + splitwait;
172 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
176 joinfloodsettings *f = jf.ext.get(chan);
177 if (f && f->islocked())
179 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, "This channel is temporarily unavailable (+j is set). Please try again later.");
183 return MOD_RES_PASSTHRU;
186 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
188 /* We arent interested in JOIN events caused by a network burst */
189 if (sync || ignoreuntil > ServerInstance->Time())
192 joinfloodsettings *f = jf.ext.get(memb->chan);
194 /* But all others are OK */
195 if ((f) && (!f->islocked()))
202 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));
207 Version GetVersion() CXX11_OVERRIDE
209 return Version("Adds channel mode j (joinflood) which helps protect against spammers which mass-join channels.", VF_VENDOR);
213 MODULE_INIT(ModuleJoinFlood)