~samwhited/xmpp

a9d9432876a49b724da80083b7e98ef29dd82ea9 — Sam Whited 3 years ago abbc291
stanza: move stanza functionality into new package
13 files changed, 41 insertions(+), 25 deletions(-)

M bind.go
M oob/oob.go
M session.go
A stanza/doc.go
R errors.go => stanza/errors.go
R errors_test.go => stanza/errors_test.go
R iq.go => stanza/iq.go
R iq_test.go => stanza/iq_test.go
R message.go => stanza/message.go
R message_test.go => stanza/message_test.go
R presence.go => stanza/presence.go
R presence_test.go => stanza/presence_test.go
R stanzatype_string.go => stanza/stanzatype_string.go
M bind.go => bind.go +6 -5
@@ 13,6 13,7 @@ import (
	"mellium.im/xmpp/internal"
	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
	"mellium.im/xmpp/stream"
)



@@ 73,11 74,11 @@ func BindResource() StreamFeature {
				return mask, nil, stream.BadFormat
			}
			resp := struct {
				IQ
				stanza.IQ
				Bind struct {
					JID *jid.JID `xml:"jid"`
				} `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
				Err StanzaError `xml:"error"`
				Err stanza.StanzaError `xml:"error"`
			}{}
			switch start.Name {
			case xml.Name{Space: ns.Client, Local: "iq"}:


@@ 91,12 92,12 @@ func BindResource() StreamFeature {
			switch {
			case resp.ID != reqID:
				return mask, nil, stream.UndefinedCondition
			case resp.Type == ResultIQ:
			case resp.Type == stanza.ResultIQ:
				session.origin = resp.Bind.JID
			case resp.Type == ErrorIQ:
			case resp.Type == stanza.ErrorIQ:
				return mask, nil, resp.Err
			default:
				return mask, nil, StanzaError{Condition: BadRequest}
				return mask, nil, stanza.StanzaError{Condition: stanza.BadRequest}
			}
			return Ready, nil, nil
		},

M oob/oob.go => oob/oob.go +2 -2
@@ 8,7 8,7 @@ package oob // import "mellium.im/xmpp/oob"
import (
	"encoding/xml"

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

// OOB namespaces provided as a convenience.


@@ 29,7 29,7 @@ const (
//       </query>
//     </iq>
type IQ struct {
	xmpp.IQ
	stanza.IQ
	Query Query
}


M session.go => session.go +8 -8
@@ 13,6 13,7 @@ import (

	"mellium.im/xmpp/internal/ns"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/stanza"
	"mellium.im/xmpp/stream"
)



@@ 110,13 111,12 @@ func NewSession(ctx context.Context, config *Config, rw io.ReadWriter) (*Session

// Serve decodes incoming XML tokens from the connection and delegates handling
// them to the provided handler.
// If an error is returned from the handler and it is of type StanzaError or
// streamerror.StreamError, the error is marshaled and sent over the XML stream.
// If any other error type is returned, it is marshaled as an
// undefined-condition StreamError.
// If a stream error is received while serving it is not passed to the handler.
// Instead, Serve unmarshals the error, closes the session, and returns it
// (handlers handle stanza level errors, the session handles stream
// If an error is returned from the handler and it is of type stanza.StanzaError
// or stream.Error, the error is marshaled and sent over the XML stream. If any
// other error type is returned, it is marshaled as an undefined-condition
// StreamError. If a stream error is received while serving it is not passed to
// the handler. Instead, Serve unmarshals the error, closes the session, and
// returns it (handlers handle stanza level errors, the session handles stream
// level errors).
func (s *Session) Serve(handler Handler) error {
	return s.handleInputStream(handler)


@@ 231,7 231,7 @@ func (s *Session) handleInputStream(handler Handler) error {
			}
			if err = handler.HandleXMPP(s, &t); err != nil {
				switch err.(type) {
				case StanzaError:
				case stanza.StanzaError:
					err = s.Encoder().Encode(err)
					if err != nil {
						return err

A stanza/doc.go => stanza/doc.go +15 -0
@@ 0,0 1,15 @@
// 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 stanza contains functionality for dealing with XMPP stanzas and
// stanza level errors.
//
// Stanzas (Message, Presence, and IQ) are the "primitives" of XMPP. Messages
// are used to send data that is fire-and-forget such as chat messages, Presence
// is used as a general broadcast and publish-subscribe mechanism and is used to
// broadcast availability on the network (sometimes called "status" in chat, eg.
// online, offline, or away), and IQ (Info-Query) is used as a request response
// mechanism for data that requires a response (eg. fetching an avatar or a list
// of client features).
package stanza // import "mellium.im/xmpp/stanza"

R errors.go => stanza/errors.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R errors_test.go => stanza/errors_test.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R iq.go => stanza/iq.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R iq_test.go => stanza/iq_test.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R message.go => stanza/message.go +2 -2
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"


@@ 13,7 13,7 @@ import (
)

// 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
// 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 {

R message_test.go => stanza/message_test.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R presence.go => stanza/presence.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"encoding/xml"

R presence_test.go => stanza/presence_test.go +1 -1
@@ 2,7 2,7 @@
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp
package stanza

import (
	"fmt"

R stanzatype_string.go => stanza/stanzatype_string.go +1 -1
@@ 1,6 1,6 @@
// Code generated by "stringer -type=iqType,errorType,messageType,presenceType -output stanzatype_string.go"; DO NOT EDIT

package xmpp
package stanza

import "fmt"