]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Add Module* creator to Command and ModeHandler
[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, 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), 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         virtual ~ModuleAuditorium()
58         {
59                 ServerInstance->Modes->DelMode(&aum);
60         }
61
62         virtual 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         virtual Version GetVersion()
70         {
71                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
72         }
73
74         virtual void OnNamesListItem(User* issuer, User* user, Channel* channel, std::string &prefixes, std::string &nick)
75         {
76                 if (!channel->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 != user) && (channel->GetStatus(user) < STATUS_OP))
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 != 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 WriteOverride(User* source, Channel* channel, const std::string &text)
103         {
104                 if (!OperOverride)
105                         return;
106
107                 CUList *ulist = channel->GetUsers();
108                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
109                 {
110                         if (i->first->HasPrivPermission("channels/auspex") && source != i->first)
111                                 if (!ShowOps || (ShowOps && channel->GetStatus(i->first) < STATUS_OP))
112                                         i->first->WriteFrom(source, "%s",text.c_str());
113                 }
114         }
115
116         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
117         {
118                 if (channel->IsModeSet('u'))
119                 {
120                         silent = true;
121                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
122                         user->WriteFrom(user, "JOIN %s", channel->name.c_str());
123                         if (ShowOps)
124                                 channel->WriteAllExceptSender(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', "JOIN %s", channel->name.c_str());
125                         WriteOverride(user, channel, "JOIN "+channel->name);
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->WriteAllExceptSender(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', "PART %s%s%s", channel->name.c_str(), partmessage.empty() ? "" : " :",
141                                                 partmessage.empty() ? "" : partmessage.c_str());
142                         }
143                         WriteOverride(user, channel, "PART " + channel->name + (partmessage.empty() ? "" : (" :" + partmessage)));
144                 }
145         }
146
147         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
148         {
149                 if (chan->IsModeSet('u'))
150                 {
151                         silent = true;
152                         /* Send silenced event only to the user being kicked and the user doing the kick */
153                         source->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
154                         if (ShowOps)
155                                 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());
156                         if ((!ShowOps) || (chan->GetStatus(user) < STATUS_OP)) /* make sure the target gets the event */
157                                 user->WriteFrom(source, "KICK %s %s %s", chan->name.c_str(), user->nick.c_str(), reason.c_str());
158                         WriteOverride(source, chan, "KICK " + chan->name + " " + user->nick + " " + reason);
159                 }
160         }
161
162         bool OnHostCycle(User* user)
163         {
164                 for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
165                         if (f->first->IsModeSet('u'))
166                                 return true;
167
168                 return false;
169         }
170
171         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
172         {
173                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
174                 std::vector<std::string> to_leave;
175                 if (parthandler)
176                 {
177                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
178                         {
179                                 if (f->first->IsModeSet('u'))
180                                         to_leave.push_back(f->first->name);
181                         }
182                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
183                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
184                         {
185                                 std::vector<std::string> parameters;
186                                 parameters.push_back(*n);
187                                 /* This triggers our OnUserPart, above, making the PART silent */
188                                 parthandler->Handle(parameters, user);
189                         }
190                 }
191         }
192 };
193
194 MODULE_INIT(ModuleAuditorium)