1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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
27 std::map<User*,int> counters;
29 floodsettings() : ban(0), secs(0), lines(0) {};
30 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
32 reset = time(NULL) + secs;
35 void addmessage(User* who)
37 std::map<User*,int>::iterator iter = counters.find(who);
38 if (iter != counters.end())
46 if (time(NULL) > reset)
49 reset = time(NULL) + secs;
53 bool shouldkick(User* who)
55 std::map<User*,int>::iterator iter = counters.find(who);
56 if (iter != counters.end())
58 return (iter->second >= this->lines);
65 std::map<User*,int>::iterator iter = counters.find(who);
66 if (iter != counters.end())
73 /** Handles channel mode +f
75 class MsgFlood : public ModeHandler
78 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
80 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
83 if (channel->GetExt("flood",x))
84 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
86 return std::make_pair(false, parameter);
89 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
91 /* When TS is equal, the alphabetically later one wins */
92 return (their_param < our_param);
95 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
103 strlcpy(ndata,parameter.c_str(),MAXBUF);
129 /* Set up the flood parameters for this channel */
130 int nlines = atoi(lines);
131 int nsecs = atoi(secs);
132 if ((nlines<1) || (nsecs<1))
134 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
136 return MODEACTION_DENY;
140 if (!channel->GetExt("flood", f))
142 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
143 floodsettings *f = new floodsettings(ban,nsecs,nlines);
144 channel->Extend("flood",f);
145 channel->SetMode('f', true);
146 channel->SetModeParam('f', parameter.c_str(), true);
147 return MODEACTION_ALLOW;
151 std::string cur_param = channel->GetModeParameter('f');
152 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
153 if (cur_param == parameter)
156 return MODEACTION_DENY;
160 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)) || (ban != f->ban))
163 floodsettings *f = new floodsettings(ban,nsecs,nlines);
164 channel->Shrink("flood");
165 channel->Extend("flood",f);
166 channel->SetModeParam('f', cur_param.c_str(), false);
167 channel->SetModeParam('f', parameter.c_str(), true);
168 return MODEACTION_ALLOW;
172 return MODEACTION_DENY;
180 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
182 return MODEACTION_DENY;
187 if (channel->GetExt("flood", f))
190 channel->Shrink("flood");
191 channel->SetMode('f', false);
192 return MODEACTION_ALLOW;
196 return MODEACTION_DENY;
200 class ModuleMsgFlood : public Module
207 ModuleMsgFlood(InspIRCd* Me)
211 mf = new MsgFlood(ServerInstance);
212 if (!ServerInstance->AddMode(mf))
213 throw ModuleException("Could not add new modes!");
216 int ProcessMessages(User* user,Channel* dest, const std::string &text)
218 if (!IS_LOCAL(user) || CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP)
224 if (dest->GetExt("flood", f))
227 if (f->shouldkick(user))
229 /* Youre outttta here! */
233 const char* parameters[3];
234 parameters[0] = dest->name;
235 parameters[1] = "+b";
236 parameters[2] = user->MakeWildHost();
237 ServerInstance->SendMode(parameters,3,user);
238 std::deque<std::string> n;
239 /* Propogate the ban to other servers.
240 * We dont know what protocol we may be using,
241 * so this event is picked up by our protocol
242 * module and formed into a ban command that
243 * suits the protocol in use.
245 n.push_back(dest->name);
247 n.push_back(user->MakeWildHost());
248 Event rmode((char *)&n, NULL, "send_mode");
249 rmode.Send(ServerInstance);
251 char kickmessage[MAXBUF];
252 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
253 if (!dest->ServerKickUser(user, kickmessage, true))
264 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
266 if (target_type == TYPE_CHANNEL)
267 return ProcessMessages(user,(Channel*)dest,text);
272 virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
274 if (target_type == TYPE_CHANNEL)
275 return ProcessMessages(user,(Channel*)dest,text);
280 void OnChannelDelete(Channel* chan)
283 if (chan->GetExt("flood", f))
286 chan->Shrink("flood");
290 void Implements(char* List)
292 List[I_OnChannelDelete] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = 1;
295 virtual ~ModuleMsgFlood()
297 ServerInstance->Modes->DelMode(mf);
301 virtual Version GetVersion()
303 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
307 MODULE_INIT(ModuleMsgFlood)