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