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