-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
cap_tags.go
324 lines (265 loc) · 7.27 KB
/
cap_tags.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (c) Liam Stanley <me@liamstanley.io>. All rights reserved. Use
// of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package girc
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
)
// handleTags handles any messages that have tags that will affect state. (e.g.
// 'account' tags.)
func handleTags(c *Client, e Event) {
if len(e.Tags) == 0 {
return
}
account, ok := e.Tags.Get("account")
if !ok {
return
}
c.state.Lock()
user := c.state.lookupUser(e.Source.ID())
if user != nil {
user.Extras.Account = account
}
c.state.Unlock()
c.state.notify(c, UPDATE_STATE)
}
const (
prefixTag byte = '@'
prefixTagValue byte = '='
prefixUserTag byte = '+'
tagSeparator byte = ';'
maxTagLength int = 4094 // 4094 + @ and " " (space) = 4096, though space usually not included.
)
// Tags represents the key-value pairs in IRCv3 message tags. The map contains
// the encoded message-tag values. If the tag is present, it may still be
// empty. See Tags.Get() and Tags.Set() for use with getting/setting
// information within the tags.
//
// Note that retrieving and setting tags are not concurrent safe. If this is
// necessary, you will need to implement it yourself.
type Tags map[string]string
// ParseTags parses out the key-value map of tags. raw should only be the tag
// data, not a full message. For example:
//
// @aaa=bbb;ccc;example.com/ddd=eee
//
// NOT:
//
// @aaa=bbb;ccc;example.com/ddd=eee :nick!ident@host.com PRIVMSG me :Hello
//
// Technically, there is a length limit of 4096, but the server should reject
// tag messages longer than this.
func ParseTags(raw string) (t Tags) {
t = make(Tags)
if len(raw) > 0 && raw[0] == prefixTag {
raw = raw[1:]
}
parts := strings.Split(raw, string(tagSeparator))
var hasValue int
for i := 0; i < len(parts); i++ {
hasValue = strings.IndexByte(parts[i], prefixTagValue)
// The tag doesn't contain a value or has a splitter with no value.
if hasValue < 1 || len(parts[i]) < hasValue+1 {
if !validTag(parts[i]) {
continue
}
t[parts[i]] = ""
continue
}
// Check if tag key or decoded value are invalid.
// if !validTag(parts[i][:hasValue]) || !validTagValue(tagDecoder.Replace(parts[i][hasValue+1:])) {
// continue
// }
t[parts[i][:hasValue]] = tagDecoder.Replace(parts[i][hasValue+1:])
}
return t
}
// Len determines the length of the bytes representation of this tag map. This
// does not include the trailing space required when creating an event, but
// does include the tag prefix ("@").
func (t Tags) Len() (length int) {
if t == nil {
return 0
}
return len(t.Bytes())
}
// Equals compares two Tags for equality. With the msgid IRCv3 spec +\
// echo-message (amongst others), we may receive events that have msgid's,
// whereas our local events will not have the msgid. As such, don't compare
// all tags, only the necessary/important tags.
func (t Tags) Equals(tt Tags) bool {
// The only tag which is important at this time.
taccount, _ := t.Get("account")
ttaccount, _ := tt.Get("account")
return taccount == ttaccount
}
// Keys returns a slice of (unsorted) tag keys.
func (t Tags) Keys() (keys []string) {
keys = make([]string, 0, t.Count())
for key := range t {
keys = append(keys, key)
}
return keys
}
// Count finds how many total tags that there are.
func (t Tags) Count() int {
if t == nil {
return 0
}
return len(t)
}
// Bytes returns a []byte representation of this tag map, including the tag
// prefix ("@"). Note that this will return the tags sorted, regardless of
// the order of how they were originally parsed.
func (t Tags) Bytes() []byte {
if t == nil {
return []byte{}
}
max := len(t)
if max == 0 {
return nil
}
buffer := new(bytes.Buffer)
buffer.WriteByte(prefixTag)
var current int
// Sort the writing of tags so we can at least guarantee that they will
// be in order, and testable.
var names []string
for tagName := range t {
names = append(names, tagName)
}
sort.Strings(names)
for i := 0; i < len(names); i++ {
// Trim at max allowed chars.
if (buffer.Len() + len(names[i]) + len(t[names[i]]) + 2) > maxTagLength {
return buffer.Bytes()
}
buffer.WriteString(names[i])
// Write the value as necessary.
if len(t[names[i]]) > 0 {
buffer.WriteByte(prefixTagValue)
buffer.WriteString(t[names[i]])
}
// add the separator ";" between tags.
if current < max-1 {
buffer.WriteByte(tagSeparator)
}
current++
}
return buffer.Bytes()
}
// String returns a string representation of this tag map.
func (t Tags) String() string {
if t == nil {
return ""
}
return string(t.Bytes())
}
// writeTo writes the necessary tag bytes to an io.Writer, including a trailing
// space-separator.
func (t Tags) writeTo(w io.Writer) (n int, err error) {
b := t.Bytes()
if len(b) == 0 {
return n, err
}
n, err = w.Write(b)
if err != nil {
return n, err
}
var j int
j, err = w.Write([]byte{eventSpace})
n += j
return n, err
}
// tagDecode are encoded -> decoded pairs for replacement to decode.
var tagDecode = []string{
"\\:", ";",
"\\s", " ",
"\\\\", "\\",
"\\r", "\r",
"\\n", "\n",
}
var tagDecoder = strings.NewReplacer(tagDecode...)
// tagEncode are decoded -> encoded pairs for replacement to decode.
var tagEncode = []string{
";", "\\:",
" ", "\\s",
"\\", "\\\\",
"\r", "\\r",
"\n", "\\n",
}
var tagEncoder = strings.NewReplacer(tagEncode...)
// Get returns the unescaped value of given tag key. Note that this is not
// concurrent safe.
func (t Tags) Get(key string) (tag string, success bool) {
if t == nil {
return "", false
}
if _, ok := t[key]; ok {
tag = tagDecoder.Replace(t[key])
success = true
}
return tag, success
}
// Set escapes given value and saves it as the value for given key. Note that
// this is not concurrent safe.
func (t Tags) Set(key, value string) error {
if t == nil {
t = make(Tags)
}
if !validTag(key) {
return fmt.Errorf("tag key %q is invalid", key)
}
value = tagEncoder.Replace(value)
if len(value) > 0 && !validTagValue(value) {
return fmt.Errorf("tag value %q of key %q is invalid", value, key)
}
// Check to make sure it's not too long here.
if (t.Len() + len(key) + len(value) + 2) > maxTagLength {
return fmt.Errorf("unable to set tag %q [value %q]: tags too long for message", key, value)
}
t[key] = value
return nil
}
// Remove deletes the tag frwom the tag map.
func (t Tags) Remove(key string) (success bool) {
if t == nil {
return false
}
if _, success = t[key]; success {
delete(t, key)
}
return success
}
// validTag validates an IRC tag.
func validTag(name string) bool {
if len(name) < 1 {
return false
}
// Allow user tags to be passed to validTag.
if len(name) >= 2 && name[0] == prefixUserTag {
name = name[1:]
}
for i := 0; i < len(name); i++ {
// A-Z, a-z, 0-9, -/._
if (name[i] < 'A' || name[i] > 'Z') && (name[i] < 'a' || name[i] > 'z') && (name[i] < '-' || name[i] > '9') && name[i] != '_' {
return false
}
}
return true
}
// validTagValue valids a decoded IRC tag value. If the value is not decoded
// with tagDecoder first, it may be seen as invalid.
func validTagValue(value string) bool {
for i := 0; i < len(value); i++ {
// Don't allow any invisible chars within the tag, or semicolons.
if value[i] < '!' || value[i] > '~' || value[i] == ';' {
return false
}
}
return true
}