]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmd_names.cpp
Allow modules to prevent a failed connection from being closed.
[user/henk/code/inspircd.git] / src / coremods / core_channel / cmd_names.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22 #include "core_channel.h"
23 #include "modules/names.h"
24
25 CommandNames::CommandNames(Module* parent)
26         : SplitCommand(parent, "NAMES", 0, 0)
27         , secretmode(parent, "secret")
28         , privatemode(parent, "private")
29         , invisiblemode(parent, "invisible")
30         , namesevprov(parent, "event/names")
31 {
32         syntax = "[<channel>[,<channel>]+]";
33 }
34
35 /** Handle /NAMES
36  */
37 CmdResult CommandNames::HandleLocal(LocalUser* user, const Params& parameters)
38 {
39         Channel* c;
40
41         if (parameters.empty())
42         {
43                 user->WriteNumeric(RPL_ENDOFNAMES, '*', "End of /NAMES list.");
44                 return CMD_SUCCESS;
45         }
46
47         if (CommandParser::LoopCall(user, this, parameters, 0))
48                 return CMD_SUCCESS;
49
50         c = ServerInstance->FindChan(parameters[0]);
51         if (c)
52         {
53                 // Show the NAMES list if one of the following is true:
54                 // - the channel is not secret
55                 // - the user doing the /NAMES is inside the channel
56                 // - the user doing the /NAMES has the channels/auspex privilege
57
58                 // If the user is inside the channel or has privs, instruct SendNames() to show invisible (+i) members
59                 bool show_invisible = ((c->HasUser(user)) || (user->HasPrivPermission("channels/auspex")));
60                 if ((show_invisible) || (!c->IsModeSet(secretmode)))
61                 {
62                         SendNames(user, c, show_invisible);
63                         return CMD_SUCCESS;
64                 }
65         }
66
67         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
68         return CMD_FAILURE;
69 }
70
71 void CommandNames::SendNames(LocalUser* user, Channel* chan, bool show_invisible)
72 {
73         Numeric::Builder<' '> reply(user, RPL_NAMREPLY, false, chan->name.size() + 3);
74         Numeric::Numeric& numeric = reply.GetNumeric();
75         if (chan->IsModeSet(secretmode))
76                 numeric.push(std::string(1, '@'));
77         else if (chan->IsModeSet(privatemode))
78                 numeric.push(std::string(1, '*'));
79         else
80                 numeric.push(std::string(1, '='));
81
82         numeric.push(chan->name);
83         numeric.push(std::string());
84
85         std::string prefixlist;
86         std::string nick;
87         const Channel::MemberMap& members = chan->GetUsers();
88         for (Channel::MemberMap::const_iterator i = members.begin(); i != members.end(); ++i)
89         {
90                 if ((!show_invisible) && (i->first->IsModeSet(invisiblemode)))
91                 {
92                         // Member is invisible and we are not supposed to show them
93                         continue;
94                 }
95
96                 Membership* const memb = i->second;
97
98                 prefixlist.clear();
99                 char prefix = memb->GetPrefixChar();
100                 if (prefix)
101                         prefixlist.push_back(prefix);
102                 nick = i->first->nick;
103
104                 ModResult res;
105                 FIRST_MOD_RESULT_CUSTOM(namesevprov, Names::EventListener, OnNamesListItem, res, (user, memb, prefixlist, nick));
106                 if (res != MOD_RES_DENY)
107                         reply.Add(prefixlist, nick);
108         }
109
110         reply.Flush();
111         user->WriteNumeric(RPL_ENDOFNAMES, chan->name, "End of /NAMES list.");
112 }