| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"time" |
| 8 |
|
| 9 |
"git.sr.ht/~rjarry/aerc/app" |
| 10 |
"git.sr.ht/~rjarry/aerc/commands" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib" |
| 12 |
cryptoutil "git.sr.ht/~rjarry/aerc/lib/crypto/util" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 14 |
"git.sr.ht/~rjarry/aerc/models" |
| 15 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 16 |
"github.com/emersion/go-message/mail" |
| 17 |
"github.com/pkg/errors" |
| 18 |
) |
| 19 |
|
| 20 |
type Copy struct { |
| 21 |
CreateFolders bool `opt:"-p" desc:"Create folder if it does not exist."` |
| 22 |
Decrypt bool `opt:"-d" desc:"Decrypt the message before copying."` |
| 23 |
Account string `opt:"-a" complete:"CompleteAccount" desc:"Copy to the specified account."` |
| 24 |
MultiFileStrategy *types.MultiFileStrategy `opt:"-m" action:"ParseMFS" complete:"CompleteMFS" desc:"Multi-file strategy."` |
| 25 |
Folder string `opt:"folder" complete:"CompleteFolder" desc:"Target folder."` |
| 26 |
} |
| 27 |
|
| 28 |
func init() { |
| 29 |
commands.Register(Copy{}) |
| 30 |
} |
| 31 |
|
| 32 |
func (Copy) Description() string { |
| 33 |
return "Copy the selected message(s) to the specified folder." |
| 34 |
} |
| 35 |
|
| 36 |
func (Copy) Context() commands.CommandContext { |
| 37 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 38 |
} |
| 39 |
|
| 40 |
func (Copy) Aliases() []string { |
| 41 |
return []string{"cp", "copy"} |
| 42 |
} |
| 43 |
|
| 44 |
func (c *Copy) ParseMFS(arg string) error { |
| 45 |
if arg != "" { |
| 46 |
mfs, ok := types.StrToStrategy[arg] |
| 47 |
if !ok { |
| 48 |
return fmt.Errorf("invalid multi-file strategy %s", arg) |
| 49 |
} |
| 50 |
c.MultiFileStrategy = &mfs |
| 51 |
} |
| 52 |
return nil |
| 53 |
} |
| 54 |
|
| 55 |
func (*Copy) CompleteAccount(arg string) []string { |
| 56 |
return commands.FilterList(app.AccountNames(), arg, nil) |
| 57 |
} |
| 58 |
|
| 59 |
func (c *Copy) CompleteFolder(arg string) []string { |
| 60 |
var acct *app.AccountView |
| 61 |
if len(c.Account) > 0 { |
| 62 |
acct, _ = app.Account(c.Account) |
| 63 |
} else { |
| 64 |
acct = app.SelectedAccount() |
| 65 |
} |
| 66 |
if acct == nil { |
| 67 |
return nil |
| 68 |
} |
| 69 |
return commands.FilterList(acct.Directories().List(), arg, nil) |
| 70 |
} |
| 71 |
|
| 72 |
func (Copy) CompleteMFS(arg string) []string { |
| 73 |
return commands.FilterList(types.StrategyStrs(), arg, nil) |
| 74 |
} |
| 75 |
|
| 76 |
func (c Copy) Execute(args []string) error { |
| 77 |
h := newHelper() |
| 78 |
uids, err := h.markedOrSelectedUids() |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
store, err := h.store() |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
// when the decrypt flag is set, add the current account to c.Account to |
| 88 |
// ensure that we do not take the store.Copy route. |
| 89 |
if c.Decrypt { |
| 90 |
if acct := app.SelectedAccount(); acct != nil { |
| 91 |
c.Account = acct.Name() |
| 92 |
} else { |
| 93 |
return errors.New("no account name found") |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if len(c.Account) == 0 { |
| 98 |
store.Copy(uids, c.Folder, c.CreateFolders, c.MultiFileStrategy, |
| 99 |
func(msg types.WorkerMessage) { |
| 100 |
c.CallBack(msg, uids, store) |
| 101 |
}) |
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
destAcct, err := app.Account(c.Account) |
| 106 |
if err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
destStore := destAcct.Store() |
| 111 |
if destStore == nil { |
| 112 |
app.PushError(fmt.Sprintf("No message store in %s", c.Account)) |
| 113 |
return nil |
| 114 |
} |
| 115 |
|
| 116 |
var messages []*types.FullMessage |
| 117 |
fetchDone := make(chan bool, 1) |
| 118 |
store.FetchFull(context.TODO(), uids, func(fm *types.FullMessage) { |
| 119 |
if fm == nil { |
| 120 |
return |
| 121 |
} |
| 122 |
|
| 123 |
if c.Decrypt { |
| 124 |
h := new(mail.Header) |
| 125 |
msg, ok := store.Messages[fm.Content.Uid] |
| 126 |
if ok { |
| 127 |
h = msg.RFC822Headers |
| 128 |
} |
| 129 |
cleartext, err := cryptoutil.Cleartext(fm.Content.Reader, *h) |
| 130 |
if err != nil { |
| 131 |
log.Debugf("could not decrypt message %v", fm.Content.Uid) |
| 132 |
} else { |
| 133 |
fm.Content.Reader = bytes.NewReader(cleartext) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
messages = append(messages, fm) |
| 138 |
if len(messages) == len(uids) { |
| 139 |
fetchDone <- true |
| 140 |
} |
| 141 |
}) |
| 142 |
|
| 143 |
// Since this operation can take some time with some backends |
| 144 |
// (e.g. IMAP), provide some feedback to inform the user that |
| 145 |
// something is happening |
| 146 |
app.PushStatus("Copying messages...", 10*time.Second) |
| 147 |
go func() { |
| 148 |
defer log.PanicHandler() |
| 149 |
|
| 150 |
select { |
| 151 |
case <-fetchDone: |
| 152 |
break |
| 153 |
case <-time.After(30 * time.Second): |
| 154 |
// TODO: find a better way to determine if store.FetchFull() |
| 155 |
// has finished with some errors. |
| 156 |
app.PushError("Failed to fetch all messages") |
| 157 |
if len(messages) == 0 { |
| 158 |
return |
| 159 |
} |
| 160 |
} |
| 161 |
for _, fm := range messages { |
| 162 |
buf := new(bytes.Buffer) |
| 163 |
_, err = buf.ReadFrom(fm.Content.Reader) |
| 164 |
if err != nil { |
| 165 |
log.Warnf("failed to read message: %v", err) |
| 166 |
continue |
| 167 |
} |
| 168 |
destStore.Append( |
| 169 |
c.Folder, |
| 170 |
models.SeenFlag, |
| 171 |
time.Now(), |
| 172 |
buf, |
| 173 |
buf.Len(), |
| 174 |
func(msg types.WorkerMessage) { |
| 175 |
c.CallBack(msg, uids, store) |
| 176 |
}, |
| 177 |
) |
| 178 |
} |
| 179 |
}() |
| 180 |
return nil |
| 181 |
} |
| 182 |
|
| 183 |
func (c Copy) CallBack(msg types.WorkerMessage, uids []models.UID, store *lib.MessageStore) { |
| 184 |
dest := c.Folder |
| 185 |
if len(c.Account) != 0 { |
| 186 |
dest = fmt.Sprintf("%s in %s", c.Folder, c.Account) |
| 187 |
} |
| 188 |
|
| 189 |
switch msg := msg.(type) { |
| 190 |
case *types.Done: |
| 191 |
var s string |
| 192 |
if len(uids) > 1 { |
| 193 |
s = "%d messages copied to %s" |
| 194 |
} else { |
| 195 |
s = "%d message copied to %s" |
| 196 |
} |
| 197 |
app.PushStatus(fmt.Sprintf(s, len(uids), dest), 10*time.Second) |
| 198 |
store.Marker().ClearVisualMark() |
| 199 |
case *types.Error: |
| 200 |
app.PushError(msg.Error.Error()) |
| 201 |
} |
| 202 |
} |