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 : public classbase
23 InspIRCd* ServerInstance;
32 joinfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), joins(c)
34 reset = ServerInstance->Time() + secs;
42 if (ServerInstance->Time() > reset)
45 reset = ServerInstance->Time() + secs;
51 return (counter >= this->joins);
63 if (ServerInstance->Time() > unlocktime)
79 unlocktime = ServerInstance->Time() + 60;
84 /** Handles channel mode +j
86 class JoinFlood : public ModeHandler
89 SimpleExtItem<joinfloodsettings> ext;
90 JoinFlood(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
91 ext("joinflood", Creator) { }
93 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
95 joinfloodsettings* x = ext.get(channel);
97 return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
99 return std::make_pair(false, parameter);
102 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
108 strlcpy(ndata,parameter.c_str(),MAXBUF);
125 /* Set up the flood parameters for this channel */
126 int njoins = atoi(joins);
127 int nsecs = atoi(secs);
128 if ((njoins<1) || (nsecs<1))
130 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
132 return MODEACTION_DENY;
136 joinfloodsettings* f = ext.get(channel);
139 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
140 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
142 channel->SetModeParam('j', parameter);
143 return MODEACTION_ALLOW;
147 std::string cur_param = channel->GetModeParameter('j');
148 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
149 if (cur_param == parameter)
152 return MODEACTION_DENY;
156 // new mode param, replace old with new
157 if ((nsecs > 0) && (njoins > 0))
159 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
161 channel->SetModeParam('j', parameter);
162 return MODEACTION_ALLOW;
166 return MODEACTION_DENY;
174 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
175 return MODEACTION_DENY;
180 joinfloodsettings* f = ext.get(channel);
184 channel->SetModeParam('j', "");
185 return MODEACTION_ALLOW;
188 return MODEACTION_DENY;
192 class ModuleJoinFlood : public Module
199 ModuleJoinFlood(InspIRCd* Me)
200 : Module(Me), jf(Me, this)
203 if (!ServerInstance->Modes->AddMode(&jf))
204 throw ModuleException("Could not add new modes!");
205 Extensible::Register(&jf.ext);
206 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
207 ServerInstance->Modules->Attach(eventlist, this, 3);
210 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
214 joinfloodsettings *f = jf.ext.get(chan);
215 if (f && f->islocked())
217 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
221 return MOD_RES_PASSTHRU;
224 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
226 /* We arent interested in JOIN events caused by a network burst */
230 joinfloodsettings *f = jf.ext.get(memb->chan);
232 /* But all others are OK */
240 memb->chan->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "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);
247 ServerInstance->Modes->DelMode(&jf);
252 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR, API_VERSION);
256 MODULE_INIT(ModuleJoinFlood)