mirror of https://github.com/FluuxIO/go-xmpp
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
2.5 KiB
Go
131 lines
2.5 KiB
Go
5 years ago
|
package stanza
|
||
9 years ago
|
|
||
9 years ago
|
import (
|
||
|
"encoding/xml"
|
||
5 years ago
|
"fmt"
|
||
9 years ago
|
)
|
||
|
|
||
7 years ago
|
/*
|
||
7 years ago
|
TODO support ability to put Raw payload inside IQ
|
||
7 years ago
|
*/
|
||
|
|
||
7 years ago
|
// ============================================================================
|
||
|
// IQ Packet
|
||
|
|
||
5 years ago
|
// IQ implements RFC 6120 - A.5 Client Namespace (a part)
|
||
7 years ago
|
type IQ struct { // Info/Query
|
||
|
XMLName xml.Name `xml:"iq"`
|
||
5 years ago
|
// MUST have a ID
|
||
|
Attrs
|
||
5 years ago
|
// We can only have one payload on IQ:
|
||
5 years ago
|
// "An IQ stanza of type "get" or "set" MUST contain exactly one
|
||
|
// child element, which specifies the semantics of the particular
|
||
|
// request."
|
||
5 years ago
|
Payload IQPayload `xml:",omitempty"`
|
||
|
Error Err `xml:"error,omitempty"`
|
||
5 years ago
|
// Any is used to decode unknown payload as a generique structure
|
||
|
Any *Node `xml:",any"`
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
5 years ago
|
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
|
||
7 years ago
|
return IQ{
|
||
|
XMLName: xml.Name{Local: "iq"},
|
||
5 years ago
|
Attrs: a,
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
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
|
||
|
}
|
||
|
|
||
7 years ago
|
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
|
||
|
}
|
||
|
|
||
9 years ago
|
// UnmarshalXML implements custom parsing for IQs
|
||
7 years ago
|
func (iq *IQ) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||
9 years ago
|
iq.XMLName = start.Name
|
||
7 years ago
|
|
||
9 years ago
|
// Extract IQ attributes
|
||
|
for _, attr := range start.Attr {
|
||
|
if attr.Name.Local == "id" {
|
||
|
iq.Id = attr.Value
|
||
|
}
|
||
9 years ago
|
if attr.Name.Local == "type" {
|
||
5 years ago
|
iq.Type = StanzaType(attr.Value)
|
||
9 years ago
|
}
|
||
9 years ago
|
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:
|
||
5 years ago
|
if tt.Name.Local == "error" {
|
||
|
var xmppError Err
|
||
|
err = d.DecodeElement(&xmppError, &tt)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return err
|
||
9 years ago
|
}
|
||
5 years ago
|
iq.Error = xmppError
|
||
|
continue
|
||
9 years ago
|
}
|
||
5 years ago
|
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
|
||
|
}
|
||
5 years ago
|
// TODO: If unknown decode as generic node
|
||
5 years ago
|
node := new(Node)
|
||
|
err = d.DecodeElement(node, &tt)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
iq.Any = node
|
||
9 years ago
|
case xml.EndElement:
|
||
|
if tt == start.End() {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|