]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Convert a number of modules to using privs.
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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) : ModeHandler(Instance, 'u', 0, 0, false, MODETYPE_CHANNEL, false) { }
22
23         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
24         {
25                 if (channel->IsModeSet('u') != adding)
26                 {
27                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
28                         {
29                                 source->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only channel operators may %sset channel mode +u", source->nick.c_str(), channel->name.c_str(), adding ? "" : "un");
30                                 return MODEACTION_DENY;
31                         }
32                         else
33                         {
34                                 channel->SetMode('u', adding);
35                                 return MODEACTION_ALLOW;
36                         }
37                 }
38                 else
39                 {
40                         return MODEACTION_DENY;
41                 }
42         }
43 };
44
45 class ModuleAuditorium : public Module
46 {
47  private:
48         AuditoriumMode* aum;
49         bool ShowOps;
50         bool OperOverride;
51         CUList nl;
52         CUList except_list;
53  public:
54         ModuleAuditorium(InspIRCd* Me)
55                 : Module(Me)
56         {
57                 aum = new AuditoriumMode(ServerInstance);
58                 if (!ServerInstance->Modes->AddMode(aum))
59                 {
60                         delete aum;
61                         throw ModuleException("Could not add new modes!");
62                 }
63
64                 OnRehash(NULL, "");
65
66                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnRehash, I_OnHostCycle };
67                 Me->Modules->Attach(eventlist, this, 7);
68
69         }
70
71         virtual ~ModuleAuditorium()
72         {
73                 ServerInstance->Modes->DelMode(aum);
74                 delete aum;
75         }
76
77         virtual void OnRehash(User* user, const std::string &parameter)
78         {
79                 ConfigReader conf(ServerInstance);
80                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
81                 OperOverride = conf.ReadFlag("auditorium", "operoverride", 0);
82         }
83
84         virtual Version GetVersion()
85         {
86                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
87         }
88
89         virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
90         {
91                 if (!channel->IsModeSet('u'))
92                         return;
93
94                 /* Some module hid this from being displayed, dont bother */
95                 if (nick.empty())
96                         return;
97
98                 /* If user is oper and operoverride is on, don't touch the list */
99                 if (OperOverride && user->HasPrivPermission("channels/auspex"))
100                         return;
101
102                 if (ShowOps && (issuer != user) && (channel->GetStatus(user) < STATUS_OP))
103                 {
104                         /* Showops is set, hide all non-ops from the user, except themselves */
105                         nick.clear();
106                         return;
107                 }
108
109                 if (!ShowOps && (issuer != user))
110                 {
111                         /* ShowOps is not set, hide everyone except the user whos requesting NAMES */
112                         nick.clear();
113                         return;
114                 }
115         }
116
117         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
118         {
119                 if (channel->IsModeSet('u'))
120                 {
121                         silent = true;
122                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
123                         user->WriteFrom(user, "JOIN %s", channel->name.c_str());
124                         if (ShowOps)
125                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "JOIN %s", channel->name.c_str());
126                 }
127         }
128
129         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
130         {
131                 if (channel->IsModeSet('u'))
132                 {
133                         silent = true;
134                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
135                         user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(),
136                                         partmessage.empty() ? "" : " :",
137                                         partmessage.empty() ? "" : partmessage.c_str());
138                         if (ShowOps)
139                         {
140                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :",
141                                                 partmessage.empty() ? "" : partmessage.c_str());
142                         }
143                 }
144         }
145
146         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
147         {
148                 if (chan->IsModeSet('u'))
149                 {
150                         silent = true;
151                         /* Send silenced event only to the user being kicked and the user doing the kick */
152                         source->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
153                         if (ShowOps)
154                                 chan->WriteAllExcept(source, false, chan->GetStatus(source) >= STATUS_OP ? 0 : '@', except_list, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
155                         else
156                                 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
157                 }
158         }
159
160         bool OnHostCycle(User* user)
161         {
162                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
163                         if (f->first->IsModeSet('u'))
164                                 return true;
165
166                 return false;
167         }
168
169         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
170         {
171                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
172                 std::vector<std::string> to_leave;
173                 if (parthandler)
174                 {
175                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
176                         {
177                                 if (f->first->IsModeSet('u'))
178                                         to_leave.push_back(f->first->name);
179                         }
180                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
181                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
182                         {
183                                 std::vector<std::string> parameters;
184                                 parameters.push_back(*n);
185                                 /* This triggers our OnUserPart, above, making the PART silent */
186                                 parthandler->Handle(parameters, user);
187                         }
188                 }
189         }
190 };
191
192 MODULE_INIT(ModuleAuditorium)