ergo/irc/isupport.go

147 lines
3.6 KiB
Go
Raw Normal View History

2017-03-27 14:15:02 +02:00
// Copyright (c) 2016 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
2016-06-19 13:59:18 +02:00
import "fmt"
2016-10-19 13:38:31 +02:00
const isupportSupportedString = "are supported by this server"
// ISupportList holds a list of ISUPPORT tokens
type ISupportList struct {
Tokens map[string]*string
2016-06-19 13:59:18 +02:00
CachedReply [][]string
}
// NewISupportList returns a new ISupportList
func NewISupportList() *ISupportList {
var il ISupportList
il.Tokens = make(map[string]*string)
2016-06-19 13:59:18 +02:00
il.CachedReply = make([][]string, 0)
return &il
}
// Add adds an RPL_ISUPPORT token to our internal list
func (il *ISupportList) Add(name string, value string) {
il.Tokens[name] = &value
}
// AddNoValue adds an RPL_ISUPPORT token that does not have a value
func (il *ISupportList) AddNoValue(name string) {
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.
func (il *ISupportList) GetDifference(newil *ISupportList) [][]string {
replies := make([][]string, 0)
var length int // Length of the current cache
var cache []string // Token list cache
// append removed tokens
for name := range il.Tokens {
_, exists := newil.Tokens[name]
if exists {
continue
}
token := fmt.Sprintf("-%s", name)
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
cache = append(cache, isupportSupportedString)
replies = append(replies, cache)
cache = make([]string, 0)
length = 0
}
}
// append added tokens
for name, value := range newil.Tokens {
newval, exists := il.Tokens[name]
if exists && *value == *newval {
continue
}
token := getTokenString(name, value)
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
cache = append(cache, isupportSupportedString)
replies = append(replies, cache)
cache = make([]string, 0)
length = 0
}
}
if len(cache) > 0 {
cache = append(cache, isupportSupportedString)
replies = append(replies, cache)
}
return replies
}
// RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
func (il *ISupportList) RegenerateCachedReply() {
2016-06-19 13:59:18 +02:00
il.CachedReply = make([][]string, 0)
var length int // Length of the current cache
var cache []string // Token list cache
for name, value := range il.Tokens {
2016-10-19 13:38:31 +02:00
token := getTokenString(name, value)
2016-10-23 12:24:02 +02:00
if len(token)+length <= maxLastArgLength {
// 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
cache = append(cache, isupportSupportedString)
2016-06-19 13:59:18 +02:00
il.CachedReply = append(il.CachedReply, cache)
cache = make([]string, 0)
length = 0
}
}
if len(cache) > 0 {
2016-10-19 13:38:31 +02:00
cache = append(cache, isupportSupportedString)
2016-06-19 13:59:18 +02:00
il.CachedReply = append(il.CachedReply, cache)
}
}
2016-10-13 09:36:44 +02:00
// RplISupport outputs our ISUPPORT lines to the client. This is used on connection and in VERSION responses.
2016-06-19 13:59:18 +02:00
func (client *Client) RplISupport() {
for _, tokenline := range client.server.isupport.CachedReply {
// ugly trickery ahead
client.Send(nil, client.server.name, RPL_ISUPPORT, append([]string{client.nick}, tokenline...)...)
}
}