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