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