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 /** Holds settings and state associated with channel mode +j
28 class joinfloodsettings
37 joinfloodsettings(unsigned int b, unsigned int c)
38 : secs(b), joins(c), unlocktime(0), counter(0)
40 reset = ServerInstance->Time() + secs;
45 if (ServerInstance->Time() > reset)
48 reset = ServerInstance->Time() + secs;
56 return (counter >= this->joins);
66 if (ServerInstance->Time() > unlocktime)
69 return (unlocktime != 0);
74 unlocktime = ServerInstance->Time() + 60;
77 bool operator==(const joinfloodsettings& other) const
79 return ((this->secs == other.secs) && (this->joins == other.joins));
83 /** Handles channel mode +j
85 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
88 JoinFlood(Module* Creator)
89 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
93 ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
95 std::string::size_type colon = parameter.find(':');
96 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
98 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
99 return MODEACTION_DENY;
102 /* Set up the flood parameters for this channel */
103 unsigned int njoins = ConvToInt(parameter.substr(0, colon));
104 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
105 if ((njoins<1) || (nsecs<1))
107 source->WriteNumeric(608, channel->name, "Invalid flood parameter");
108 return MODEACTION_DENY;
111 ext.set(channel, new joinfloodsettings(nsecs, njoins));
112 return MODEACTION_ALLOW;
115 void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
117 out.append(ConvToStr(jfs->joins)).push_back(':');
118 out.append(ConvToStr(jfs->secs));
122 class ModuleJoinFlood : public Module
132 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
136 joinfloodsettings *f = jf.ext.get(chan);
137 if (f && f->islocked())
139 user->WriteNumeric(609, chan->name, "This channel is temporarily unavailable (+j). Please try again later.");
143 return MOD_RES_PASSTHRU;
146 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
148 /* We arent interested in JOIN events caused by a network burst */
152 joinfloodsettings *f = jf.ext.get(memb->chan);
154 /* But all others are OK */
155 if ((f) && (!f->islocked()))
162 memb->chan->WriteNotice(InspIRCd::Format("This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", f->joins, f->secs));
167 Version GetVersion() CXX11_OVERRIDE
169 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
173 MODULE_INIT(ModuleJoinFlood)