From 30e6adc073d2fbb0b57371bb88b2446ae5fe61bc Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Mon, 10 Jun 2019 16:36:47 +0200 Subject: [PATCH] Add support for detecting Stream Management --- stream.go | 24 +++++++++++++++++++----- stream_test.go | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/stream.go b/stream.go index b887c55..b23ba69 100644 --- a/stream.go +++ b/stream.go @@ -13,11 +13,12 @@ type StreamFeatures struct { // Server capabilities hash Caps Caps // Stream features - StartTLS tlsStartTLS - Mechanisms saslMechanisms - Bind BindBind - Session sessionSession - Any []xml.Name `xml:",any"` + StartTLS tlsStartTLS + Mechanisms saslMechanisms + Bind BindBind + Session sessionSession + StreamManagement streamManagement + Any []xml.Name `xml:",any"` } func (StreamFeatures) Name() string { @@ -104,6 +105,19 @@ type saslMechanisms struct { Mechanism []string `xml:"mechanism"` } +// StreamManagement +// Reference: XEP-0198 - https://xmpp.org/extensions/xep-0198.html#feature +type streamManagement struct { + XMLName xml.Name `xml:"urn:xmpp:sm:3 sm"` +} + +func (sf *StreamFeatures) DoesStreamManagement() (isSupported bool) { + if sf.StreamManagement.XMLName.Space+" "+sf.StreamManagement.XMLName.Local == "urn:xmpp:sm:3 sm" { + return true + } + return false +} + // ============================================================================ // StreamError Packet diff --git a/stream_test.go b/stream_test.go index f10d1de..fac1446 100644 --- a/stream_test.go +++ b/stream_test.go @@ -45,3 +45,20 @@ func TestStartTLS(t *testing.T) { t.Error("StartTLS feature should be required") } } + +// TODO: Ability to support / detect previous version of stream management feature +func TestStreamManagement(t *testing.T) { + streamFeatures := ` + +` + + var parsedSF xmpp.StreamFeatures + if err := xml.Unmarshal([]byte(streamFeatures), &parsedSF); err != nil { + t.Errorf("Unmarshal(%s) returned error: %v", streamFeatures, err) + } + + ok := parsedSF.DoesStreamManagement() + if !ok { + t.Error("Stream Management feature should have been detected") + } +}