]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
72bdad1551ae0368f0ef38493797559f430ad644
[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
32 class DelayJoinMode : public ModeHandler
33 {
34  private:
35         LocalIntExt& unjoined;
36         IRCv3::ServerTime::API servertime;
37
38  public:
39         DelayJoinMode(Module* Parent, LocalIntExt& ext)
40                 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
41                 , unjoined(ext)
42                 , servertime(Parent)
43         {
44                 ranktoset = ranktounset = OP_VALUE;
45         }
46
47         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
48         void RevealUser(User* user, Channel* chan);
49 };
50
51
52 namespace
53 {
54
55 /** Hook handler for join client protocol events.
56  * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
57  * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
58  * such as hostcycle generates a join.
59  */
60 class JoinHook : public ClientProtocol::EventHook
61 {
62  private:
63         const LocalIntExt& unjoined;
64
65  public:
66         JoinHook(Module* mod, const LocalIntExt& unjoinedref)
67                 : ClientProtocol::EventHook(mod, "JOIN", 10)
68                 , unjoined(unjoinedref)
69         {
70         }
71
72         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
73         {
74                 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
75                 const Membership* const memb = join.GetMember();
76                 const User* const u = memb->user;
77                 if ((unjoined.get(memb)) && (u != user))
78                         return MOD_RES_DENY;
79                 return MOD_RES_PASSTHRU;
80         }
81 };
82
83 }
84
85 class ModuleDelayJoin
86         : public Module
87         , public CTCTags::EventListener
88         , public Names::EventListener
89 {
90  public:
91         LocalIntExt unjoined;
92         JoinHook joinhook;
93         DelayJoinMode djm;
94
95         ModuleDelayJoin()
96                 : CTCTags::EventListener(this)
97                 , Names::EventListener(this)
98                 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
99                 , joinhook(this, unjoined)
100                 , djm(this, unjoined)
101         {
102         }
103
104         Version GetVersion() CXX11_OVERRIDE;
105         ModResult OnNamesListItem(LocalUser* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
106         void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
107         void CleanUser(User* user);
108         void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
109         void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
110         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
111         void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
112         void OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
113         ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
114 };
115
116 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
117 {
118         /* no change */
119         if (channel->IsModeSet(this) == adding)
120                 return MODEACTION_DENY;
121
122         if (!adding)
123         {
124                 /*
125                  * Make all users visible, as +D is being removed. If we don't do this,
126                  * they remain permanently invisible on this channel!
127                  */
128                 const Channel::MemberMap& users = channel->GetUsers();
129                 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
130                         RevealUser(n->first, channel);
131         }
132         channel->SetMode(this, adding);
133         return MODEACTION_ALLOW;
134 }
135
136 Version ModuleDelayJoin::GetVersion()
137 {
138         return Version("Adds channel mode D (delayjoin) which hides JOIN messages from users until they speak.", VF_VENDOR);
139 }
140
141 ModResult ModuleDelayJoin::OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick)
142 {
143         /* don't prevent the user from seeing themself */
144         if (issuer == memb->user)
145                 return MOD_RES_PASSTHRU;
146
147         /* If the user is hidden by delayed join, hide them from the NAMES list */
148         if (unjoined.get(memb))
149                 return MOD_RES_DENY;
150
151         return MOD_RES_PASSTHRU;
152 }
153
154 static void populate(CUList& except, Membership* memb)
155 {
156         const Channel::MemberMap& users = memb->chan->GetUsers();
157         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
158         {
159                 if (i->first == memb->user || !IS_LOCAL(i->first))
160                         continue;
161                 except.insert(i->first);
162         }
163 }
164
165 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
166 {
167         if (memb->chan->IsModeSet(djm))
168                 unjoined.set(memb, ServerInstance->Time());
169 }
170
171 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
172 {
173         if (unjoined.set(memb, 0))
174                 populate(except, memb);
175 }
176
177 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
178 {
179         if (unjoined.set(memb, 0))
180                 populate(except, memb);
181 }
182
183 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
184 {
185         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
186         {
187                 Membership* memb = *i;
188                 if (unjoined.get(memb))
189                         i = include.erase(i);
190                 else
191                         ++i;
192         }
193 }
194
195 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
196 {
197         if (target.type != MessageTarget::TYPE_CHANNEL)
198                 return;
199
200         Channel* channel = target.Get<Channel>();
201         djm.RevealUser(user, channel);
202 }
203
204 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
205 {
206         if (target.type != MessageTarget::TYPE_CHANNEL)
207                 return;
208
209         Channel* channel = target.Get<Channel>();
210         djm.RevealUser(user, channel);
211 }
212
213 void DelayJoinMode::RevealUser(User* user, Channel* chan)
214 {
215         Membership* memb = chan->GetUser(user);
216         if (!memb)
217                 return;
218
219         time_t jointime = unjoined.set(memb, 0);
220         if (!jointime)
221                 return;
222
223         /* Display the join to everyone else (the user who joined got it earlier) */
224         CUList except_list;
225         except_list.insert(user);
226         ClientProtocol::Events::Join joinevent(memb);
227         if (servertime)
228                 servertime->Set(joinevent, jointime);
229         chan->Write(joinevent, 0, except_list);
230 }
231
232 /* make the user visible if they receive any mode change */
233 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
234 {
235         if (!channel || param.empty())
236                 return MOD_RES_PASSTHRU;
237
238         // If not a prefix mode then we got nothing to do here
239         if (!mh->IsPrefixMode())
240                 return MOD_RES_PASSTHRU;
241
242         User* dest;
243         if (IS_LOCAL(user))
244                 dest = ServerInstance->FindNickOnly(param);
245         else
246                 dest = ServerInstance->FindNick(param);
247
248         if (!dest)
249                 return MOD_RES_PASSTHRU;
250
251         djm.RevealUser(dest, channel);
252         return MOD_RES_PASSTHRU;
253 }
254
255 MODULE_INIT(ModuleDelayJoin)