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
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, bool)
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->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
136 return MODEACTION_DENY;
140 if (!channel->GetExt("flood", f))
142 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
143 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
144 channel->Extend("flood",fs);
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) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
163 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
164 channel->Shrink("flood");
165 channel->Extend("flood",fs);
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->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
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->Modes->AddMode(mf))
213 throw ModuleException("Could not add new modes!");
214 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNotice, I_OnUserPreMessage };
215 ServerInstance->Modules->Attach(eventlist, this, 3);
218 int ProcessMessages(User* user,Channel* dest, const std::string &text)
220 if (!IS_LOCAL(user) || (CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP))
226 if (dest->GetExt("flood", f))
229 if (f->shouldkick(user))
231 /* Youre outttta here! */
235 std::vector<std::string> parameters(3);
236 parameters.push_back(dest->name);
237 parameters.push_back("+b");
238 parameters.push_back(user->MakeWildHost());
239 ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
241 ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
244 char kickmessage[MAXBUF];
245 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
247 if (!dest->ServerKickUser(user, kickmessage, true))
259 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
261 if (target_type == TYPE_CHANNEL)
262 return ProcessMessages(user,(Channel*)dest,text);
267 virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
269 if (target_type == TYPE_CHANNEL)
270 return ProcessMessages(user,(Channel*)dest,text);
275 void OnChannelDelete(Channel* chan)
278 if (chan->GetExt("flood", f))
281 chan->Shrink("flood");
286 virtual ~ModuleMsgFlood()
288 ServerInstance->Modes->DelMode(mf);
292 virtual Version GetVersion()
294 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
298 MODULE_INIT(ModuleMsgFlood)