]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
436e578671a1af4876a95bc8e138c5373e584ec9
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <map>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "inspircd.h"
25
26 /* $ModDesc: Provides channel mode +f (message flood protection) */
27
28 /** Holds flood settings and state for mode +f
29  */
30 class floodsettings : public classbase
31 {
32  public:
33         bool ban;
34         int secs;
35         int lines;
36         time_t reset;
37         std::map<userrec*,int> counters;
38
39         floodsettings() : ban(0), secs(0), lines(0) {};
40         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
41         {
42                 reset = time(NULL) + secs;
43         };
44
45         void addmessage(userrec* who)
46         {
47                 std::map<userrec*,int>::iterator iter = counters.find(who);
48                 if (iter != counters.end())
49                 {
50                         iter->second++;
51                 }
52                 else
53                 {
54                         counters[who] = 1;
55                 }
56                 if (time(NULL) > reset)
57                 {
58                         counters.clear();
59                         reset = time(NULL) + secs;
60                 }
61         }
62
63         bool shouldkick(userrec* who)
64         {
65                 std::map<userrec*,int>::iterator iter = counters.find(who);
66                 if (iter != counters.end())
67                 {
68                         return (iter->second >= this->lines);
69                 }
70                 else return false;
71         }
72
73         void clear(userrec* who)
74         {
75                 std::map<userrec*,int>::iterator iter = counters.find(who);
76                 if (iter != counters.end())
77                 {
78                         counters.erase(iter);
79                 }
80         }
81 };
82
83 /** Handles channel mode +f
84  */
85 class MsgFlood : public ModeHandler
86 {
87  public:
88         MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
89
90         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
91         {
92                 floodsettings* x;
93                 if (channel->GetExt("flood",x))
94                         return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
95                 else
96                         return std::make_pair(false, parameter);
97         }
98
99         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
100         {
101                 /* When TS is equal, the alphabetically later one wins */
102                 return (their_param < our_param);
103         }
104
105         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
106         {
107                 floodsettings *f;
108
109                 if (adding)
110                 {
111                         char ndata[MAXBUF];
112                         char* data = ndata;
113                         strlcpy(ndata,parameter.c_str(),MAXBUF);
114                         char* lines = data;
115                         char* secs = NULL;
116                         bool ban = false;
117                         if (*data == '*')
118                         {
119                                 ban = true;
120                                 lines++;
121                         }
122                         else
123                         {
124                                 ban = false;
125                         }
126                         while (*data)
127                         {
128                                 if (*data == ':')
129                                 {
130                                         *data = 0;
131                                         data++;
132                                         secs = data;
133                                         break;
134                                 }
135                                 else data++;
136                         }
137                         if (secs)
138                         {
139                                 /* Set up the flood parameters for this channel */
140                                 int nlines = atoi(lines);
141                                 int nsecs = atoi(secs);
142                                 if ((nlines<1) || (nsecs<1))
143                                 {
144                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
145                                         parameter = "";
146                                         return MODEACTION_DENY;
147                                 }
148                                 else
149                                 {
150                                         if (!channel->GetExt("flood", f))
151                                         {
152                                                 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
153                                                 floodsettings *f = new floodsettings(ban,nsecs,nlines);
154                                                 channel->Extend("flood",f);
155                                                 channel->SetMode('f', true);
156                                                 channel->SetModeParam('f', parameter.c_str(), true);
157                                                 return MODEACTION_ALLOW;
158                                         }
159                                         else
160                                         {
161                                                 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)))
162                                                 {
163                                                         delete f;
164                                                         floodsettings *f = new floodsettings(ban,nsecs,nlines);
165                                                         parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
166                                                         channel->Shrink("flood");
167                                                         channel->Extend("flood",f);
168                                                         channel->SetModeParam('f', parameter.c_str(), true);
169                                                         return MODEACTION_ALLOW;
170                                                 }
171                                         }
172                                 }
173                         }
174                         else
175                         {
176                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
177                                 parameter = "";
178                                 return MODEACTION_DENY;
179                         }
180                 }
181                 else
182                 {
183                         if (channel->GetExt("flood", f))
184                         {
185                                 DELETE(f);
186                                 channel->Shrink("flood");
187                                 channel->SetMode('f', false);
188                                 return MODEACTION_ALLOW;
189                         }
190                 }
191                 
192                 return MODEACTION_DENY;
193         }
194 };
195
196 class ModuleMsgFlood : public Module
197 {
198         
199         MsgFlood* mf;
200         
201  public:
202  
203         ModuleMsgFlood(InspIRCd* Me)
204                 : Module::Module(Me)
205         {
206                 
207                 mf = new MsgFlood(ServerInstance);
208                 ServerInstance->AddMode(mf, 'f');
209         }
210         
211         void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
212         {
213                 if (IS_LOCAL(user))
214                 {
215                         floodsettings *f;
216                         if (dest->GetExt("flood", f))
217                         {
218                                 f->addmessage(user);
219                                 if (f->shouldkick(user))
220                                 {
221                                         /* Youre outttta here! */
222                                         f->clear(user);
223                                         if (f->ban)
224                                         {
225                                                 const char* parameters[3];
226                                                 parameters[0] = dest->name;
227                                                 parameters[1] = "+b";
228                                                 parameters[2] = user->MakeWildHost();
229                                                 ServerInstance->SendMode(parameters,3,user);
230                                                 std::deque<std::string> n;
231                                                 /* Propogate the ban to other servers.
232                                                  * We dont know what protocol we may be using,
233                                                  * so this event is picked up by our protocol
234                                                  * module and formed into a ban command that
235                                                  * suits the protocol in use.
236                                                  */
237                                                 n.push_back(dest->name);
238                                                 n.push_back("+b");
239                                                 n.push_back(user->MakeWildHost());
240                                                 Event rmode((char *)&n, NULL, "send_mode");
241                                                 rmode.Send(ServerInstance);
242                                         }
243                                         dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
244                                 }
245                         }
246                 }
247         }
248
249         virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
250         {
251                 if (target_type == TYPE_CHANNEL)
252                 {
253                         ProcessMessages(user,(chanrec*)dest,text);
254                 }
255         }
256
257         virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
258         {
259                 if (target_type == TYPE_CHANNEL)
260                 {
261                         ProcessMessages(user,(chanrec*)dest,text);
262                 }
263         }
264
265         void OnChannelDelete(chanrec* chan)
266         {
267                 floodsettings* f;
268                 if (chan->GetExt("flood", f))
269                 {
270                         DELETE(f);
271                         chan->Shrink("flood");
272                 }
273         }
274
275         void Implements(char* List)
276         {
277                 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
278         }
279
280         virtual ~ModuleMsgFlood()
281         {
282                 ServerInstance->Modes->DelMode(mf);
283                 DELETE(mf);
284         }
285         
286         virtual Version GetVersion()
287         {
288                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
289         }
290 };
291
292
293 class ModuleMsgFloodFactory : public ModuleFactory
294 {
295  public:
296         ModuleMsgFloodFactory()
297         {
298         }
299         
300         ~ModuleMsgFloodFactory()
301         {
302         }
303         
304         virtual Module * CreateModule(InspIRCd* Me)
305         {
306                 return new ModuleMsgFlood(Me);
307         }
308         
309 };
310
311
312 extern "C" void * init_module( void )
313 {
314         return new ModuleMsgFloodFactory;
315 }
316