]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 A_D <A-UNDERSCORE-D@users.noreply.github.com>
5  *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
6  *   Copyright (C) 2013, 2017-2020 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
13  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
14  *
15  * This file is part of InspIRCd.  InspIRCd is free software: you can
16  * redistribute it and/or modify it under the terms of the GNU General Public
17  * License as published by the Free Software Foundation, version 2.
18  *
19  * This program is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26  */
27
28
29 #include "inspircd.h"
30 #include "modules/invite.h"
31
32 enum
33 {
34         // From RFC 2182.
35         ERR_UNAVAILRESOURCE = 437
36 };
37
38
39 class KickRejoinData
40 {
41         struct KickedUser
42         {
43                 std::string uuid;
44                 time_t expire;
45
46                 KickedUser(User* user, unsigned int Delay)
47                         : uuid(user->uuid)
48                         , expire(ServerInstance->Time() + Delay)
49                 {
50                 }
51         };
52
53         typedef std::vector<KickedUser> KickedList;
54
55         mutable KickedList kicked;
56
57  public:
58         const unsigned int delay;
59
60         KickRejoinData(unsigned int Delay) : delay(Delay) { }
61
62         bool canjoin(LocalUser* user) const
63         {
64                 for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
65                 {
66                         KickedUser& rec = *i;
67                         if (rec.expire > ServerInstance->Time())
68                         {
69                                 if (rec.uuid == user->uuid)
70                                         return false;
71                                 ++i;
72                         }
73                         else
74                         {
75                                 // Expired record, remove.
76                                 stdalgo::vector::swaperase(kicked, i);
77                                 if (kicked.empty())
78                                         break;
79                         }
80                 }
81                 return true;
82         }
83
84         void add(User* user)
85         {
86                 // One user can be in the list multiple times if the user gets kicked, force joins
87                 // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
88                 // works correctly in this case as well
89                 kicked.push_back(KickedUser(user, delay));
90         }
91 };
92
93 /** Handles channel mode +J
94  */
95 class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
96 {
97         const unsigned int max;
98  public:
99         KickRejoin(Module* Creator)
100                 : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
101                 , max(60)
102         {
103                 syntax = "<seconds>";
104         }
105
106         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
107         {
108                 unsigned int v = ConvToNum<unsigned int>(parameter);
109                 if (v <= 0)
110                 {
111                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
112                         return MODEACTION_DENY;
113                 }
114
115                 if (IS_LOCAL(source) && v > max)
116                         v = max;
117
118                 ext.set(channel, new KickRejoinData(v));
119                 return MODEACTION_ALLOW;
120         }
121
122         void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
123         {
124                 out.append(ConvToStr(krd->delay));
125         }
126
127         std::string GetModuleSettings() const
128         {
129                 return ConvToStr(max);
130         }
131 };
132
133 class ModuleKickNoRejoin : public Module
134 {
135         KickRejoin kr;
136         Invite::API invapi;
137
138 public:
139         ModuleKickNoRejoin()
140                 : kr(this)
141                 , invapi(this)
142         {
143         }
144
145         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
146         {
147                 if (chan)
148                 {
149                         const KickRejoinData* data = kr.ext.get(chan);
150                         if ((data) && !invapi->IsInvited(user, chan) && (!data->canjoin(user)))
151                         {
152                                 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J is set)", data->delay));
153                                 return MOD_RES_DENY;
154                         }
155                 }
156                 return MOD_RES_PASSTHRU;
157         }
158
159         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
160         {
161                 if ((!IS_LOCAL(memb->user)) || (source == memb->user))
162                         return;
163
164                 KickRejoinData* data = kr.ext.get(memb->chan);
165                 if (data)
166                 {
167                         data->add(memb->user);
168                 }
169         }
170
171         Version GetVersion() CXX11_OVERRIDE
172         {
173                 return Version("Adds channel mode J (kicknorejoin) which prevents users from rejoining after being kicked from a channel.", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
174         }
175 };
176
177 MODULE_INIT(ModuleKickNoRejoin)