From 73f06c9f3d1fc0e1e4c5eb78ace5d2c89a47ac4f Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Sun, 10 Mar 2024 13:44:33 +0100 Subject: [PATCH] Catch stream error after bind request. --- xmpp.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/xmpp.go b/xmpp.go index 5e06275..463c57d 100644 --- a/xmpp.go +++ b/xmpp.go @@ -753,16 +753,27 @@ func (c *Client) init(o *Options) error { } else { fmt.Fprintf(c.stanzaWriter, "%s\n", cookie, nsBind, o.Resource) } - var iq clientIQ - if err = c.p.DecodeElement(&iq, nil); err != nil { - return errors.New("unmarshal : " + err.Error()) + _, val, err = next(c.p) + if err != nil { + return err } - if &iq.Bind == nil { - return errors.New(" result missing ") + switch v := val.(type) { + case *streamError: + errorMessage := v.Text.Text + if errorMessage == "" { + // v.Any is type of sub-element in failure, + // which gives a description of what failed if there was no text element + errorMessage = v.Any.Space + } + return errors.New("stream error: " + errorMessage) + case *clientIQ: + if v.Bind.XMLName.Space == nsBind { + c.jid = v.Bind.Jid // our local id + c.domain = domain + } else { + return errors.New("bind: unexpected reply to xmpp-bind IQ") + } } - c.jid = iq.Bind.Jid // our local id - c.domain = domain - if o.Session { // if server support session, open it fmt.Fprintf(c.stanzaWriter, "\n", xmlEscape(domain), cookie, nsSession) @@ -1271,7 +1282,11 @@ type streamFeatures struct { type streamError struct { XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"` Any xml.Name - Text string + Text struct { + Text string `xml:",chardata"` + Lang string `xml:"lang,attr"` + Xmlns string `xml:"xmlns,attr"` + } `xml:"text"` } // RFC 3920 C.3 TLS name space