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