-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd.go
168 lines (158 loc) · 4.05 KB
/
cmd.go
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
// Copyright 2022 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package main
import (
"context"
"encoding/xml"
"log"
"mellium.im/communique/internal/client"
"mellium.im/communique/internal/ui"
"mellium.im/xmlstream"
"mellium.im/xmpp/commands"
"mellium.im/xmpp/form"
"mellium.im/xmpp/oob"
)
func showCmd(pane *ui.UI, client *client.Client, resp commands.Response, payload xmlstream.TokenReadCloser, debug *log.Logger) error {
var (
actions commands.Actions
note commands.Note
formData *form.Data
)
err := func() (err error) {
defer func() {
e := payload.Close()
if err == nil && e != nil {
err = e
}
}()
iter := xmlstream.NewIter(payload)
for iter.Next() {
start, inner := iter.Current()
if start == nil {
continue
}
d := xml.NewTokenDecoder(xmlstream.Wrap(inner, *start))
// Pop the start element to put the decoder in the correct state.
_, err := d.Token()
if err != nil {
return err
}
switch {
case start.Name.Space == commands.NS && start.Name.Local == "note":
if note.Value != "" || formData != nil {
continue
}
err := d.DecodeElement(¬e, start)
if err != nil {
return err
}
case start.Name.Space == oob.NS:
if note.Value != "" || formData != nil {
continue
}
var oobURL oob.Data
err := d.DecodeElement(&oobURL, start)
if err != nil {
return err
}
note = commands.Note{
Value: oobURL.Desc + "\n\n" + oobURL.URL,
}
case start.Name.Space == form.NS:
if note.Value != "" || formData != nil {
continue
}
formData = &form.Data{}
err := d.DecodeElement(formData, start)
if err != nil {
return err
}
case start.Name.Space == commands.NS && start.Name.Local == "actions":
// Just decode the actions, they will be displayed at the end.
err := d.DecodeElement(&actions, start)
if err != nil {
return err
}
}
}
return iter.Err()
}()
if err != nil {
return err
}
const (
prevBtn = "Prev"
nextBtn = "Next"
completeBtn = "Complete"
cancelBtn = "Cancel"
)
var buttons []string
if actions&commands.Prev == commands.Prev {
buttons = append(buttons, prevBtn)
}
if actions&commands.Next == commands.Next {
buttons = append(buttons, nextBtn)
}
if actions == 0 || actions&commands.Complete == commands.Complete {
buttons = append(buttons, completeBtn)
}
buttons = append(buttons, cancelBtn)
onDone := func(label string) {
pane.SelectRoster()
var nextCmd commands.Command
switch label {
case prevBtn:
nextCmd = resp.Prev()
case nextBtn:
nextCmd = resp.Next()
case completeBtn:
nextCmd = resp.Complete()
default:
if resp.Status != "completed" && resp.Status != "canceled" {
// If for some reason we can't cancel the command we don't need to wait
// on it to complete to close the dialog.
go func() {
ctx, cancel := context.WithTimeout(context.Background(), client.Timeout())
defer cancel()
_, trc, err := resp.Cancel().Execute(ctx, nil, client.Session)
if err != nil {
debug.Printf("error canceling command session: %v", err)
}
if trc != nil {
err = trc.Close()
if err != nil {
debug.Printf("error closing cancel command payload: %v", err)
}
}
}()
}
return
}
ctx, cancel := context.WithTimeout(context.Background(), client.Timeout())
defer cancel()
var payload xml.TokenReader
if formData != nil {
payload, _ = formData.Submit()
}
if resp.Status != "completed" && resp.Status != "canceled" {
resp, trc, err := nextCmd.Execute(ctx, payload, client.Session)
if err != nil {
debug.Printf("error closing command session: %v", err)
}
go func() {
err = showCmd(pane, client, resp, trc, debug)
if err != nil {
debug.Printf("error showing next command: %v", err)
}
}()
}
}
switch {
case formData != nil:
pane.ShowForm(formData, buttons, onDone)
case note.Value != "":
pane.ShowNote(note, buttons, onDone)
}
return nil
}