2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2010 Jens Voss <DukePyrolator@anope.org>
5 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
6 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "modules/ctctags.h"
25 #include "modules/names.h"
27 class DelayJoinMode : public ModeHandler
30 LocalIntExt& unjoined;
33 DelayJoinMode(Module* Parent, LocalIntExt& ext)
34 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
37 ranktoset = ranktounset = OP_VALUE;
40 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
41 void RevealUser(User* user, Channel* chan);
48 /** Hook handler for join client protocol events.
49 * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
50 * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
51 * such as hostcycle generates a join.
53 class JoinHook : public ClientProtocol::EventHook
55 const LocalIntExt& unjoined;
58 JoinHook(Module* mod, const LocalIntExt& unjoinedref)
59 : ClientProtocol::EventHook(mod, "JOIN", 10)
60 , unjoined(unjoinedref)
64 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
66 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
67 const Membership* const memb = join.GetMember();
68 const User* const u = memb->user;
69 if ((unjoined.get(memb)) && (u != user))
71 return MOD_RES_PASSTHRU;
79 , public CTCTags::EventListener
80 , public Names::EventListener
88 : CTCTags::EventListener(this)
89 , Names::EventListener(this)
90 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
91 , joinhook(this, unjoined)
96 Version GetVersion() CXX11_OVERRIDE;
97 ModResult OnNamesListItem(LocalUser* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
98 void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
99 void CleanUser(User* user);
100 void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
101 void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
102 void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
103 void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
104 void OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
105 ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
108 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
111 if (channel->IsModeSet(this) == adding)
112 return MODEACTION_DENY;
117 * Make all users visible, as +D is being removed. If we don't do this,
118 * they remain permanently invisible on this channel!
120 const Channel::MemberMap& users = channel->GetUsers();
121 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
122 RevealUser(n->first, channel);
124 channel->SetMode(this, adding);
125 return MODEACTION_ALLOW;
128 Version ModuleDelayJoin::GetVersion()
130 return Version("Provides channel mode +D, delay-join, users don't appear as joined to others until they speak", VF_VENDOR);
133 ModResult ModuleDelayJoin::OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick)
135 /* don't prevent the user from seeing themself */
136 if (issuer == memb->user)
137 return MOD_RES_PASSTHRU;
139 /* If the user is hidden by delayed join, hide them from the NAMES list */
140 if (unjoined.get(memb))
143 return MOD_RES_PASSTHRU;
146 static void populate(CUList& except, Membership* memb)
148 const Channel::MemberMap& users = memb->chan->GetUsers();
149 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
151 if (i->first == memb->user || !IS_LOCAL(i->first))
153 except.insert(i->first);
157 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
159 if (memb->chan->IsModeSet(djm))
160 unjoined.set(memb, 1);
163 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
165 if (unjoined.set(memb, 0))
166 populate(except, memb);
169 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
171 if (unjoined.set(memb, 0))
172 populate(except, memb);
175 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
177 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
179 Membership* memb = *i;
180 if (unjoined.get(memb))
181 i = include.erase(i);
187 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
189 if (target.type != MessageTarget::TYPE_CHANNEL)
192 Channel* channel = target.Get<Channel>();
193 djm.RevealUser(user, channel);
196 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
198 if (target.type != MessageTarget::TYPE_CHANNEL)
201 Channel* channel = target.Get<Channel>();
202 djm.RevealUser(user, channel);
205 void DelayJoinMode::RevealUser(User* user, Channel* chan)
207 Membership* memb = chan->GetUser(user);
208 if (!memb || !unjoined.set(memb, 0))
211 /* Display the join to everyone else (the user who joined got it earlier) */
213 except_list.insert(user);
214 ClientProtocol::Events::Join joinevent(memb);
215 chan->Write(joinevent, 0, except_list);
218 /* make the user visible if they receive any mode change */
219 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
221 if (!channel || param.empty())
222 return MOD_RES_PASSTHRU;
224 // If not a prefix mode then we got nothing to do here
225 if (!mh->IsPrefixMode())
226 return MOD_RES_PASSTHRU;
230 dest = ServerInstance->FindNickOnly(param);
232 dest = ServerInstance->FindNick(param);
235 return MOD_RES_PASSTHRU;
237 djm.RevealUser(dest, channel);
238 return MOD_RES_PASSTHRU;
241 MODULE_INIT(ModuleDelayJoin)