Send compact channel name lists

This commit resolves `sendNames`' TODO.
This commit is contained in:
Hubert Hirtz 2020-06-30 09:11:30 +02:00 committed by Simon Ser
parent a9887114d5
commit dc59263681
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
2 changed files with 29 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package soju
import ( import (
"gopkg.in/irc.v3" "gopkg.in/irc.v3"
"strings"
) )
func forwardChannel(dc *downstreamConn, ch *upstreamChannel) { func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
@ -34,17 +35,40 @@ func sendTopic(dc *downstreamConn, ch *upstreamChannel) {
} }
func sendNames(dc *downstreamConn, ch *upstreamChannel) { func sendNames(dc *downstreamConn, ch *upstreamChannel) {
// TODO: send multiple members in each message
downstreamName := dc.marshalEntity(ch.conn.network, ch.Name) downstreamName := dc.marshalEntity(ch.conn.network, ch.Name)
emptyNameReply := &irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, string(ch.Status), downstreamName, ""},
}
maxLength := maxMessageLength - len(emptyNameReply.String())
var buf strings.Builder
for nick, memberships := range ch.Members { for nick, memberships := range ch.Members {
s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick) s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)
if buf.Len() != 0 && maxLength < buf.Len()+1+len(s) {
// There's not enough space for the next space + nick.
dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
})
buf.Reset()
}
if buf.Len() != 0 {
buf.WriteByte(' ')
}
buf.WriteString(s)
}
if buf.Len() != 0 {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
Command: irc.RPL_NAMREPLY, Command: irc.RPL_NAMREPLY,
Params: []string{dc.nick, string(ch.Status), downstreamName, s}, Params: []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
}) })
} }

2
irc.go
View File

@ -16,6 +16,8 @@ const (
err_invalidcapcmd = "410" err_invalidcapcmd = "410"
) )
const maxMessageLength = 512
type userModes string type userModes string
func (ms userModes) Has(c byte) bool { func (ms userModes) Has(c byte) bool {