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 JoinFlood(InspIRCd* Instance, Module* Creator) : ModeHandler(Instance, Creator, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
91 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
94 if (channel->GetExt("joinflood",x))
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)
102 joinfloodsettings* dummy;
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 if (!channel->GetExt("joinflood", dummy))
138 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
139 joinfloodsettings *f = new joinfloodsettings(ServerInstance, nsecs, njoins);
140 channel->Extend("joinflood", f);
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 joinfloodsettings* f;
159 channel->GetExt("joinflood", f);
161 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
162 channel->Shrink("joinflood");
163 channel->Extend("joinflood", f);
164 channel->SetModeParam('j', parameter);
165 return MODEACTION_ALLOW;
169 return MODEACTION_DENY;
177 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
178 return MODEACTION_DENY;
183 if (channel->GetExt("joinflood", dummy))
185 joinfloodsettings *f;
186 channel->GetExt("joinflood", f);
188 channel->Shrink("joinflood");
189 channel->SetModeParam('j', "");
190 return MODEACTION_ALLOW;
193 return MODEACTION_DENY;
197 class ModuleJoinFlood : public Module
204 ModuleJoinFlood(InspIRCd* Me)
205 : Module(Me), jf(Me, this)
208 if (!ServerInstance->Modes->AddMode(&jf))
209 throw ModuleException("Could not add new modes!");
210 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
211 ServerInstance->Modules->Attach(eventlist, this, 3);
214 virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
218 joinfloodsettings *f;
219 if (chan->GetExt("joinflood", f))
223 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
231 virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
233 joinfloodsettings *f;
235 /* We arent interested in JOIN events caused by a network burst */
239 /* But all others are OK */
240 if (channel->GetExt("joinflood",f))
247 channel->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.", channel->name.c_str(), f->joins, f->secs);
252 void OnChannelDelete(Channel* chan)
254 joinfloodsettings *f;
255 if (chan->GetExt("joinflood",f))
258 chan->Shrink("joinflood");
263 virtual ~ModuleJoinFlood()
265 ServerInstance->Modes->DelMode(&jf);
268 virtual Version GetVersion()
270 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
274 MODULE_INIT(ModuleJoinFlood)