]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
class command_t -> class Command. Whey :D
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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(userrec* source, userrec* dest, chanrec* 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         CUList nl;
51         CUList except_list;
52  public:
53         ModuleAuditorium(InspIRCd* Me)
54                 : Module(Me)
55         {
56                 aum = new AuditoriumMode(ServerInstance);
57                 if (!ServerInstance->AddMode(aum, 'u'))
58                         throw ModuleException("Could not add new modes!");
59                 OnRehash(NULL, "");
60         }
61         
62         virtual ~ModuleAuditorium()
63         {
64                 ServerInstance->Modes->DelMode(aum);
65                 DELETE(aum);
66         }
67
68         virtual void OnRehash(userrec* user, const std::string &parameter)
69         {
70                 ConfigReader conf(ServerInstance);
71                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
72         }
73
74         Priority Prioritize()
75         {
76                 /* To ensure that we get priority over namesx for names list generation on +u channels */
77                 return (Priority)ServerInstance->Modules->PriorityBefore("m_namesx.so");
78         }
79
80         virtual Version GetVersion()
81         {
82                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
83         }
84
85         void Implements(char* List)
86         {
87                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = List[I_OnRehash] = 1;
88         }
89
90         virtual int OnUserList(userrec* user, chanrec* Ptr, CUList* &nameslist)
91         {
92                 if (Ptr->IsModeSet('u'))
93                 {
94                         if (ShowOps)
95                         {
96                                 /* Leave the names list alone, theyre an op
97                                  * doing /names on the channel after joining it
98                                  */
99                                 if (Ptr->GetStatus(user) >= STATUS_OP)
100                                 {
101                                         nameslist = Ptr->GetUsers();
102                                         return 0;
103                                 }
104
105                                 /* Show all the opped users */
106                                 nl = *(Ptr->GetOppedUsers());
107                                 nl[user] = user->nick;
108                                 nameslist = &nl;
109                                 return 0;
110                         }
111                         else
112                         {
113                                 /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
114                                 user->WriteServ("353 %s %c %s :%s", user->nick, Ptr->IsModeSet('s') ? '@' : Ptr->IsModeSet('p') ? '*' : '=', Ptr->name, user->nick);
115                                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
116                                 return 1;
117                         }
118                 }
119                 return 0;
120         }
121         
122         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
123         {
124                 if (channel->IsModeSet('u'))
125                 {
126                         silent = true;
127                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
128                         user->WriteFrom(user, "JOIN %s", channel->name);
129                         if (ShowOps)
130                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "JOIN %s", channel->name);
131                 }
132         }
133
134         void OnUserPart(userrec* user, chanrec* channel, const 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,
141                                         partmessage.empty() ? "" : " :",
142                                         partmessage.empty() ? "" : partmessage.c_str());
143                         if (ShowOps)
144                         {
145                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :",
146                                                 partmessage.empty() ? "" : partmessage.c_str());
147                         }
148                 }
149         }
150
151         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
152         {
153                 if (chan->IsModeSet('u'))
154                 {
155                         silent = true;
156                         /* Send silenced event only to the user being kicked and the user doing the kick */
157                         source->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
158                         if (ShowOps)
159                                 chan->WriteAllExcept(source, false, chan->GetStatus(source) >= STATUS_OP ? 0 : '@', except_list, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
160                         else
161                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
162                 }
163         }
164
165         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
166         {
167                 Command* parthandler = ServerInstance->Parser->GetHandler("PART");
168                 std::vector<std::string> to_leave;
169                 const char* parameters[2];
170                 if (parthandler)
171                 {
172                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
173                         {
174                                 if (f->first->IsModeSet('u'))
175                                         to_leave.push_back(f->first->name);
176                         }
177                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
178                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
179                         {
180                                 parameters[0] = n->c_str();
181                                 /* This triggers our OnUserPart, above, making the PART silent */
182                                 parthandler->Handle(parameters, 1, user);
183                         }
184                 }
185         }
186 };
187
188 MODULE_INIT(ModuleAuditorium)