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/>.
25 class DelayJoinMode : public ModeHandler
28 DelayJoinMode(Module* Parent) : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
30 ranktoset = ranktounset = OP_VALUE;
33 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
40 /** Hook handler for join client protocol events.
41 * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
42 * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
43 * such as hostcycle generates a join.
45 class JoinHook : public ClientProtocol::EventHook
47 const LocalIntExt& unjoined;
50 JoinHook(Module* mod, const LocalIntExt& unjoinedref)
51 : ClientProtocol::EventHook(mod, "JOIN", 10)
52 , unjoined(unjoinedref)
56 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
58 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
59 const User* const u = join.GetMember()->user;
60 if ((unjoined.get(u)) && (u != user))
62 return MOD_RES_PASSTHRU;
68 class ModuleDelayJoin : public Module
71 void RevealUser(User* user, Channel* chan);
78 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
79 , joinhook(this, unjoined)
83 Version GetVersion() CXX11_OVERRIDE;
84 ModResult OnNamesListItem(User* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
85 void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
86 void CleanUser(User* user);
87 void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
88 void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
89 void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
90 void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
91 ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
94 // TODO: make RevealUser accessible from DelayJoinMode and get rid of this.
95 class DummyMessageDetails : public MessageDetails
98 DummyMessageDetails() : MessageDetails(MSG_PRIVMSG, "", ClientProtocol::TagMap()) { }
99 bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE { return false; }
100 bool IsCTCP(std::string&) const CXX11_OVERRIDE { return false; }
101 bool IsCTCP() const CXX11_OVERRIDE { return false; }
104 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
107 if (channel->IsModeSet(this) == adding)
108 return MODEACTION_DENY;
113 * Make all users visible, as +D is being removed. If we don't do this,
114 * they remain permanently invisible on this channel!
116 MessageTarget msgtarget(channel, 0);
117 DummyMessageDetails msgdetails;
118 const Channel::MemberMap& users = channel->GetUsers();
119 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
121 creator->OnUserMessage(n->first, msgtarget, msgdetails);
124 channel->SetMode(this, adding);
125 return MODEACTION_ALLOW;
128 Version ModuleDelayJoin::GetVersion()
130 return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
133 ModResult ModuleDelayJoin::OnNamesListItem(User* 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::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
189 if (target.type != MessageTarget::TYPE_CHANNEL)
192 Channel* channel = target.Get<Channel>();
193 RevealUser(user, channel);
196 void ModuleDelayJoin::RevealUser(User* user, Channel* chan)
198 Membership* memb = chan->GetUser(user);
199 if (!memb || !unjoined.set(memb, 0))
202 /* Display the join to everyone else (the user who joined got it earlier) */
204 except_list.insert(user);
205 ClientProtocol::Events::Join joinevent(memb);
206 chan->Write(joinevent, 0, except_list);
209 /* make the user visible if he receives any mode change */
210 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
212 if (!channel || param.empty())
213 return MOD_RES_PASSTHRU;
215 // If not a prefix mode then we got nothing to do here
216 if (!mh->IsPrefixMode())
217 return MOD_RES_PASSTHRU;
221 dest = ServerInstance->FindNickOnly(param);
223 dest = ServerInstance->FindNick(param);
226 return MOD_RES_PASSTHRU;
228 RevealUser(dest, channel);
229 return MOD_RES_PASSTHRU;
232 MODULE_INIT(ModuleDelayJoin)