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 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
97 strlcpy(ndata,parameter.c_str(),MAXBUF);
114 /* Set up the flood parameters for this channel */
115 int njoins = atoi(joins);
116 int nsecs = atoi(secs);
117 if ((njoins<1) || (nsecs<1))
119 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
121 return MODEACTION_DENY;
125 joinfloodsettings* f = ext.get(channel);
128 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
129 f = new joinfloodsettings(nsecs, njoins);
131 channel->SetModeParam('j', parameter);
132 return MODEACTION_ALLOW;
136 std::string cur_param = channel->GetModeParameter('j');
137 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138 if (cur_param == parameter)
141 return MODEACTION_DENY;
145 // new mode param, replace old with new
146 if ((nsecs > 0) && (njoins > 0))
148 f = new joinfloodsettings(nsecs, njoins);
150 channel->SetModeParam('j', parameter);
151 return MODEACTION_ALLOW;
155 return MODEACTION_DENY;
163 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
164 return MODEACTION_DENY;
169 joinfloodsettings* f = ext.get(channel);
173 channel->SetModeParam('j', "");
174 return MODEACTION_ALLOW;
177 return MODEACTION_DENY;
181 class ModuleJoinFlood : public Module
192 if (!ServerInstance->Modes->AddMode(&jf))
193 throw ModuleException("Could not add new modes!");
194 ServerInstance->Extensions.Register(&jf.ext);
195 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
196 ServerInstance->Modules->Attach(eventlist, this, 2);
199 ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
203 joinfloodsettings *f = jf.ext.get(chan);
204 if (f && f->islocked())
206 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
210 return MOD_RES_PASSTHRU;
213 void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
215 /* We arent interested in JOIN events caused by a network burst */
219 joinfloodsettings *f = jf.ext.get(memb->chan);
221 /* But all others are OK */
229 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);
240 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR);
244 MODULE_INIT(ModuleJoinFlood)