2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2012-2015, 2017-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>
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.
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
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/>.
28 #include "modules/ctctags.h"
29 #include "modules/names.h"
31 class DelayJoinMode : public ModeHandler
34 LocalIntExt& unjoined;
37 DelayJoinMode(Module* Parent, LocalIntExt& ext)
38 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
41 ranktoset = ranktounset = OP_VALUE;
44 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
45 void RevealUser(User* user, Channel* chan);
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.
57 class JoinHook : public ClientProtocol::EventHook
59 const LocalIntExt& unjoined;
62 JoinHook(Module* mod, const LocalIntExt& unjoinedref)
63 : ClientProtocol::EventHook(mod, "JOIN", 10)
64 , unjoined(unjoinedref)
68 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
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))
75 return MOD_RES_PASSTHRU;
83 , public CTCTags::EventListener
84 , public Names::EventListener
92 : CTCTags::EventListener(this)
93 , Names::EventListener(this)
94 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
95 , joinhook(this, unjoined)
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;
112 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
115 if (channel->IsModeSet(this) == adding)
116 return MODEACTION_DENY;
121 * Make all users visible, as +D is being removed. If we don't do this,
122 * they remain permanently invisible on this channel!
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);
128 channel->SetMode(this, adding);
129 return MODEACTION_ALLOW;
132 Version ModuleDelayJoin::GetVersion()
134 return Version("Adds channel mode D (delayjoin) which hides JOIN messages from users until they speak.", VF_VENDOR);
137 ModResult ModuleDelayJoin::OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick)
139 /* don't prevent the user from seeing themself */
140 if (issuer == memb->user)
141 return MOD_RES_PASSTHRU;
143 /* If the user is hidden by delayed join, hide them from the NAMES list */
144 if (unjoined.get(memb))
147 return MOD_RES_PASSTHRU;
150 static void populate(CUList& except, Membership* memb)
152 const Channel::MemberMap& users = memb->chan->GetUsers();
153 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
155 if (i->first == memb->user || !IS_LOCAL(i->first))
157 except.insert(i->first);
161 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
163 if (memb->chan->IsModeSet(djm))
164 unjoined.set(memb, 1);
167 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
169 if (unjoined.set(memb, 0))
170 populate(except, memb);
173 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
175 if (unjoined.set(memb, 0))
176 populate(except, memb);
179 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
181 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
183 Membership* memb = *i;
184 if (unjoined.get(memb))
185 i = include.erase(i);
191 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
193 if (target.type != MessageTarget::TYPE_CHANNEL)
196 Channel* channel = target.Get<Channel>();
197 djm.RevealUser(user, channel);
200 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
202 if (target.type != MessageTarget::TYPE_CHANNEL)
205 Channel* channel = target.Get<Channel>();
206 djm.RevealUser(user, channel);
209 void DelayJoinMode::RevealUser(User* user, Channel* chan)
211 Membership* memb = chan->GetUser(user);
212 if (!memb || !unjoined.set(memb, 0))
215 /* Display the join to everyone else (the user who joined got it earlier) */
217 except_list.insert(user);
218 ClientProtocol::Events::Join joinevent(memb);
219 chan->Write(joinevent, 0, except_list);
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)
225 if (!channel || param.empty())
226 return MOD_RES_PASSTHRU;
228 // If not a prefix mode then we got nothing to do here
229 if (!mh->IsPrefixMode())
230 return MOD_RES_PASSTHRU;
234 dest = ServerInstance->FindNickOnly(param);
236 dest = ServerInstance->FindNick(param);
239 return MOD_RES_PASSTHRU;
241 djm.RevealUser(dest, channel);
242 return MOD_RES_PASSTHRU;
245 MODULE_INIT(ModuleDelayJoin)