]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
Header update: 2007 -> 2008
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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(User* source, User* dest, Channel* 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))
58                 {
59                         delete aum;
60                         throw ModuleException("Could not add new modes!");
61                 }
62
63                 OnRehash(NULL, "");
64
65                 Implementation eventlist[] = { I_OnUserJoin, I_OnUserPart, I_OnUserKick, I_OnUserQuit, I_OnUserList, I_OnRehash };
66                 Me->Modules->Attach(eventlist, this, 6);
67
68         }
69         
70         virtual ~ModuleAuditorium()
71         {
72                 ServerInstance->Modes->DelMode(aum);
73                 delete aum;
74         }
75
76         void Prioritize()
77         {
78                 Module* namesx = ServerInstance->Modules->Find("m_namesx.so");
79                 ServerInstance->Modules->SetPriority(this, I_OnUserList, PRIO_BEFORE, &namesx);
80         }
81
82         virtual void OnRehash(User* user, const std::string &parameter)
83         {
84                 ConfigReader conf(ServerInstance);
85                 ShowOps = conf.ReadFlag("auditorium", "showops", 0);
86         }
87
88         virtual Version GetVersion()
89         {
90                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
91         }
92
93         virtual int OnUserList(User* user, Channel* 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 %c %s :%s", user->nick, Ptr->IsModeSet('s') ? '@' : Ptr->IsModeSet('p') ? '*' : '=', 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(User* user, Channel* channel, bool sync, 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(User* user, Channel* 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(User* source, User* user, Channel* 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(User* user, const std::string &reason, const std::string &oper_message)
169         {
170                 Command* 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)