From db213fd0aed98a79ecb1be2c7b7d61c7d983bedf Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 14 May 2019 16:34:42 -0400 Subject: [PATCH] Implement :copy (aka :cp) --- commands/account/copy.go | 38 ++++++++++++++++++++++++++++++ commands/account/delete-message.go | 14 ++++++++++- lib/msgstore.go | 18 ++++++++++++-- widgets/account.go | 4 ++++ worker/imap/movecopy.go | 16 +++++++++++++ worker/imap/worker.go | 2 ++ worker/types/messages.go | 12 ++++++++++ 7 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 commands/account/copy.go create mode 100644 worker/imap/movecopy.go diff --git a/commands/account/copy.go b/commands/account/copy.go new file mode 100644 index 0000000..2a811c8 --- /dev/null +++ b/commands/account/copy.go @@ -0,0 +1,38 @@ +package account + +import ( + "errors" + "time" + + "github.com/gdamore/tcell" + + "git.sr.ht/~sircmpwn/aerc2/widgets" + "git.sr.ht/~sircmpwn/aerc2/worker/types" +) + +func init() { + register("cp", Copy) + register("copy", Copy) +} + +func Copy(aerc *widgets.Aerc, args []string) error { + if len(args) != 2 { + return errors.New("Usage: mv ") + } + acct := aerc.SelectedAccount() + if acct == nil { + return errors.New("No account selected") + } + msg := acct.Messages().Selected() + store := acct.Messages().Store() + store.Copy([]uint32{msg.Uid}, args[1], func(msg types.WorkerMessage) { + switch msg := msg.(type) { + case *types.Done: + aerc.PushStatus("Messages copied.", 10*time.Second) + case *types.Error: + aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second). + Color(tcell.ColorDefault, tcell.ColorRed) + } + }) + return nil +} diff --git a/commands/account/delete-message.go b/commands/account/delete-message.go index 40e3bba..2461d64 100644 --- a/commands/account/delete-message.go +++ b/commands/account/delete-message.go @@ -2,8 +2,12 @@ package account import ( "errors" + "time" + + "github.com/gdamore/tcell" "git.sr.ht/~sircmpwn/aerc2/widgets" + "git.sr.ht/~sircmpwn/aerc2/worker/types" ) func init() { @@ -20,6 +24,14 @@ func DeleteMessage(aerc *widgets.Aerc, args []string) error { } store := acct.Messages().Store() msg := acct.Messages().Selected() - store.Delete([]uint32{msg.Uid}) + store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) { + switch msg := msg.(type) { + case *types.Done: + aerc.PushStatus("Messages deleted.", 10*time.Second) + case *types.Error: + aerc.PushStatus(" "+msg.Error.Error(), 10*time.Second). + Color(tcell.ColorDefault, tcell.ColorRed) + } + }) return nil } diff --git a/lib/msgstore.go b/lib/msgstore.go index 32edfc5..c4f5326 100644 --- a/lib/msgstore.go +++ b/lib/msgstore.go @@ -218,7 +218,8 @@ func (store *MessageStore) update() { } } -func (store *MessageStore) Delete(uids []uint32) { +func (store *MessageStore) Delete(uids []uint32, + cb func(msg types.WorkerMessage)) { store.Lock() var set imap.SeqSet @@ -229,6 +230,19 @@ func (store *MessageStore) Delete(uids []uint32) { store.Unlock() - store.worker.PostAction(&types.DeleteMessages{Uids: set}, nil) + store.worker.PostAction(&types.DeleteMessages{Uids: set}, cb) store.update() } + +func (store *MessageStore) Copy(uids []uint32, dest string, + cb func(msg types.WorkerMessage)) { + var set imap.SeqSet + for _, uid := range uids { + set.AddNum(uid) + } + + store.worker.PostAction(&types.CopyMessages{ + Destination: dest, + Uids: set, + }, cb) +} diff --git a/widgets/account.go b/widgets/account.go index ba2b8d3..c252e38 100644 --- a/widgets/account.go +++ b/widgets/account.go @@ -110,6 +110,10 @@ func (acct *AccountView) Focus(focus bool) { // TODO: Unfocus children I guess } +func (acct *AccountView) Worker() *types.Worker { + return acct.worker +} + func (acct *AccountView) connected(msg types.WorkerMessage) { switch msg := msg.(type) { case *types.Done: diff --git a/worker/imap/movecopy.go b/worker/imap/movecopy.go new file mode 100644 index 0000000..8c42a2e --- /dev/null +++ b/worker/imap/movecopy.go @@ -0,0 +1,16 @@ +package imap + +import ( + "git.sr.ht/~sircmpwn/aerc2/worker/types" +) + +func (imapw *IMAPWorker) handleCopyMessages(msg *types.CopyMessages) { + if err := imapw.client.UidCopy(&msg.Uids, msg.Destination); err != nil { + imapw.worker.PostMessage(&types.Error{ + Message: types.RespondTo(msg), + Error: err, + }, nil) + } else { + imapw.worker.PostMessage(&types.Done{types.RespondTo(msg)}, nil) + } +} diff --git a/worker/imap/worker.go b/worker/imap/worker.go index d0b0e71..1fab3bb 100644 --- a/worker/imap/worker.go +++ b/worker/imap/worker.go @@ -174,6 +174,8 @@ func (w *IMAPWorker) handleMessage(msg types.WorkerMessage) error { w.handleFetchFullMessages(msg) case *types.DeleteMessages: w.handleDeleteMessages(msg) + case *types.CopyMessages: + w.handleCopyMessages(msg) default: return errUnsupported } diff --git a/worker/types/messages.go b/worker/types/messages.go index b17d53d..fdd3263 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -97,6 +97,18 @@ type DeleteMessages struct { Uids imap.SeqSet } +type CopyMessages struct { + Message + Destination string + Uids imap.SeqSet +} + +type MoveMessages struct { + Message + Destination string + Uids imap.SeqSet +} + // Messages type CertificateApprovalRequest struct {