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