2 * InspIRCd -- Internet Relay Chat Daemon
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>
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/ircv3_servertime.h"
30 #include "modules/names.h"
31 #include "modules/who.h"
33 class DelayJoinMode : public ModeHandler
36 LocalIntExt& unjoined;
37 IRCv3::ServerTime::API servertime;
40 DelayJoinMode(Module* Parent, LocalIntExt& ext)
41 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
45 ranktoset = ranktounset = OP_VALUE;
48 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
49 void RevealUser(User* user, Channel* chan);
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.
61 class JoinHook : public ClientProtocol::EventHook
64 const LocalIntExt& unjoined;
67 JoinHook(Module* mod, const LocalIntExt& unjoinedref)
68 : ClientProtocol::EventHook(mod, "JOIN", 10)
69 , unjoined(unjoinedref)
73 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
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))
80 return MOD_RES_PASSTHRU;
88 , public CTCTags::EventListener
89 , public Names::EventListener
90 , public Who::EventListener
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)
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;
120 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
123 if (channel->IsModeSet(this) == adding)
124 return MODEACTION_DENY;
129 * Make all users visible, as +D is being removed. If we don't do this,
130 * they remain permanently invisible on this channel!
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);
136 channel->SetMode(this, adding);
137 return MODEACTION_ALLOW;
140 Version ModuleDelayJoin::GetVersion()
142 return Version("Adds channel mode D (delayjoin) which hides JOIN messages from users until they speak.", VF_VENDOR);
145 ModResult ModuleDelayJoin::OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick)
147 /* don't prevent the user from seeing themself */
148 if (issuer == memb->user)
149 return MOD_RES_PASSTHRU;
151 /* If the user is hidden by delayed join, hide them from the NAMES list */
152 if (unjoined.get(memb))
155 return MOD_RES_PASSTHRU;
158 ModResult ModuleDelayJoin::OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric)
160 // We don't need to do anything if they're not delayjoined.
161 if (!memb || !unjoined.get(memb))
162 return MOD_RES_PASSTHRU;
164 // Only show delayjoined users if the d flag has been specified.
165 if (!request.flags['d'])
168 // Add the < flag to mark the user as delayjoined.
170 if (request.GetFieldIndex('f', flag_index))
171 numeric.GetParams()[flag_index].push_back('<');
172 return MOD_RES_PASSTHRU;
175 static void populate(CUList& except, Membership* memb)
177 const Channel::MemberMap& users = memb->chan->GetUsers();
178 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
180 if (i->first == memb->user || !IS_LOCAL(i->first))
182 except.insert(i->first);
186 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
188 if (memb->chan->IsModeSet(djm))
189 unjoined.set(memb, ServerInstance->Time());
192 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
194 if (unjoined.set(memb, 0))
195 populate(except, memb);
198 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
200 if (unjoined.set(memb, 0))
201 populate(except, memb);
204 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
206 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
208 Membership* memb = *i;
209 if (unjoined.get(memb))
210 i = include.erase(i);
216 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
218 if (target.type != MessageTarget::TYPE_CHANNEL)
221 Channel* channel = target.Get<Channel>();
222 djm.RevealUser(user, channel);
225 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
227 if (target.type != MessageTarget::TYPE_CHANNEL)
230 Channel* channel = target.Get<Channel>();
231 djm.RevealUser(user, channel);
234 void DelayJoinMode::RevealUser(User* user, Channel* chan)
236 Membership* memb = chan->GetUser(user);
240 time_t jointime = unjoined.set(memb, 0);
244 /* Display the join to everyone else (the user who joined got it earlier) */
246 except_list.insert(user);
247 ClientProtocol::Events::Join joinevent(memb);
249 servertime->Set(joinevent, jointime);
250 chan->Write(joinevent, 0, except_list);
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)
256 if (!channel || param.empty())
257 return MOD_RES_PASSTHRU;
259 // If not a prefix mode then we got nothing to do here
260 if (!mh->IsPrefixMode())
261 return MOD_RES_PASSTHRU;
265 dest = ServerInstance->FindNickOnly(param);
267 dest = ServerInstance->FindNick(param);
270 return MOD_RES_PASSTHRU;
272 djm.RevealUser(dest, channel);
273 return MOD_RES_PASSTHRU;
276 MODULE_INIT(ModuleDelayJoin)