2018-01-01 17:12:33 +00:00
|
|
|
package xmpp // import "fluux.io/xmpp"
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2016-02-15 10:08:54 +00:00
|
|
|
import (
|
|
|
|
"encoding/xml"
|
2016-02-15 15:33:27 +00:00
|
|
|
"fmt"
|
2016-01-06 15:51:12 +00:00
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
"reflect"
|
|
|
|
|
2018-01-01 17:35:47 +00:00
|
|
|
"fluux.io/xmpp/iot"
|
2016-02-15 10:08:54 +00:00
|
|
|
)
|
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
/*
|
|
|
|
TODO I would like to be able to write
|
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
NewIQ(Id, From, To, Type, Lang).AddPayload(IQPayload)
|
|
|
|
Payload would be:
|
2018-01-15 11:28:34 +00:00
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
payload := Node{
|
|
|
|
Ns: "http://jabber.org/protocol/disco#info",
|
|
|
|
Tag: "identity",
|
|
|
|
Attrs: map[string]string{
|
|
|
|
"category":"gateway",
|
|
|
|
"type": "skype",
|
|
|
|
"name": "Test Gateway",
|
|
|
|
},
|
|
|
|
Nodes: []Node{},
|
|
|
|
}
|
|
|
|
|
|
|
|
AddPayload(Ns, Tag, Attrs)
|
|
|
|
|
|
|
|
ex:
|
|
|
|
|
|
|
|
NewIQ("get", "test@localhost", "admin@localhost", "en")
|
|
|
|
.AddPayload("http://jabber.org/protocol/disco#info",
|
|
|
|
"identity",
|
|
|
|
map[string]string{
|
|
|
|
"category":"gateway",
|
|
|
|
"type": "skype",
|
|
|
|
"name": "Test Gateway",
|
|
|
|
})
|
|
|
|
|
|
|
|
NewNode(Ns, Tag, Attrs)
|
|
|
|
NewNodeWithChildren(Ns, Tag, Attrs, Nodes)
|
|
|
|
|
|
|
|
Attr {
|
|
|
|
K string
|
|
|
|
V string
|
|
|
|
}
|
|
|
|
|
|
|
|
xmpp.Elt.DiscoInfo("identity", "gateway", "skype", "Test Gateway")
|
|
|
|
xmppElt.DiscoInfo.identity("
|
|
|
|
import xmpp/node/discoinfo
|
|
|
|
|
|
|
|
discoinfo.Identity("gateway", "skype", "Test Gateway")
|
|
|
|
|
|
|
|
|
|
|
|
[]Attr{{"category", "gateway"}
|
|
|
|
|
|
|
|
TODO support ability to put Raw payload
|
2018-01-15 11:28:34 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2018-01-13 17:50:17 +00:00
|
|
|
// ============================================================================
|
|
|
|
// IQ Packet
|
|
|
|
|
|
|
|
type IQ struct { // Info/Query
|
|
|
|
XMLName xml.Name `xml:"iq"`
|
2018-01-13 16:54:07 +00:00
|
|
|
PacketAttrs
|
2018-01-15 11:28:34 +00:00
|
|
|
Payload []IQPayload `xml:",omitempty"`
|
|
|
|
RawXML string `xml:",innerxml"`
|
2016-01-06 15:51:12 +00:00
|
|
|
// Error clientError
|
|
|
|
}
|
2016-02-15 14:22:51 +00:00
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
func NewIQ(iqtype, from, to, id, lang string) IQ {
|
|
|
|
return IQ{
|
|
|
|
XMLName: xml.Name{Local: "iq"},
|
|
|
|
PacketAttrs: PacketAttrs{
|
|
|
|
Id: id,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Type: iqtype,
|
|
|
|
Lang: lang,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iq *IQ) AddPayload(payload IQPayload) {
|
|
|
|
iq.Payload = append(iq.Payload, payload)
|
|
|
|
}
|
|
|
|
|
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-16 21:33:21 +00:00
|
|
|
fmt.Println("IQ Name", iq.XMLName)
|
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" {
|
|
|
|
iq.Type = attr.Value
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "lang" {
|
|
|
|
iq.Lang = attr.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// decode inner elements
|
2018-01-16 21:33:21 +00:00
|
|
|
level := 0
|
2016-02-15 14:22:51 +00:00
|
|
|
for {
|
|
|
|
t, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch tt := t.(type) {
|
|
|
|
|
|
|
|
case xml.StartElement:
|
2018-01-16 21:33:21 +00:00
|
|
|
level++
|
|
|
|
if level <= 1 {
|
|
|
|
var elt interface{}
|
|
|
|
payloadType := tt.Name.Space + " " + tt.Name.Local
|
|
|
|
if payloadType := typeRegistry[payloadType]; payloadType != nil {
|
|
|
|
val := reflect.New(payloadType)
|
|
|
|
elt = val.Interface()
|
|
|
|
} else {
|
|
|
|
elt = new(Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
if iqPl, ok := elt.(IQPayload); ok {
|
|
|
|
err = d.DecodeElement(elt, &tt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iq.Payload = append(iq.Payload, iqPl) // []IQPayload{iqPl}
|
2016-02-15 14:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case xml.EndElement:
|
2018-01-16 21:33:21 +00:00
|
|
|
level--
|
2016-02-15 14:22:51 +00:00
|
|
|
if tt == start.End() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:33:27 +00:00
|
|
|
|
2016-02-15 17:33:51 +00:00
|
|
|
// XMPPFormat returns the string representation of the XMPP packet.
|
|
|
|
// TODO: Should I simply rely on xml.Marshal ?
|
2018-01-13 17:50:17 +00:00
|
|
|
func (iq *IQ) XMPPFormat() string {
|
2016-02-15 15:33:27 +00:00
|
|
|
if iq.Payload != nil {
|
|
|
|
var payload []byte
|
|
|
|
var err error
|
|
|
|
if payload, err = xml.Marshal(iq.Payload); err != nil {
|
|
|
|
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
|
|
|
|
"</iq>",
|
|
|
|
iq.To, iq.Type, iq.Id)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
|
|
|
|
"%s</iq>",
|
|
|
|
iq.To, iq.Type, iq.Id, payload)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<iq to='%s' type='%s' id='%s' xml:lang='en'>"+
|
|
|
|
"%s</iq>",
|
|
|
|
iq.To, iq.Type, iq.Id,
|
|
|
|
iq.RawXML)
|
|
|
|
}
|
2018-01-13 18:14:26 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
2018-01-15 11:28:34 +00:00
|
|
|
// Generic IQ Payload
|
|
|
|
|
|
|
|
type IQPayload interface {
|
|
|
|
IsIQPayload()
|
|
|
|
}
|
2018-01-13 18:14:26 +00:00
|
|
|
|
2018-01-13 18:27:46 +00:00
|
|
|
type Node struct {
|
|
|
|
XMLName xml.Name
|
2018-01-15 11:28:34 +00:00
|
|
|
Attrs []xml.Attr `xml:"-"`
|
|
|
|
// Content []byte `xml:",innerxml"`
|
|
|
|
Nodes []Node `xml:",any"`
|
|
|
|
}
|
|
|
|
|
2018-01-16 21:33:21 +00:00
|
|
|
type Attr struct {
|
|
|
|
K string
|
|
|
|
V string
|
|
|
|
}
|
|
|
|
|
2018-01-15 11:28:34 +00:00
|
|
|
func (n *Node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
2018-01-16 21:33:21 +00:00
|
|
|
// Assign "n.Attrs = start.Attr", without repeating xmlns in attributes:
|
2018-01-15 11:47:26 +00:00
|
|
|
for _, attr := range start.Attr {
|
2018-01-16 21:33:21 +00:00
|
|
|
// Do not repeat xmlns, it is already in XMLName
|
2018-01-15 11:47:26 +00:00
|
|
|
if attr.Name.Local != "xmlns" {
|
|
|
|
n.Attrs = append(n.Attrs, attr)
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 11:28:34 +00:00
|
|
|
type node Node
|
|
|
|
return d.DecodeElement((*node)(n), &start)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
|
|
|
|
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 18:14:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-13 18:27:46 +00:00
|
|
|
func (*Node) IsIQPayload() {}
|
2018-01-16 21:33:21 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// Disco
|
|
|
|
|
|
|
|
type DiscoInfo struct {
|
|
|
|
XMLName xml.Name `xml:"http://jabber.org/protocol/disco#info query"`
|
|
|
|
Identity Identity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*DiscoInfo) IsIQPayload() {}
|
|
|
|
|
|
|
|
type Identity struct {
|
|
|
|
XMLName xml.Name `xml:"identity"`
|
|
|
|
Name string `xml:"name,attr"`
|
|
|
|
Category string `xml:"category,attr"`
|
|
|
|
Type string `xml:"type,attr"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
var typeRegistry = make(map[string]reflect.Type)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
typeRegistry["http://jabber.org/protocol/disco#info query"] = reflect.TypeOf(DiscoInfo{})
|
|
|
|
typeRegistry["urn:ietf:params:xml:ns:xmpp-bind bind"] = reflect.TypeOf(BindBind{})
|
|
|
|
typeRegistry["urn:xmpp:iot:control set"] = reflect.TypeOf(iot.ControlSet{})
|
|
|
|
}
|