From 6e51f3a4d0051997d01a8a95b7f64ead60cbf461 Mon Sep 17 00:00:00 2001 From: Russ Garrett Date: Fri, 1 Mar 2019 10:20:59 +0000 Subject: [PATCH] Fix issue with SASL auth on some servers On some servers SASL auth would fail because we didn't fully comply with the spec. Multiple caps can be returned in a `CAP ACK`, and sometimes servers add a trailing space. Parse these responses more correctly. --- irc_sasl.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/irc_sasl.go b/irc_sasl.go index 3346a46..fb8c292 100644 --- a/irc_sasl.go +++ b/irc_sasl.go @@ -12,15 +12,25 @@ type SASLResult struct { Err error } +// Check if a space-separated list of arguments contains a value. +func listContains(list string, value string) bool { + for _, arg_name := range strings.Split(strings.TrimSpace(list), " ") { + if arg_name == value { + return true + } + } + return false +} + func (irc *Connection) setupSASLCallbacks(result chan<- *SASLResult) { irc.AddCallback("CAP", func(e *Event) { if len(e.Arguments) == 3 { if e.Arguments[1] == "LS" { - if !strings.Contains(e.Arguments[2], "sasl") { + if !listContains(e.Arguments[2], "sasl") { result <- &SASLResult{true, errors.New("no SASL capability " + e.Arguments[2])} } } - if e.Arguments[1] == "ACK" && e.Arguments[2] == "sasl" { + if e.Arguments[1] == "ACK" && listContains(e.Arguments[2], "sasl") { if irc.SASLMech != "PLAIN" { result <- &SASLResult{true, errors.New("only PLAIN is supported")} }