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