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