| 1 |
package msg |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"math/rand" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
|
| 10 |
_ "github.com/emersion/go-message/charset" |
| 11 |
"github.com/pkg/errors" |
| 12 |
|
| 13 |
"git.sr.ht/~rjarry/aerc/app" |
| 14 |
"git.sr.ht/~rjarry/aerc/commands" |
| 15 |
"git.sr.ht/~rjarry/aerc/config" |
| 16 |
"git.sr.ht/~rjarry/aerc/lib" |
| 17 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 18 |
"git.sr.ht/~rjarry/aerc/models" |
| 19 |
"git.sr.ht/~rjarry/aerc/worker/types" |
| 20 |
) |
| 21 |
|
| 22 |
type Recall struct { |
| 23 |
Force bool `opt:"-f" desc:"Force recall if not in postpone directory."` |
| 24 |
Edit bool `opt:"-e" desc:"Force [compose].edit-headers = true."` |
| 25 |
NoEdit bool `opt:"-E" desc:"Force [compose].edit-headers = false."` |
| 26 |
SkipEditor bool `opt:"-s" desc:"Skip the editor and go directly to the review screen."` |
| 27 |
} |
| 28 |
|
| 29 |
func init() { |
| 30 |
commands.Register(Recall{}) |
| 31 |
} |
| 32 |
|
| 33 |
func (Recall) Description() string { |
| 34 |
return "Open a postponed message for re-editing." |
| 35 |
} |
| 36 |
|
| 37 |
func (Recall) Context() commands.CommandContext { |
| 38 |
return commands.MESSAGE_LIST | commands.MESSAGE_VIEWER |
| 39 |
} |
| 40 |
|
| 41 |
func (Recall) Aliases() []string { |
| 42 |
return []string{"recall"} |
| 43 |
} |
| 44 |
|
| 45 |
func (r Recall) Execute(args []string) error { |
| 46 |
editHeaders := (config.Compose().EditHeaders || r.Edit) && !r.NoEdit |
| 47 |
|
| 48 |
widget := app.SelectedTabContent().(app.ProvidesMessage) |
| 49 |
acct := widget.SelectedAccount() |
| 50 |
if acct == nil { |
| 51 |
return errors.New("No account selected") |
| 52 |
} |
| 53 |
store := widget.Store() |
| 54 |
if store == nil { |
| 55 |
return errors.New("Cannot perform action. Messages still loading") |
| 56 |
} |
| 57 |
|
| 58 |
msgInfo, err := widget.SelectedMessage() |
| 59 |
if err != nil { |
| 60 |
return errors.Wrap(err, "Recall failed") |
| 61 |
} |
| 62 |
|
| 63 |
if acct.SelectedDirectory() != acct.AccountConfig().Postpone && |
| 64 |
!msgInfo.Flags.Has(models.DraftFlag) && !r.Force { |
| 65 |
return errors.New("Use -f to recall non-draft messages from outside the " + |
| 66 |
acct.AccountConfig().Postpone + " directory.") |
| 67 |
} |
| 68 |
|
| 69 |
log.Debugf("Recalling message <%s>", msgInfo.Envelope.MessageId) |
| 70 |
|
| 71 |
addTab := func(composer *app.Composer) { |
| 72 |
subject := msgInfo.Envelope.Subject |
| 73 |
if subject == "" { |
| 74 |
subject = "Recalled email" |
| 75 |
} |
| 76 |
composer.Tab = app.NewTab(composer, subject) |
| 77 |
composer.OnClose(func(composer *app.Composer) { |
| 78 |
uids := []models.UID{msgInfo.Uid} |
| 79 |
|
| 80 |
deleteMessage := func() { |
| 81 |
store.Delete( |
| 82 |
uids, |
| 83 |
nil, |
| 84 |
func(msg types.WorkerMessage) { |
| 85 |
switch msg := msg.(type) { |
| 86 |
case *types.Done: |
| 87 |
app.PushStatus("Recalled message deleted", 10*time.Second) |
| 88 |
case *types.Error: |
| 89 |
app.PushError(msg.Error.Error()) |
| 90 |
} |
| 91 |
}, |
| 92 |
) |
| 93 |
} |
| 94 |
|
| 95 |
if composer.Sent() || composer.Postponed() { |
| 96 |
deleteMessage() |
| 97 |
} |
| 98 |
}) |
| 99 |
} |
| 100 |
|
| 101 |
lib.NewMessageStoreView(msgInfo, acct.UiConfig().AutoMarkRead, |
| 102 |
store, app.CryptoProvider(), app.DecryptKeys, |
| 103 |
func(msg lib.MessageView, err error) { |
| 104 |
if err != nil { |
| 105 |
app.PushError(err.Error()) |
| 106 |
return |
| 107 |
} |
| 108 |
var path []int |
| 109 |
if len(msg.BodyStructure().Parts) != 0 { |
| 110 |
path = lib.FindPlaintext(msg.BodyStructure(), path) |
| 111 |
} |
| 112 |
|
| 113 |
msg.FetchBodyPart(path, func(reader io.Reader) { |
| 114 |
composer, err := app.NewComposer(acct, |
| 115 |
acct.AccountConfig(), acct.Worker(), editHeaders, |
| 116 |
"", msgInfo.RFC822Headers, nil, reader) |
| 117 |
if err != nil { |
| 118 |
app.PushError(err.Error()) |
| 119 |
return |
| 120 |
} |
| 121 |
if md := msg.MessageDetails(); md != nil { |
| 122 |
if md.IsEncrypted { |
| 123 |
composer.SetEncrypt(md.IsEncrypted) |
| 124 |
} |
| 125 |
if md.IsSigned { |
| 126 |
err = composer.SetSign(md.IsSigned) |
| 127 |
if err != nil { |
| 128 |
log.Warnf("failed to set signed state: %v", err) |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// add attachments if present |
| 134 |
var mu sync.Mutex |
| 135 |
parts := lib.FindAllNonMultipart(msg.BodyStructure(), nil, nil) |
| 136 |
for _, p := range parts { |
| 137 |
if lib.EqualParts(p, path) { |
| 138 |
continue |
| 139 |
} |
| 140 |
bs, err := msg.BodyStructure().PartAtIndex(p) |
| 141 |
if err != nil { |
| 142 |
log.Warnf("cannot get PartAtIndex %v: %v", p, err) |
| 143 |
continue |
| 144 |
} |
| 145 |
msg.FetchBodyPart(p, func(reader io.Reader) { |
| 146 |
mime := bs.FullMIMEType() |
| 147 |
params := lib.SetUtf8Charset(bs.Params) |
| 148 |
name, ok := params["name"] |
| 149 |
if !ok { |
| 150 |
name = fmt.Sprintf("%s_%s_%d", bs.MIMEType, bs.MIMESubType, rand.Uint64()) |
| 151 |
} |
| 152 |
mu.Lock() |
| 153 |
err := composer.AddPartAttachment(name, mime, params, reader) |
| 154 |
mu.Unlock() |
| 155 |
if err != nil { |
| 156 |
log.Errorf(err.Error()) |
| 157 |
app.PushError(err.Error()) |
| 158 |
} |
| 159 |
}) |
| 160 |
} |
| 161 |
|
| 162 |
if r.Force { |
| 163 |
composer.SetRecalledFrom(acct.SelectedDirectory()) |
| 164 |
} |
| 165 |
|
| 166 |
if r.SkipEditor { |
| 167 |
composer.Terminal().Close() |
| 168 |
} else { |
| 169 |
// focus the terminal since the header fields are likely already done |
| 170 |
composer.FocusTerminal() |
| 171 |
} |
| 172 |
addTab(composer) |
| 173 |
}) |
| 174 |
}) |
| 175 |
|
| 176 |
return nil |
| 177 |
} |