From da8f626e512d67f2edf266ecc4ebf6ec4d7baac2 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 30 May 2022 09:41:47 +0200 Subject: [PATCH] xirc: add GenerateSASL --- downstream.go | 2 +- irc.go | 2 -- upstream.go | 21 ++------------------- xirc/genmsg.go | 26 ++++++++++++++++++++++++++ xirc/xirc.go | 2 ++ 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/downstream.go b/downstream.go index a008c0d..58a3f2f 100644 --- a/downstream.go +++ b/downstream.go @@ -1007,7 +1007,7 @@ func (dc *downstreamConn) handleAuthenticateCommand(msg *irc.Message) (result *d dc.sasl.pendingResp.WriteString(chunk) - if len(chunk) == maxSASLLength { + if len(chunk) == xirc.MaxSASLLength { return nil, nil // Multi-line response, wait for the next command } diff --git a/irc.go b/irc.go index fc4ee11..9ae5d4e 100644 --- a/irc.go +++ b/irc.go @@ -15,8 +15,6 @@ import ( // TODO: generalize and move helpers to the xirc package -const maxSASLLength = 400 - type userModes string func (ms userModes) Has(c byte) bool { diff --git a/upstream.go b/upstream.go index c474083..59cbfba 100644 --- a/upstream.go +++ b/upstream.go @@ -641,25 +641,8 @@ func (uc *upstreamConn) handleMessage(ctx context.Context, msg *irc.Message) err return err } - // <= instead of < because we need to send a final empty response if - // the last chunk is exactly 400 bytes long - for i := 0; i <= len(resp); i += maxSASLLength { - j := i + maxSASLLength - if j > len(resp) { - j = len(resp) - } - - chunk := resp[i:j] - - var respStr = "+" - if len(chunk) != 0 { - respStr = base64.StdEncoding.EncodeToString(chunk) - } - - uc.SendMessage(ctx, &irc.Message{ - Command: "AUTHENTICATE", - Params: []string{respStr}, - }) + for _, msg := range xirc.GenerateSASL(resp) { + uc.SendMessage(ctx, msg) } case irc.RPL_LOGGEDIN: var rawPrefix string diff --git a/xirc/genmsg.go b/xirc/genmsg.go index a4abc1c..e796e9a 100644 --- a/xirc/genmsg.go +++ b/xirc/genmsg.go @@ -1,6 +1,7 @@ package xirc import ( + "encoding/base64" "fmt" "sort" "strings" @@ -211,3 +212,28 @@ func GenerateNamesReply(prefix *irc.Prefix, nick string, channel string, status }) return msgs } + +func GenerateSASL(resp []byte) []*irc.Message { + // <= instead of < because we need to send a final empty response if + // the last chunk is exactly 400 bytes long + var msgs []*irc.Message + for i := 0; i <= len(resp); i += MaxSASLLength { + j := i + MaxSASLLength + if j > len(resp) { + j = len(resp) + } + + chunk := resp[i:j] + + var respStr = "+" + if len(chunk) != 0 { + respStr = base64.StdEncoding.EncodeToString(chunk) + } + + msgs = append(msgs, &irc.Message{ + Command: "AUTHENTICATE", + Params: []string{respStr}, + }) + } + return msgs +} diff --git a/xirc/xirc.go b/xirc/xirc.go index 0a9ac33..f321071 100644 --- a/xirc/xirc.go +++ b/xirc/xirc.go @@ -14,6 +14,8 @@ const ( maxMessageParams = 15 ) +const MaxSASLLength = 400 + const ( RPL_STATSPING = "246" RPL_LOCALUSERS = "265"