~samwhited/xmpp

223a5626746a96d5a11e5a537c0e5add800a9caf — Sam Whited 3 years ago 4063f61 v0.2.0
ping: implement XEP-0199: XMPP Ping
2 files changed, 54 insertions(+), 0 deletions(-)

A ping/example_test.go
A ping/ping.go
A ping/example_test.go => ping/example_test.go +30 -0
@@ 0,0 1,30 @@
// Copyright 2017 Sam Whited.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package ping_test

import (
	"encoding/xml"
	"log"
	"os"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/ping"
)

func Example() {
	j := jid.MustParse("feste@example.net/siJo4eeT")
	e := xml.NewEncoder(os.Stdout)
	e.Indent("", "\t")

	ping := ping.PingIQ(j)
	if _, err := xmlstream.Copy(e, ping); err != nil {
		log.Fatal(err)
	}
	// Output:
	// <iq to="feste@example.net/siJo4eeT" type="get">
	//	<ping xmlns="urn:xmpp:ping"></ping>
	// </iq>
}

A ping/ping.go => ping/ping.go +24 -0
@@ 0,0 1,24 @@
// Copyright 2017 Sam Whited.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package ping implements XEP-0199: XMPP Ping.
package ping // import "mellium.im/xmpp/ping"

import (
	"encoding/xml"

	"mellium.im/xmlstream"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
)

// NS is the XML namespace used by XMPP pings. It is provided as a convenience.
const NS = `urn:xmpp:ping`

// PingIQ returns an xmlstream.TokenReader that outputs a new IQ stanza with a
// ping payload.
func PingIQ(to *jid.JID) xmlstream.TokenReader {
	start := xml.StartElement{Name: xml.Name{Local: "ping", Space: NS}}
	return stanza.WrapIQ(to, stanza.GetIQ, xmlstream.Wrap(nil, start))
}