]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
c89b4f511059d16db187f5f9728f875364b2abc1
[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://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 +j (join flood protection) */
17
18 /** Holds settings and state associated with channel mode +j
19  */
20 class joinfloodsettings
21 {
22  public:
23         int secs;
24         int joins;
25         time_t reset;
26         time_t unlocktime;
27         int counter;
28         bool locked;
29
30         joinfloodsettings(int b, int c) : secs(b), joins(c)
31         {
32                 reset = ServerInstance->Time() + secs;
33                 counter = 0;
34                 locked = false;
35         };
36
37         void addjoin()
38         {
39                 counter++;
40                 if (ServerInstance->Time() > reset)
41                 {
42                         counter = 0;
43                         reset = ServerInstance->Time() + secs;
44                 }
45         }
46
47         bool shouldlock()
48         {
49                 return (counter >= this->joins);
50         }
51
52         void clear()
53         {
54                 counter = 0;
55         }
56
57         bool islocked()
58         {
59                 if (locked)
60                 {
61                         if (ServerInstance->Time() > unlocktime)
62                         {
63                                 locked = false;
64                                 return false;
65                         }
66                         else
67                         {
68                                 return true;
69                         }
70                 }
71                 return false;
72         }
73
74         void lock()
75         {
76                 locked = true;
77                 unlocktime = ServerInstance->Time() + 60;
78         }
79
80 };
81
82 /** Handles channel mode +j
83  */
84 class JoinFlood : public ModeHandler
85 {
86  public:
87         SimpleExtItem<joinfloodsettings> ext;
88         JoinFlood(Module* Creator) : ModeHandler(Creator, "joinflood", 'j', PARAM_SETONLY, MODETYPE_CHANNEL),
89                 ext("joinflood", Creator) { }
90
91         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
92         {
93                 if (adding)
94                 {
95                         char ndata[MAXBUF];
96                         char* data = ndata;
97                         strlcpy(ndata,parameter.c_str(),MAXBUF);
98                         char* joins = data;
99                         char* secs = NULL;
100                         while (*data)
101                         {
102                                 if (*data == ':')
103                                 {
104                                         *data = 0;
105                                         data++;
106                                         secs = data;
107                                         break;
108                                 }
109                                 else data++;
110                         }
111                         if (secs)
112
113                         {
114                                 /* Set up the flood parameters for this channel */
115                                 int njoins = atoi(joins);
116                                 int nsecs = atoi(secs);
117                                 if ((njoins<1) || (nsecs<1))
118                                 {
119                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
120                                         parameter.clear();
121                                         return MODEACTION_DENY;
122                                 }
123                                 else
124                                 {
125                                         joinfloodsettings* f = ext.get(channel);
126                                         if (!f)
127                                         {
128                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
129                                                 f = new joinfloodsettings(nsecs, njoins);
130                                                 ext.set(channel, f);
131                                                 channel->SetModeParam('j', parameter);
132                                                 return MODEACTION_ALLOW;
133                                         }
134                                         else
135                                         {
136                                                 std::string cur_param = channel->GetModeParameter('j');
137                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
138                                                 if (cur_param == parameter)
139                                                 {
140                                                         // mode params match
141                                                         return MODEACTION_DENY;
142                                                 }
143                                                 else
144                                                 {
145                                                         // new mode param, replace old with new
146                                                         if ((nsecs > 0) && (njoins > 0))
147                                                         {
148                                                                 f = new joinfloodsettings(nsecs, njoins);
149                                                                 ext.set(channel, f);
150                                                                 channel->SetModeParam('j', parameter);
151                                                                 return MODEACTION_ALLOW;
152                                                         }
153                                                         else
154                                                         {
155                                                                 return MODEACTION_DENY;
156                                                         }
157                                                 }
158                                         }
159                                 }
160                         }
161                         else
162                         {
163                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
164                                 return MODEACTION_DENY;
165                         }
166                 }
167                 else
168                 {
169                         joinfloodsettings* f = ext.get(channel);
170                         if (f)
171                         {
172                                 ext.unset(channel);
173                                 channel->SetModeParam('j', "");
174                                 return MODEACTION_ALLOW;
175                         }
176                 }
177                 return MODEACTION_DENY;
178         }
179 };
180
181 class ModuleJoinFlood : public Module
182 {
183
184         JoinFlood jf;
185
186  public:
187
188         ModuleJoinFlood()
189                 : jf(this)
190         {
191
192                 if (!ServerInstance->Modes->AddMode(&jf))
193                         throw ModuleException("Could not add new modes!");
194                 ServerInstance->Extensions.Register(&jf.ext);
195                 Implementation eventlist[] = { I_OnUserPreJoin, I_OnUserJoin };
196                 ServerInstance->Modules->Attach(eventlist, this, 2);
197         }
198
199         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
200         {
201                 if (chan)
202                 {
203                         joinfloodsettings *f = jf.ext.get(chan);
204                         if (f && f->islocked())
205                         {
206                                 user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
207                                 return MOD_RES_DENY;
208                         }
209                 }
210                 return MOD_RES_PASSTHRU;
211         }
212
213         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
214         {
215                 /* We arent interested in JOIN events caused by a network burst */
216                 if (sync)
217                         return;
218
219                 joinfloodsettings *f = jf.ext.get(memb->chan);
220
221                 /* But all others are OK */
222                 if (f)
223                 {
224                         f->addjoin();
225                         if (f->shouldlock())
226                         {
227                                 f->clear();
228                                 f->lock();
229                                 memb->chan->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", memb->chan->name.c_str(), f->joins, f->secs);
230                         }
231                 }
232         }
233
234         ~ModuleJoinFlood()
235         {
236         }
237
238         Version GetVersion()
239         {
240                 return Version("Provides channel mode +j (join flood protection)", VF_COMMON | VF_VENDOR);
241         }
242 };
243
244 MODULE_INIT(ModuleJoinFlood)