| 1 |
package account |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
|
| 7 |
"git.sr.ht/~rjarry/aerc/app" |
| 8 |
"git.sr.ht/~rjarry/aerc/commands" |
| 9 |
"git.sr.ht/~rjarry/aerc/lib" |
| 10 |
"git.sr.ht/~rjarry/aerc/lib/state" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/templates" |
| 12 |
"git.sr.ht/~rjarry/aerc/models" |
| 13 |
) |
| 14 |
|
| 15 |
type ViewMessage struct { |
| 16 |
Peek bool `opt:"-p" desc:"Peek message without marking it as read."` |
| 17 |
Background bool `opt:"-b" desc:"Open message in a background tab."` |
| 18 |
} |
| 19 |
|
| 20 |
func init() { |
| 21 |
commands.Register(ViewMessage{}) |
| 22 |
} |
| 23 |
|
| 24 |
func (ViewMessage) Description() string { |
| 25 |
return "View the selected message in a new tab." |
| 26 |
} |
| 27 |
|
| 28 |
func (ViewMessage) Context() commands.CommandContext { |
| 29 |
return commands.MESSAGE_LIST |
| 30 |
} |
| 31 |
|
| 32 |
func (ViewMessage) Aliases() []string { |
| 33 |
return []string{"view-message", "view"} |
| 34 |
} |
| 35 |
|
| 36 |
func (v ViewMessage) Execute(args []string) error { |
| 37 |
acct := app.SelectedAccount() |
| 38 |
if acct == nil { |
| 39 |
return errors.New("No account selected") |
| 40 |
} |
| 41 |
if acct.Messages().Empty() { |
| 42 |
return nil |
| 43 |
} |
| 44 |
store := acct.Messages().Store() |
| 45 |
msg := acct.Messages().Selected() |
| 46 |
if msg == nil { |
| 47 |
return nil |
| 48 |
} |
| 49 |
_, deleted := store.Deleted[msg.Uid] |
| 50 |
if deleted { |
| 51 |
return nil |
| 52 |
} |
| 53 |
if msg.Error != nil { |
| 54 |
app.PushError(msg.Error.Error()) |
| 55 |
return nil |
| 56 |
} |
| 57 |
lib.NewMessageStoreView( |
| 58 |
msg, |
| 59 |
!v.Peek && acct.UiConfig().AutoMarkRead, |
| 60 |
store, |
| 61 |
app.CryptoProvider(), |
| 62 |
app.DecryptKeys, |
| 63 |
func(view lib.MessageView, err error) { |
| 64 |
if err != nil { |
| 65 |
app.PushError(err.Error()) |
| 66 |
return |
| 67 |
} |
| 68 |
viewer, err := app.NewMessageViewer(acct, view) |
| 69 |
if err != nil { |
| 70 |
app.PushError(err.Error()) |
| 71 |
return |
| 72 |
} |
| 73 |
data := state.NewDataSetter() |
| 74 |
data.SetAccount(acct.AccountConfig()) |
| 75 |
data.SetFolder(acct.Directories().SelectedDirectory()) |
| 76 |
data.SetHeaders(msg.RFC822Headers, &models.OriginalMail{}) |
| 77 |
var buf bytes.Buffer |
| 78 |
err = templates.Render(acct.UiConfig().TabTitleViewer, &buf, |
| 79 |
data.Data()) |
| 80 |
if err != nil { |
| 81 |
acct.PushError(err) |
| 82 |
return |
| 83 |
} |
| 84 |
if v.Background { |
| 85 |
app.NewBackgroundTab(viewer, buf.String()) |
| 86 |
} else { |
| 87 |
app.NewTab(viewer, buf.String()) |
| 88 |
} |
| 89 |
}) |
| 90 |
return nil |
| 91 |
} |