~samwhited/xmpp

23b0d064042787acbda2d15860973d1a77b6d1b2 — Sam Whited 10 months ago ed13a4c
ibr2: use new OOB WriterTo implementation

Signed-off-by: Sam Whited <sam@samwhited.com>
1 files changed, 3 insertions(+), 43 deletions(-)

M ibr2/oob.go
M ibr2/oob.go => ibr2/oob.go +3 -43
@@ 20,8 20,9 @@ import (
func OOB(data *oob.Data, f func(*oob.Data) error) Challenge {
	return Challenge{
		Type: oob.NS,
		Send: func(ctx context.Context, w xmlstream.TokenWriter) (err error) {
			return writeDataTo(w, data)
		Send: func(ctx context.Context, w xmlstream.TokenWriter) error {
			_, err := data.WriteXML(w)
			return err
		},
		Receive: func(ctx context.Context, server bool, r xml.TokenReader, start *xml.StartElement) error {
			// The server does not receive a reply for this mechanism.


@@ 39,44 40,3 @@ 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())
}