]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Fix a regression from fcb51634669e6ed588e42f2072c6f910e267126f ("fix uid collision...
[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) : ModeHandler(Instance, 'u', 0, 0, false, MODETYPE_CHANNEL, false, 0, '@') { }
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                         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)
46         {
47                 aum = new AuditoriumMode(ServerInstance);
48                 if (!ServerInstance->Modes->AddMode(aum))
49                 {
50                         delete aum;
51                         throw ModuleException("Could not add new modes!");
52                 }
53
54                 OnRehash(NULL);
55
56                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnNamesListItem, I_OnRehash, I_OnHostCycle };
57                 Me->Modules->Attach(eventlist, this, 7);
58
59         }
60
61         virtual ~ModuleAuditorium()
62         {
63                 ServerInstance->Modes->DelMode(aum);
64                 delete aum;
65         }
66
67         virtual void OnRehash(User* user)
68         {
69                 ConfigReader conf(ServerInstance);
70                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
71                 OperOverride = conf.ReadFlag("auditorium", "operoverride", 0);
72         }
73
74         virtual Version GetVersion()
75         {
76                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
77         }
78
79         virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
80         {
81                 if (!channel->IsModeSet('u'))
82                         return;
83
84                 /* Some module hid this from being displayed, dont bother */
85                 if (nick.empty())
86                         return;
87
88                 /* If user is oper and operoverride is on, don't touch the list */
89                 if (OperOverride && issuer->HasPrivPermission("channels/auspex"))
90                         return;
91
92                 if (ShowOps && (issuer != user) && (channel->GetStatus(user) < STATUS_OP))
93                 {
94                         /* Showops is set, hide all non-ops from the user, except themselves */
95                         nick.clear();
96                         return;
97                 }
98
99                 if (!ShowOps && (issuer != user))
100                 {
101                         /* ShowOps is not set, hide everyone except the user whos requesting NAMES */
102                         nick.clear();
103                         return;
104                 }
105         }
106
107         void WriteOverride(User* source, Channel* channel, const std::string &text)
108         {
109                 if (!OperOverride)
110                         return;
111
112                 CUList *ulist = channel->GetUsers();
113                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
114                 {
115                         if (i->first->HasPrivPermission("channels/auspex") && source != i->first)
116                                 if (!ShowOps || (ShowOps && channel->GetStatus(i->first) < STATUS_OP))
117                                         i->first->WriteFrom(source, "%s",text.c_str());
118                 }
119         }
120
121         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
122         {
123                 if (channel->IsModeSet('u'))
124                 {
125                         silent = true;
126                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
127                         user->WriteFrom(user, "JOIN %s", channel->name.c_str());
128                         if (ShowOps)
129                                 channel->WriteAllExceptSender(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', "JOIN %s", channel->name.c_str());
130                         WriteOverride(user, channel, "JOIN "+channel->name);
131                 }
132         }
133
134         void OnUserPart(User* user, Channel* channel, std::string &partmessage, bool &silent)
135         {
136                 if (channel->IsModeSet('u'))
137                 {
138                         silent = true;
139                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
140                         user->WriteFrom(user, "PART %s%s%s", channel->name.c_str(),
141                                         partmessage.empty() ? "" : " :",
142                                         partmessage.empty() ? "" : partmessage.c_str());
143                         if (ShowOps)
144                         {
145                                 channel->WriteAllExceptSender(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :",
146                                                 partmessage.empty() ? "" : partmessage.c_str());
147                         }
148                         WriteOverride(user, channel, "PART " + channel->name + (partmessage.empty() ? "" : (" :" + partmessage)));
149                 }
150         }
151
152         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
153         {
154                 if (chan->IsModeSet('u'))
155                 {
156                         silent = true;
157                         /* Send silenced event only to the user being kicked and the user doing the kick */
158                         source->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
159                         if (ShowOps)
160                                 chan->WriteAllExceptSender(source, false, chan->GetStatus(user) >= STATUS_OP ? 0 : '@', "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
161                         if ((!ShowOps) || (chan->GetStatus(user) < STATUS_OP)) /* make sure the target gets the event */
162                                 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
163                         WriteOverride(source, chan, "KICK " + chan->name + " " + user->nick + " " + reason);
164                 }
165         }
166
167         bool OnHostCycle(User* user)
168         {
169                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
170                         if (f->first->IsModeSet('u'))
171                                 return true;
172
173                 return false;
174         }
175
176         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
177         {
178                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
179                 std::vector<std::string> to_leave;
180                 if (parthandler)
181                 {
182                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
183                         {
184                                 if (f->first->IsModeSet('u'))
185                                         to_leave.push_back(f->first->name);
186                         }
187                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
188                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
189                         {
190                                 std::vector<std::string> parameters;
191                                 parameters.push_back(*n);
192                                 /* This triggers our OnUserPart, above, making the PART silent */
193                                 parthandler->Handle(parameters, user);
194                         }
195                 }
196         }
197 };
198
199 MODULE_INIT(ModuleAuditorium)