~emersion/public-inbox

soju: Send compact channel name lists v1 SUPERSEDED

Hubert Hirtz: 1
 Send compact channel name lists

 2 files changed, 29 insertions(+), 3 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/public-inbox/patches/11380/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH soju] Send compact channel name lists Export this patch

This commit resolves `sendNames`' TODO.
---
 bridge.go | 30 +++++++++++++++++++++++++++---
 irc.go    |  2 ++
 2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/bridge.go b/bridge.go
index c6080af..1abf5b9 100644
--- a/bridge.go
@@ -2,6 +2,7 @@ package soju

import (
	"gopkg.in/irc.v3"
	"strings"
)

func forwardChannel(dc *downstreamConn, ch *upstreamChannel) {
@@ -34,17 +35,40 @@ func sendTopic(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)

	maxLength := messageLength
	maxLength -= 1 + len(dc.srv.prefix().String())
	maxLength -= 1 + 3 // 353 RPL_NAMREPLY
	maxLength -= 1 + len(dc.nick)
	maxLength -= 1 + len(string(ch.Status))
	maxLength -= 1 + len(downstreamName)
	maxLength -= 4 // " :" and "\r\n"
	var buf strings.Builder

	for nick, memberships := range ch.Members {
		s := memberships.Format(dc) + dc.marshalEntity(ch.conn.network, nick)

		if buf.Len() != 0 && maxLength < buf.Len()+len(s) {
			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{
			Prefix:  dc.srv.prefix(),
			Command: irc.RPL_NAMREPLY,
			Params:  []string{dc.nick, string(ch.Status), downstreamName, s},
			Params:  []string{dc.nick, string(ch.Status), downstreamName, buf.String()},
		})
	}

diff --git a/irc.go b/irc.go
index 8fae660..c2676bb 100644
--- a/irc.go
+++ b/irc.go
@@ -16,6 +16,8 @@ const (
	err_invalidcapcmd = "410"
)

const messageLength = 512

type userModes string

func (ms userModes) Has(c byte) bool {
-- 
2.27.0
Thanks for the patch! Here are a few comments.