2019-06-26 15:14:52 +00:00
|
|
|
package stanza
|
2016-01-06 15:51:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
2017-10-21 13:14:13 +00:00
|
|
|
"fmt"
|
2016-01-06 15:51:12 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reads and checks the opening XMPP stream element.
|
2018-01-11 22:00:59 +00:00
|
|
|
// TODO It returns a stream structure containing:
|
2016-01-06 15:51:12 +00:00
|
|
|
// - Host: You can check the host against the host you were expecting to connect to
|
|
|
|
// - Id: the Stream ID is a temporary shared secret used for some hash calculation. It is also used by ProcessOne
|
|
|
|
// reattach features (allowing to resume an existing stream at the point the connection was interrupted, without
|
|
|
|
// getting through the authentication process.
|
2018-01-11 22:00:59 +00:00
|
|
|
// TODO We should handle stream error from XEP-0114 ( <conflict/> or <host-unknown/> )
|
2019-06-26 15:14:52 +00:00
|
|
|
func InitStream(p *xml.Decoder) (sessionID string, err error) {
|
2016-01-06 15:51:12 +00:00
|
|
|
for {
|
|
|
|
var t xml.Token
|
|
|
|
t, err = p.Token()
|
|
|
|
if err != nil {
|
2019-06-22 09:13:33 +00:00
|
|
|
return sessionID, err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch elem := t.(type) {
|
|
|
|
case xml.StartElement:
|
2019-10-25 13:55:27 +00:00
|
|
|
isStreamOpen := elem.Name.Space == NSStream && elem.Name.Local == "stream"
|
|
|
|
isFrameOpen := elem.Name.Space == NSFraming && elem.Name.Local == "open"
|
|
|
|
if !isStreamOpen && !isFrameOpen {
|
|
|
|
err = errors.New("xmpp: expected <stream> or <open> but got <" + elem.Name.Local + "> in " + elem.Name.Space)
|
2019-06-22 09:13:33 +00:00
|
|
|
return sessionID, err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
// Parse XMPP stream attributes
|
2016-01-06 15:51:12 +00:00
|
|
|
for _, attrs := range elem.Attr {
|
|
|
|
switch attrs.Name.Local {
|
|
|
|
case "id":
|
|
|
|
sessionID = attrs.Value
|
|
|
|
}
|
|
|
|
}
|
2019-06-22 09:13:33 +00:00
|
|
|
return sessionID, err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 15:14:52 +00:00
|
|
|
// NextPacket scans XML token stream for next complete XMPP stanza.
|
2019-06-22 09:13:33 +00:00
|
|
|
// Once the type of stanza has been identified, a structure is created to decode
|
|
|
|
// that stanza and returned.
|
2018-01-12 18:08:47 +00:00
|
|
|
// TODO Use an interface to return packets interface xmppDecoder
|
2019-06-26 15:14:52 +00:00
|
|
|
// TODO make auth and bind use NextPacket instead of directly NextStart
|
|
|
|
func NextPacket(p *xml.Decoder) (Packet, error) {
|
2018-01-13 17:50:17 +00:00
|
|
|
// Read start element to find out how we want to parse the XMPP packet
|
2019-12-26 13:47:02 +00:00
|
|
|
t, err := NextXmppToken(p)
|
2016-01-06 15:51:12 +00:00
|
|
|
if err != nil {
|
2018-01-13 17:50:17 +00:00
|
|
|
return nil, err
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-26 13:47:02 +00:00
|
|
|
if ee, ok := t.(xml.EndElement); ok {
|
|
|
|
return decodeStream(p, ee)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not an end element, then must be a start
|
|
|
|
se, ok := t.(xml.StartElement)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("unknown token ")
|
|
|
|
}
|
2018-01-23 08:08:21 +00:00
|
|
|
// Decode one of the top level XMPP namespace
|
2018-01-13 16:46:10 +00:00
|
|
|
switch se.Name.Space {
|
|
|
|
case NSStream:
|
2018-01-13 17:50:17 +00:00
|
|
|
return decodeStream(p, se)
|
2019-06-26 15:14:52 +00:00
|
|
|
case NSSASL:
|
2018-01-13 17:50:17 +00:00
|
|
|
return decodeSASL(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case NSClient:
|
2018-01-13 17:50:17 +00:00
|
|
|
return decodeClient(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case NSComponent:
|
2018-01-13 17:50:17 +00:00
|
|
|
return decodeComponent(p, se)
|
2019-07-31 16:47:30 +00:00
|
|
|
case NSStreamManagement:
|
|
|
|
return sm.decode(p, se)
|
2016-01-06 15:51:12 +00:00
|
|
|
default:
|
2018-01-13 17:50:17 +00:00
|
|
|
return nil, errors.New("unknown namespace " +
|
2016-01-06 15:51:12 +00:00
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 16:46:10 +00:00
|
|
|
|
2019-12-26 13:47:02 +00:00
|
|
|
// NextXmppToken scans XML token stream to find next StartElement or stream EndElement.
|
|
|
|
// We need the EndElement scan, because we must register stream close tags
|
|
|
|
func NextXmppToken(p *xml.Decoder) (xml.Token, error) {
|
|
|
|
for {
|
|
|
|
t, err := p.Token()
|
|
|
|
if err == io.EOF {
|
|
|
|
return xml.StartElement{}, errors.New("connection closed")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return xml.StartElement{}, fmt.Errorf("NextStart %s", err)
|
|
|
|
}
|
|
|
|
switch t := t.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
return t, nil
|
|
|
|
case xml.EndElement:
|
|
|
|
if t.Name.Space == NSStream && t.Name.Local == "stream" {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextStart scans XML token stream to find next StartElement.
|
2019-06-26 15:14:52 +00:00
|
|
|
func NextStart(p *xml.Decoder) (xml.StartElement, error) {
|
|
|
|
for {
|
|
|
|
t, err := p.Token()
|
|
|
|
if err == io.EOF {
|
|
|
|
return xml.StartElement{}, errors.New("connection closed")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return xml.StartElement{}, fmt.Errorf("NextStart %s", err)
|
|
|
|
}
|
|
|
|
switch t := t.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
/*
|
|
|
|
TODO: From all the decoder, we can return a pointer to the actual concrete type, instead of directly that
|
|
|
|
type.
|
|
|
|
That way, we have a consistent way to do type assertion, always matching against pointers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// decodeStream will fully decode a stream packet
|
2019-12-26 13:47:02 +00:00
|
|
|
func decodeStream(p *xml.Decoder, t xml.Token) (Packet, error) {
|
|
|
|
if se, ok := t.(xml.StartElement); ok {
|
|
|
|
switch se.Name.Local {
|
|
|
|
case "error":
|
|
|
|
return streamError.decode(p, se)
|
|
|
|
case "features":
|
|
|
|
return streamFeatures.decode(p, se)
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected XMPP packet " +
|
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ee, ok := t.(xml.EndElement); ok {
|
|
|
|
if ee.Name.Local == "stream" {
|
|
|
|
return streamClose.decode(ee), nil
|
|
|
|
}
|
2018-01-13 16:46:10 +00:00
|
|
|
return nil, errors.New("unexpected XMPP packet " +
|
2019-12-26 13:47:02 +00:00
|
|
|
ee.Name.Space + " <" + ee.Name.Local + "/>")
|
2018-01-13 16:46:10 +00:00
|
|
|
}
|
2019-12-26 13:47:02 +00:00
|
|
|
|
|
|
|
// Should not happen
|
|
|
|
return nil, errors.New("unexpected XML token ")
|
2018-01-13 16:46:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
// decodeSASL decodes a packet related to SASL authentication.
|
2018-01-13 17:50:17 +00:00
|
|
|
func decodeSASL(p *xml.Decoder, se xml.StartElement) (Packet, error) {
|
2018-01-13 16:46:10 +00:00
|
|
|
switch se.Name.Local {
|
|
|
|
case "success":
|
2018-01-13 17:50:17 +00:00
|
|
|
return saslSuccess.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case "failure":
|
2018-01-13 17:50:17 +00:00
|
|
|
return saslFailure.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected XMPP packet " +
|
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
// decodeClient decodes all known packets in the client namespace.
|
2018-01-13 17:50:17 +00:00
|
|
|
func decodeClient(p *xml.Decoder, se xml.StartElement) (Packet, error) {
|
2018-01-13 16:46:10 +00:00
|
|
|
switch se.Name.Local {
|
|
|
|
case "message":
|
2018-01-13 17:50:17 +00:00
|
|
|
return message.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case "presence":
|
2018-01-13 17:50:17 +00:00
|
|
|
return presence.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case "iq":
|
2018-01-13 17:50:17 +00:00
|
|
|
return iq.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected XMPP packet " +
|
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-31 16:47:30 +00:00
|
|
|
// decodeComponent decodes all known packets in the component namespace.
|
2018-01-13 17:50:17 +00:00
|
|
|
func decodeComponent(p *xml.Decoder, se xml.StartElement) (Packet, error) {
|
2018-01-13 16:46:10 +00:00
|
|
|
switch se.Name.Local {
|
2019-06-22 09:13:33 +00:00
|
|
|
case "handshake": // handshake is used to authenticate components
|
2018-01-13 17:50:17 +00:00
|
|
|
return handshake.decode(p, se)
|
2018-01-23 07:55:15 +00:00
|
|
|
case "message":
|
|
|
|
return message.decode(p, se)
|
|
|
|
case "presence":
|
|
|
|
return presence.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
case "iq":
|
2018-01-13 17:50:17 +00:00
|
|
|
return iq.decode(p, se)
|
2018-01-13 16:46:10 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unexpected XMPP packet " +
|
|
|
|
se.Name.Space + " <" + se.Name.Local + "/>")
|
|
|
|
}
|
|
|
|
}
|