]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_channel/cmd_names.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[user/henk/code/inspircd.git] / src / coremods / core_channel / cmd_names.cpp
index 8c8bc58ddd1a529e50368c318c9b9dc4ce29609a..53934b5e3a06bf36d5a5416b124b4410560acf02 100644 (file)
 
 
 #include "inspircd.h"
+#include "core_channel.h"
 
-/** Handle /NAMES.
- */
-class CommandNames : public Command
+CommandNames::CommandNames(Module* parent)
+       : SplitCommand(parent, "NAMES", 0, 0)
+       , secretmode(parent, "secret")
+       , privatemode(parent, "private")
+       , invisiblemode(parent, "invisible")
 {
-       ChanModeReference secretmode;
-
- public:
-       /** Constructor for names.
-        */
-       CommandNames(Module* parent)
-               : Command(parent, "NAMES", 0, 0)
-               , secretmode(parent, "secret")
-       {
-               syntax = "{<channel>{,<channel>}}";
-       }
-
-       /** Handle command.
-        * @param parameters The parameters to the command
-        * @param user The user issuing the command
-        * @return A value from CmdResult to indicate command success or failure.
-        */
-       CmdResult Handle(const std::vector<std::string>& parameters, User *user);
-};
+       syntax = "{<channel>{,<channel>}}";
+}
 
 /** Handle /NAMES
  */
-CmdResult CommandNames::Handle (const std::vector<std::string>& parameters, User *user)
+CmdResult CommandNames::HandleLocal(const std::vector<std::string>& parameters, LocalUser* user)
 {
        Channel* c;
 
        if (!parameters.size())
        {
-               user->WriteNumeric(RPL_ENDOFNAMES, "* :End of /NAMES list.");
+               user->WriteNumeric(RPL_ENDOFNAMES, '*', "End of /NAMES list.");
                return CMD_SUCCESS;
        }
 
@@ -62,19 +48,67 @@ CmdResult CommandNames::Handle (const std::vector<std::string>& parameters, User
        c = ServerInstance->FindChan(parameters[0]);
        if (c)
        {
-               if ((c->IsModeSet(secretmode)) && (!c->HasUser(user)))
+               // Show the NAMES list if one of the following is true:
+               // - the channel is not secret
+               // - the user doing the /NAMES is inside the channel
+               // - the user doing the /NAMES has the channels/auspex privilege
+
+               // If the user is inside the channel or has privs, instruct SendNames() to show invisible (+i) members
+               bool show_invisible = ((c->HasUser(user)) || (user->HasPrivPermission("channels/auspex")));
+               if ((show_invisible) || (!c->IsModeSet(secretmode)))
                {
-                     user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", c->name.c_str());
-                     return CMD_FAILURE;
+                       SendNames(user, c, show_invisible);
+                       return CMD_SUCCESS;
                }
-               c->UserList(user);
        }
+
+       user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
+       return CMD_FAILURE;
+}
+
+void CommandNames::SendNames(LocalUser* user, Channel* chan, bool show_invisible)
+{
+       Numeric::Builder<' '> reply(user, RPL_NAMREPLY, false, chan->name.size() + 4);
+       Numeric::Numeric& numeric = reply.GetNumeric();
+       if (chan->IsModeSet(secretmode))
+               numeric.push(std::string(1, '@'));
+       else if (chan->IsModeSet(privatemode))
+               numeric.push(std::string(1, '*'));
        else
+               numeric.push(std::string(1, '='));
+
+       numeric.push(chan->name);
+       numeric.push(std::string());
+
+       std::string prefixlist;
+       std::string nick;
+       const Channel::MemberMap& members = chan->GetUsers();
+       for (Channel::MemberMap::const_iterator i = members.begin(); i != members.end(); ++i)
        {
-               user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
+               if ((!show_invisible) && (i->first->IsModeSet(invisiblemode)))
+               {
+                       // Member is invisible and we are not supposed to show them
+                       continue;
+               }
+
+               Membership* const memb = i->second;
+
+               prefixlist.clear();
+               char prefix = memb->GetPrefixChar();
+               if (prefix)
+                       prefixlist.push_back(prefix);
+               nick = i->first->nick;
+
+               ModResult res;
+               FIRST_MOD_RESULT(OnNamesListItem, res, (user, memb, prefixlist, nick));
+
+               // See if a module wants us to exclude this user from NAMES
+               if (res == MOD_RES_DENY)
+                       continue;
+
+               reply.Add(prefixlist, nick);
        }
 
-       return CMD_SUCCESS;
+       reply.Flush();
+       user->WriteNumeric(RPL_ENDOFNAMES, chan->name, "End of /NAMES list.");
 }
-
-COMMAND_INIT(CommandNames)