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