fix SASL stalling due to duplicated callbacks

Each reconnection would add redundant callbacks for CAP and the
SASL numerics. This would result in `AUTHENTICATE PLAIN` being sent
multiple times (once for each callback), which would confuse the server.
This commit is contained in:
Shivaram Lingamneni
2021-02-11 10:40:29 -05:00
parent 8e7ce4b5a1
commit ca8c401467
4 changed files with 43 additions and 15 deletions

View File

@@ -22,8 +22,8 @@ func listContains(list string, value string) bool {
return false
}
func (irc *Connection) setupSASLCallbacks(result chan<- *SASLResult) {
irc.AddCallback("CAP", func(e *Event) {
func (irc *Connection) setupSASLCallbacks(result chan<- *SASLResult) (callbacks []CallbackID) {
id := irc.AddCallback("CAP", func(e *Event) {
if len(e.Arguments) == 3 {
if e.Arguments[1] == "LS" {
if !listContains(e.Arguments[2], "sasl") {
@@ -38,26 +38,39 @@ func (irc *Connection) setupSASLCallbacks(result chan<- *SASLResult) {
}
}
})
irc.AddCallback("AUTHENTICATE", func(e *Event) {
callbacks = append(callbacks, CallbackID{"CAP", id})
id = irc.AddCallback("AUTHENTICATE", func(e *Event) {
str := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s\x00%s\x00%s", irc.SASLLogin, irc.SASLLogin, irc.SASLPassword)))
irc.SendRaw("AUTHENTICATE " + str)
})
irc.AddCallback("901", func(e *Event) {
callbacks = append(callbacks, CallbackID{"AUTHENTICATE", id})
id = irc.AddCallback("901", func(e *Event) {
irc.SendRaw("CAP END")
irc.SendRaw("QUIT")
result <- &SASLResult{true, errors.New(e.Arguments[1])}
})
irc.AddCallback("902", func(e *Event) {
callbacks = append(callbacks, CallbackID{"901", id})
id = irc.AddCallback("902", func(e *Event) {
irc.SendRaw("CAP END")
irc.SendRaw("QUIT")
result <- &SASLResult{true, errors.New(e.Arguments[1])}
})
irc.AddCallback("903", func(e *Event) {
callbacks = append(callbacks, CallbackID{"902", id})
id = irc.AddCallback("903", func(e *Event) {
result <- &SASLResult{false, nil}
})
irc.AddCallback("904", func(e *Event) {
callbacks = append(callbacks, CallbackID{"903", id})
id = irc.AddCallback("904", func(e *Event) {
irc.SendRaw("CAP END")
irc.SendRaw("QUIT")
result <- &SASLResult{true, errors.New(e.Arguments[1])}
})
callbacks = append(callbacks, CallbackID{"904", id})
return
}