~samwhited/xmpp

xmpp/commands/commands.go -rw-r--r-- 4.8 KiB
c9743d9fSam Whited docs: update changelog to include deprecations 7 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2021 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

//go:generate go run ../internal/genfeature

// Package commands implements executable ad-hoc commands.
package commands // import "mellium.im/xmpp/commands"

import (
	"context"
	"encoding/xml"
	"errors"
	"io"

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

// NS is the namespace used by commands, provided as a convenience.
const NS = `http://jabber.org/protocol/commands`

// Command is an ad-hoc command that can be executed by a client.
type Command struct {
	JID    jid.JID `xml:"jid,attr"`
	Action string  `xml:"action,attr"`
	Name   string  `xml:"name,attr"`
	Node   string  `xml:"node,attr"`
	SID    string  `xml:"sessionid,attr"`
}

// Execute runs the given command and returns the next command or any errors
// encountered during processing.
// The returned tokens are the commands payload(s).
//
// If the response is not nil it must be closed before stream processing will
// continue.
func (c Command) Execute(ctx context.Context, payload xml.TokenReader, s *xmpp.Session) (Response, xmlstream.TokenReadCloser, error) {
	return c.ExecuteIQ(ctx, stanza.IQ{
		Type: stanza.SetIQ,
		To:   c.JID,
	}, payload, s)
}

// ExecuteIQ is like Execute except that it allows you to customize the IQ.
// Changing the type has no effect.
//
// If the response is not nil it must be closed before stream processing will
// continue.
func (c Command) ExecuteIQ(ctx context.Context, iq stanza.IQ, payload xml.TokenReader, s *xmpp.Session) (resp Response, respPayload xmlstream.TokenReadCloser, err error) {
	if iq.Type != stanza.SetIQ {
		iq.Type = stanza.SetIQ
	}

	respPayload, err = s.SendIQ(ctx, iq.Wrap(Command{
		SID:    c.SID,
		Node:   c.Node,
		Action: c.Action,
	}.wrap(payload)))
	if err != nil {
		return resp, nil, err
	}
	defer func() {
		respPayload := respPayload
		if err != nil && respPayload != nil {
			/* #nosec */
			respPayload.Close()
		}
	}()
	var t xml.Token
	t, err = respPayload.Token()
	if err != nil {
		return resp, nil, err
	}
	start := t.(xml.StartElement)
	respIQ, err := stanza.UnmarshalIQError(respPayload, start)
	if err != nil {
		return resp, nil, err
	}

	t, err = respPayload.Token()
	if err != nil {
		return resp, nil, err
	}
	start = t.(xml.StartElement)
	resp, err = respFromStart(start, respIQ)
	if err != nil {
		return resp, nil, err
	}

	return resp, struct {
		xml.TokenReader
		io.Closer
	}{
		TokenReader: xmlstream.Inner(respPayload),
		Closer:      respPayload,
	}, nil
}

func respFromStart(start xml.StartElement, stanzaIQ stanza.IQ) (Response, error) {
	resp := Response{
		IQ: stanzaIQ,
	}
	if start.Name.Local != "command" || start.Name.Space != NS {
		return resp, errors.New("commands: unexpected response to command")
	}
	for _, attr := range start.Attr {
		switch attr.Name.Local {
		case "status":
			resp.Status = attr.Value
		case "node":
			resp.Node = attr.Value
		case "sessionid":
			resp.SID = attr.Value
		}
	}
	return resp, nil
}

// TokenReader satisfies the xmlstream.Marshaler interface.
func (c Command) TokenReader() xml.TokenReader {
	return c.wrap(nil)
}

func (c Command) wrap(payload xml.TokenReader) xml.TokenReader {
	attrs := []xml.Attr{
		{Name: xml.Name{Local: "node"}, Value: c.Node},
	}
	if !c.JID.Equal(jid.JID{}) {
		attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "jid"}, Value: c.JID.String()})
	}
	if c.Action != "" {
		attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "action"}, Value: c.Action})
	}
	if c.Name != "" {
		attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "name"}, Value: c.Name})
	}
	if c.SID != "" {
		attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "sessionid"}, Value: c.SID})
	}

	return xmlstream.Wrap(
		payload,
		xml.StartElement{
			Name: xml.Name{Space: NS, Local: "command"},
			Attr: attrs,
		},
	)
}

// WriteXML satisfies the xmlstream.WriterTo interface.
// It is like MarshalXML except it writes tokens to w.
func (c Command) WriteXML(w xmlstream.TokenWriter) (n int, err error) {
	return xmlstream.Copy(w, c.TokenReader())
}

// MarshalXML implements xml.Marshaler.
func (c Command) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
	_, err := c.WriteXML(e)
	return err
}

// ForEach executes each command in a multi-command chain, returning when the
// final command is marked as completed.
func (c Command) ForEach(ctx context.Context, payload xml.TokenReader, s *xmpp.Session, f func(Response, xml.TokenReader) (Command, xml.TokenReader, error)) error {
	for {
		resp, respPayload, err := c.Execute(ctx, payload, s)
		if err != nil {
			return err
		}
		c, payload, err = f(resp, respPayload)
		if err != nil {
			return err
		}
		err = respPayload.Close()
		if err != nil {
			return err
		}
		if resp.Status != "executing" || c.Node == "" {
			return nil
		}
	}
}