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 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
97 if (channel->IsModeSet(this) == adding)
98 return MODEACTION_DENY;
103 * Make all users visible, as +D is being removed. If we don't do this,
104 * they remain permanently invisible on this channel!
106 MessageTarget msgtarget(channel, 0);
107 MessageDetails msgdetails(MSG_PRIVMSG, "", ClientProtocol::TagMap());
108 const Channel::MemberMap& users = channel->GetUsers();
109 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
111 creator->OnUserMessage(n->first, msgtarget, msgdetails);
114 channel->SetMode(this, adding);
115 return MODEACTION_ALLOW;
118 Version ModuleDelayJoin::GetVersion()
120 return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
123 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
125 /* don't prevent the user from seeing themself */
126 if (issuer == memb->user)
127 return MOD_RES_PASSTHRU;
129 /* If the user is hidden by delayed join, hide them from the NAMES list */
130 if (unjoined.get(memb))
133 return MOD_RES_PASSTHRU;
136 static void populate(CUList& except, Membership* memb)
138 const Channel::MemberMap& users = memb->chan->GetUsers();
139 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
141 if (i->first == memb->user || !IS_LOCAL(i->first))
143 except.insert(i->first);
147 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
149 if (memb->chan->IsModeSet(djm))
150 unjoined.set(memb, 1);
153 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
155 if (unjoined.set(memb, 0))
156 populate(except, memb);
159 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
161 if (unjoined.set(memb, 0))
162 populate(except, memb);
165 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
167 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
169 Membership* memb = *i;
170 if (unjoined.get(memb))
171 i = include.erase(i);
177 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
179 if (target.type != MessageTarget::TYPE_CHANNEL)
182 Channel* channel = target.Get<Channel>();
183 RevealUser(user, channel);
186 void ModuleDelayJoin::RevealUser(User* user, Channel* chan)
188 Membership* memb = chan->GetUser(user);
189 if (!memb || !unjoined.set(memb, 0))
192 /* Display the join to everyone else (the user who joined got it earlier) */
194 except_list.insert(user);
195 ClientProtocol::Events::Join joinevent(memb);
196 chan->Write(joinevent, 0, except_list);
199 /* make the user visible if he receives any mode change */
200 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
202 if (!channel || param.empty())
203 return MOD_RES_PASSTHRU;
205 // If not a prefix mode then we got nothing to do here
206 if (!mh->IsPrefixMode())
207 return MOD_RES_PASSTHRU;
211 dest = ServerInstance->FindNickOnly(param);
213 dest = ServerInstance->FindNick(param);
216 return MOD_RES_PASSTHRU;
218 RevealUser(dest, channel);
219 return MOD_RES_PASSTHRU;
222 MODULE_INIT(ModuleDelayJoin)