]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Only initialise request type once
[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
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 class MsgFlood : public ModeHandler
84 {
85  public:
86         MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
87
88         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
89         {
90                 floodsettings* x;
91                 if (channel->GetExt("flood",x))
92                         return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
93                 else
94                         return std::make_pair(false, parameter);
95         }
96
97         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
98         {
99                 /* When TS is equal, the alphabetically later one wins */
100                 return (their_param < our_param);
101         }
102
103         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
104         {
105                 floodsettings *f;
106
107                 if (adding)
108                 {
109                         char ndata[MAXBUF];
110                         char* data = ndata;
111                         strlcpy(ndata,parameter.c_str(),MAXBUF);
112                         char* lines = data;
113                         char* secs = NULL;
114                         bool ban = false;
115                         if (*data == '*')
116                         {
117                                 ban = true;
118                                 lines++;
119                         }
120                         else
121                         {
122                                 ban = false;
123                         }
124                         while (*data)
125                         {
126                                 if (*data == ':')
127                                 {
128                                         *data = 0;
129                                         data++;
130                                         secs = data;
131                                         break;
132                                 }
133                                 else data++;
134                         }
135                         if (secs)
136                         {
137                                 /* Set up the flood parameters for this channel */
138                                 int nlines = atoi(lines);
139                                 int nsecs = atoi(secs);
140                                 if ((nlines<1) || (nsecs<1))
141                                 {
142                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
143                                         parameter = "";
144                                         return MODEACTION_DENY;
145                                 }
146                                 else
147                                 {
148                                         if (!channel->GetExt("flood", f))
149                                         {
150                                                 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
151                                                 floodsettings *f = new floodsettings(ban,nsecs,nlines);
152                                                 channel->Extend("flood",f);
153                                                 channel->SetMode('f', true);
154                                                 channel->SetModeParam('f', parameter.c_str(), true);
155                                                 return MODEACTION_ALLOW;
156                                         }
157                                         else
158                                         {
159                                                 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)))
160                                                 {
161                                                         delete f;
162                                                         floodsettings *f = new floodsettings(ban,nsecs,nlines);
163                                                         parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
164                                                         channel->Shrink("flood");
165                                                         channel->Extend("flood",f);
166                                                         channel->SetModeParam('f', parameter.c_str(), true);
167                                                         return MODEACTION_ALLOW;
168                                                 }
169                                         }
170                                 }
171                         }
172                         else
173                         {
174                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
175                                 parameter = "";
176                                 return MODEACTION_DENY;
177                         }
178                 }
179                 else
180                 {
181                         if (channel->GetExt("flood", f))
182                         {
183                                 DELETE(f);
184                                 channel->Shrink("flood");
185                                 channel->SetMode('f', false);
186                                 return MODEACTION_ALLOW;
187                         }
188                 }
189                 
190                 return MODEACTION_DENY;
191         }
192 };
193
194 class ModuleMsgFlood : public Module
195 {
196         
197         MsgFlood* mf;
198         
199  public:
200  
201         ModuleMsgFlood(InspIRCd* Me)
202                 : Module::Module(Me)
203         {
204                 
205                 mf = new MsgFlood(ServerInstance);
206                 ServerInstance->AddMode(mf, 'f');
207         }
208         
209         void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
210         {
211                 if (IS_LOCAL(user))
212                 {
213                         floodsettings *f;
214                         if (dest->GetExt("flood", f))
215                         {
216                                 f->addmessage(user);
217                                 if (f->shouldkick(user))
218                                 {
219                                         /* Youre outttta here! */
220                                         f->clear(user);
221                                         if (f->ban)
222                                         {
223                                                 const char* parameters[3];
224                                                 parameters[0] = dest->name;
225                                                 parameters[1] = "+b";
226                                                 parameters[2] = user->MakeWildHost();
227                                                 ServerInstance->SendMode(parameters,3,user);
228                                                 std::deque<std::string> n;
229                                                 /* Propogate the ban to other servers.
230                                                  * We dont know what protocol we may be using,
231                                                  * so this event is picked up by our protocol
232                                                  * module and formed into a ban command that
233                                                  * suits the protocol in use.
234                                                  */
235                                                 n.push_back(dest->name);
236                                                 n.push_back("+b");
237                                                 n.push_back(user->MakeWildHost());
238                                                 Event rmode((char *)&n, NULL, "send_mode");
239                                                 rmode.Send(ServerInstance);
240                                         }
241                                         dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
242                                 }
243                         }
244                 }
245         }
246
247         virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
248         {
249                 if (target_type == TYPE_CHANNEL)
250                 {
251                         ProcessMessages(user,(chanrec*)dest,text);
252                 }
253         }
254
255         virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
256         {
257                 if (target_type == TYPE_CHANNEL)
258                 {
259                         ProcessMessages(user,(chanrec*)dest,text);
260                 }
261         }
262
263         void OnChannelDelete(chanrec* chan)
264         {
265                 floodsettings* f;
266                 if (chan->GetExt("flood", f))
267                 {
268                         DELETE(f);
269                         chan->Shrink("flood");
270                 }
271         }
272
273         void Implements(char* List)
274         {
275                 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
276         }
277
278         virtual ~ModuleMsgFlood()
279         {
280                 ServerInstance->Modes->DelMode(mf);
281                 DELETE(mf);
282         }
283         
284         virtual Version GetVersion()
285         {
286                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
287         }
288 };
289
290
291 class ModuleMsgFloodFactory : public ModuleFactory
292 {
293  public:
294         ModuleMsgFloodFactory()
295         {
296         }
297         
298         ~ModuleMsgFloodFactory()
299         {
300         }
301         
302         virtual Module * CreateModule(InspIRCd* Me)
303         {
304                 return new ModuleMsgFlood(Me);
305         }
306         
307 };
308
309
310 extern "C" void * init_module( void )
311 {
312         return new ModuleMsgFloodFactory;
313 }
314