]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
m_chanfilter: Apply filters to part messages (#1702)
[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 enum
27 {
28         // From RFC 2182.
29         ERR_UNAVAILRESOURCE = 437
30 };
31
32 // The number of seconds the channel will be closed for.
33 static unsigned int duration;
34
35 /** Holds settings and state associated with channel mode +j
36  */
37 class joinfloodsettings
38 {
39  public:
40         unsigned int secs;
41         unsigned int joins;
42         time_t reset;
43         time_t unlocktime;
44         unsigned int counter;
45
46         joinfloodsettings(unsigned int b, unsigned int c)
47                 : secs(b), joins(c), unlocktime(0), counter(0)
48         {
49                 reset = ServerInstance->Time() + secs;
50         }
51
52         void addjoin()
53         {
54                 if (ServerInstance->Time() > reset)
55                 {
56                         counter = 1;
57                         reset = ServerInstance->Time() + secs;
58                 }
59                 else
60                         counter++;
61         }
62
63         bool shouldlock()
64         {
65                 return (counter >= this->joins);
66         }
67
68         void clear()
69         {
70                 counter = 0;
71         }
72
73         bool islocked()
74         {
75                 if (ServerInstance->Time() > unlocktime)
76                         unlocktime = 0;
77
78                 return (unlocktime != 0);
79         }
80
81         void lock()
82         {
83                 unlocktime = ServerInstance->Time() + duration;
84         }
85
86         bool operator==(const joinfloodsettings& other) const
87         {
88                 return ((this->secs == other.secs) && (this->joins == other.joins));
89         }
90 };
91
92 /** Handles channel mode +j
93  */
94 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
95 {
96  public:
97         JoinFlood(Module* Creator)
98                 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
99         {
100                 syntax = "<joins>:<seconds>";
101         }
102
103         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
104         {
105                 std::string::size_type colon = parameter.find(':');
106                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
107                 {
108                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
109                         return MODEACTION_DENY;
110                 }
111
112                 /* Set up the flood parameters for this channel */
113                 unsigned int njoins = ConvToNum<unsigned int>(parameter.substr(0, colon));
114                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
115                 if ((njoins<1) || (nsecs<1))
116                 {
117                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
118                         return MODEACTION_DENY;
119                 }
120
121                 ext.set(channel, new joinfloodsettings(nsecs, njoins));
122                 return MODEACTION_ALLOW;
123         }
124
125         void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
126         {
127                 out.append(ConvToStr(jfs->joins)).push_back(':');
128                 out.append(ConvToStr(jfs->secs));
129         }
130 };
131
132 class ModuleJoinFlood : public Module
133 {
134         JoinFlood jf;
135
136  public:
137         ModuleJoinFlood()
138                 : jf(this)
139         {
140         }
141
142         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
143         {
144                 ConfigTag* tag = ServerInstance->Config->ConfValue("joinflood");
145                 duration = tag->getDuration("duration", 60, 10, 600);
146         }
147
148         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
149         {
150                 if (chan)
151                 {
152                         joinfloodsettings *f = jf.ext.get(chan);
153                         if (f && f->islocked())
154                         {
155                                 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, "This channel is temporarily unavailable (+j is set). Please try again later.");
156                                 return MOD_RES_DENY;
157                         }
158                 }
159                 return MOD_RES_PASSTHRU;
160         }
161
162         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
163         {
164                 /* We arent interested in JOIN events caused by a network burst */
165                 if (sync)
166                         return;
167
168                 joinfloodsettings *f = jf.ext.get(memb->chan);
169
170                 /* But all others are OK */
171                 if ((f) && (!f->islocked()))
172                 {
173                         f->addjoin();
174                         if (f->shouldlock())
175                         {
176                                 f->clear();
177                                 f->lock();
178                                 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));
179                         }
180                 }
181         }
182
183         Version GetVersion() CXX11_OVERRIDE
184         {
185                 return Version("Provides channel mode +j, join flood protection", VF_VENDOR);
186         }
187 };
188
189 MODULE_INIT(ModuleJoinFlood)