2016-06-15 13:50:56 +02:00
|
|
|
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
|
|
|
|
// released under the MIT license
|
|
|
|
|
2016-04-12 07:38:42 +02:00
|
|
|
package irc
|
|
|
|
|
2016-06-19 13:59:18 +02:00
|
|
|
import "fmt"
|
2016-04-12 07:38:42 +02:00
|
|
|
|
|
|
|
// ISupportList holds a list of ISUPPORT tokens
|
|
|
|
type ISupportList struct {
|
|
|
|
Tokens map[string]*string
|
2016-06-19 13:59:18 +02:00
|
|
|
CachedReply [][]string
|
2016-04-12 07:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2016-04-12 07:38:42 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
|
|
|
|
func (il *ISupportList) RegenerateCachedReply() {
|
2016-06-19 13:59:18 +02:00
|
|
|
il.CachedReply = make([][]string, 0)
|
2016-04-12 07:38:42 +02:00
|
|
|
maxlen := 400 // Max length of a single ISUPPORT token line
|
|
|
|
var length int // Length of the current cache
|
|
|
|
var cache []string // Token list cache
|
|
|
|
|
|
|
|
for name, value := range il.Tokens {
|
|
|
|
var token string
|
|
|
|
if value == nil {
|
|
|
|
token = name
|
|
|
|
} else {
|
|
|
|
token = fmt.Sprintf("%s=%s", name, *value)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(token)+length <= maxlen {
|
|
|
|
// account for the space separating tokens
|
|
|
|
if len(cache) > 0 {
|
|
|
|
length++
|
|
|
|
}
|
|
|
|
cache = append(cache, token)
|
|
|
|
length += len(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cache) == 13 || len(token)+length >= maxlen {
|
2016-06-19 13:59:18 +02:00
|
|
|
cache = append(cache, "are supported by this server")
|
|
|
|
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
|
|
|
cache = append(cache, "are supported by this server")
|
|
|
|
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 {
|
2016-06-20 14:53:45 +02:00
|
|
|
// ugly trickery ahead
|
2016-10-11 15:51:46 +02:00
|
|
|
client.Send(nil, client.server.name, RPL_ISUPPORT, append([]string{client.nick}, tokenline...)...)
|
2016-04-12 07:38:42 +02:00
|
|
|
}
|
|
|
|
}
|