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