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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
package msg
import (
"bytes"
"fmt"
"time"
"git.sr.ht/~rjarry/aerc/app"
"git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/config"
"git.sr.ht/~rjarry/aerc/lib"
"git.sr.ht/~rjarry/aerc/lib/log"
"git.sr.ht/~rjarry/aerc/lib/marker"
"git.sr.ht/~rjarry/aerc/lib/ui"
"git.sr.ht/~rjarry/aerc/models"
"git.sr.ht/~rjarry/aerc/worker/types"
)
type Move struct {
CreateFolders bool `opt:"-p"`
Account string `opt:"-a" complete:"CompleteAccount"`
Folder string `opt:"folder" complete:"CompleteFolder"`
}
func init() {
commands.Register(Move{})
}
func (Move) Context() commands.CommandContext {
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER
}
func (Move) Aliases() []string {
return []string{"mv", "move"}
}
func (*Move) CompleteAccount(arg string) []string {
return commands.FilterList(app.AccountNames(), arg, commands.QuoteSpace)
}
func (m *Move) CompleteFolder(arg string) []string {
var acct *app.AccountView
if len(m.Account) > 0 {
acct, _ = app.Account(m.Account)
} else {
acct = app.SelectedAccount()
}
if acct == nil {
return nil
}
return commands.FilterList(acct.Directories().List(), arg, nil)
}
func (m Move) Execute(args []string) error {
h := newHelper()
acct, err := h.account()
if err != nil {
return err
}
store, err := h.store()
if err != nil {
return err
}
uids, err := h.markedOrSelectedUids()
if err != nil {
return err
}
next := findNextNonDeleted(uids, store)
marker := store.Marker()
marker.ClearVisualMark()
if len(m.Account) == 0 {
store.Move(uids, m.Folder, m.CreateFolders, func(msg types.WorkerMessage) {
m.CallBack(msg, acct, uids, next, marker, false)
})
return nil
}
destAcct, err := app.Account(m.Account)
if err != nil {
return err
}
destStore := destAcct.Store()
if destStore == nil {
app.PushError(fmt.Sprintf("No message store in %s", m.Account))
return nil
}
var messages []*types.FullMessage
fetchDone := make(chan bool, 1)
store.FetchFull(uids, func(fm *types.FullMessage) {
messages = append(messages, fm)
if len(messages) == len(uids) {
fetchDone <- true
}
})
// Since this operation can take some time with some backends
// (e.g. IMAP), provide some feedback to inform the user that
// something is happening
app.PushStatus("Moving messages...", 10*time.Second)
var appended []uint32
var timeout bool
go func() {
defer log.PanicHandler()
select {
case <-fetchDone:
break
case <-time.After(30 * time.Second):
// TODO: find a better way to determine if store.FetchFull()
// has finished with some errors.
app.PushError("Failed to fetch all messages")
if len(messages) == 0 {
return
}
}
AppendLoop:
for _, fm := range messages {
done := make(chan bool, 1)
uid := fm.Content.Uid
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(fm.Content.Reader)
if err != nil {
log.Errorf("could not get reader for uid %d", uid)
break
}
destStore.Append(
m.Folder,
models.SeenFlag,
time.Now(),
buf,
buf.Len(),
func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
appended = append(appended, uid)
done <- true
case *types.Error:
log.Errorf("AppendMessage failed: %v", msg.Error)
done <- false
}
},
)
select {
case ok := <-done:
if !ok {
break AppendLoop
}
case <-time.After(30 * time.Second):
log.Warnf("timed-out: appended %d of %d", len(appended), len(messages))
timeout = true
break AppendLoop
}
}
if len(appended) > 0 {
store.Delete(appended, func(msg types.WorkerMessage) {
m.CallBack(msg, acct, appended, next, marker, timeout)
})
}
}()
return nil
}
func (m Move) CallBack(
msg types.WorkerMessage,
acct *app.AccountView,
uids []uint32,
next *models.MessageInfo,
marker marker.Marker,
timeout bool,
) {
store := acct.Store()
sel := store.Selected()
dest := m.Folder
if len(m.Account) > 0 {
dest = fmt.Sprintf("%s in %s", m.Folder, m.Account)
}
switch msg := msg.(type) {
case *types.Done:
var s string
if len(uids) > 1 {
s = "%d messages moved to %s"
} else {
s = "%d message moved to %s"
}
if timeout {
s = "timed-out: only " + s
app.PushError(fmt.Sprintf(s, len(uids), dest))
} else {
app.PushStatus(fmt.Sprintf(s, len(uids), dest), 10*time.Second)
}
handleDone(acct, next, store)
case *types.Error:
app.PushError(msg.Error.Error())
marker.Remark()
case *types.Unsupported:
marker.Remark()
store.Select(sel.Uid)
app.PushError("error, unsupported for this worker")
}
}
func handleDone(
acct *app.AccountView,
next *models.MessageInfo,
store *lib.MessageStore,
) {
h := newHelper()
mv, isMsgView := h.msgProvider.(*app.MessageViewer)
switch {
case isMsgView && !config.Ui.NextMessageOnDelete:
app.RemoveTab(h.msgProvider, true)
case isMsgView:
if next == nil {
app.RemoveTab(h.msgProvider, true)
acct.Messages().Select(-1)
ui.Invalidate()
return
}
lib.NewMessageStoreView(next, mv.MessageView().SeenFlagSet(),
store, app.CryptoProvider(), app.DecryptKeys,
func(view lib.MessageView, err error) {
if err != nil {
app.PushError(err.Error())
return
}
nextMv := app.NewMessageViewer(acct, view)
app.ReplaceTab(mv, nextMv, next.Envelope.Subject, true)
})
default:
if next == nil {
// We moved the last message, select the new last message
// instead of the first message
acct.Messages().Select(-1)
}
}
}
|