1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* $ModDesc: Provides channel mode +f (message flood protection) */
18 /** Holds flood settings and state for mode +f
20 class floodsettings : public classbase
23 InspIRCd *ServerInstance;
29 std::map<User*,int> counters;
31 floodsettings(InspIRCd *Instance) : ServerInstance(Instance), ban(0), secs(0), lines(0) {};
32 floodsettings(InspIRCd *Instance, bool a, int b, int c) : ServerInstance(Instance), ban(a), secs(b), lines(c)
34 reset = ServerInstance->Time() + secs;
37 void addmessage(User* who)
39 std::map<User*,int>::iterator iter = counters.find(who);
40 if (iter != counters.end())
48 if (ServerInstance->Time() > reset)
51 reset = ServerInstance->Time() + secs;
55 bool shouldkick(User* who)
57 std::map<User*,int>::iterator iter = counters.find(who);
58 if (iter != counters.end())
60 return (iter->second >= this->lines);
67 std::map<User*,int>::iterator iter = counters.find(who);
68 if (iter != counters.end())
75 /** Handles channel mode +f
77 class MsgFlood : public ModeHandler
80 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
82 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
85 if (channel->GetExt("flood",x))
86 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
88 return std::make_pair(false, parameter);
91 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
93 /* When TS is equal, the alphabetically later one wins */
94 return (their_param < our_param);
97 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
105 strlcpy(ndata,parameter.c_str(),MAXBUF);
131 /* Set up the flood parameters for this channel */
132 int nlines = atoi(lines);
133 int nsecs = atoi(secs);
134 if ((nlines<1) || (nsecs<1))
136 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
138 return MODEACTION_DENY;
142 if (!channel->GetExt("flood", f))
144 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
145 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
146 channel->Extend("flood",fs);
147 channel->SetMode('f', true);
148 channel->SetModeParam('f', parameter.c_str(), true);
149 return MODEACTION_ALLOW;
153 std::string cur_param = channel->GetModeParameter('f');
154 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
155 if (cur_param == parameter)
158 return MODEACTION_DENY;
162 if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
165 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
166 channel->Shrink("flood");
167 channel->Extend("flood",fs);
168 channel->SetModeParam('f', cur_param.c_str(), false);
169 channel->SetModeParam('f', parameter.c_str(), true);
170 return MODEACTION_ALLOW;
174 return MODEACTION_DENY;
182 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
184 return MODEACTION_DENY;
189 if (channel->GetExt("flood", f))
192 channel->Shrink("flood");
193 channel->SetMode('f', false);
194 return MODEACTION_ALLOW;
198 return MODEACTION_DENY;
202 class ModuleMsgFlood : public Module
209 ModuleMsgFlood(InspIRCd* Me)
213 mf = new MsgFlood(ServerInstance);
214 if (!ServerInstance->Modes->AddMode(mf))
215 throw ModuleException("Could not add new modes!");
216 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNotice, I_OnUserPreMessage };
217 ServerInstance->Modules->Attach(eventlist, this, 3);
220 int ProcessMessages(User* user,Channel* dest, const std::string &text)
222 if (!IS_LOCAL(user) || (CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP))
228 if (dest->GetExt("flood", f))
231 if (f->shouldkick(user))
233 /* Youre outttta here! */
237 std::vector<std::string> parameters;
238 parameters.push_back(dest->name);
239 parameters.push_back("+b");
240 parameters.push_back(user->MakeWildHost());
241 ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
243 ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
246 char kickmessage[MAXBUF];
247 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
249 if (!dest->ServerKickUser(user, kickmessage, true))
261 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
263 if (target_type == TYPE_CHANNEL)
264 return ProcessMessages(user,(Channel*)dest,text);
269 virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
271 if (target_type == TYPE_CHANNEL)
272 return ProcessMessages(user,(Channel*)dest,text);
277 void OnChannelDelete(Channel* chan)
280 if (chan->GetExt("flood", f))
283 chan->Shrink("flood");
288 virtual ~ModuleMsgFlood()
290 ServerInstance->Modes->DelMode(mf);
294 virtual Version GetVersion()
296 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
300 MODULE_INIT(ModuleMsgFlood)