Catch stream error after bind request.

This commit is contained in:
Martin Dosch 2024-03-10 13:44:33 +01:00
parent 9c5e758356
commit 73f06c9f3d

31
xmpp.go
View File

@ -753,16 +753,27 @@ func (c *Client) init(o *Options) error {
} else { } else {
fmt.Fprintf(c.stanzaWriter, "<iq type='set' id='%x'><bind xmlns='%s'><resource>%s</resource></bind></iq>\n", cookie, nsBind, o.Resource) fmt.Fprintf(c.stanzaWriter, "<iq type='set' id='%x'><bind xmlns='%s'><resource>%s</resource></bind></iq>\n", cookie, nsBind, o.Resource)
} }
var iq clientIQ _, val, err = next(c.p)
if err = c.p.DecodeElement(&iq, nil); err != nil { if err != nil {
return errors.New("unmarshal <iq>: " + err.Error()) return err
} }
if &iq.Bind == nil { switch v := val.(type) {
return errors.New("<iq> result missing <bind>") 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
} }
c.jid = iq.Bind.Jid // our local id 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 c.domain = domain
} else {
return errors.New("bind: unexpected reply to xmpp-bind IQ")
}
}
if o.Session { if o.Session {
// if server support session, open it // if server support session, open it
fmt.Fprintf(c.stanzaWriter, "<iq to='%s' type='set' id='%x'><session xmlns='%s'/></iq>\n", xmlEscape(domain), cookie, nsSession) fmt.Fprintf(c.stanzaWriter, "<iq to='%s' type='set' id='%x'><session xmlns='%s'/></iq>\n", xmlEscape(domain), cookie, nsSession)
@ -1271,7 +1282,11 @@ type streamFeatures struct {
type streamError struct { type streamError struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"` XMLName xml.Name `xml:"http://etherx.jabber.org/streams error"`
Any xml.Name 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 // RFC 3920 C.3 TLS name space