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