From 61cdac89e029584637028017b4e97a1a52b0a3b5 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Tue, 18 Jun 2019 09:58:43 +0200 Subject: [PATCH] Add support for generating delegation forwarded iq response --- component.go | 37 ++++++++++++++++++++++++++++++------- component_test.go | 12 +++++++++--- pep.go | 8 ++++++-- pubsub.go | 7 ++++--- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/component.go b/component.go index c1d41c3..dece671 100644 --- a/component.go +++ b/component.go @@ -224,9 +224,9 @@ func (handshakeDecoder) decode(p *xml.Decoder, se xml.StartElement) (Handshake, // depending on the context. type Delegation struct { MsgExtension - XMLName xml.Name `xml:"urn:xmpp:delegation:1 delegation"` - Forwarded Forwarded // This is used in iq to wrap delegated iqs - Delegated Delegated // This is used in a message to confirm delegated namespace + XMLName xml.Name `xml:"urn:xmpp:delegation:1 delegation"` + Forwarded *Forwarded // This is used in iq to wrap delegated iqs + Delegated *Delegated // This is used in a message to confirm delegated namespace } func (d *Delegation) Namespace() string { @@ -234,10 +234,33 @@ func (d *Delegation) Namespace() string { } type Forwarded struct { - XMLName xml.Name `xml:"urn:xmpp:forward:0 forwarded"` - IQ IQ - Message Message - Presence Presence + XMLName xml.Name `xml:"urn:xmpp:forward:0 forwarded"` + Stanza Packet +} + +// UnmarshalXML is a custom unmarshal function used by xml.Unmarshal to +// transform generic XML content into hierarchical Node structure. +func (f *Forwarded) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { + // Check subelements to extract required field as boolean + for { + t, err := d.Token() + if err != nil { + return err + } + + switch tt := t.(type) { + + case xml.StartElement: + if packet, err := decodeClient(d, tt); err == nil { + f.Stanza = packet + } + + case xml.EndElement: + if tt == start.End() { + return nil + } + } + } } type Delegated struct { diff --git a/component_test.go b/component_test.go index ee933aa..55c4569 100644 --- a/component_test.go +++ b/component_test.go @@ -80,10 +80,16 @@ func TestParsingDelegationIQ(t *testing.T) { var node string for _, ext := range iq.Payload { if delegation, ok := ext.(*Delegation); ok { - payload := delegation.Forwarded.IQ.Payload + packet := delegation.Forwarded.Stanza + forwardedIQ, ok := packet.(IQ) + if !ok { + t.Errorf("Could not extract packet IQ") + return + } + payload := forwardedIQ.Payload if len(payload) > 0 { - payload := delegation.Forwarded.IQ.Payload[0] - if pubsub, ok := payload.(*PubSub); ok { + pl := payload[0] + if pubsub, ok := pl.(*PubSub); ok { node = pubsub.Publish.Node } } diff --git a/pep.go b/pep.go index 91c0188..a3d0497 100644 --- a/pep.go +++ b/pep.go @@ -17,9 +17,13 @@ type Tune struct { Uri string `xml:"uri,omitempty"` } +// Mood defines deta model for XEP-0107 - User Mood +// See: https://xmpp.org/extensions/xep-0107.html type Mood struct { - XMLName xml.Name `xml:"http://jabber.org/protocol/mood mood"` - // TODO: Custom parsing to extract mood type from tag name + MsgExtension // Mood can be added as a message extension + XMLName xml.Name `xml:"http://jabber.org/protocol/mood mood"` + // TODO: Custom parsing to extract mood type from tag name. + // Note: the list is predefined. // Mood type Text string `xml:"text,omitempty"` } diff --git a/pubsub.go b/pubsub.go index 341c54f..88e3b35 100644 --- a/pubsub.go +++ b/pubsub.go @@ -6,8 +6,8 @@ import ( type PubSub struct { XMLName xml.Name `xml:"http://jabber.org/protocol/pubsub pubsub"` - Publish Publish - Retract Retract + Publish *Publish + Retract *Retract // TODO } @@ -24,7 +24,8 @@ type Publish struct { type Item struct { XMLName xml.Name `xml:"item"` Id string `xml:"id,attr,omitempty"` - Tune Tune + Tune *Tune + Mood *Mood } type Retract struct {