2019-06-26 15:14:52 +00:00
|
|
|
package stanza
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2016-02-15 10:08:54 +00:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
)
|
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
/*
|
2018-02-13 21:07:15 +00:00
|
|
|
TODO support ability to put Raw payload inside IQ
|
2018-01-15 11:28:34 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
// ============================================================================
|
|
|
|
// IQ Packet
|
|
|
|
|
2019-06-22 09:13:33 +00:00
|
|
|
// IQ implements RFC 6120 - A.5 Client Namespace (a part)
|
2018-01-13 17:50:17 +00:00
|
|
|
type IQ struct { // Info/Query
|
|
|
|
XMLName xml.Name `xml:"iq"`
|
2019-06-22 09:13:33 +00:00
|
|
|
// MUST have a ID
|
|
|
|
Attrs
|
2019-06-19 08:21:57 +00:00
|
|
|
// We can only have one payload on IQ:
|
2019-06-13 15:22:39 +00: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 08:21:57 +00:00
|
|
|
Payload IQPayload `xml:",omitempty"`
|
|
|
|
Error Err `xml:"error,omitempty"`
|
2019-06-24 10:15:29 +00:00
|
|
|
// Any is used to decode unknown payload as a generique structure
|
|
|
|
Any *Node `xml:",any"`
|
2016-01-06 15:51:12 +00:00
|
|
|
}
|
2016-02-15 14:22:51 +00:00
|
|
|
|
2019-06-22 09:13:33 +00: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 11:28:34 +00:00
|
|
|
return IQ{
|
|
|
|
XMLName: xml.Name{Local: "iq"},
|
2019-06-22 09:13:33 +00:00
|
|
|
Attrs: a,
|
2018-01-15 11:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 17:56:07 +00: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 17:50:17 +00: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 14:22:51 +00:00
|
|
|
// UnmarshalXML implements custom parsing for IQs
|
2018-01-13 17:50:17 +00:00
|
|
|
func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2016-02-15 14:22:51 +00:00
|
|
|
iq.XMLName = start.Name
|
2018-01-18 16:03:54 +00:00
|
|
|
|
2016-02-15 14:22:51 +00:00
|
|
|
// Extract IQ attributes
|
|
|
|
for _, attr := range start.Attr {
|
|
|
|
if attr.Name.Local == "id" {
|
|
|
|
iq.Id = attr.Value
|
|
|
|
}
|
2016-02-15 17:33:51 +00:00
|
|
|
if attr.Name.Local == "type" {
|
2019-06-22 09:13:33 +00:00
|
|
|
iq.Type = StanzaType(attr.Value)
|
2016-02-15 17:33:51 +00:00
|
|
|
}
|
2016-02-15 14:22:51 +00: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 11:46:02 +00:00
|
|
|
if tt.Name.Local == "error" {
|
|
|
|
var xmppError Err
|
|
|
|
err = d.DecodeElement(&xmppError, &tt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-15 14:22:51 +00:00
|
|
|
}
|
2019-06-19 11:46:02 +00:00
|
|
|
iq.Error = xmppError
|
|
|
|
continue
|
2016-02-15 14:22:51 +00:00
|
|
|
}
|
2019-06-19 11:46:02 +00: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 09:13:33 +00:00
|
|
|
// TODO: If unknown decode as generic node
|
2019-06-24 10:15:29 +00:00
|
|
|
node := new(Node)
|
|
|
|
err = d.DecodeElement(node, &tt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iq.Any = node
|
2016-02-15 14:22:51 +00:00
|
|
|
case xml.EndElement:
|
|
|
|
if tt == start.End() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|