1
0
Fork 0
forked from External/ergo
This commit is contained in:
Shivaram Lingamneni 2019-08-27 00:51:09 -04:00
parent 014a5fe63c
commit 7ed27d4a42
9 changed files with 206 additions and 153 deletions

View file

@ -4,8 +4,8 @@
package caps
import (
"bytes"
"sort"
"strings"
"github.com/oragono/oragono/irc/utils"
)
@ -13,6 +13,9 @@ import (
// Set holds a set of enabled capabilities.
type Set [bitsetLen]uint32
// Values holds capability values.
type Values map[Capability]string
// NewSet returns a new Set, with the given capabilities enabled.
func NewSet(capabs ...Capability) *Set {
var newSet Set
@ -88,8 +91,10 @@ func (s *Set) Empty() bool {
return utils.BitsetEmpty(s[:])
}
const maxPayloadLength = 440
// String returns all of our enabled capabilities as a string.
func (s *Set) String(version Version, values *Values) string {
func (s *Set) String(version Version, values Values) (result []string) {
var strs sort.StringSlice
var capab Capability
@ -101,7 +106,7 @@ func (s *Set) String(version Version, values *Values) string {
}
capString := capab.Name()
if version == Cap302 {
val, exists := values.Get(capab)
val, exists := values[capab]
if exists {
capString += "=" + val
}
@ -109,8 +114,31 @@ func (s *Set) String(version Version, values *Values) string {
strs = append(strs, capString)
}
if len(strs) == 0 {
return []string{""}
}
// sort the cap string before we send it out
sort.Sort(strs)
return strings.Join(strs, " ")
var buf bytes.Buffer
for _, str := range strs {
tokenLen := len(str)
if buf.Len() != 0 {
tokenLen += 1
}
if maxPayloadLength < buf.Len()+tokenLen {
result = append(result, buf.String())
buf.Reset()
}
if buf.Len() != 0 {
buf.WriteByte(' ')
}
buf.WriteString(str)
}
if buf.Len() != 0 {
result = append(result, buf.String())
}
return
}