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;
92 /** Handles channel mode +j
94 class JoinFlood : public ModeHandler
97 SimpleExtItem<joinfloodsettings> ext;
98 JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
99 ext("joinflood", Creator) { }
101 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
107 strlcpy(ndata,parameter.c_str(),MAXBUF);
124 /* Set up the flood parameters for this channel */
125 int njoins = atoi(joins);
126 int nsecs = atoi(secs);
127 if ((njoins<1) || (nsecs<1))
129 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
131 return MODEACTION_DENY;
135 joinfloodsettings* f = ext.get(channel);
138 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
139 f = new joinfloodsettings(nsecs, njoins);
141 channel->SetModeParam('j', parameter);
142 return MODEACTION_ALLOW;
146 std::string cur_param = channel->GetModeParameter('j');
147 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
148 if (cur_param == parameter)
151 return MODEACTION_DENY;
155 // new mode param, replace old with new
156 if ((nsecs > 0) && (njoins > 0))
158 f = new joinfloodsettings(nsecs, njoins);
160 channel->SetModeParam('j', parameter);
161 return MODEACTION_ALLOW;
165 return MODEACTION_DENY;
173 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
174 return MODEACTION_DENY;
179 joinfloodsettings* f = ext.get(channel);
183 channel->SetModeParam('j', "");
184 return MODEACTION_ALLOW;
187 return MODEACTION_DENY;
191 class ModuleJoinFlood : public Module
202 if (!ServerInstance->Modes->AddMode(&jf))
203 throw ModuleException("Could not add new modes!");
204 ServerInstance->Extensions.Register(&jf.ext);
205 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
206 ServerInstance->Modules->Attach(eventlist, this, 2);
209 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
213 joinfloodsettings *f = jf.ext.get(chan);
214 if (f && f->islocked())
216 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
220 return MOD_RES_PASSTHRU;
223 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
225 /* We arent interested in JOIN events caused by a network burst */
229 joinfloodsettings *f = jf.ext.get(memb->chan);
231 /* But all others are OK */
239 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);
250 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
254 MODULE_INIT(ModuleJoinFlood)