]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Merge pull request #574 from SaberUK/master+build-comment-cleanup
[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
25 class AuditoriumMode : public ModeHandler
26 {
27  public:
28         AuditoriumMode(Module* Creator) : ModeHandler(Creator, "auditorium", 'u', PARAM_NONE, MODETYPE_CHANNEL)
29         {
30                 levelrequired = OP_VALUE;
31         }
32
33         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
34         {
35                 if (channel->IsModeSet(this) == adding)
36                         return MODEACTION_DENY;
37                 channel->SetMode(this, adding);
38                 return MODEACTION_ALLOW;
39         }
40 };
41
42 class ModuleAuditorium : public Module
43 {
44         AuditoriumMode aum;
45         bool OpsVisible;
46         bool OpsCanSee;
47         bool OperCanSee;
48
49  public:
50         ModuleAuditorium() : aum(this)
51         {
52         }
53
54         void init() CXX11_OVERRIDE
55         {
56                 ServerInstance->Modules->AddService(aum);
57
58                 OnRehash(NULL);
59
60                 Implementation eventlist[] = {
61                         I_OnUserJoin, I_OnUserPart, I_OnUserKick,
62                         I_OnBuildNeighborList, I_OnNamesListItem, I_OnSendWhoLine,
63                         I_OnRehash };
64                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
65         }
66
67         void OnRehash(User* user) CXX11_OVERRIDE
68         {
69                 ConfigTag* tag = ServerInstance->Config->ConfValue("auditorium");
70                 OpsVisible = tag->getBool("opvisible");
71                 OpsCanSee = tag->getBool("opcansee");
72                 OperCanSee = tag->getBool("opercansee", true);
73         }
74
75         Version GetVersion() CXX11_OVERRIDE
76         {
77                 return Version("Allows for auditorium channels (+u) where nobody can see others joining and parting or the nick list", VF_VENDOR);
78         }
79
80         /* Can they be seen by everyone? */
81         bool IsVisible(Membership* memb)
82         {
83                 if (!memb->chan->IsModeSet(&aum))
84                         return true;
85
86                 ModResult res = ServerInstance->OnCheckExemption(memb->user, memb->chan, "auditorium-vis");
87                 return res.check(OpsVisible && memb->getRank() >= OP_VALUE);
88         }
89
90         /* Can they see this specific membership? */
91         bool CanSee(User* issuer, Membership* memb)
92         {
93                 // If user is oper and operoverride is on, don't touch the list
94                 if (OperCanSee && issuer->HasPrivPermission("channels/auspex"))
95                         return true;
96
97                 // You can always see yourself
98                 if (issuer == memb->user)
99                         return true;
100
101                 // Can you see the list by permission?
102                 ModResult res = ServerInstance->OnCheckExemption(issuer,memb->chan,"auditorium-see");
103                 if (res.check(OpsCanSee && memb->chan->GetPrefixValue(issuer) >= OP_VALUE))
104                         return true;
105
106                 return false;
107         }
108
109         void OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick) CXX11_OVERRIDE
110         {
111                 // Some module already hid this from being displayed, don't bother
112                 if (nick.empty())
113                         return;
114
115                 if (IsVisible(memb))
116                         return;
117
118                 if (CanSee(issuer, memb))
119                         return;
120
121                 nick.clear();
122         }
123
124         /** Build CUList for showing this join/part/kick */
125         void BuildExcept(Membership* memb, CUList& excepts)
126         {
127                 if (IsVisible(memb))
128                         return;
129
130                 const UserMembList* users = memb->chan->GetUsers();
131                 for(UserMembCIter i = users->begin(); i != users->end(); i++)
132                 {
133                         if (IS_LOCAL(i->first) && !CanSee(i->first, memb))
134                                 excepts.insert(i->first);
135                 }
136         }
137
138         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
139         {
140                 BuildExcept(memb, excepts);
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, UserChanList &include, std::map<User*,bool> &exception) CXX11_OVERRIDE
154         {
155                 UCListIter i = include.begin();
156                 while (i != include.end())
157                 {
158                         Channel* c = *i++;
159                         Membership* memb = c->GetUser(source);
160                         if (!memb || IsVisible(memb))
161                                 continue;
162                         // this channel should not be considered when listing my neighbors
163                         include.erase(c);
164                         // however, that might hide me from ops that can see me...
165                         const UserMembList* users = c->GetUsers();
166                         for(UserMembCIter j = users->begin(); j != users->end(); j++)
167                         {
168                                 if (IS_LOCAL(j->first) && CanSee(j->first, memb))
169                                         exception[j->first] = true;
170                         }
171                 }
172         }
173
174         void OnSendWhoLine(User* source, const std::vector<std::string>& params, User* user, std::string& line) CXX11_OVERRIDE
175         {
176                 Channel* channel = ServerInstance->FindChan(params[0]);
177                 if (!channel)
178                         return;
179                 Membership* memb = channel->GetUser(user);
180                 if (IsVisible(memb))
181                         return;
182                 if (CanSee(source, memb))
183                         return;
184                 line.clear();
185         }
186 };
187
188 MODULE_INIT(ModuleAuditorium)