]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Convert more modules
[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 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Allows for auditorium channels (+u) where nobody can see others joining and parting or the nick list */
20
21 class AuditoriumMode : public ModeHandler
22 {
23  public:
24         AuditoriumMode(InspIRCd* Instance) : ModeHandler(Instance, 'u', 0, 0, false, MODETYPE_CHANNEL, false) { }
25
26         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
27         {
28                 if (channel->IsModeSet('u') != adding)
29                 {
30                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
31                         {
32                                 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +u", source->nick, channel->name, adding ? "" : "un");
33                                 return MODEACTION_DENY;
34                         }
35                         else
36                         {
37                                 channel->SetMode('u', adding);
38                                 return MODEACTION_ALLOW;
39                         }
40                 }
41                 else
42                 {
43                         return MODEACTION_DENY;
44                 }
45         }
46 };
47
48 class ModuleAuditorium : public Module
49 {
50  private:
51         AuditoriumMode* aum;
52         bool ShowOps;
53         CUList nl;
54         CUList except_list;
55  public:
56         ModuleAuditorium(InspIRCd* Me)
57                 : Module(Me)
58         {
59                 aum = new AuditoriumMode(ServerInstance);
60                 if (!ServerInstance->AddMode(aum, 'u'))
61                         throw ModuleException("Could not add new modes!");
62                 OnRehash(NULL, "");
63         }
64         
65         virtual ~ModuleAuditorium()
66         {
67                 ServerInstance->Modes->DelMode(aum);
68                 DELETE(aum);
69         }
70
71         virtual void OnRehash(userrec* user, const std::string &parameter)
72         {
73                 ConfigReader conf(ServerInstance);
74                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
75         }
76
77         Priority Prioritize()
78         {
79                 /* To ensure that we get priority over namesx for names list generation on +u channels */
80                 return (Priority)ServerInstance->PriorityBefore("m_namesx.so");
81         }
82
83         virtual Version GetVersion()
84         {
85                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
86         }
87
88         void Implements(char* List)
89         {
90                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = List[I_OnRehash] = 1;
91         }
92
93         virtual int OnUserList(userrec* user, chanrec* Ptr, CUList* &nameslist)
94         {
95                 if (Ptr->IsModeSet('u'))
96                 {
97                         if (ShowOps)
98                         {
99                                 /* Leave the names list alone, theyre an op
100                                  * doing /names on the channel after joining it
101                                  */
102                                 if (Ptr->GetStatus(user) >= STATUS_OP)
103                                 {
104                                         nameslist = Ptr->GetUsers();
105                                         return 0;
106                                 }
107
108                                 /* Show all the opped users */
109                                 nl = *(Ptr->GetOppedUsers());
110                                 nl[user] = user->nick;
111                                 nameslist = &nl;
112                                 return 0;
113                         }
114                         else
115                         {
116                                 /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
117                                 user->WriteServ("353 %s = %s :%s", user->nick, Ptr->name, user->nick);
118                                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
119                                 return 1;
120                         }
121                 }
122                 return 0;
123         }
124         
125         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
126         {
127                 if (channel->IsModeSet('u'))
128                 {
129                         silent = true;
130                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
131                         user->WriteFrom(user, "JOIN %s", channel->name);
132                         if (ShowOps)
133                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "JOIN %s", channel->name);
134                 }
135         }
136
137         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
138         {
139                 if (channel->IsModeSet('u'))
140                 {
141                         silent = true;
142                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
143                         user->WriteFrom(user, "PART %s%s%s", channel->name,
144                                         partmessage.empty() ? "" : " :",
145                                         partmessage.empty() ? "" : partmessage.c_str());
146                         if (ShowOps)
147                         {
148                                 channel->WriteAllExcept(user, false, channel->GetStatus(user) >= STATUS_OP ? 0 : '@', except_list, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :",
149                                                 partmessage.empty() ? "" : partmessage.c_str());
150                         }
151                 }
152         }
153
154         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
155         {
156                 if (chan->IsModeSet('u'))
157                 {
158                         silent = true;
159                         /* Send silenced event only to the user being kicked and the user doing the kick */
160                         source->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
161                         if (ShowOps)
162                                 chan->WriteAllExcept(source, false, chan->GetStatus(source) >= STATUS_OP ? 0 : '@', except_list, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
163                         else
164                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
165                 }
166         }
167
168         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
169         {
170                 command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
171                 std::vector<std::string> to_leave;
172                 const char* parameters[2];
173                 if (parthandler)
174                 {
175                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
176                         {
177                                 if (f->first->IsModeSet('u'))
178                                         to_leave.push_back(f->first->name);
179                         }
180                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
181                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
182                         {
183                                 parameters[0] = n->c_str();
184                                 /* This triggers our OnUserPart, above, making the PART silent */
185                                 parthandler->Handle(parameters, 1, user);
186                         }
187                 }
188         }
189 };
190
191 MODULE_INIT(ModuleAuditorium);