-
Notifications
You must be signed in to change notification settings - Fork 42
/
sasl.go
69 lines (57 loc) · 1.54 KB
/
sasl.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
package hbot
import (
"bytes"
"encoding/base64"
"strings"
"sync"
)
type saslAuth struct {
mu sync.Mutex
enable bool
user string
pass string
}
func (h *saslAuth) SetAuth(user, pass string) {
h.mu.Lock()
defer h.mu.Unlock()
h.user = user
h.pass = pass
}
func (h *saslAuth) IsAuthMessage(m *Message) bool {
return (strings.TrimSpace(m.Content) == "sasl" && m.Param(1) == "ACK") ||
(m.Command == "AUTHENTICATE" && m.Param(0) == "+") ||
(m.Command == "903" || m.Command == "904")
}
func (h *saslAuth) Handle(bot *Bot, m *Message) bool {
h.mu.Lock()
defer h.mu.Unlock()
if !h.IsAuthMessage(m) {
return false
}
if strings.TrimSpace(m.Content) == "sasl" && m.Param(1) == "ACK" {
bot.Debug("Recieved SASL ACK")
bot.Send("AUTHENTICATE PLAIN")
}
if m.Command == "AUTHENTICATE" && m.Param(0) == "+" {
bot.Debug("Got auth message!")
out := bytes.Join([][]byte{[]byte(h.user), []byte(h.user), []byte(h.pass)}, []byte{0})
encpass := base64.StdEncoding.EncodeToString(out)
bot.Send("AUTHENTICATE " + encpass)
}
// 903 RPL_SASLSUCCESS
// 904 ERR_SASLFAIL
if m.Command == "903" || m.Command == "904" {
bot.Send("CAP END")
}
return false
}
// SASLAuthenticate performs SASL authentication
// ref: https://github.com/atheme/charybdis/blob/master/doc/sasl.txt
func (bot *Bot) SASLAuthenticate(user, pass string) {
bot.sasl.SetAuth(user, pass)
bot.addSASL.Do(func() { bot.AddTrigger(bot.sasl) })
bot.Debug("Beginning SASL Authentication")
bot.Send("CAP REQ :sasl")
bot.SetNick(bot.Nick)
bot.sendUserCommand(bot.Nick, bot.Nick)
}