]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
aa1aa633abc166fbe27841a551ac9debd1fcd715
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Allows for auditorium channels (+u) where nobody can see others joining and parting or the nick list */
17
18 class AuditoriumMode : public ModeHandler
19 {
20  public:
21         AuditoriumMode(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'u', PARAM_NONE, MODETYPE_CHANNEL)
22         {
23                 levelrequired = OP_VALUE;
24         }
25
26         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
27         {
28                 if (channel->IsModeSet('u') != adding)
29                 {
30                         channel->SetMode('u', adding);
31                         return MODEACTION_ALLOW;
32                 }
33                 else
34                 {
35                         return MODEACTION_DENY;
36                 }
37         }
38 };
39
40 class ModuleAuditorium : public Module
41 {
42  private:
43         AuditoriumMode aum;
44         bool ShowOps;
45         bool OperOverride;
46  public:
47         ModuleAuditorium(InspIRCd* Me)
48                 : Module(Me), aum(Me, this)
49         {
50                 if (!ServerInstance->Modes->AddMode(&aum))
51                         throw ModuleException("Could not add new modes!");
52
53                 OnRehash(NULL);
54
55                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnRehash, I_OnHostCycle };
56                 Me->Modules->Attach(eventlist, this, 7);
57
58         }
59
60         ~ModuleAuditorium()
61         {
62                 ServerInstance->Modes->DelMode(&aum);
63         }
64
65         void OnRehash(User* user)
66         {
67                 ConfigReader conf(ServerInstance);
68                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
69                 OperOverride = conf.ReadFlag("auditorium", "operoverride", 0);
70         }
71
72         Version GetVersion()
73         {
74                 return Version("Allows for auditorium channels (+u) where nobody can see others joining and parting or the nick list", VF_COMMON | VF_VENDOR, API_VERSION);
75         }
76
77         void OnNamesListItem(User* issuer, Membership* memb, std::string &prefixes, std::string &nick)
78         {
79                 if (!memb->chan->IsModeSet('u'))
80                         return;
81
82                 /* Some module hid this from being displayed, dont bother */
83                 if (nick.empty())
84                         return;
85
86                 /* If user is oper and operoverride is on, don't touch the list */
87                 if (OperOverride && issuer->HasPrivPermission("channels/auspex"))
88                         return;
89
90                 if (ShowOps && (issuer != memb->user) && (memb->getRank() < OP_VALUE))
91                 {
92                         /* Showops is set, hide all non-ops from the user, except themselves */
93                         nick.clear();
94                         return;
95                 }
96
97                 if (!ShowOps && (issuer != memb->user))
98                 {
99                         /* ShowOps is not set, hide everyone except the user whos requesting NAMES */
100                         nick.clear();
101                         return;
102                 }
103         }
104
105         void BuildExcept(Membership* memb, CUList& excepts)
106         {
107                 if (!memb->chan->IsModeSet('u'))
108                         return;
109                 if (ShowOps && memb->getRank() >= OP_VALUE)
110                         return;
111
112                 const UserMembList* users = memb->chan->GetUsers();
113                 for(UserMembCIter i = users->begin(); i != users->end(); i++)
114                 {
115                         if (i->first == memb->user || !IS_LOCAL(i->first))
116                                 continue;
117                         if (ShowOps && i->second->getRank() >= OP_VALUE)
118                                 continue;
119                         if (OperOverride && i->first->HasPrivPermission("channels/auspex"))
120                                 continue;
121                         // This is a different user in the channel, local, and not op/oper
122                         // so, hide the join from them
123                         excepts.insert(i->first);
124                 }
125         }
126         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts)
127         {
128                 BuildExcept(memb, excepts);
129         }
130
131         void OnUserPart(Membership* memb, std::string &partmessage, CUList& excepts)
132         {
133                 BuildExcept(memb, excepts);
134         }
135
136         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts)
137         {
138                 BuildExcept(memb, excepts);
139         }
140
141         ModResult OnHostCycle(User* user)
142         {
143                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
144                         if ((*f)->IsModeSet('u'))
145                                 return MOD_RES_DENY;
146
147                 return MOD_RES_PASSTHRU;
148         }
149
150         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
151         {
152                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
153                 std::vector<std::string> to_leave;
154                 if (parthandler)
155                 {
156                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
157                         {
158                                 if ((*f)->IsModeSet('u'))
159                                         to_leave.push_back((*f)->name);
160                         }
161                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
162                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
163                         {
164                                 std::vector<std::string> parameters;
165                                 parameters.push_back(*n);
166                                 /* This triggers our OnUserPart, above, making the PART silent */
167                                 parthandler->Handle(parameters, user);
168                         }
169                 }
170         }
171 };
172
173 MODULE_INIT(ModuleAuditorium)