go-xmpp/parser.go

142 lines
3.6 KiB
Go
Raw Normal View History

package xmpp // import "gosrc.io/xmpp"
import (
"encoding/xml"
"errors"
"fmt"
"io"
)
// Reads and checks the opening XMPP stream element.
2018-01-11 22:00:59 +00:00
// TODO It returns a stream structure containing:
// - 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/> )
func initDecoder(p *xml.Decoder) (sessionID string, err error) {
for {
var t xml.Token
t, err = p.Token()
if err != nil {
return
}
switch elem := t.(type) {
case xml.StartElement:
if elem.Name.Space != NSStream || elem.Name.Local != "stream" {
err = errors.New("xmpp: expected <stream> but got <" + elem.Name.Local + "> in " + elem.Name.Space)
return
}
// Parse Stream attributes
for _, attrs := range elem.Attr {
switch attrs.Name.Local {
case "id":
sessionID = attrs.Value
}
}
return
}
}
panic("unreachable")
}
// Scan XML token stream to find next StartElement.
func nextStart(p *xml.Decoder) (xml.StartElement, error) {
for {
t, err := p.Token()
if err == io.EOF {
return xml.StartElement{}, nil
}
if err != nil {
return xml.StartElement{}, fmt.Errorf("nextStart %s", err)
}
switch t := t.(type) {
case xml.StartElement:
return t, nil
}
}
panic("unreachable")
}
2018-01-13 16:46:10 +00:00
// next scans XML token stream for next element and then assign a structure to decode
// that elements.
2018-01-12 18:08:47 +00:00
// TODO Use an interface to return packets interface xmppDecoder
2018-01-13 17:50:17 +00:00
func next(p *xml.Decoder) (Packet, error) {
// Read start element to find out how we want to parse the XMPP packet
se, err := nextStart(p)
if err != nil {
2018-01-13 17:50:17 +00:00
return nil, err
}
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)
2018-01-13 16:46:10 +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)
default:
2018-01-13 17:50:17 +00:00
return nil, errors.New("unknown namespace " +
se.Name.Space + " <" + se.Name.Local + "/>")
}
}
2018-01-13 16:46:10 +00:00
2018-01-13 17:50:17 +00:00
func decodeStream(p *xml.Decoder, se xml.StartElement) (Packet, error) {
2018-01-13 16:46:10 +00:00
switch se.Name.Local {
case "error":
2018-01-13 17:50:17 +00:00
return streamError.decode(p, se)
case "features":
return streamFeatures.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 + "/>")
}
}
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 + "/>")
}
}
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 + "/>")
}
}
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 {
case "handshake":
2018-01-13 17:50:17 +00:00
return handshake.decode(p, se)
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 + "/>")
}
}