-
Notifications
You must be signed in to change notification settings - Fork 176
/
isupport.go
75 lines (63 loc) · 1.93 KB
/
isupport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright (c) 2016- Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import "fmt"
// ISupportList holds a list of ISUPPORT tokens
type ISupportList struct {
Tokens map[string]*string
CachedReply [][]string
}
// NewISupportList returns a new ISupportList
func NewISupportList() *ISupportList {
var il ISupportList
il.Tokens = make(map[string]*string)
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
}
// RegenerateCachedReply regenerates the cached RPL_ISUPPORT reply
func (il *ISupportList) RegenerateCachedReply() {
il.CachedReply = make([][]string, 0)
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 {
cache = append(cache, "are supported by this server")
il.CachedReply = append(il.CachedReply, cache)
cache = make([]string, 0)
length = 0
}
}
if len(cache) > 0 {
cache = append(cache, "are supported by this server")
il.CachedReply = append(il.CachedReply, cache)
}
}
func (client *Client) RplISupport() {
for _, tokenline := range client.server.isupport.CachedReply {
// ugly trickery ahead
client.Send(nil, client.server.nameString, RPL_ISUPPORT, append([]string{client.nickString}, tokenline...)...)
}
}