~samwhited/xmpp

46e6a1d255239c0f8136fdd4a001a5344c0d361e — Sam Whited 3 years ago 0dd242f
all: remove use of session.Encoder method
7 files changed, 62 insertions(+), 27 deletions(-)

M bind.go
M features.go
M ibr2/challenge.go
M ibr2/ibr2.go
M ibr2/oob.go
M oob/oob.go
M session.go
M bind.go => bind.go +2 -3
@@ 78,7 78,6 @@ func bind(server func(*jid.JID, string) (*jid.JID, error)) StreamFeature {
			return true, nil, xml.NewTokenDecoder(r).DecodeElement(&parsed, start)
		},
		Negotiate: func(ctx context.Context, session *Session, data interface{}) (mask SessionState, rw io.ReadWriter, err error) {
			e := session.Encoder()
			d := xml.NewTokenDecoder(session)

			// Handle the server side of resource binding if we're on the receiving


@@ 132,12 131,12 @@ func bind(server func(*jid.JID, string) (*jid.JID, error)) StreamFeature {
					resp.Bind = bindPayload{JID: j}
				}

				return mask, nil, e.Encode(resp)
				return mask, nil, session.encode(resp)
			}

			// Client encodes an IQ requesting resource binding.
			reqID := internal.RandomID()
			err = e.Encode(bindIQ{
			err = session.encode(bindIQ{
				IQ: stanza.IQ{
					ID:   reqID,
					Type: stanza.SetIQ,

M features.go => features.go +3 -5
@@ 196,10 196,8 @@ type streamFeaturesList struct {
}

func writeStreamFeatures(ctx context.Context, s *Session) (list *streamFeaturesList, err error) {
	e := s.Encoder()

	start := xml.StartElement{Name: xml.Name{Space: "", Local: "stream:features"}}
	if err = e.EncodeToken(start); err != nil {
	if err = s.EncodeToken(start); err != nil {
		return
	}



@@ 230,10 228,10 @@ func writeStreamFeatures(ctx context.Context, s *Session) (list *streamFeaturesL
			list.total++
		}
	}
	if err = e.EncodeToken(start.End()); err != nil {
	if err = s.EncodeToken(start.End()); err != nil {
		return
	}
	if err = e.Flush(); err != nil {
	if err = s.Flush(); err != nil {
		return
	}
	return

M ibr2/challenge.go => ibr2/challenge.go +2 -2
@@ 18,10 18,10 @@ type Challenge struct {
	Type string

	// Send is used by the server to send the challenge to the client.
	Send func(ctx context.Context, e *xml.Encoder) error
	Send func(context.Context, xmlstream.TokenWriter) error

	// Respond is used by the client to send a response or reply to the challenge.
	Respond func(context.Context, *xml.Encoder) error
	Respond func(context.Context, xmlstream.TokenWriter) error

	// Receive is used by the client to receive and decode the server's challenge
	// and by the server to receive and decode the clients response.

M ibr2/ibr2.go => ibr2/ibr2.go +7 -8
@@ 144,25 144,24 @@ func negotiateFunc(challenges ...Challenge) func(context.Context, *xmpp.Session,
		}

		var tok xml.Token
		e := session.Encoder()

		if server {
			for _, c := range challenges {
				// Send the challenge.
				start := challengeStart(c.Type)
				err = e.EncodeToken(start)
				err = session.EncodeToken(start)
				if err != nil {
					return
				}
				err = c.Send(ctx, e)
				err = c.Send(ctx, session)
				if err != nil {
					return
				}
				err = e.EncodeToken(start.End())
				err = session.EncodeToken(start.End())
				if err != nil {
					return
				}
				err = e.Flush()
				err = session.Flush()
				if err != nil {
					return
				}


@@ 217,16 216,16 @@ func negotiateFunc(challenges ...Challenge) func(context.Context, *xmpp.Session,
			respStart := xml.StartElement{
				Name: xml.Name{Local: "response"},
			}
			if err = e.EncodeToken(respStart); err != nil {
			if err = session.EncodeToken(respStart); err != nil {
				return
			}
			if c.Respond != nil {
				err = c.Respond(ctx, e)
				err = c.Respond(ctx, session)
				if err != nil {
					return
				}
			}
			if err = e.EncodeToken(respStart.End()); err != nil {
			if err = session.EncodeToken(respStart.End()); err != nil {
				return
			}


M ibr2/oob.go => ibr2/oob.go +46 -2
@@ 20,8 20,11 @@ import (
func OOB(data *oob.Data, f func(*oob.Data) error) Challenge {
	return Challenge{
		Type: oob.NS,
		Send: func(ctx context.Context, e *xml.Encoder) error {
			return e.Encode(data)
		Send: func(ctx context.Context, w xmlstream.TokenWriter) (err error) {
			if err = writeDataTo(w, data); err != nil {
				return err
			}
			return w.Flush()
		},
		Receive: func(ctx context.Context, server bool, r xmlstream.TokenReader, start *xml.StartElement) error {
			// The server does not receive a reply for this mechanism.


@@ 39,3 42,44 @@ func OOB(data *oob.Data, f func(*oob.Data) error) Challenge {
		},
	}
}

var (
	xStartToken = xml.StartElement{
		Name: xml.Name{Space: oob.NS, Local: "x"},
	}
	urlStartToken = xml.StartElement{
		Name: xml.Name{Local: "url"},
	}
	descStartToken = xml.StartElement{
		Name: xml.Name{Local: "desc"},
	}
)

// TODO: move this to the mellium.im/xmpp/oob package as a method on Data?
// Also, possibly add a matching interface in mellium.im/xmlstream.

// writeDataTo encodes d to w.
func writeDataTo(w xmlstream.TokenWriter, d *oob.Data) (err error) {
	if err = w.EncodeToken(xStartToken); err != nil {
		return err
	}
	if err = w.EncodeToken(urlStartToken); err != nil {
		return err
	}
	if err = w.EncodeToken(xml.CharData(d.URL)); err != nil {
		return err
	}
	if err = w.EncodeToken(urlStartToken.End()); err != nil {
		return err
	}
	if err = w.EncodeToken(descStartToken); err != nil {
		return err
	}
	if err = w.EncodeToken(xml.CharData(d.Desc)); err != nil {
		return err
	}
	if err = w.EncodeToken(descStartToken.End()); err != nil {
		return err
	}
	return w.EncodeToken(xStartToken.End())
}

M oob/oob.go => oob/oob.go +2 -2
@@ 1,6 1,6 @@
// 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.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

// Package oob implements XEP-0066: Out of Band Data.
package oob // import "mellium.im/xmpp/oob"

M session.go => session.go +0 -5
@@ 203,11 203,6 @@ func (s *Session) Token() (xml.Token, error) {
	return s.in.d.Token()
}

// Encoder returns the XML encoder that was used to negotiate the latest stream.
func (s *Session) Encoder() *xml.Encoder {
	return s.out.e
}

// EncodeToken satisfies the xmlstream.TokenWriter interface.
func (s *Session) EncodeToken(t xml.Token) error {
	return s.out.e.EncodeToken(t)