]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_channel/cmd_names.cpp
cmd_names Inherit handler from SplitCommand
[user/henk/code/inspircd.git] / src / coremods / core_channel / cmd_names.cpp
index 558ad84078ae950faebd82bffd58e2120a73f572..05f16e10b22d30f0e0ff2115349548aa5611bea9 100644 (file)
 #include "core_channel.h"
 
 CommandNames::CommandNames(Module* parent)
-       : Command(parent, "NAMES", 0, 0)
+       : SplitCommand(parent, "NAMES", 0, 0)
        , secretmode(parent, "secret")
+       , privatemode(parent, "private")
+       , invisiblemode(parent, "invisible")
 {
        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;
 
@@ -51,10 +53,11 @@ CmdResult CommandNames::Handle (const std::vector<std::string>& parameters, User
                // - the user doing the /NAMES is inside the channel
                // - the user doing the /NAMES has the channels/auspex privilege
 
-               bool has_user = c->HasUser(user);
-               if ((!c->IsModeSet(secretmode)) || (has_user) || (user->HasPrivPermission("channels/auspex")))
+               // 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)))
                {
-                       c->UserList(user);
+                       SendNames(user, c, show_invisible);
                        return CMD_SUCCESS;
                }
        }
@@ -62,3 +65,63 @@ CmdResult CommandNames::Handle (const std::vector<std::string>& parameters, User
        user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", parameters[0].c_str());
        return CMD_FAILURE;
 }
+
+void CommandNames::SendNames(LocalUser* user, Channel* chan, bool show_invisible)
+{
+       std::string list;
+       if (chan->IsModeSet(secretmode))
+               list.push_back('@');
+       else if (chan->IsModeSet(privatemode))
+               list.push_back('*');
+       else
+               list.push_back('=');
+
+       list.push_back(' ');
+       list.append(chan->name).append(" :");
+       std::string::size_type pos = list.size();
+
+       const size_t maxlen = ServerInstance->Config->Limits.MaxLine - 10 - ServerInstance->Config->ServerName.size() - user->nick.size();
+       std::string prefixlist;
+       std::string nick;
+       const Channel::MemberMap& members = chan->GetUsers();
+       for (Channel::MemberMap::const_iterator i = members.begin(); i != members.end(); ++i)
+       {
+               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;
+
+               if (list.size() + prefixlist.length() + nick.length() + 1 > maxlen)
+               {
+                       // List overflowed into multiple numerics
+                       user->WriteNumeric(RPL_NAMREPLY, list);
+
+                       // Erase all nicks, keep the constant part
+                       list.erase(pos);
+               }
+
+               list.append(prefixlist).append(nick).push_back(' ');
+       }
+
+       // Only send the user list numeric if there is at least one user in it
+       if (list.size() != pos)
+               user->WriteNumeric(RPL_NAMREPLY, list);
+
+       user->WriteNumeric(RPL_ENDOFNAMES, "%s :End of /NAMES list.", chan->name.c_str());
+}