2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
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.
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
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/>.
22 #include "core_channel.h"
24 CommandNames::CommandNames(Module* parent)
25 : Command(parent, "NAMES", 0, 0)
26 , secretmode(parent, "secret")
27 , privatemode(parent, "private")
28 , invisiblemode(parent, "invisible")
30 syntax = "{<channel>{,<channel>}}";
35 CmdResult CommandNames::Handle (const std::vector<std::string>& parameters, User *user)
39 if (!parameters.size())
41 user->WriteNumeric(RPL_ENDOFNAMES, "* :End of /NAMES list.");
45 if (CommandParser::LoopCall(user, this, parameters, 0))
48 c = ServerInstance->FindChan(parameters[0]);
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
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)))
60 SendNames(user, c, show_invisible);
65 user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
69 void CommandNames::SendNames(User* user, Channel* chan, bool show_invisible)
72 if (chan->IsModeSet(secretmode))
74 else if (chan->IsModeSet(privatemode))
80 list.append(chan->name).append(" :");
81 std::string::size_type pos = list.size();
83 const size_t maxlen = ServerInstance->Config->Limits.MaxLine - 10 - ServerInstance->Config->ServerName.size() - user->nick.size();
84 std::string prefixlist;
86 const Channel::MemberMap& members = chan->GetUsers();
87 for (Channel::MemberMap::const_iterator i = members.begin(); i != members.end(); ++i)
89 if ((!show_invisible) && (i->first->IsModeSet(invisiblemode)))
91 // Member is invisible and we are not supposed to show them
95 Membership* const memb = i->second;
98 char prefix = memb->GetPrefixChar();
100 prefixlist.push_back(prefix);
101 nick = i->first->nick;
104 FIRST_MOD_RESULT(OnNamesListItem, res, (user, memb, prefixlist, nick));
106 // See if a module wants us to exclude this user from NAMES
107 if (res == MOD_RES_DENY)
110 if (list.size() + prefixlist.length() + nick.length() + 1 > maxlen)
112 // List overflowed into multiple numerics
113 user->WriteNumeric(RPL_NAMREPLY, list);
115 // Erase all nicks, keep the constant part
119 list.append(prefixlist).append(nick).push_back(' ');
122 // Only send the user list numeric if there is at least one user in it
123 if (list.size() != pos)
124 user->WriteNumeric(RPL_NAMREPLY, list);
126 user->WriteNumeric(RPL_ENDOFNAMES, "%s :End of /NAMES list.", chan->name.c_str());