2017-03-27 14:15:02 +02:00
|
|
|
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
|
2016-06-15 13:50:56 +02:00
|
|
|
// released under the MIT license
|
|
|
|
|
2017-10-05 15:39:57 +02:00
|
|
|
package isupport
|
2016-04-12 07:38:42 +02:00
|
|
|
|
2018-12-30 23:26:39 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
)
|
2016-04-12 07:38:42 +02:00
|
|
|
|
2017-10-05 15:39:57 +02:00
|
|
|
const (
|
|
|
|
maxLastArgLength = 400
|
|
|
|
)
|
2016-10-19 13:38:31 +02:00
|
|
|
|
2017-10-05 15:39:57 +02:00
|
|
|
// List holds a list of ISUPPORT tokens
|
|
|
|
type List struct {
|
2016-04-12 07:38:42 +02:00
|
|
|
Tokens map[string]*string
|
2016-06-19 13:59:18 +02:00
|
|
|
CachedReply [][]string
|
2016-04-12 07:38:42 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 15:39:57 +02:00
|
|
|
// NewList returns a new List
|
|
|
|
func NewList() *List {
|
|
|
|
var il List
|
2016-04-12 07:38:42 +02:00
|
|
|
il.Tokens = make(map[string]*string)
|
2016-06-19 13:59:18 +02:00
|
|
|
il.CachedReply = make([][]string, 0)
|
2016-04-12 07:38:42 +02:00
|
|
|
return &il
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds an RPL_ISUPPORT token to our internal list
|
2017-10-05 15:39:57 +02:00
|
|
|
func (il *List) Add(name string, value string) {
|
2016-04-12 07:38:42 +02:00
|
|
|
il.Tokens[name] = &value
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNoValue adds an RPL_ISUPPORT token that does not have a value
|
2017-10-05 15:39:57 +02:00
|
|
|
func (il *List) AddNoValue(name string) {
|
2016-04-12 07:38:42 +02:00
|
|
|
il.Tokens[name] = nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 13:38:31 +02:00
|
|
|
// getTokenString gets the appropriate string for a token+value.
|
|
|
|
func getTokenString(name string, value *string) string {
|
|
|
|
if value == nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s=%s", name, *value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDifference returns the difference between two token lists.
|
2017-10-05 15:39:57 +02:00
|
|
|
func (il *List) GetDifference(newil *List) [][]string {
|
2017-07-30 14:42:37 +02:00
|
|
|
var outTokens sort.StringSlice
|
2016-10-19 13:38:31 +02:00
|
|
|
|
|
|
|
// append removed tokens
|
|
|
|
for name := range il.Tokens {
|
|
|
|
_, exists := newil.Tokens[name]
|
|
|
|
if exists {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
token := fmt.Sprintf("-%s", name)
|
|
|
|
|
2017-07-30 14:42:37 +02:00
|
|
|
outTokens = append(outTokens, token)
|
2016-10-19 13:38:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// append added tokens
|
|
|
|
for name, value := range newil.Tokens {
|
|
|
|
newval, exists := il.Tokens[name]
|
2017-07-30 14:42:37 +02:00
|
|
|
if exists && ((value == nil && newval == nil) || (value != nil && newval != nil && *value == *newval)) {
|
2016-10-19 13:38:31 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
token := getTokenString(name, value)
|
|
|
|
|
2017-07-30 14:42:37 +02:00
|
|
|
outTokens = append(outTokens, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(outTokens)
|
|
|
|
|
|
|
|
// create output list
|
|
|
|
replies := make([][]string, 0)
|
|
|
|
var length int // Length of the current cache
|
|
|
|
var cache []string // Token list cache
|
|
|
|
|
|
|
|
for _, token := range outTokens {
|
2016-10-23 12:24:02 +02:00
|
|
|
if len(token)+length <= maxLastArgLength {
|
2016-10-19 13:38:31 +02:00
|
|
|
// account for the space separating tokens
|
|
|
|
if len(cache) > 0 {
|
|
|
|
length++
|
|
|
|
}
|
|
|
|
cache = append(cache, token)
|
|
|
|
length += len(token)
|
|
|
|
}
|
|
|
|
|
2016-10-23 12:24:02 +02:00
|
|
|
if len(cache) == 13 || len(token)+length >= maxLastArgLength {
|
2016-10-19 13:38:31 +02:00
|
|
|
replies = append(replies, cache)
|
|
|
|
cache = make([]string, 0)
|
|
|
|
length = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cache) > 0 {
|
|
|
|
replies = append(replies, cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
return replies
|
|
|
|
}
|
|
|
|
|
2016-04-12 07:38:42 +02:00
|
|
|
// RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
|
2018-12-30 23:26:39 +01:00
|
|
|
func (il *List) RegenerateCachedReply() (err error) {
|
2016-06-19 13:59:18 +02:00
|
|
|
il.CachedReply = make([][]string, 0)
|
2016-04-12 07:38:42 +02:00
|
|
|
var length int // Length of the current cache
|
|
|
|
var cache []string // Token list cache
|
|
|
|
|
2017-07-30 14:42:37 +02:00
|
|
|
// make sure we get a sorted list of tokens, needed for tests and looks nice
|
|
|
|
var tokens sort.StringSlice
|
|
|
|
for name := range il.Tokens {
|
|
|
|
tokens = append(tokens, name)
|
|
|
|
}
|
|
|
|
sort.Sort(tokens)
|
|
|
|
|
|
|
|
for _, name := range tokens {
|
|
|
|
token := getTokenString(name, il.Tokens[name])
|
2018-12-30 23:26:39 +01:00
|
|
|
if token[0] == ':' || strings.Contains(token, " ") {
|
|
|
|
err = fmt.Errorf("bad isupport token (cannot contain spaces or start with :): %s", token)
|
|
|
|
continue
|
|
|
|
}
|
2016-04-12 07:38:42 +02:00
|
|
|
|
2016-10-23 12:24:02 +02:00
|
|
|
if len(token)+length <= maxLastArgLength {
|
2016-04-12 07:38:42 +02:00
|
|
|
// account for the space separating tokens
|
|
|
|
if len(cache) > 0 {
|
|
|
|
length++
|
|
|
|
}
|
|
|
|
cache = append(cache, token)
|
|
|
|
length += len(token)
|
|
|
|
}
|
|
|
|
|
2016-10-23 12:24:02 +02:00
|
|
|
if len(cache) == 13 || len(token)+length >= maxLastArgLength {
|
2016-06-19 13:59:18 +02:00
|
|
|
il.CachedReply = append(il.CachedReply, cache)
|
2016-04-12 07:38:42 +02:00
|
|
|
cache = make([]string, 0)
|
|
|
|
length = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cache) > 0 {
|
2016-06-19 13:59:18 +02:00
|
|
|
il.CachedReply = append(il.CachedReply, cache)
|
|
|
|
}
|
2018-12-30 23:26:39 +01:00
|
|
|
|
|
|
|
return
|
2016-06-19 13:59:18 +02:00
|
|
|
}
|