1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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
27 std::map<User*,int> counters;
29 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
31 reset = ServerInstance->Time() + secs;
34 void addmessage(User* who)
36 std::map<User*,int>::iterator iter = counters.find(who);
37 if (iter != counters.end())
45 if (ServerInstance->Time() > reset)
48 reset = ServerInstance->Time() + secs;
52 bool shouldkick(User* who)
54 std::map<User*,int>::iterator iter = counters.find(who);
55 if (iter != counters.end())
57 return (iter->second >= this->lines);
64 std::map<User*,int>::iterator iter = counters.find(who);
65 if (iter != counters.end())
72 /** Handles channel mode +f
74 class MsgFlood : public ModeHandler
77 SimpleExtItem<floodsettings> ext;
78 MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
79 ext("messageflood", Creator) { }
81 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
83 floodsettings *f = ext.get(channel);
89 strlcpy(ndata,parameter.c_str(),MAXBUF);
115 /* Set up the flood parameters for this channel */
116 int nlines = atoi(lines);
117 int nsecs = atoi(secs);
118 if ((nlines<2) || (nsecs<1))
120 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
122 return MODEACTION_DENY;
128 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
129 f = new floodsettings(ban,nsecs,nlines);
131 channel->SetModeParam('f', parameter);
132 return MODEACTION_ALLOW;
136 std::string cur_param = channel->GetModeParameter('f');
137 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
138 if (cur_param == parameter)
141 return MODEACTION_DENY;
145 if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
147 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
148 ext.set(channel, fs);
149 channel->SetModeParam('f', parameter);
150 return MODEACTION_ALLOW;
154 return MODEACTION_DENY;
162 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
164 return MODEACTION_DENY;
172 channel->SetModeParam('f', "");
173 return MODEACTION_ALLOW;
177 return MODEACTION_DENY;
181 class ModuleMsgFlood : public Module
190 if (!ServerInstance->Modes->AddMode(&mf))
191 throw ModuleException("Could not add new modes!");
192 ServerInstance->Extensions.Register(&mf.ext);
193 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
194 ServerInstance->Modules->Attach(eventlist, this, 2);
197 ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
199 ModResult res = ServerInstance->OnCheckExemption(user,dest,"flood");
200 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
201 return MOD_RES_PASSTHRU;
203 floodsettings *f = mf.ext.get(dest);
207 if (f->shouldkick(user))
209 /* Youre outttta here! */
213 std::vector<std::string> parameters;
214 parameters.push_back(dest->name);
215 parameters.push_back("+b");
216 parameters.push_back(user->MakeWildHost());
217 ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
220 char kickmessage[MAXBUF];
221 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
223 dest->KickUser(ServerInstance->FakeClient, user, kickmessage);
229 return MOD_RES_PASSTHRU;
232 ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
234 if (target_type == TYPE_CHANNEL)
235 return ProcessMessages(user,(Channel*)dest,text);
237 return MOD_RES_PASSTHRU;
240 ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
242 if (target_type == TYPE_CHANNEL)
243 return ProcessMessages(user,(Channel*)dest,text);
245 return MOD_RES_PASSTHRU;
254 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
258 MODULE_INIT(ModuleMsgFlood)