From a64563466ddcb9fc321b8dd540f929c979310af9 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Fri, 14 Jun 2024 23:06:58 +0200 Subject: copy: add -d flag to decrypt before copying Add -d flag to the copy command to decrypt a message before copying it. Implements: https://todo.sr.ht/~rjarry/aerc/238 Signed-off-by: Koni Marti Tested-by: Jens Grassel Reviewed-by: Tim Culverhouse Acked-by: Robin Jarry --- commands/msg/copy.go | 32 ++++++++++++++++++++++++++++++++ doc/aerc.1.scd | 6 ++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/commands/msg/copy.go b/commands/msg/copy.go index 9ea81055..d9c40ee2 100644 --- a/commands/msg/copy.go +++ b/commands/msg/copy.go @@ -8,13 +8,17 @@ import ( "git.sr.ht/~rjarry/aerc/app" "git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/lib" + cryptoutil "git.sr.ht/~rjarry/aerc/lib/crypto/util" "git.sr.ht/~rjarry/aerc/lib/log" "git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/worker/types" + "github.com/emersion/go-message/mail" + "github.com/pkg/errors" ) type Copy struct { CreateFolders bool `opt:"-p"` + Decrypt bool `opt:"-d"` Account string `opt:"-a" complete:"CompleteAccount"` MultiFileStrategy *types.MultiFileStrategy `opt:"-m" action:"ParseMFS" complete:"CompleteMFS"` Folder string `opt:"folder" complete:"CompleteFolder"` @@ -75,6 +79,16 @@ func (c Copy) Execute(args []string) error { return err } + // when the decrypt flag is set, add the current account to c.Account to + // ensure that we do not take the store.Copy route. + if c.Decrypt { + if acct := app.SelectedAccount(); acct != nil { + c.Account = acct.Name() + } else { + return errors.New("no account name found") + } + } + if len(c.Account) == 0 { store.Copy(uids, c.Folder, c.CreateFolders, c.MultiFileStrategy, func(msg types.WorkerMessage) { @@ -97,6 +111,24 @@ func (c Copy) Execute(args []string) error { var messages []*types.FullMessage fetchDone := make(chan bool, 1) store.FetchFull(uids, func(fm *types.FullMessage) { + if fm == nil { + return + } + + if c.Decrypt { + h := new(mail.Header) + msg, ok := store.Messages[fm.Content.Uid] + if ok { + h = msg.RFC822Headers + } + cleartext, err := cryptoutil.Cleartext(fm.Content.Reader, *h) + if err != nil { + log.Debugf("could not decrypt message %v", fm.Content.Uid) + } else { + fm.Content.Reader = bytes.NewReader(cleartext) + } + } + messages = append(messages, fm) if len(messages) == len(uids) { fetchDone <- true diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index 7d6da4b9..26713206 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -319,10 +319,12 @@ message list, the message in the message viewer, etc). *-E*: Forces *[compose].edit-headers* = _false_ for this message only. -*:copy* [*-p*] [*-a* __] [*-m* __] __++ -*:cp* [*-p*] [*-a* __] [*-m* __] __ +*:copy* [*-dp*] [*-a* __] [*-m* __] __++ +*:cp* [*-dp*] [*-a* __] [*-m* __] __ Copies the selected message(s) to __. + *-d*: Decrypt the message before copying. + *-p*: Create __ if it does not exist. *-a*: Copy to __ of __. If __ does -- cgit