From f08063c943ae111a8c6afcb223395d3b1cbf5e3f Mon Sep 17 00:00:00 2001 From: delthas Date: Fri, 27 Mar 2020 20:09:38 +0100 Subject: [PATCH] Fix parsing wrong empty element in RPL_WHOISCHANNELS channel list Some servers add a trailing space to the channel list in RPL_WHOISCHANNELS. This commit works around this issue by removing any empty trailing element after splitting. Since RPL_WHOISCHANNELS could send an empty channel parameter, we can't just use strings.TrimRight(s, " "), because splitting on an empty string would still return an empty element. Closes: https://todo.sr.ht/~emersion/soju/25 --- upstream.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/upstream.go b/upstream.go index cb5208c..5c6c9e4 100644 --- a/upstream.go +++ b/upstream.go @@ -821,14 +821,13 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { if err := parseMessageParams(msg, nil, &statusStr, &name, &members); err != nil { return err } - members = strings.TrimRight(members, " ") ch, ok := uc.channels[name] if !ok { // NAMES on a channel we have not joined, forward to downstream uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { channel := dc.marshalChannel(uc, name) - members := strings.Split(members, " ") + members := splitSpace(members) for i, member := range members { membership, nick := uc.parseMembershipPrefix(member) members[i] = membership.String() + dc.marshalNick(uc, nick) @@ -850,7 +849,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { } ch.Status = status - for _, s := range strings.Split(members, " ") { + for _, s := range splitSpace(members) { membership, nick := uc.parseMembershipPrefix(s) ch.Members[nick] = membership } @@ -995,7 +994,7 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { if err := parseMessageParams(msg, nil, &nick, &channelList); err != nil { return err } - channels := strings.Split(channelList, " ") + channels := splitSpace(channelList) uc.forEachDownstreamByID(downstreamID, func(dc *downstreamConn) { nick := dc.marshalNick(uc, nick) @@ -1094,6 +1093,12 @@ func (uc *upstreamConn) handleMessage(msg *irc.Message) error { return nil } +func splitSpace(s string) []string { + return strings.FieldsFunc(s, func(r rune) bool { + return r == ' ' + }) +} + func (uc *upstreamConn) register() { uc.nick = uc.network.Nick uc.username = uc.network.Username