-
Notifications
You must be signed in to change notification settings - Fork 1
/
irc_caps.go
157 lines (129 loc) · 3.28 KB
/
irc_caps.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
package kitty
import (
"bytes"
"encoding/base64"
"strings"
"sync"
)
type ircCaps struct {
saslOn bool
saslUser string
saslPass string
caps []string
capsEnabled map[string]bool
mu sync.Mutex
done bool
}
func (c *ircCaps) saslEnable() {
c.mu.Lock()
c.saslOn = true
c.mu.Unlock()
}
func (c *ircCaps) saslCreds(user, pass string) {
c.mu.Lock()
c.saslUser = user
c.saslPass = pass
c.mu.Unlock()
}
func (c *ircCaps) reset() {
c.mu.Lock()
c.saslOn = false
c.done = false
c.caps = []string{}
c.capsEnabled = make(map[string]bool)
c.mu.Unlock()
}
func (c *ircCaps) isSaslAuth(m *Message) bool {
return (m.Command == "AUTHENTICATE" && m.Param(0) == "+")
}
func (c *ircCaps) isCapLS(m *Message) bool {
return m.Command == "CAP" && m.Param(1) == "LS"
}
func (c *ircCaps) isCapACK(m *Message) bool {
return m.Command == "CAP" && m.Param(1) == "ACK"
}
func (c *ircCaps) Handle(bot *Bot, m *Message) {
c.mu.Lock()
defer c.mu.Unlock()
if c.done {
return
}
if c.isCapLS(m) {
for _, cap := range strings.Split(m.Content, " ") {
if _, ok := allowedCAPs[cap]; ok {
c.capsEnabled[cap] = true
c.caps = append(c.caps, cap)
} else {
c.capsEnabled[cap] = false
}
}
bot.Send("CAP REQ :" + strings.Join(c.caps, " "))
}
if c.isCapACK(m) {
bot.Info("ircv3", "capabilities", m.Content)
if c.saslOn && strings.Contains(m.Content, "sasl") {
bot.Debug("recieved sasl ack")
bot.Send("AUTHENTICATE PLAIN")
} else {
if c.saslOn {
bot.Crit("sasl not supported")
}
bot.Send("CAP END")
c.done = true
}
}
if c.isSaslAuth(m) {
bot.Debug("got auth message")
out := bytes.Join([][]byte{[]byte(c.saslUser), []byte(c.saslUser), []byte(c.saslPass)}, []byte{0})
encpass := base64.StdEncoding.EncodeToString(out)
bot.Send("AUTHENTICATE " + encpass)
bot.Send("CAP END")
c.done = true
}
}
// Capabilities we can deal with
// without doing crazy things in the library
var allowedCAPs = map[string]struct{}{
CapAccountNotify: {},
CapAwayNotify: {},
CapExtendedJoin: {},
CapSASL: {},
CapChghost: {},
CapInviteNotify: {},
CapMultiPrefix: {},
CapCapNotify: {},
CapSetName: {},
CapServerTime: {},
CapAccountTag: {},
CapMessageTags: {},
}
// CapAccountNotify is account-notify CAP
const CapAccountNotify = "account-notify"
// CapAwayNotify is away-notify CAP
const CapAwayNotify = "away-notify"
// CapExtendedJoin is extended-join CAP
const CapExtendedJoin = "extended-join"
// CapSASL is SASL CAP
const CapSASL = "sasl"
// CapChghost is chghost CAP
const CapChghost = "chghost"
// CapInviteNotify is invite-notify CAP
const CapInviteNotify = "invite-notify"
// CapMultiPrefix is multi-prefix CAP
const CapMultiPrefix = "multi-prefix"
// CapUserhostInNames is userhost-in-names CAP
const CapUserhostInNames = "userhost-in-names"
// CapCapNotify is cap-notify CAP
const CapCapNotify = "cap-notify"
// CapIdentifyMsg is identify-msg CAP
const CapIdentifyMsg = "identify-msg"
// CapTLS is tls CAP
const CapTLS = "tls"
// CapSetName is setname CAP
const CapSetName = "setname"
// CapServerTime is server-time CAP
const CapServerTime = "server-time"
// CapAccountTag is account-tag CAP
const CapAccountTag = "account-tag"
// CapMessageTags is message-tags CAP
const CapMessageTags = "message-tags"