]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
9d8ff4b9a01128149f52ac81edb6fc303ef42327
[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)
24         {
25                 if (channel->IsModeSet('u') != adding)
26                 {
27                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
28                         {
29                                 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +u", source->nick, channel->name, 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_OnUserList, I_OnRehash };
67                 Me->Modules->Attach(eventlist, this, 6);
68
69         }
70         
71         virtual ~ModuleAuditorium()
72         {
73                 ServerInstance->Modes->DelMode(aum);
74                 delete aum;
75         }
76
77         void Prioritize()
78         {
79                 Module* namesx = ServerInstance->Modules->Find("m_namesx.so");
80                 ServerInstance->Modules->SetPriority(this, I_OnUserList, PRIO_BEFORE, &namesx);
81         }
82
83         virtual void OnRehash(User* user, const std::string &parameter)
84         {
85                 ConfigReader conf(ServerInstance);
86                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
87                 OperOverride = conf.ReadFlag("auditorium", "operoverride", 0);
88         }
89
90         virtual Version GetVersion()
91         {
92                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
93         }
94
95         virtual int OnUserList(User* user, Channel* Ptr, CUList* &nameslist)
96         {
97                 if (Ptr->IsModeSet('u'))
98                 {
99                         if (OperOverride)
100                         {
101                                 if (IS_OPER(user))
102                                 {
103                                         /*
104                                          * If user is oper and operoverride is on, don't touch the list
105                                          */
106                                         return 0;
107                                 }
108                         }
109                         
110                         if (ShowOps)
111                         {
112                                 /* Leave the names list alone, theyre an op
113                                  * doing /names on the channel after joining it
114                                  */
115                                 if (Ptr->GetStatus(user) >= STATUS_OP)
116                                 {
117                                         nameslist = Ptr->GetUsers();
118                                         return 0;
119                                 }
120
121                                 /* Show all the opped users */
122                                 nl = *(Ptr->GetOppedUsers());
123                                 nl[user] = user->nick;
124                                 nameslist = &nl;
125                                 return 0;
126                         }
127                         else
128                         {
129                                 /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
130                                 user->WriteServ("353 %s %c %s :%s", user->nick, Ptr->IsModeSet('s') ? '@' : Ptr->IsModeSet('p') ? '*' : '=', Ptr->name, user->nick);
131                                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
132                                 return 1;
133                         }
134                 }
135                 return 0;
136         }
137         
138         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent)
139         {
140                 if (channel->IsModeSet('u'))
141                 {
142                         silent = true;
143                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
144                         user->WriteFrom(user, "JOIN %s", channel->name);
145                         if (ShowOps)
146                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "JOIN %s", channel->name);
147                 }
148         }
149
150         void OnUserPart(User* user, Channel* channel, const std::string &partmessage, bool &silent)
151         {
152                 if (channel->IsModeSet('u'))
153                 {
154                         silent = true;
155                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
156                         user->WriteFrom(user, "PART %s%s%s", channel->name,
157                                         partmessage.empty() ? "" : " :",
158                                         partmessage.empty() ? "" : partmessage.c_str());
159                         if (ShowOps)
160                         {
161                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :",
162                                                 partmessage.empty() ? "" : partmessage.c_str());
163                         }
164                 }
165         }
166
167         void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
168         {
169                 if (chan->IsModeSet('u'))
170                 {
171                         silent = true;
172                         /* Send silenced event only to the user being kicked and the user doing the kick */
173                         source->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
174                         if (ShowOps)
175                                 chan->WriteAllExcept(source, false, chan->GetStatus(source) >= STATUS_OP ? 0 : '@', except_list, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
176                         else
177                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
178                 }
179         }
180
181         void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
182         {
183                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
184                 std::vector<std::string> to_leave;
185                 const char* parameters[2];
186                 if (parthandler)
187                 {
188                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
189                         {
190                                 if (f->first->IsModeSet('u'))
191                                         to_leave.push_back(f->first->name);
192                         }
193                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
194                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
195                         {
196                                 parameters[0] = n->c_str();
197                                 /* This triggers our OnUserPart, above, making the PART silent */
198                                 parthandler->Handle(parameters, 1, user);
199                         }
200                 }
201         }
202 };
203
204 MODULE_INIT(ModuleAuditorium)