]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
svn add helps, too. :<
[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                         silent = true;
99         }
100
101         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
102         {
103                 if (channel->IsModeSet('u'))
104                         silent = true;
105         }
106
107         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
108         {
109                 if (chan->IsModeSet('u'))
110                         silent = true;
111         }
112
113         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
114         {
115                 command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
116                 std::vector<std::string> to_leave;
117                 const char* parameters[2];
118                 if (parthandler)
119                 {
120                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
121                         {
122                                 if (f->first->IsModeSet('u'))
123                                         to_leave.push_back(f->first->name);
124                         }
125                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
126                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
127                         {
128                                 parameters[0] = n->c_str();
129                                 /* This triggers our OnUserPart, above, making the PART silent */
130                                 parthandler->Handle(parameters, 1, user);
131                         }
132                 }
133         }
134 };
135
136 class ModuleAuditoriumFactory : public ModuleFactory
137 {
138  public:
139         ModuleAuditoriumFactory()
140         {
141         }
142         
143         ~ModuleAuditoriumFactory()
144         {
145         }
146         
147         virtual Module * CreateModule(InspIRCd* Me)
148         {
149                 return new ModuleAuditorium(Me);
150         }
151         
152 };
153
154 extern "C" void * init_module( void )
155 {
156         return new ModuleAuditoriumFactory;
157 }
158