1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +j (join flood protection) */
18 /** Holds settings and state associated with channel mode +j
20 class joinfloodsettings
30 joinfloodsettings(int b, int c) : secs(b), joins(c)
32 reset = ServerInstance->Time() + secs;
40 if (ServerInstance->Time() > reset)
43 reset = ServerInstance->Time() + secs;
49 return (counter >= this->joins);
61 if (ServerInstance->Time() > unlocktime)
77 unlocktime = ServerInstance->Time() + 60;
82 /** Handles channel mode +j
84 class JoinFlood : public ModeHandler
87 SimpleExtItem<joinfloodsettings> ext;
88 JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
89 ext("joinflood", Creator) { }
91 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
93 joinfloodsettings* x = ext.get(channel);
95 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
97 return std::make_pair(false, parameter);
100 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
106 strlcpy(ndata,parameter.c_str(),MAXBUF);
123 /* Set up the flood parameters for this channel */
124 int njoins = atoi(joins);
125 int nsecs = atoi(secs);
126 if ((njoins<1) || (nsecs<1))
128 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
130 return MODEACTION_DENY;
134 joinfloodsettings* f = ext.get(channel);
137 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138 f = new joinfloodsettings(nsecs, njoins);
140 channel->SetModeParam('j', parameter);
141 return MODEACTION_ALLOW;
145 std::string cur_param = channel->GetModeParameter('j');
146 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
147 if (cur_param == parameter)
150 return MODEACTION_DENY;
154 // new mode param, replace old with new
155 if ((nsecs > 0) && (njoins > 0))
157 f = new joinfloodsettings(nsecs, njoins);
159 channel->SetModeParam('j', parameter);
160 return MODEACTION_ALLOW;
164 return MODEACTION_DENY;
172 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
173 return MODEACTION_DENY;
178 joinfloodsettings* f = ext.get(channel);
182 channel->SetModeParam('j', "");
183 return MODEACTION_ALLOW;
186 return MODEACTION_DENY;
190 class ModuleJoinFlood : public Module
201 if (!ServerInstance->Modes->AddMode(&jf))
202 throw ModuleException("Could not add new modes!");
203 ServerInstance->Extensions.Register(&jf.ext);
204 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
205 ServerInstance->Modules->Attach(eventlist, this, 3);
208 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
212 joinfloodsettings *f = jf.ext.get(chan);
213 if (f && f->islocked())
215 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
219 return MOD_RES_PASSTHRU;
222 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
224 /* We arent interested in JOIN events caused by a network burst */
228 joinfloodsettings *f = jf.ext.get(memb->chan);
230 /* But all others are OK */
238 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);
249 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR);
253 MODULE_INIT(ModuleJoinFlood)