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"
26 class DelayJoinMode : public ModeHandler
29 LocalIntExt& unjoined;
32 DelayJoinMode(Module* Parent, LocalIntExt& ext)
33 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
36 ranktoset = ranktounset = OP_VALUE;
39 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
40 void RevealUser(User* user, Channel* chan);
47 /** Hook handler for join client protocol events.
48 * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
49 * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
50 * such as hostcycle generates a join.
52 class JoinHook : public ClientProtocol::EventHook
54 const LocalIntExt& unjoined;
57 JoinHook(Module* mod, const LocalIntExt& unjoinedref)
58 : ClientProtocol::EventHook(mod, "JOIN", 10)
59 , unjoined(unjoinedref)
63 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
65 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
66 const Membership* const memb = join.GetMember();
67 const User* const u = memb->user;
68 if ((unjoined.get(memb)) && (u != user))
70 return MOD_RES_PASSTHRU;
78 , public CTCTags::EventListener
86 : CTCTags::EventListener(this)
87 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
88 , joinhook(this, unjoined)
93 Version GetVersion() CXX11_OVERRIDE;
94 ModResult OnNamesListItem(User* issuer, Membership*, std::string& prefixes, std::string& nick) CXX11_OVERRIDE;
95 void OnUserJoin(Membership*, bool, bool, CUList&) CXX11_OVERRIDE;
96 void CleanUser(User* user);
97 void OnUserPart(Membership*, std::string &partmessage, CUList&) CXX11_OVERRIDE;
98 void OnUserKick(User* source, Membership*, const std::string &reason, CUList&) CXX11_OVERRIDE;
99 void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE;
100 void OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE;
101 void OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details) CXX11_OVERRIDE;
102 ModResult OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding) CXX11_OVERRIDE;
105 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
108 if (channel->IsModeSet(this) == adding)
109 return MODEACTION_DENY;
114 * Make all users visible, as +D is being removed. If we don't do this,
115 * they remain permanently invisible on this channel!
117 const Channel::MemberMap& users = channel->GetUsers();
118 for (Channel::MemberMap::const_iterator n = users.begin(); n != users.end(); ++n)
119 RevealUser(n->first, channel);
121 channel->SetMode(this, adding);
122 return MODEACTION_ALLOW;
125 Version ModuleDelayJoin::GetVersion()
127 return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
130 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
132 /* don't prevent the user from seeing themself */
133 if (issuer == memb->user)
134 return MOD_RES_PASSTHRU;
136 /* If the user is hidden by delayed join, hide them from the NAMES list */
137 if (unjoined.get(memb))
140 return MOD_RES_PASSTHRU;
143 static void populate(CUList& except, Membership* memb)
145 const Channel::MemberMap& users = memb->chan->GetUsers();
146 for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
148 if (i->first == memb->user || !IS_LOCAL(i->first))
150 except.insert(i->first);
154 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
156 if (memb->chan->IsModeSet(djm))
157 unjoined.set(memb, 1);
160 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
162 if (unjoined.set(memb, 0))
163 populate(except, memb);
166 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
168 if (unjoined.set(memb, 0))
169 populate(except, memb);
172 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
174 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
176 Membership* memb = *i;
177 if (unjoined.get(memb))
178 i = include.erase(i);
184 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
186 if (target.type != MessageTarget::TYPE_CHANNEL)
189 Channel* channel = target.Get<Channel>();
190 djm.RevealUser(user, channel);
193 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
195 if (target.type != MessageTarget::TYPE_CHANNEL)
198 Channel* channel = target.Get<Channel>();
199 djm.RevealUser(user, channel);
202 void DelayJoinMode::RevealUser(User* user, Channel* chan)
204 Membership* memb = chan->GetUser(user);
205 if (!memb || !unjoined.set(memb, 0))
208 /* Display the join to everyone else (the user who joined got it earlier) */
210 except_list.insert(user);
211 ClientProtocol::Events::Join joinevent(memb);
212 chan->Write(joinevent, 0, except_list);
215 /* make the user visible if he receives any mode change */
216 ModResult ModuleDelayJoin::OnRawMode(User* user, Channel* channel, ModeHandler* mh, const std::string& param, bool adding)
218 if (!channel || param.empty())
219 return MOD_RES_PASSTHRU;
221 // If not a prefix mode then we got nothing to do here
222 if (!mh->IsPrefixMode())
223 return MOD_RES_PASSTHRU;
227 dest = ServerInstance->FindNickOnly(param);
229 dest = ServerInstance->FindNick(param);
232 return MOD_RES_PASSTHRU;
234 djm.RevealUser(dest, channel);
235 return MOD_RES_PASSTHRU;
238 MODULE_INIT(ModuleDelayJoin)