]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_delayjoin.cpp
Implement support for IRCv3 client-to-client tags.
[user/henk/code/inspircd.git] / src / modules / m_delayjoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "modules/ctctags.h"
25
26 class DelayJoinMode : public ModeHandler
27 {
28  private:
29         LocalIntExt& unjoined;
30
31  public:
32         DelayJoinMode(Module* Parent, LocalIntExt& ext)
33                 : ModeHandler(Parent, "delayjoin", 'D', PARAM_NONE, MODETYPE_CHANNEL)
34                 , unjoined(ext)
35         {
36                 ranktoset = ranktounset = OP_VALUE;
37         }
38
39         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) CXX11_OVERRIDE;
40         void RevealUser(User* user, Channel* chan);
41 };
42
43
44 namespace
45 {
46
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.
51  */
52 class JoinHook : public ClientProtocol::EventHook
53 {
54         const LocalIntExt& unjoined;
55
56  public:
57         JoinHook(Module* mod, const LocalIntExt& unjoinedref)
58                 : ClientProtocol::EventHook(mod, "JOIN", 10)
59                 , unjoined(unjoinedref)
60         {
61         }
62
63         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
64         {
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))
69                         return MOD_RES_DENY;
70                 return MOD_RES_PASSTHRU;
71         }
72 };
73
74 }
75
76 class ModuleDelayJoin 
77         : public Module
78         , public CTCTags::EventListener
79 {
80  public:
81         LocalIntExt unjoined;
82         JoinHook joinhook;
83         DelayJoinMode djm;
84
85         ModuleDelayJoin()
86                 : CTCTags::EventListener(this)
87                 , unjoined("delayjoin", ExtensionItem::EXT_MEMBERSHIP, this)
88                 , joinhook(this, unjoined)
89                 , djm(this, unjoined)
90         {
91         }
92
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;
103 };
104
105 ModeAction DelayJoinMode::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
106 {
107         /* no change */
108         if (channel->IsModeSet(this) == adding)
109                 return MODEACTION_DENY;
110
111         if (!adding)
112         {
113                 /*
114                  * Make all users visible, as +D is being removed. If we don't do this,
115                  * they remain permanently invisible on this channel!
116                  */
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);
120         }
121         channel->SetMode(this, adding);
122         return MODEACTION_ALLOW;
123 }
124
125 Version ModuleDelayJoin::GetVersion()
126 {
127         return Version("Allows for delay-join channels (+D) where users don't appear to join until they speak", VF_VENDOR);
128 }
129
130 ModResult ModuleDelayJoin::OnNamesListItem(User* issuer, Membership* memb, std::string& prefixes, std::string& nick)
131 {
132         /* don't prevent the user from seeing themself */
133         if (issuer == memb->user)
134                 return MOD_RES_PASSTHRU;
135
136         /* If the user is hidden by delayed join, hide them from the NAMES list */
137         if (unjoined.get(memb))
138                 return MOD_RES_DENY;
139
140         return MOD_RES_PASSTHRU;
141 }
142
143 static void populate(CUList& except, Membership* memb)
144 {
145         const Channel::MemberMap& users = memb->chan->GetUsers();
146         for (Channel::MemberMap::const_iterator i = users.begin(); i != users.end(); ++i)
147         {
148                 if (i->first == memb->user || !IS_LOCAL(i->first))
149                         continue;
150                 except.insert(i->first);
151         }
152 }
153
154 void ModuleDelayJoin::OnUserJoin(Membership* memb, bool sync, bool created, CUList& except)
155 {
156         if (memb->chan->IsModeSet(djm))
157                 unjoined.set(memb, 1);
158 }
159
160 void ModuleDelayJoin::OnUserPart(Membership* memb, std::string &partmessage, CUList& except)
161 {
162         if (unjoined.set(memb, 0))
163                 populate(except, memb);
164 }
165
166 void ModuleDelayJoin::OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& except)
167 {
168         if (unjoined.set(memb, 0))
169                 populate(except, memb);
170 }
171
172 void ModuleDelayJoin::OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception)
173 {
174         for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
175         {
176                 Membership* memb = *i;
177                 if (unjoined.get(memb))
178                         i = include.erase(i);
179                 else
180                         ++i;
181         }
182 }
183
184 void ModuleDelayJoin::OnUserTagMessage(User* user, const MessageTarget& target, const CTCTags::TagMessageDetails& details)
185 {
186         if (target.type != MessageTarget::TYPE_CHANNEL)
187                 return;
188
189         Channel* channel = target.Get<Channel>();
190         djm.RevealUser(user, channel);
191 }
192
193 void ModuleDelayJoin::OnUserMessage(User* user, const MessageTarget& target, const MessageDetails& details)
194 {
195         if (target.type != MessageTarget::TYPE_CHANNEL)
196                 return;
197
198         Channel* channel = target.Get<Channel>();
199         djm.RevealUser(user, channel);
200 }
201
202 void DelayJoinMode::RevealUser(User* user, Channel* chan)
203 {
204         Membership* memb = chan->GetUser(user);
205         if (!memb || !unjoined.set(memb, 0))
206                 return;
207
208         /* Display the join to everyone else (the user who joined got it earlier) */
209         CUList except_list;
210         except_list.insert(user);
211         ClientProtocol::Events::Join joinevent(memb);
212         chan->Write(joinevent, 0, except_list);
213 }
214
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)
217 {
218         if (!channel || param.empty())
219                 return MOD_RES_PASSTHRU;
220
221         // If not a prefix mode then we got nothing to do here
222         if (!mh->IsPrefixMode())
223                 return MOD_RES_PASSTHRU;
224
225         User* dest;
226         if (IS_LOCAL(user))
227                 dest = ServerInstance->FindNickOnly(param);
228         else
229                 dest = ServerInstance->FindNick(param);
230
231         if (!dest)
232                 return MOD_RES_PASSTHRU;
233
234         djm.RevealUser(dest, channel);
235         return MOD_RES_PASSTHRU;
236 }
237
238 MODULE_INIT(ModuleDelayJoin)