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