]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Implement support for the delayjoin WHOX flag.
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2015, 2018 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2010 Jens Voss <DukePyrolator@anope.org>
8  *   Copyright (C) 2009 Matt Smith <dz@inspircd.org>
9  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
10  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
11  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "modules/ctctags.h"
29 #include "modules/ircv3_servertime.h"
30 #include "modules/names.h"
31 #include "modules/who.h"
32
33 class DelayJoinMode : public ModeHandler
34 {
35  private:
36         LocalIntExt& unjoined;
37         IRCv3::ServerTime::API servertime;
38
39  public:
40         DelayJoinMode(Module* Parent, LocalIntExt& ext)
41                 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
42                 , unjoined(ext)
43                 , servertime(Parent)
44         {
45                 ranktoset = ranktounset = OP_VALUE;
46         }
47
48         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
49         void RevealUser(User* user, Channel* chan);
50 };
51
52
53 namespace
54 {
55
56 /** Hook handler for join client protocol events.
57  * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
58  * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
59  * such as hostcycle generates a join.
60  */
61 class JoinHook : public ClientProtocol::EventHook
62 {
63  private:
64         const LocalIntExt& unjoined;
65
66  public:
67         JoinHook(Module* mod, const LocalIntExt& unjoinedref)
68                 : ClientProtocol::EventHook(mod, "JOIN", 10)
69                 , unjoined(unjoinedref)
70         {
71         }
72
73         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
74         {
75                 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
76                 const Membership* const memb = join.GetMember();
77                 const User* const u = memb->user;
78                 if ((unjoined.get(memb)) && (u != user))
79                         return MOD_RES_DENY;
80                 return MOD_RES_PASSTHRU;
81         }
82 };
83
84 }
85
86 class ModuleDelayJoin
87         : public Module
88         , public CTCTags::EventListener
89         , public Names::EventListener
90         , public Who::EventListener
91 {
92  public:
93         LocalIntExt unjoined;
94         JoinHook joinhook;
95         DelayJoinMode djm;
96
97         ModuleDelayJoin()
98                 : CTCTags::EventListener(this)
99                 , Names::EventListener(this)
100                 , Who::EventListener(this)
101                 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
102                 , joinhook(this, unjoined)
103                 , djm(this, unjoined)
104         {
105         }
106
107         Version GetVersion() CXX11_OVERRIDE;
108         ModResult OnNamesListItem(LocalUser* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
109         ModResult OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE;
110         void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
111         void CleanUser(User* user);
112         void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
113         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
114         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
115         void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
116         void OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
117         ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
118 };
119
120 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
121 {
122         /* no change */
123         if (channel->IsModeSet(this) == adding)
124                 return MODEACTION_DENY;
125
126         if (!adding)
127         {
128                 /*
129                  * Make all users visible, as +D is being removed. If we don't do this,
130                  * they remain permanently invisible on this channel!
131                  */
132                 const Channel::MemberMap& users = channel->GetUsers();
133                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
134                         RevealUser(n->first, channel);
135         }
136         channel->SetMode(this, adding);
137         return MODEACTION_ALLOW;
138 }
139
140 Version ModuleDelayJoin::GetVersion()
141 {
142         return Version("Adds channel mode D (delayjoin) which hides JOIN messages from users until they speak.", VF_VENDOR);
143 }
144
145 ModResult ModuleDelayJoin::OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick)
146 {
147         /* don't prevent the user from seeing themself */
148         if (issuer == memb->user)
149                 return MOD_RES_PASSTHRU;
150
151         /* If the user is hidden by delayed join, hide them from the NAMES list */
152         if (unjoined.get(memb))
153                 return MOD_RES_DENY;
154
155         return MOD_RES_PASSTHRU;
156 }
157
158 ModResult ModuleDelayJoin::OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric)
159 {
160         // We don't need to do anything if they're not delayjoined.
161         if (!memb || !unjoined.get(memb))
162                 return MOD_RES_PASSTHRU;
163
164         // Only show delayjoined users if the d flag has been specified.
165         if (!request.flags['d'])
166                 return MOD_RES_DENY;
167
168         // Add the < flag to mark the user as delayjoined.
169         size_t flag_index;
170         if (request.GetFieldIndex('f', flag_index))
171                 numeric.GetParams()[flag_index].push_back('<');
172         return MOD_RES_PASSTHRU;
173 }
174
175 static void populate(CUList& except, Membership* memb)
176 {
177         const Channel::MemberMap& users = memb->chan->GetUsers();
178         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
179         {
180                 if (i->first == memb->user || !IS_LOCAL(i->first))
181                         continue;
182                 except.insert(i->first);
183         }
184 }
185
186 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
187 {
188         if (memb->chan->IsModeSet(djm))
189                 unjoined.set(memb, ServerInstance->Time());
190 }
191
192 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
193 {
194         if (unjoined.set(memb, 0))
195                 populate(except, memb);
196 }
197
198 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
199 {
200         if (unjoined.set(memb, 0))
201                 populate(except, memb);
202 }
203
204 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
205 {
206         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
207         {
208                 Membership* memb = *i;
209                 if (unjoined.get(memb))
210                         i = include.erase(i);
211                 else
212                         ++i;
213         }
214 }
215
216 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
217 {
218         if (target.type != MessageTarget::TYPE_CHANNEL)
219                 return;
220
221         Channel* channel = target.Get<Channel>();
222         djm.RevealUser(user, channel);
223 }
224
225 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
226 {
227         if (target.type != MessageTarget::TYPE_CHANNEL)
228                 return;
229
230         Channel* channel = target.Get<Channel>();
231         djm.RevealUser(user, channel);
232 }
233
234 void DelayJoinMode::RevealUser(User* user, Channel* chan)
235 {
236         Membership* memb = chan->GetUser(user);
237         if (!memb)
238                 return;
239
240         time_t jointime = unjoined.set(memb, 0);
241         if (!jointime)
242                 return;
243
244         /* Display the join to everyone else (the user who joined got it earlier) */
245         CUList except_list;
246         except_list.insert(user);
247         ClientProtocol::Events::Join joinevent(memb);
248         if (servertime)
249                 servertime->Set(joinevent, jointime);
250         chan->Write(joinevent, 0, except_list);
251 }
252
253 /* make the user visible if they receive any mode change */
254 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
255 {
256         if (!channel || param.empty())
257                 return MOD_RES_PASSTHRU;
258
259         // If not a prefix mode then we got nothing to do here
260         if (!mh->IsPrefixMode())
261                 return MOD_RES_PASSTHRU;
262
263         User* dest;
264         if (IS_LOCAL(user))
265                 dest = ServerInstance->FindNickOnly(param);
266         else
267                 dest = ServerInstance->FindNick(param);
268
269         if (!dest)
270                 return MOD_RES_PASSTHRU;
271
272         djm.RevealUser(dest, channel);
273         return MOD_RES_PASSTHRU;
274 }
275
276 MODULE_INIT(ModuleDelayJoin)