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 f = new joinfloodsettings(nsecs, njoins);
158 channel->SetModeParam('j', parameter);
159 return MODEACTION_ALLOW;
166 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
167 return MODEACTION_DENY;
172 if (channel->IsModeSet('j'))
175 channel->SetModeParam('j', "");
176 return MODEACTION_ALLOW;
179 return MODEACTION_DENY;
183 class ModuleJoinFlood : public Module
197 ServerInstance->Modules->AddService(jf);
198 ServerInstance->Modules->AddService(jf.ext);
199 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
200 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
203 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
207 joinfloodsettings *f = jf.ext.get(chan);
208 if (f && f->islocked())
210 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
214 return MOD_RES_PASSTHRU;
217 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
219 /* We arent interested in JOIN events caused by a network burst */
223 joinfloodsettings *f = jf.ext.get(memb->chan);
225 /* But all others are OK */
226 if ((f) && (!f->islocked()))
233 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);
244 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
248 MODULE_INIT(ModuleJoinFlood)