From aa1780a4153f5d68c004116d08638d7a3a781f86 Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 9 Aug 2015 10:37:52 -0700 Subject: [PATCH] IRC WHO command now handles a global mask/request --- client.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ commands.go | 6 ++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index a7a021c..876a47d 100644 --- a/client.go +++ b/client.go @@ -245,3 +245,59 @@ func (c *Client) UpdateNick(newNick string) { c.Prefix.Name = newNick } + +// GetVisible returns a map of clients visible to this client +func (c *Client) GetVisible() map[string]*Client { + clients := map[string]*Client{} + for name, client := range c.Server.clientsByNick { + if client.HasMode(UserModeInvisible) { + continue + } + clients[name] = client + } + for _, channel := range c.channels { + for member := range channel.members { + tmp, ok := c.Server.GetClientByNick(member) + if ok { + clients[member] = tmp + } + } + } + return clients +} + +// SendMessagetoVisible sends a message to all other visible clients +func (c *Client) SendMessagetoVisible(m *irc.Message) { + for _, client := range c.GetVisible() { + client.Encode(m) + } +} + +// SendWho rmanages responding to the WHO request for all visible clients of this client +func (c *Client) SendWho() { + clients := map[string]*Client{} + for name, client := range c.Server.clientsByNick { + if client.HasMode(UserModeInvisible) { + continue + } + msg := whoLine(client, nil, c.Nickname) + m := irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_WHOREPLY, Params: strings.Fields(msg)} + c.Encode(&m) + + clients[name] = client + } + for _, channel := range c.channels { + for member := range channel.members { + tmp, ok := c.Server.GetClientByNick(member) + _, alreadySent := clients[member] + if ok && !alreadySent { + msg := whoLine(tmp, channel, c.Nickname) + m := irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_WHOREPLY, Params: strings.Fields(msg)} + c.Encode(&m) + clients[member] = tmp + } + } + } + m := irc.Message{Prefix: c.Server.Prefix, Command: irc.RPL_ENDOFWHO, Params: []string{c.Nickname, "*"}, Trailing: "End of WHO list"} + c.Encode(&m) +} diff --git a/commands.go b/commands.go index fb4a0e6..470390b 100644 --- a/commands.go +++ b/commands.go @@ -313,8 +313,10 @@ func NoticeHandler(message *irc.Message, client *Client) { // WhoHandler is a CommandHandler to respond to IRC WHO commands from a client // Implemented according to RFC 1459 Section 4.5.1 and RFC 2812 Section 3.6.1 func WhoHandler(message *irc.Message, client *Client) { - if len(message.Params) == 0 { - //return listing of all users + if len(message.Params) == 0 || len(message.Params[0]) == 0 || message.Params[0][0] == '*' { + //return listing of all visible users - visible people and people in channels with this client + client.SendWho() + return } ch, ok := client.Server.GetChannel(message.Params[0])