]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Merge pull request #1361 from genius3000/master+rline_IP
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 // The number of seconds the channel will be closed for.
27 static unsigned int duration;
28
29 /** Holds settings and state associated with channel mode +j
30  */
31 class joinfloodsettings
32 {
33  public:
34         unsigned int secs;
35         unsigned int joins;
36         time_t reset;
37         time_t unlocktime;
38         unsigned int counter;
39
40         joinfloodsettings(unsigned int b, unsigned int c)
41                 : secs(b), joins(c), unlocktime(0), counter(0)
42         {
43                 reset = ServerInstance->Time() + secs;
44         }
45
46         void addjoin()
47         {
48                 if (ServerInstance->Time() > reset)
49                 {
50                         counter = 1;
51                         reset = ServerInstance->Time() + secs;
52                 }
53                 else
54                         counter++;
55         }
56
57         bool shouldlock()
58         {
59                 return (counter >= this->joins);
60         }
61
62         void clear()
63         {
64                 counter = 0;
65         }
66
67         bool islocked()
68         {
69                 if (ServerInstance->Time() > unlocktime)
70                         unlocktime = 0;
71
72                 return (unlocktime != 0);
73         }
74
75         void lock()
76         {
77                 unlocktime = ServerInstance->Time() + duration;
78         }
79
80         bool operator==(const joinfloodsettings& other) const
81         {
82                 return ((this->secs == other.secs) && (this->joins == other.joins));
83         }
84 };
85
86 /** Handles channel mode +j
87  */
88 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
89 {
90  public:
91         JoinFlood(Module* Creator)
92                 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
93         {
94         }
95
96         ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
97         {
98                 std::string::size_type colon = parameter.find(':');
99                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
100                 {
101                         source->WriteNumeric(608, channel->name, "Invalid flood parameter");
102                         return MODEACTION_DENY;
103                 }
104
105                 /* Set up the flood parameters for this channel */
106                 unsigned int njoins = ConvToInt(parameter.substr(0, colon));
107                 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
108                 if ((njoins<1) || (nsecs<1))
109                 {
110                         source->WriteNumeric(608, channel->name, "Invalid flood parameter");
111                         return MODEACTION_DENY;
112                 }
113
114                 ext.set(channel, new joinfloodsettings(nsecs, njoins));
115                 return MODEACTION_ALLOW;
116         }
117
118         void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
119         {
120                 out.append(ConvToStr(jfs->joins)).push_back(':');
121                 out.append(ConvToStr(jfs->secs));
122         }
123 };
124
125 class ModuleJoinFlood : public Module
126 {
127         JoinFlood jf;
128
129  public:
130         ModuleJoinFlood()
131                 : jf(this)
132         {
133         }
134
135         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
136         {
137                 ConfigTag* tag = ServerInstance->Config->ConfValue("joinflood");
138                 duration = tag->getDuration("duration", 60, 10, 600);
139         }
140
141         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
142         {
143                 if (chan)
144                 {
145                         joinfloodsettings *f = jf.ext.get(chan);
146                         if (f && f->islocked())
147                         {
148                                 user->WriteNumeric(609, chan->name, "This channel is temporarily unavailable (+j). Please try again later.");
149                                 return MOD_RES_DENY;
150                         }
151                 }
152                 return MOD_RES_PASSTHRU;
153         }
154
155         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
156         {
157                 /* We arent interested in JOIN events caused by a network burst */
158                 if (sync)
159                         return;
160
161                 joinfloodsettings *f = jf.ext.get(memb->chan);
162
163                 /* But all others are OK */
164                 if ((f) && (!f->islocked()))
165                 {
166                         f->addjoin();
167                         if (f->shouldlock())
168                         {
169                                 f->clear();
170                                 f->lock();
171                                 memb->chan->WriteNotice(InspIRCd::Format("This channel has been closed to new users for %u seconds because there have been more than %d joins in %d seconds.", duration, f->joins, f->secs));
172                         }
173                 }
174         }
175
176         Version GetVersion() CXX11_OVERRIDE
177         {
178                 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
179         }
180 };
181
182 MODULE_INIT(ModuleJoinFlood)