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 ModeHandler
88 SimpleExtItem<joinfloodsettings> ext;
89 JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
90 ext("joinflood", Creator) { }
92 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
96 std::string::size_type colon = parameter.find(':');
97 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
99 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
100 return MODEACTION_DENY;
103 /* Set up the flood parameters for this channel */
104 unsigned int njoins = ConvToInt(parameter.substr(0, colon));
105 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
106 if ((njoins<1) || (nsecs<1))
108 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
109 return MODEACTION_DENY;
112 joinfloodsettings jfs(nsecs, njoins);
113 joinfloodsettings* f = ext.get(channel);
114 if ((f) && (*f == jfs))
116 return MODEACTION_DENY;
118 ext.set(channel, jfs);
119 parameter = ConvToStr(njoins) + ":" + ConvToStr(nsecs);
120 return MODEACTION_ALLOW;
124 if (!channel->IsModeSet(this))
125 return MODEACTION_DENY;
127 joinfloodsettings* f = ext.get(channel);
131 return MODEACTION_ALLOW;
134 return MODEACTION_DENY;
138 class ModuleJoinFlood : public Module
148 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
152 joinfloodsettings *f = jf.ext.get(chan);
153 if (f && f->islocked())
155 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
159 return MOD_RES_PASSTHRU;
162 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
164 /* We arent interested in JOIN events caused by a network burst */
168 joinfloodsettings *f = jf.ext.get(memb->chan);
170 /* But all others are OK */
178 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);
183 Version GetVersion() CXX11_OVERRIDE
185 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
189 MODULE_INIT(ModuleJoinFlood)