xirc: add GenerateSASL

This commit is contained in:
Simon Ser 2022-05-30 09:41:47 +02:00
parent f9c4ba636f
commit da8f626e51
5 changed files with 31 additions and 22 deletions

View file

@ -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
}

2
irc.go
View file

@ -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 {

View file

@ -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

View file

@ -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
}

View file

@ -14,6 +14,8 @@ const (
maxMessageParams = 15
)
const MaxSASLLength = 400
const (
RPL_STATSPING = "246"
RPL_LOCALUSERS = "265"