aboutsummaryrefslogtreecommitdiffstats
path: root/hbot/irc_caps.go
blob: b5b881fed0a669dd46c6337faf8245051455e5f8 (plain) (blame)
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
package hbot

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) saslAuth(m *Message) bool {
	return m.Command == "AUTHENTICATE" && m.Param(0) == "+"
}

func (c *ircCaps) capLS(m *Message) bool {
	return m.Command == "CAP" && m.Param(1) == "LS"
}

func (c *ircCaps) capACK(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.capLS(m) {
		for _, cap := range strings.Split(m.Trailing(), " ") {
			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.capACK(m) {
		if c.saslOn && strings.Contains(m.Trailing(), "sasl") {
			bot.Send("AUTHENTICATE PLAIN")
		} else {
			if c.saslOn {
				bot.config.Logger.Errorf("SASL is not supported, despite request")
			}
			bot.Send("CAP END")
			c.done = true
		}
	}

	if c.saslAuth(m) {
		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"