]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
fixed some indentation and spacing in modules
[user/henk/code/inspircd.git] / src / modules / m_joinflood.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 +j (join flood protection) */
17
18 /** Holds settings and state associated with channel mode +j
19  */
20 class joinfloodsettings : public classbase
21 {
22  public:
23
24         int secs;
25         int joins;
26         time_t reset;
27         time_t unlocktime;
28         int counter;
29         bool locked;
30         InspIRCd* ServerInstance;
31
32         joinfloodsettings() : secs(0), joins(0) {};
33
34         joinfloodsettings(int b, int c) : secs(b), joins(c)
35         {
36                 reset = time(NULL) + secs;
37                 counter = 0;
38                 locked = false;
39         };
40
41         void addjoin()
42         {
43                 counter++;
44                 if (time(NULL) > reset)
45                 {
46                         counter = 0;
47                         reset = time(NULL) + secs;
48                 }
49         }
50
51         bool shouldlock()
52         {
53                 return (counter >= this->joins);
54         }
55
56         void clear()
57         {
58                 counter = 0;
59         }
60
61         bool islocked()
62         {
63                 if (locked)
64                 {
65                         if (time(NULL) > unlocktime)
66                         {
67                                 locked = false;
68                                 return false;
69                         }
70                         else
71                         {
72                                 return true;
73                         }
74                 }
75                 return false;
76         }
77
78         void lock()
79         {
80                 locked = true;
81                 unlocktime = time(NULL) + 60;
82         }
83
84 };
85
86 /** Handles channel mode +j
87  */
88 class JoinFlood : public ModeHandler
89 {
90  public:
91         JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
92
93         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
94         {
95                 joinfloodsettings* x;
96                 if (channel->GetExt("joinflood",x))
97                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
98                 else
99                         return std::make_pair(false, parameter);
100         }
101
102         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
103         {
104                 /* When TS is equal, the alphabetically later one wins */
105                 return (their_param < our_param);
106         }
107
108         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
109         {
110                 joinfloodsettings* dummy;
111
112                 if (adding)
113                 {
114                         char ndata[MAXBUF];
115                         char* data = ndata;
116                         strlcpy(ndata,parameter.c_str(),MAXBUF);
117                         char* joins = data;
118                         char* secs = NULL;
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                         {
133                                 /* Set up the flood parameters for this channel */
134                                 int njoins = atoi(joins);
135                                 int nsecs = atoi(secs);
136                                 if ((njoins<1) || (nsecs<1))
137                                 {
138                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
139                                         parameter.clear();
140                                         return MODEACTION_DENY;
141                                 }
142                                 else
143                                 {
144                                         if (!channel->GetExt("joinflood", dummy))
145                                         {
146                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
147                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
148                                                 channel->Extend("joinflood", f);
149                                                 channel->SetMode('j', true);
150                                                 channel->SetModeParam('j', parameter.c_str(), true);
151                                                 return MODEACTION_ALLOW;
152                                         }
153                                         else
154                                         {
155                                                 std::string cur_param = channel->GetModeParameter('j');
156                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
157                                                 if (cur_param == parameter)
158                                                 {
159                                                         // mode params match
160                                                         return MODEACTION_DENY;
161                                                 }
162                                                 else
163                                                 {
164                                                         // new mode param, replace old with new
165                                                         if ((nsecs > 0) && (njoins > 0))
166                                                         {
167                                                                 joinfloodsettings* f;
168                                                                 channel->GetExt("joinflood", f);
169                                                                 delete f;
170                                                                 f = new joinfloodsettings(nsecs,njoins);
171                                                                 channel->Shrink("joinflood");
172                                                                 channel->Extend("joinflood", f);
173                                                                 channel->SetModeParam('j', cur_param.c_str(), false);
174                                                                 channel->SetModeParam('j', parameter.c_str(), true);
175                                                                 return MODEACTION_ALLOW;
176                                                         }
177                                                         else
178                                                         {
179                                                                 return MODEACTION_DENY;
180                                                         }
181                                                 }
182                                         }
183                                 }
184                         }
185                         else
186                         {
187                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
188                                 return MODEACTION_DENY;
189                         }
190                 }
191                 else
192                 {
193                         if (channel->GetExt("joinflood", dummy))
194                         {
195                                 joinfloodsettings *f;
196                                 channel->GetExt("joinflood", f);
197                                 delete f;
198                                 channel->Shrink("joinflood");
199                                 channel->SetMode('j', false);
200                                 return MODEACTION_ALLOW;
201                         }
202                 }
203                 return MODEACTION_DENY;
204         }
205 };
206
207 class ModuleJoinFlood : public Module
208 {
209
210         JoinFlood* jf;
211
212  public:
213
214         ModuleJoinFlood(InspIRCd* Me)
215                 : Module(Me)
216         {
217
218                 jf = new JoinFlood(ServerInstance);
219                 if (!ServerInstance->Modes->AddMode(jf))
220                         throw ModuleException("Could not add new modes!");
221                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
222                 ServerInstance->Modules->Attach(eventlist, this, 3);
223         }
224
225         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
226         {
227                 if (chan)
228                 {
229                         joinfloodsettings *f;
230                         if (chan->GetExt("joinflood", f))
231                         {
232                                 if (f->islocked())
233                                 {
234                                         user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
235                                         return 1;
236                                 }
237                         }
238                 }
239                 return 0;
240         }
241
242         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
243         {
244                 joinfloodsettings *f;
245
246                 /* We arent interested in JOIN events caused by a network burst */
247                 if (sync)
248                         return;
249
250                 /* But all others are OK */
251                 if (channel->GetExt("joinflood",f))
252                 {
253                         f->addjoin();
254                         if (f->shouldlock())
255                         {
256                                 f->clear();
257                                 f->lock();
258                                 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", channel->name.c_str(), f->joins, f->secs);
259                         }
260                 }
261         }
262
263         void OnChannelDelete(Channel* chan)
264         {
265                 joinfloodsettings *f;
266                 if (chan->GetExt("joinflood",f))
267                 {
268                         delete f;
269                         chan->Shrink("joinflood");
270                 }
271         }
272
273
274         virtual ~ModuleJoinFlood()
275         {
276                 ServerInstance->Modes->DelMode(jf);
277                 delete jf;
278         }
279
280         virtual Version GetVersion()
281         {
282                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
283         }
284 };
285
286 MODULE_INIT(ModuleJoinFlood)