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
This commit is contained in:
delthas 2020-03-27 20:09:38 +01:00 committed by Simon Ser
parent 2fff645129
commit f08063c943
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -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