]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Remove On005Numeric event from a ton of modules which no longer need it (as CHANMODES...
[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                                 }
158                         }
159                         else
160                         {
161                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
162                                 parameter = "";
163                                 return MODEACTION_DENY;
164                         }
165                 }
166                 else
167                 {
168                         if (channel->GetExt("flood", f))
169                         {
170                                 DELETE(f);
171                                 channel->Shrink("flood");
172                                 channel->SetMode('f', false);
173                                 return MODEACTION_ALLOW;
174                         }
175                 }
176                 
177                 return MODEACTION_DENY;
178         }
179 };
180
181 class ModuleMsgFlood : public Module
182 {
183         
184         MsgFlood* mf;
185         
186  public:
187  
188         ModuleMsgFlood(InspIRCd* Me)
189                 : Module::Module(Me)
190         {
191                 
192                 mf = new MsgFlood(ServerInstance);
193                 ServerInstance->AddMode(mf, 'f');
194         }
195         
196         void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
197         {
198                 if (IS_LOCAL(user))
199                 {
200                         floodsettings *f;
201                         if (dest->GetExt("flood", f))
202                         {
203                                 f->addmessage(user);
204                                 if (f->shouldkick(user))
205                                 {
206                                         /* Youre outttta here! */
207                                         f->clear(user);
208                                         if (f->ban)
209                                         {
210                                                 const char* parameters[3];
211                                                 parameters[0] = dest->name;
212                                                 parameters[1] = "+b";
213                                                 parameters[2] = user->MakeWildHost();
214                                                 ServerInstance->SendMode(parameters,3,user);
215                                                 std::deque<std::string> n;
216                                                 /* Propogate the ban to other servers.
217                                                  * We dont know what protocol we may be using,
218                                                  * so this event is picked up by our protocol
219                                                  * module and formed into a ban command that
220                                                  * suits the protocol in use.
221                                                  */
222                                                 n.push_back(dest->name);
223                                                 n.push_back("+b");
224                                                 n.push_back(user->MakeWildHost());
225                                                 Event rmode((char *)&n, NULL, "send_mode");
226                                                 rmode.Send(ServerInstance);
227                                         }
228                                         dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
229                                 }
230                         }
231                 }
232         }
233
234         virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
235         {
236                 if (target_type == TYPE_CHANNEL)
237                 {
238                         ProcessMessages(user,(chanrec*)dest,text);
239                 }
240         }
241
242         virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
243         {
244                 if (target_type == TYPE_CHANNEL)
245                 {
246                         ProcessMessages(user,(chanrec*)dest,text);
247                 }
248         }
249
250         void OnChannelDelete(chanrec* chan)
251         {
252                 floodsettings* f;
253                 if (chan->GetExt("flood", f))
254                 {
255                         DELETE(f);
256                         chan->Shrink("flood");
257                 }
258         }
259
260         void Implements(char* List)
261         {
262                 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
263         }
264
265         virtual ~ModuleMsgFlood()
266         {
267                 DELETE(mf);
268         }
269         
270         virtual Version GetVersion()
271         {
272                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
273         }
274 };
275
276
277 class ModuleMsgFloodFactory : public ModuleFactory
278 {
279  public:
280         ModuleMsgFloodFactory()
281         {
282         }
283         
284         ~ModuleMsgFloodFactory()
285         {
286         }
287         
288         virtual Module * CreateModule(InspIRCd* Me)
289         {
290                 return new ModuleMsgFlood(Me);
291         }
292         
293 };
294
295
296 extern "C" void * init_module( void )
297 {
298         return new ModuleMsgFloodFactory;
299 }
300