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