]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Add <auditorium showops> which determines if auditorium works like ircnet +a (anonymo...
[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         bool ShowOps;
53         CUList nl;
54         CUList except_list;
55  public:
56         ModuleAuditorium(InspIRCd* Me)
57                 : Module::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                                 /* Show all the opped users */
100                                 nl = *(Ptr->GetOppedUsers());
101                                 nl[user] = user;
102                                 nameslist = &nl;
103                                 return 0;
104                         }
105                         else
106                         {
107                                 /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
108                                 user->WriteServ("353 %s = %s :%s", user->nick, Ptr->name, user->nick);
109                                 user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
110                                 return 1;
111                         }
112                 }
113                 return 0;
114         }
115         
116         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
117         {
118                 if (channel->IsModeSet('u'))
119                 {
120                         silent = true;
121                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
122                         user->WriteFrom(user, "JOIN %s", channel->name);
123                         if (ShowOps)
124                                 channel->WriteAllExcept(user, false, '@', except_list, "JOIN %s", channel->name);
125                 }
126         }
127
128         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
129         {
130                 if (channel->IsModeSet('u'))
131                 {
132                         silent = true;
133                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
134                         user->WriteFrom(user, "PART %s%s%s", channel->name,
135                                         partmessage.empty() ? "" : " :",
136                                         partmessage.empty() ? "" : partmessage.c_str());
137                         if (ShowOps)
138                                 channel->WriteAllExcept(user, false, '@', except_list, "PART %s%s%s", channel->name, partmessage.empty() ? "" : " :",
139                                                 partmessage.empty() ? "" : partmessage.c_str());
140                 }
141         }
142
143         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
144         {
145                 if (chan->IsModeSet('u'))
146                 {
147                         silent = true;
148                         /* Send silenced event only to the user being kicked and the user doing the kick */
149                         source->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
150                         if (ShowOps)
151                                 chan->WriteAllExcept(source, false, '@', except_list, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
152                         else
153                                 user->WriteFrom(source, "KICK %s %s %s", chan->name, user->nick, reason.c_str());
154                 }
155         }
156
157         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
158         {
159                 command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
160                 std::vector<std::string> to_leave;
161                 const char* parameters[2];
162                 if (parthandler)
163                 {
164                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
165                         {
166                                 if (f->first->IsModeSet('u'))
167                                         to_leave.push_back(f->first->name);
168                         }
169                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
170                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
171                         {
172                                 parameters[0] = n->c_str();
173                                 /* This triggers our OnUserPart, above, making the PART silent */
174                                 parthandler->Handle(parameters, 1, user);
175                         }
176                 }
177         }
178 };
179
180 class ModuleAuditoriumFactory : public ModuleFactory
181 {
182  public:
183         ModuleAuditoriumFactory()
184         {
185         }
186         
187         ~ModuleAuditoriumFactory()
188         {
189         }
190         
191         virtual Module * CreateModule(InspIRCd* Me)
192         {
193                 return new ModuleAuditorium(Me);
194         }
195         
196 };
197
198 extern "C" void * init_module( void )
199 {
200         return new ModuleAuditoriumFactory;
201 }
202