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 +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, bool a, int b, int c) : ServerInstance(Instance), ban(a), secs(b), lines(c)
33 reset = ServerInstance->Time() + secs;
36 void addmessage(User* who)
38 std::map<User*,int>::iterator iter = counters.find(who);
39 if (iter != counters.end())
47 if (ServerInstance->Time() > reset)
50 reset = ServerInstance->Time() + secs;
54 bool shouldkick(User* who)
56 std::map<User*,int>::iterator iter = counters.find(who);
57 if (iter != counters.end())
59 return (iter->second >= this->lines);
66 std::map<User*,int>::iterator iter = counters.find(who);
67 if (iter != counters.end())
74 /** Handles channel mode +f
76 class MsgFlood : public ModeHandler
79 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
81 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
84 if (channel->GetExt("flood",x))
85 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
87 return std::make_pair(false, parameter);
90 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
92 /* When TS is equal, the alphabetically later one wins */
93 return (their_param < our_param);
96 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
104 strlcpy(ndata,parameter.c_str(),MAXBUF);
130 /* Set up the flood parameters for this channel */
131 int nlines = atoi(lines);
132 int nsecs = atoi(secs);
133 if ((nlines<2) || (nsecs<1))
135 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
137 return MODEACTION_DENY;
141 if (!channel->GetExt("flood", f))
143 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
144 floodsettings *fs = new floodsettings(ServerInstance,ban,nsecs,nlines);
145 channel->Extend("flood",fs);
146 channel->SetModeParam('f', parameter);
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(ServerInstance,ban,nsecs,nlines);
164 channel->Shrink("flood");
165 channel->Extend("flood",fs);
166 channel->SetModeParam('f', parameter);
167 return MODEACTION_ALLOW;
171 return MODEACTION_DENY;
179 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
181 return MODEACTION_DENY;
186 if (channel->GetExt("flood", f))
189 channel->Shrink("flood");
190 channel->SetModeParam('f', "");
191 return MODEACTION_ALLOW;
195 return MODEACTION_DENY;
199 class ModuleMsgFlood : public Module
206 ModuleMsgFlood(InspIRCd* Me)
210 mf = new MsgFlood(ServerInstance);
211 if (!ServerInstance->Modes->AddMode(mf))
212 throw ModuleException("Could not add new modes!");
213 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNotice, I_OnUserPreMessage };
214 ServerInstance->Modules->Attach(eventlist, this, 3);
217 int ProcessMessages(User* user,Channel* dest, const std::string &text)
219 if (!IS_LOCAL(user) || (CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP))
225 if (dest->GetExt("flood", f))
228 if (f->shouldkick(user))
230 /* Youre outttta here! */
234 std::vector<std::string> parameters;
235 parameters.push_back(dest->name);
236 parameters.push_back("+b");
237 parameters.push_back(user->MakeWildHost());
238 ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
240 ServerInstance->PI->SendModeStr(dest->name, std::string("+b ") + user->MakeWildHost());
243 char kickmessage[MAXBUF];
244 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
246 if (!dest->ServerKickUser(user, kickmessage))
258 virtual int OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
260 if (target_type == TYPE_CHANNEL)
261 return ProcessMessages(user,(Channel*)dest,text);
266 virtual int OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
268 if (target_type == TYPE_CHANNEL)
269 return ProcessMessages(user,(Channel*)dest,text);
274 void OnChannelDelete(Channel* chan)
277 if (chan->GetExt("flood", f))
280 chan->Shrink("flood");
285 virtual ~ModuleMsgFlood()
287 ServerInstance->Modes->DelMode(mf);
291 virtual Version GetVersion()
293 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
297 MODULE_INIT(ModuleMsgFlood)