2019-06-18 07:28:30 -07:00
|
|
|
package xmpp
|
2016-01-06 07:51:12 -08:00
|
|
|
|
2016-02-15 02:08:54 -08:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2019-06-19 04:46:02 -07:00
|
|
|
"fmt"
|
2016-02-15 02:08:54 -08:00
|
|
|
)
|
|
|
|
|
2018-01-15 03:28:34 -08:00
|
|
|
/*
|
2018-02-13 13:07:15 -08:00
|
|
|
TODO support ability to put Raw payload inside IQ
|
2018-01-15 03:28:34 -08:00
|
|
|
*/
|
|
|
|
|
2018-01-13 09:50:17 -08:00
|
|
|
// ============================================================================
|
|
|
|
// IQ Packet
|
|
|
|
|
2019-06-22 02:13:33 -07:00
|
|
|
// IQ implements RFC 6120 - A.5 Client Namespace (a part)
|
2018-01-13 09:50:17 -08:00
|
|
|
type IQ struct { // Info/Query
|
|
|
|
XMLName xml.Name `xml:"iq"`
|
2019-06-22 02:13:33 -07:00
|
|
|
// MUST have a ID
|
|
|
|
Attrs
|
2019-06-19 01:21:57 -07:00
|
|
|
// We can only have one payload on IQ:
|
2019-06-13 08:22:39 -07:00
|
|
|
// "An IQ stanza of type "get" or "set" MUST contain exactly one
|
|
|
|
// child element, which specifies the semantics of the particular
|
|
|
|
// request."
|
2019-06-19 01:21:57 -07:00
|
|
|
Payload IQPayload `xml:",omitempty"`
|
|
|
|
Error Err `xml:"error,omitempty"`
|
|
|
|
RawXML string `xml:",innerxml"`
|
2016-01-06 07:51:12 -08:00
|
|
|
}
|
2016-02-15 06:22:51 -08:00
|
|
|
|
2019-06-22 02:13:33 -07:00
|
|
|
type IQPayload interface {
|
|
|
|
Namespace() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIQ(a Attrs) IQ {
|
|
|
|
// TODO generate IQ ID if not set
|
|
|
|
// TODO ensure that type is set, as it is required
|
2018-01-15 03:28:34 -08:00
|
|
|
return IQ{
|
|
|
|
XMLName: xml.Name{Local: "iq"},
|
2019-06-22 02:13:33 -07:00
|
|
|
Attrs: a,
|
2018-01-15 03:28:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 09:56:07 -08:00
|
|
|
func (iq IQ) MakeError(xerror Err) IQ {
|
|
|
|
from := iq.From
|
|
|
|
to := iq.To
|
|
|
|
|
|
|
|
iq.Type = "error"
|
|
|
|
iq.From = to
|
|
|
|
iq.To = from
|
|
|
|
iq.Error = xerror
|
|
|
|
|
|
|
|
return iq
|
|
|
|
}
|
|
|
|
|
2018-01-13 09:50:17 -08:00
|
|
|
func (IQ) Name() string {
|
|
|
|
return "iq"
|
|
|
|
}
|
|
|
|
|
|
|
|
type iqDecoder struct{}
|
|
|
|
|
|
|
|
var iq iqDecoder
|
|
|
|
|
|
|
|
func (iqDecoder) decode(p *xml.Decoder, se xml.StartElement) (IQ, error) {
|
|
|
|
var packet IQ
|
|
|
|
err := p.DecodeElement(&packet, &se)
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
2016-02-15 06:22:51 -08:00
|
|
|
// UnmarshalXML implements custom parsing for IQs
|
2018-01-13 09:50:17 -08:00
|
|
|
func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2016-02-15 06:22:51 -08:00
|
|
|
iq.XMLName = start.Name
|
2018-01-18 08:03:54 -08:00
|
|
|
|
2016-02-15 06:22:51 -08:00
|
|
|
// Extract IQ attributes
|
|
|
|
for _, attr := range start.Attr {
|
|
|
|
if attr.Name.Local == "id" {
|
|
|
|
iq.Id = attr.Value
|
|
|
|
}
|
2016-02-15 09:33:51 -08:00
|
|
|
if attr.Name.Local == "type" {
|
2019-06-22 02:13:33 -07:00
|
|
|
iq.Type = StanzaType(attr.Value)
|
2016-02-15 09:33:51 -08:00
|
|
|
}
|
2016-02-15 06:22:51 -08:00
|
|
|
if attr.Name.Local == "to" {
|
|
|
|
iq.To = attr.Value
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "from" {
|
|
|
|
iq.From = attr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode inner elements
|
|
|
|
for {
|
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tt := t.(type) {
|
|
|
|
case xml.StartElement:
|
2019-06-19 04:46:02 -07:00
|
|
|
if tt.Name.Local == "error" {
|
|
|
|
var xmppError Err
|
|
|
|
err = d.DecodeElement(&xmppError, &tt)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
2016-02-15 06:22:51 -08:00
|
|
|
}
|
2019-06-19 04:46:02 -07:00
|
|
|
iq.Error = xmppError
|
|
|
|
continue
|
2016-02-15 06:22:51 -08:00
|
|
|
}
|
2019-06-19 04:46:02 -07:00
|
|
|
if iqExt := TypeRegistry.GetIQExtension(tt.Name); iqExt != nil {
|
|
|
|
// Decode payload extension
|
|
|
|
err = d.DecodeElement(iqExt, &tt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iq.Payload = iqExt
|
|
|
|
continue
|
|
|
|
}
|
2019-06-22 02:13:33 -07:00
|
|
|
// TODO: If unknown decode as generic node
|
2019-06-19 04:46:02 -07:00
|
|
|
return fmt.Errorf("unexpected element in iq: %s %s", tt.Name.Space, tt.Name.Local)
|
2016-02-15 06:22:51 -08:00
|
|
|
case xml.EndElement:
|
|
|
|
if tt == start.End() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 07:33:27 -08:00
|
|
|
|
2018-01-13 10:14:26 -08:00
|
|
|
// ============================================================================
|
2019-06-22 02:13:33 -07:00
|
|
|
// Generic / unknown content
|
2018-01-13 10:14:26 -08:00
|
|
|
|
2018-01-25 08:04:19 -08:00
|
|
|
// Node is a generic structure to represent XML data. It is used to parse
|
|
|
|
// unreferenced or custom stanza payload.
|
2018-01-13 10:27:46 -08:00
|
|
|
type Node struct {
|
|
|
|
XMLName xml.Name
|
2018-01-15 03:28:34 -08:00
|
|
|
Attrs []xml.Attr `xml:"-"`
|
2018-01-20 09:09:13 -08:00
|
|
|
Content string `xml:",innerxml"`
|
|
|
|
Nodes []Node `xml:",any"`
|
2018-01-15 03:28:34 -08:00
|
|
|
}
|
|
|
|
|
2019-06-13 08:22:39 -07:00
|
|
|
func (n *Node) Namespace() string {
|
|
|
|
return n.XMLName.Space
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:04:19 -08:00
|
|
|
// Attr represents generic XML attributes, as used on the generic XML Node
|
|
|
|
// representation.
|
2018-01-16 13:33:21 -08:00
|
|
|
type Attr struct {
|
|
|
|
K string
|
|
|
|
V string
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:04:19 -08:00
|
|
|
// UnmarshalXML is a custom unmarshal function used by xml.Unmarshal to
|
|
|
|
// transform generic XML content into hierarchical Node structure.
|
2018-01-15 03:28:34 -08:00
|
|
|
func (n *Node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2018-01-16 13:33:21 -08:00
|
|
|
// Assign "n.Attrs = start.Attr", without repeating xmlns in attributes:
|
2018-01-15 03:47:26 -08:00
|
|
|
for _, attr := range start.Attr {
|
2018-01-16 13:33:21 -08:00
|
|
|
// Do not repeat xmlns, it is already in XMLName
|
2018-01-15 03:47:26 -08:00
|
|
|
if attr.Name.Local != "xmlns" {
|
|
|
|
n.Attrs = append(n.Attrs, attr)
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 03:28:34 -08:00
|
|
|
type node Node
|
|
|
|
return d.DecodeElement((*node)(n), &start)
|
|
|
|
}
|
|
|
|
|
2018-01-25 08:04:19 -08:00
|
|
|
// MarshalXML is a custom XML serializer used by xml.Marshal to serialize a
|
|
|
|
// Node structure to XML.
|
2018-01-20 09:09:13 -08:00
|
|
|
func (n Node) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
|
2018-01-15 03:28:34 -08:00
|
|
|
start.Attr = n.Attrs
|
|
|
|
start.Name = n.XMLName
|
|
|
|
|
|
|
|
err = e.EncodeToken(start)
|
|
|
|
e.EncodeElement(n.Nodes, xml.StartElement{Name: n.XMLName})
|
|
|
|
return e.EncodeToken(xml.EndElement{Name: start.Name})
|
2018-01-13 10:14:26 -08:00
|
|
|
}
|
|
|
|
|
2018-01-16 13:33:21 -08:00
|
|
|
// ============================================================================
|
|
|
|
// Disco
|
|
|
|
|
2018-01-20 09:09:13 -08:00
|
|
|
const (
|
2018-01-26 00:24:34 -08:00
|
|
|
NSDiscoInfo = "http://jabber.org/protocol/disco#info"
|
|
|
|
NSDiscoItems = "http://jabber.org/protocol/disco#items"
|
2018-01-20 09:09:13 -08:00
|
|
|
)
|
|
|
|
|
2019-06-10 02:56:07 -07:00
|
|
|
// Disco Info
|
2018-01-16 13:33:21 -08:00
|
|
|
type DiscoInfo struct {
|
2018-01-17 09:47:34 -08:00
|
|
|
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
|
2018-01-26 02:40:34 -08:00
|
|
|
Node string `xml:"node,attr,omitempty"`
|
2018-01-17 09:47:34 -08:00
|
|
|
Identity Identity `xml:"identity"`
|
|
|
|
Features []Feature `xml:"feature"`
|
2018-01-16 13:33:21 -08:00
|
|
|
}
|
|
|
|
|
2019-06-13 08:22:39 -07:00
|
|
|
func (d *DiscoInfo) Namespace() string {
|
|
|
|
return d.XMLName.Space
|
|
|
|
}
|
|
|
|
|
2018-01-16 13:33:21 -08:00
|
|
|
type Identity struct {
|
2018-01-17 09:47:34 -08:00
|
|
|
XMLName xml.Name `xml:"identity,omitempty"`
|
|
|
|
Name string `xml:"name,attr,omitempty"`
|
|
|
|
Category string `xml:"category,attr,omitempty"`
|
|
|
|
Type string `xml:"type,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Feature struct {
|
|
|
|
XMLName xml.Name `xml:"feature"`
|
|
|
|
Var string `xml:"var,attr"`
|
2018-01-16 13:33:21 -08:00
|
|
|
}
|
|
|
|
|
2019-06-10 02:56:07 -07:00
|
|
|
// Disco Items
|
2018-01-26 00:24:34 -08:00
|
|
|
type DiscoItems struct {
|
|
|
|
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#items query"`
|
|
|
|
Node string `xml:"node,attr,omitempty"`
|
|
|
|
Items []DiscoItem `xml:"item"`
|
|
|
|
}
|
|
|
|
|
2019-06-13 08:22:39 -07:00
|
|
|
func (d *DiscoItems) Namespace() string {
|
|
|
|
return d.XMLName.Space
|
|
|
|
}
|
|
|
|
|
2018-01-26 00:24:34 -08:00
|
|
|
type DiscoItem struct {
|
|
|
|
XMLName xml.Name `xml:"item"`
|
|
|
|
Name string `xml:"name,attr,omitempty"`
|
|
|
|
JID string `xml:"jid,attr,omitempty"`
|
|
|
|
Node string `xml:"node,attr,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:56:07 -07:00
|
|
|
// ============================================================================
|
|
|
|
// Software Version (XEP-0092)
|
|
|
|
|
|
|
|
// Version
|
|
|
|
type Version struct {
|
|
|
|
XMLName xml.Name `xml:"jabber:iq:version query"`
|
|
|
|
Name string `xml:"name,omitempty"`
|
|
|
|
Version string `xml:"version,omitempty"`
|
|
|
|
OS string `xml:"os,omitempty"`
|
|
|
|
}
|
2019-06-10 03:30:01 -07:00
|
|
|
|
2019-06-13 08:22:39 -07:00
|
|
|
func (v *Version) Namespace() string {
|
|
|
|
return v.XMLName.Space
|
|
|
|
}
|
|
|
|
|
2019-06-10 03:30:01 -07:00
|
|
|
// ============================================================================
|
|
|
|
// Registry init
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoInfo, "query"}, DiscoInfo{})
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{NSDiscoItems, "query"}, DiscoItems{})
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{"urn:ietf:params:xml:ns:xmpp-bind", "bind"}, BindBind{})
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{"urn:xmpp:iot:control", "set"}, ControlSet{})
|
|
|
|
TypeRegistry.MapExtension(PKTIQ, xml.Name{"jabber:iq:version", "query"}, Version{})
|
|
|
|
}
|