]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
f6b1ff3d8d2c4ce8240cc13ab9fdd720a7faf3cc
[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 "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.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  public:
53         ModuleAuditorium(InspIRCd* Me)
54                 : Module::Module(Me)
55         {
56                 aum = new AuditoriumMode(ServerInstance);
57                 if (!ServerInstance->AddMode(aum, 'u'))
58                         throw ModuleException("Could not add new modes!");
59         }
60         
61         virtual ~ModuleAuditorium()
62         {
63                 ServerInstance->Modes->DelMode(aum);
64                 DELETE(aum);
65         }
66
67         Priority Prioritize()
68         {
69                 /* To ensure that we get priority over namesx for names list generation on +u channels */
70                 return (Priority)ServerInstance->PriorityBefore("m_namesx.so");
71         }
72
73         virtual Version GetVersion()
74         {
75                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
76         }
77
78         void Implements(char* List)
79         {
80                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = 1;
81         }
82
83         virtual int OnUserList(userrec* user, chanrec* Ptr)
84         {
85                 if (Ptr->IsModeSet('u'))
86                 {
87                         /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
88                         user->WriteServ("353 %s = %s :%s", user->nick, Ptr->name, user->nick);
89                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
90                         return 1;
91                 }
92                 return 0;
93         }
94         
95         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
96         {
97                 if (channel->IsModeSet('u'))
98                 {
99                         silent = true;
100                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
101                         user->WriteFrom(user, "JOIN %s", channel->name);
102                 }
103         }
104
105         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
106         {
107                 if (channel->IsModeSet('u'))
108                 {
109                         silent = true;
110                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
111                         user->WriteFrom(user, "PART %s%s%s", channel->name,
112                                         partmessage.empty() ? "" : " :",
113                                         partmessage.empty() ? "" : partmessage.c_str());
114                 }
115         }
116
117         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
118         {
119                 if (chan->IsModeSet('u'))
120                 {
121                         silent = true;
122                         /* Send silenced event only to the user being kicked and the user doing the kick */
123                         source->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
124                         user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
125                 }
126         }
127
128         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
129         {
130                 command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
131                 std::vector<std::string> to_leave;
132                 const char* parameters[2];
133                 if (parthandler)
134                 {
135                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
136                         {
137                                 if (f->first->IsModeSet('u'))
138                                         to_leave.push_back(f->first->name);
139                         }
140                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
141                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
142                         {
143                                 parameters[0] = n->c_str();
144                                 /* This triggers our OnUserPart, above, making the PART silent */
145                                 parthandler->Handle(parameters, 1, user);
146                         }
147                 }
148         }
149 };
150
151 class ModuleAuditoriumFactory : public ModuleFactory
152 {
153  public:
154         ModuleAuditoriumFactory()
155         {
156         }
157         
158         ~ModuleAuditoriumFactory()
159         {
160         }
161         
162         virtual Module * CreateModule(InspIRCd* Me)
163         {
164                 return new ModuleAuditorium(Me);
165         }
166         
167 };
168
169 extern "C" void * init_module( void )
170 {
171         return new ModuleAuditoriumFactory;
172 }
173