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 * ---------------------------------------------------
19 /* $ModDesc: Provides channel mode +f (message flood protection) */
21 /** Holds flood settings and state for mode +f
23 class floodsettings : public classbase
30 std::map<userrec*,int> counters;
32 floodsettings() : ban(0), secs(0), lines(0) {};
33 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
35 reset = time(NULL) + secs;
38 void addmessage(userrec* who)
40 std::map<userrec*,int>::iterator iter = counters.find(who);
41 if (iter != counters.end())
49 if (time(NULL) > reset)
52 reset = time(NULL) + secs;
56 bool shouldkick(userrec* who)
58 std::map<userrec*,int>::iterator iter = counters.find(who);
59 if (iter != counters.end())
61 return (iter->second >= this->lines);
66 void clear(userrec* who)
68 std::map<userrec*,int>::iterator iter = counters.find(who);
69 if (iter != counters.end())
76 /** Handles channel mode +f
78 class MsgFlood : public ModeHandler
81 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
83 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
86 if (channel->GetExt("flood",x))
87 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
89 return std::make_pair(false, parameter);
92 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
94 /* When TS is equal, the alphabetically later one wins */
95 return (their_param < our_param);
98 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
106 strlcpy(ndata,parameter.c_str(),MAXBUF);
132 /* Set up the flood parameters for this channel */
133 int nlines = atoi(lines);
134 int nsecs = atoi(secs);
135 if ((nlines<1) || (nsecs<1))
137 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
139 return MODEACTION_DENY;
143 if (!channel->GetExt("flood", f))
145 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
146 floodsettings *f = new floodsettings(ban,nsecs,nlines);
147 channel->Extend("flood",f);
148 channel->SetMode('f', true);
149 channel->SetModeParam('f', parameter.c_str(), true);
150 return MODEACTION_ALLOW;
154 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)) || (ban != f->ban))
157 floodsettings *f = new floodsettings(ban,nsecs,nlines);
158 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
159 channel->Shrink("flood");
160 channel->Extend("flood",f);
161 channel->SetModeParam('f', parameter.c_str(), true);
162 return MODEACTION_ALLOW;
169 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
171 return MODEACTION_DENY;
176 if (channel->GetExt("flood", f))
179 channel->Shrink("flood");
180 channel->SetMode('f', false);
181 return MODEACTION_ALLOW;
185 return MODEACTION_DENY;
189 class ModuleMsgFlood : public Module
196 ModuleMsgFlood(InspIRCd* Me)
200 mf = new MsgFlood(ServerInstance);
201 if (!ServerInstance->AddMode(mf, 'f'))
202 throw ModuleException("Could not add new modes!");
205 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
207 if (!IS_LOCAL(user) || CHANOPS_EXEMPT(ServerInstance, 'f') && dest->GetStatus(user) == STATUS_OP)
213 if (dest->GetExt("flood", f))
216 if (f->shouldkick(user))
218 /* Youre outttta here! */
222 const char* parameters[3];
223 parameters[0] = dest->name;
224 parameters[1] = "+b";
225 parameters[2] = user->MakeWildHost();
226 ServerInstance->SendMode(parameters,3,user);
227 std::deque<std::string> n;
228 /* Propogate the ban to other servers.
229 * We dont know what protocol we may be using,
230 * so this event is picked up by our protocol
231 * module and formed into a ban command that
232 * suits the protocol in use.
234 n.push_back(dest->name);
236 n.push_back(user->MakeWildHost());
237 Event rmode((char *)&n, NULL, "send_mode");
238 rmode.Send(ServerInstance);
240 char kickmessage[MAXBUF];
241 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
242 dest->ServerKickUser(user, kickmessage, true);
247 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
249 if (target_type == TYPE_CHANNEL)
251 ProcessMessages(user,(chanrec*)dest,text);
255 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
257 if (target_type == TYPE_CHANNEL)
259 ProcessMessages(user,(chanrec*)dest,text);
263 void OnChannelDelete(chanrec* chan)
266 if (chan->GetExt("flood", f))
269 chan->Shrink("flood");
273 void Implements(char* List)
275 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
278 virtual ~ModuleMsgFlood()
280 ServerInstance->Modes->DelMode(mf);
284 virtual Version GetVersion()
286 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
291 class ModuleMsgFloodFactory : public ModuleFactory
294 ModuleMsgFloodFactory()
298 ~ModuleMsgFloodFactory()
302 virtual Module * CreateModule(InspIRCd* Me)
304 return new ModuleMsgFlood(Me);
310 extern "C" DllExport void * init_module( void )
312 return new ModuleMsgFloodFactory;