]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
180b9477503ea1c22dd501364c65d7b5a2136942
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013-2014, 2016-2018 Attila Molnar <attilamolnar@hush.com>
5  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007-2008, 2010 Craig Edwards <brain@inspircd.org>
10  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/exemption.h"
28 #include "modules/names.h"
29 #include "modules/who.h"
30
31 class AuditoriumMode : public SimpleChannelModeHandler
32 {
33  public:
34         AuditoriumMode(Module* Creator) : SimpleChannelModeHandler(Creator, "auditorium", 'u')
35         {
36                 ranktoset = ranktounset = OP_VALUE;
37         }
38 };
39
40 class ModuleAuditorium;
41
42 namespace
43 {
44
45 /** Hook handler for join client protocol events.
46  * This allows us to block join protocol events completely, including all associated messages (e.g. MODE, away-notify AWAY).
47  * This is not the same as OnUserJoin() because that runs only when a real join happens but this runs also when a module
48  * such as delayjoin or hostcycle generates a join.
49  */
50 class JoinHook : public ClientProtocol::EventHook
51 {
52         ModuleAuditorium* const parentmod;
53         bool active;
54
55  public:
56         JoinHook(ModuleAuditorium* mod);
57         void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE;
58         ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE;
59 };
60
61 }
62
63 class ModuleAuditorium
64         : public Module
65         , public Names::EventListener
66         , public Who::EventListener
67 {
68         CheckExemption::EventProvider exemptionprov;
69         AuditoriumMode aum;
70         bool OpsVisible;
71         bool OpsCanSee;
72         bool OperCanSee;
73         JoinHook joinhook;
74
75  public:
76         ModuleAuditorium()
77                 : Names::EventListener(this)
78                 , Who::EventListener(this)
79                 , exemptionprov(this)
80                 , aum(this)
81                 , joinhook(this)
82         {
83         }
84
85         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
86         {
87                 ConfigTag* tag = ServerInstance->Config->ConfValue("auditorium");
88                 OpsVisible = tag->getBool("opvisible");
89                 OpsCanSee = tag->getBool("opcansee");
90                 OperCanSee = tag->getBool("opercansee", true);
91         }
92
93         Version GetVersion() CXX11_OVERRIDE
94         {
95                 return Version("Adds channel mode u (auditorium) which hides unprivileged users in a channel from each other.", VF_VENDOR);
96         }
97
98         /* Can they be seen by everyone? */
99         bool IsVisible(Membership* memb)
100         {
101                 if (!memb->chan->IsModeSet(&aum))
102                         return true;
103
104                 ModResult res = CheckExemption::Call(exemptionprov, memb->user, memb->chan, "auditorium-vis");
105                 return res.check(OpsVisible && memb->getRank() >= OP_VALUE);
106         }
107
108         /* Can they see this specific membership? */
109         bool CanSee(User* issuer, Membership* memb)
110         {
111                 // If user is oper and operoverride is on, don't touch the list
112                 if (OperCanSee && issuer->HasPrivPermission("channels/auspex"))
113                         return true;
114
115                 // You can always see yourself
116                 if (issuer == memb->user)
117                         return true;
118
119                 // Can you see the list by permission?
120                 ModResult res = CheckExemption::Call(exemptionprov, issuer, memb->chan, "auditorium-see");
121                 if (res.check(OpsCanSee && memb->chan->GetPrefixValue(issuer) >= OP_VALUE))
122                         return true;
123
124                 return false;
125         }
126
127         ModResult OnNamesListItem(LocalUser* issuer, Membership* memb, std::string& prefixes, std::string& nick) CXX11_OVERRIDE
128         {
129                 if (IsVisible(memb))
130                         return MOD_RES_PASSTHRU;
131
132                 if (CanSee(issuer, memb))
133                         return MOD_RES_PASSTHRU;
134
135                 // Don't display this user in the NAMES list
136                 return MOD_RES_DENY;
137         }
138
139         /** Build CUList for showing this join/part/kick */
140         void BuildExcept(Membership* memb, CUList& excepts)
141         {
142                 if (IsVisible(memb))
143                         return;
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 (IS_LOCAL(i->first) && !CanSee(i->first, memb))
149                                 excepts.insert(i->first);
150                 }
151         }
152
153         void OnUserPart(Membership* memb, std::string &partmessage, CUList& excepts) CXX11_OVERRIDE
154         {
155                 BuildExcept(memb, excepts);
156         }
157
158         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
159         {
160                 BuildExcept(memb, excepts);
161         }
162
163         void OnBuildNeighborList(User* source, IncludeChanList& include, std::map<User*, bool>& exception) CXX11_OVERRIDE
164         {
165                 for (IncludeChanList::iterator i = include.begin(); i != include.end(); )
166                 {
167                         Membership* memb = *i;
168                         if (IsVisible(memb))
169                         {
170                                 ++i;
171                                 continue;
172                         }
173
174                         // this channel should not be considered when listing my neighbors
175                         i = include.erase(i);
176                         // however, that might hide me from ops that can see me...
177                         const Channel::MemberMap& users = memb->chan->GetUsers();
178                         for(Channel::MemberMap::const_iterator j = users.begin(); j != users.end(); ++j)
179                         {
180                                 if (IS_LOCAL(j->first) && CanSee(j->first, memb))
181                                         exception[j->first] = true;
182                         }
183                 }
184         }
185
186         ModResult OnWhoLine(const Who::Request& request, LocalUser* source, User* user, Membership* memb, Numeric::Numeric& numeric) CXX11_OVERRIDE
187         {
188                 if (!memb)
189                         return MOD_RES_PASSTHRU;
190                 if (IsVisible(memb))
191                         return MOD_RES_PASSTHRU;
192                 if (CanSee(source, memb))
193                         return MOD_RES_PASSTHRU;
194                 return MOD_RES_DENY;
195         }
196 };
197
198 JoinHook::JoinHook(ModuleAuditorium* mod)
199         : ClientProtocol::EventHook(mod, "JOIN", 10)
200         , parentmod(mod)
201 {
202 }
203
204 void JoinHook::OnEventInit(const ClientProtocol::Event& ev)
205 {
206         const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
207         active = !parentmod->IsVisible(join.GetMember());
208 }
209
210 ModResult JoinHook::OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist)
211 {
212         if (!active)
213                 return MOD_RES_PASSTHRU;
214
215         const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
216         return ((parentmod->CanSee(user, join.GetMember())) ? MOD_RES_PASSTHRU : MOD_RES_DENY);
217 }
218
219 MODULE_INIT(ModuleAuditorium)