~samwhited/xmpp

dd07bf3e917ae651f85e1626a50e35ec5244bc46 — Sam Whited 5 years ago 60ea96a
Refactor stanzas out into their own files
6 files changed, 117 insertions(+), 110 deletions(-)

A iq.go
R types.go => message.go
R stanza_test.go => message_test.go
A presence.go
M stanza.go
D types_test.go
A iq.go => iq.go +36 -0
@@ 0,0 1,36 @@
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"encoding/xml"
)

// IQ ("Information Query") is used as a general request response mechanism.
// IQ's are one-to-one, provide get and set semantics, and always require a
// response in the form of a result or an error.
type IQ struct {
	stanza

	XMLName xml.Name `xml:"iq"`
}

type iqType int

const (
	// A GetIQ is used to query another entity for information.
	GetIQ iqType = iota

	// A SetIQ is used to provide data to another entity, set new values, replace
	// existing values, and other such operations.
	SetIQ

	// A ResultIQ is sent in response to a successful GetIQ or SetIQ stanza.
	ResultIQ

	// An ErrorIQ is sent to report that an error occured during the delivery or
	// processing of a GetIQ or SetIQ.
	ErrorIQ
)

R types.go => message.go +15 -56
@@ 1,9 1,23 @@
// Copyright 2015 Sam Whited.
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"encoding/xml"
)

// Message is an XMPP stanza that contains a payload for direct one-to-one
// communication with another network entity.  It is often used for sending chat
// messages to an individual or group chat server, or for notifications and
// alerts that don't require a response.
type Message struct {
	stanza

	XMLName xml.Name `xml:"message"`
}

type messageType int

const (


@@ 41,58 55,3 @@ const (
	// to reply).
	HeadlineMessage
)

type presenceType int

const (
	// NoTypePresence is a special type that indicates that a stanza is a presence
	// stanza without a defined type (indicating availability on the network).
	NoTypePresence presenceType = iota

	// An ErrorPresence indicates that an error has occurred regarding processing
	// of a previously sent presence stanza; if the presence stanza is of type
	// "error", it MUST include an <error/> child element
	ErrorPresence presenceType = iota

	// A ProbePresence is a request for an entity's current presence. It should
	// generally only be generated and sent by servers on behalf of a user.
	ProbePresence

	// A SubscribePresence is sent when the sender wishes to subscribe to the
	// recipient's presence.
	SubscribePresence

	// A SubscribedPresence indicates that the sender has allowed the recipient to
	// receive future presence broadcasts.
	SubscribedPresence

	// An UnavailablePresence indicates that the sender is no longer available for
	// communication.
	UnavailablePresence

	// An UnsubscribePresence indicates that the sender is unsubscribing from the
	// receiver's presence.
	UnsubscribePresence

	// An UnsubscribedPresence indicates that the subscription request has been
	// denied, or a previously granted subscription has been revoked.
	UnsubscribedPresence
)

type iqType int

const (
	// A GetIQ is used to query another entity for information.
	GetIQ iqType = iota

	// A SetIQ is used to provide data to another entity, set new values, replace
	// existing values, and other such operations.
	SetIQ

	// A ResultIQ is sent in response to a successful GetIQ or SetIQ stanza.
	ResultIQ

	// An ErrorIQ is sent to report that an error occured during the delivery or
	// processing of a GetIQ or SetIQ.
	ErrorIQ
)

R stanza_test.go => message_test.go +9 -0
@@ 15,6 15,15 @@ import (
// TODO: How should we test marshalling? Probably don't want to assume that
//       attribute order will remain stable.

func TestDefaults(t *testing.T) {
	var mt messageType

	if mt != NormalMessage {
		t.Log("Default value of message type should be 'normal'.")
		t.Fail()
	}
}

// Tests unmarshalling of a single XML blob into a message.
func TestUnmarshalMessage(t *testing.T) {
	mb := []byte(`

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

package xmpp

import (
	"encoding/xml"
)

// Presence is an XMPP stanza that is used as an indication that an entity is
// available for communication. It is used to set a status message, broadcast
// availability, and advertise entity capabilities. It can be directed
// (one-to-one), or used as a broadcast mechanism (one-to-many).
type Presence struct {
	stanza

	XMLName xml.Name `xml:"presence"`
}

type presenceType int

const (
	// NoTypePresence is a special type that indicates that a stanza is a presence
	// stanza without a defined type (indicating availability on the network).
	NoTypePresence presenceType = iota

	// An ErrorPresence indicates that an error has occurred regarding processing
	// of a previously sent presence stanza; if the presence stanza is of type
	// "error", it MUST include an <error/> child element
	ErrorPresence presenceType = iota

	// A ProbePresence is a request for an entity's current presence. It should
	// generally only be generated and sent by servers on behalf of a user.
	ProbePresence

	// A SubscribePresence is sent when the sender wishes to subscribe to the
	// recipient's presence.
	SubscribePresence

	// A SubscribedPresence indicates that the sender has allowed the recipient to
	// receive future presence broadcasts.
	SubscribedPresence

	// An UnavailablePresence indicates that the sender is no longer available for
	// communication.
	UnavailablePresence

	// An UnsubscribePresence indicates that the sender is unsubscribing from the
	// receiver's presence.
	UnsubscribePresence

	// An UnsubscribedPresence indicates that the subscription request has been
	// denied, or a previously granted subscription has been revoked.
	UnsubscribedPresence
)

M stanza.go => stanza.go +1 -32
@@ 5,13 5,11 @@
package xmpp

import (
	"encoding/xml"

	"bitbucket.org/mellium/xmpp/jid"
)

// stanza contains fields common to any any top level XMPP stanza (Presence,
// Message, or IQ)
// Message, or IQ).
type stanza struct {
	ID    string  `xml:"id,attr"`
	Inner string  `xml:",innerxml"`


@@ 19,32 17,3 @@ type stanza struct {
	From  jid.JID `xml:"from,attr"`
	Lang  string  `xml:"http://www.w3.org/XML/1998/namespace lang,attr,omitempty"`
}

// Message is an XMPP stanza that contains a payload for direct one-to-one
// communication with another network entity.  It is often used for sending chat
// messages to an individual or group chat server, or for notifications and
// alerts that don't require a response.
type Message struct {
	stanza

	XMLName xml.Name `xml:"message"`
}

// Presence is an XMPP stanza that is used as an indication that an entity is
// available for communication. It is used to set a status message, broadcast
// availability, and advertise entity capabilities. It can be directed
// (one-to-one), or used as a broadcast mechanism (one-to-many).
type Presence struct {
	stanza

	XMLName xml.Name `xml:"presence"`
}

// IQ ("Information Query") is used as a general request response mechanism.
// IQ's are one-to-one, provide get and set semantics, and always require a
// response in the form of a result or an error.
type IQ struct {
	stanza

	XMLName xml.Name `xml:"iq"`
}

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

package xmpp

import (
	"testing"
)

// TODO: Make this a table test and add some more complicated messages.
// TODO: How should we test marshalling? Probably don't want to assume that
//       attribute order will remain stable.

func TestDefaults(t *testing.T) {
	var mt messageType

	if mt != NormalMessage {
		t.Log("Default value of message type should be 'normal'.")
		t.Fail()
	}
}