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