]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_channel/cmd_names.cpp
Use CommandBase::Params instead of std::vector<std::string>.
[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
24 CommandNames::CommandNames(Module* parent)
25         : SplitCommand(parent, "NAMES", 0, 0)
26         , secretmode(parent, "secret")
27         , privatemode(parent, "private")
28         , invisiblemode(parent, "invisible")
29 {
30         syntax = "{<channel>{,<channel>}}";
31 }
32
33 /** Handle /NAMES
34  */
35 CmdResult CommandNames::HandleLocal(LocalUser* user, const Params& parameters)
36 {
37         Channel* c;
38
39         if (parameters.empty())
40         {
41                 user->WriteNumeric(RPL_ENDOFNAMES, '*', "End of /NAMES list.");
42                 return CMD_SUCCESS;
43         }
44
45         if (CommandParser::LoopCall(user, this, parameters, 0))
46                 return CMD_SUCCESS;
47
48         c = ServerInstance->FindChan(parameters[0]);
49         if (c)
50         {
51                 // Show the NAMES list if one of the following is true:
52                 // - the channel is not secret
53                 // - the user doing the /NAMES is inside the channel
54                 // - the user doing the /NAMES has the channels/auspex privilege
55
56                 // If the user is inside the channel or has privs, instruct SendNames() to show invisible (+i) members
57                 bool show_invisible = ((c->HasUser(user)) || (user->HasPrivPermission("channels/auspex")));
58                 if ((show_invisible) || (!c->IsModeSet(secretmode)))
59                 {
60                         SendNames(user, c, show_invisible);
61                         return CMD_SUCCESS;
62                 }
63         }
64
65         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
66         return CMD_FAILURE;
67 }
68
69 void CommandNames::SendNames(LocalUser* user, Channel* chan, bool show_invisible)
70 {
71         Numeric::Builder<' '> reply(user, RPL_NAMREPLY, false, chan->name.size() + 3);
72         Numeric::Numeric& numeric = reply.GetNumeric();
73         if (chan->IsModeSet(secretmode))
74                 numeric.push(std::string(1, '@'));
75         else if (chan->IsModeSet(privatemode))
76                 numeric.push(std::string(1, '*'));
77         else
78                 numeric.push(std::string(1, '='));
79
80         numeric.push(chan->name);
81         numeric.push(std::string());
82
83         std::string prefixlist;
84         std::string nick;
85         const Channel::MemberMap& members = chan->GetUsers();
86         for (Channel::MemberMap::const_iterator i = members.begin(); i != members.end(); ++i)
87         {
88                 if ((!show_invisible) && (i->first->IsModeSet(invisiblemode)))
89                 {
90                         // Member is invisible and we are not supposed to show them
91                         continue;
92                 }
93
94                 Membership* const memb = i->second;
95
96                 prefixlist.clear();
97                 char prefix = memb->GetPrefixChar();
98                 if (prefix)
99                         prefixlist.push_back(prefix);
100                 nick = i->first->nick;
101
102                 ModResult res;
103                 FIRST_MOD_RESULT(OnNamesListItem, res, (user, memb, prefixlist, nick));
104
105                 // See if a module wants us to exclude this user from NAMES
106                 if (res == MOD_RES_DENY)
107                         continue;
108
109                 reply.Add(prefixlist, nick);
110         }
111
112         reply.Flush();
113         user->WriteNumeric(RPL_ENDOFNAMES, chan->name, "End of /NAMES list.");
114 }