2019-05-12 06:06:09 +02:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-09-11 21:28:14 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-05-14 19:07:48 +02:00
|
|
|
"io"
|
2020-11-10 20:27:30 +01:00
|
|
|
"net/textproto"
|
2019-05-13 22:04:01 +02:00
|
|
|
"os"
|
2019-05-12 06:06:09 +02:00
|
|
|
"os/exec"
|
2019-08-04 02:23:08 +02:00
|
|
|
"strings"
|
2022-09-26 20:56:01 +02:00
|
|
|
"sync"
|
2019-05-14 19:07:48 +02:00
|
|
|
"time"
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2019-05-14 19:07:48 +02:00
|
|
|
"github.com/emersion/go-message/mail"
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2019-05-12 06:06:09 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2019-09-11 21:28:14 +02:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-05-25 17:56:56 +02:00
|
|
|
"github.com/pkg/errors"
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/completer"
|
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2022-06-28 23:42:07 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/lib/format"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/templates"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/ui"
|
2022-07-19 22:31:51 +02:00
|
|
|
"git.sr.ht/~rjarry/aerc/logging"
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/models"
|
|
|
|
"git.sr.ht/~rjarry/aerc/worker/types"
|
2019-05-12 06:06:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Composer struct {
|
2022-09-26 20:56:01 +02:00
|
|
|
sync.Mutex
|
2020-11-10 20:27:30 +01:00
|
|
|
editors map[string]*headerEditor // indexes in lower case (from / cc / bcc)
|
|
|
|
header *mail.Header
|
|
|
|
parent models.OriginalMail // parent of current message, only set if reply
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2020-04-24 11:42:21 +02:00
|
|
|
acctConfig *config.AccountConfig
|
|
|
|
config *config.AercConfig
|
|
|
|
acct *AccountView
|
|
|
|
aerc *Aerc
|
2019-05-13 22:04:01 +02:00
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
attachments []lib.Attachment
|
2019-07-16 22:48:25 +02:00
|
|
|
editor *Terminal
|
|
|
|
email *os.File
|
|
|
|
grid *ui.Grid
|
2020-11-03 06:56:12 +01:00
|
|
|
heditors *ui.Grid // from, to, cc display a user can jump to
|
2019-07-16 22:48:25 +02:00
|
|
|
review *reviewMessage
|
|
|
|
worker *types.Worker
|
2019-12-20 19:21:35 +01:00
|
|
|
completer *completer.Completer
|
2022-04-29 14:48:15 +02:00
|
|
|
crypto *cryptoStatus
|
2021-12-30 10:25:08 +01:00
|
|
|
sign bool
|
2021-12-30 10:25:09 +01:00
|
|
|
encrypt bool
|
2022-05-05 19:53:16 +02:00
|
|
|
attachKey bool
|
2019-05-12 06:06:09 +02:00
|
|
|
|
2019-08-01 20:29:21 +02:00
|
|
|
layout HeaderLayout
|
2019-09-06 00:32:36 +02:00
|
|
|
focusable []ui.MouseableDrawableInteractive
|
2019-05-12 06:06:09 +02:00
|
|
|
focused int
|
2020-05-25 16:59:48 +02:00
|
|
|
sent bool
|
2019-08-18 11:33:15 +02:00
|
|
|
|
|
|
|
onClose []func(ti *Composer)
|
2019-09-06 00:32:36 +02:00
|
|
|
|
|
|
|
width int
|
2022-05-24 07:36:06 +02:00
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
textParts []*lib.Part
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:42:21 +02:00
|
|
|
func NewComposer(aerc *Aerc, acct *AccountView, conf *config.AercConfig,
|
2020-04-24 11:42:22 +02:00
|
|
|
acctConfig *config.AccountConfig, worker *types.Worker, template string,
|
2022-07-31 22:16:40 +02:00
|
|
|
h *mail.Header, orig models.OriginalMail,
|
|
|
|
) (*Composer, error) {
|
2020-11-10 20:27:30 +01:00
|
|
|
if h == nil {
|
|
|
|
h = new(mail.Header)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 16:19:45 +02:00
|
|
|
email, err := os.CreateTemp("", "aerc-compose-*.eml")
|
2019-05-13 22:04:01 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: handle this better
|
2019-11-03 13:51:14 +01:00
|
|
|
return nil, err
|
2019-05-13 22:04:01 +02:00
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
c := &Composer{
|
2020-04-24 11:42:21 +02:00
|
|
|
acct: acct,
|
|
|
|
acctConfig: acctConfig,
|
|
|
|
aerc: aerc,
|
|
|
|
config: conf,
|
2020-11-10 20:27:30 +01:00
|
|
|
header: h,
|
|
|
|
parent: orig,
|
2020-04-24 11:42:21 +02:00
|
|
|
email: email,
|
|
|
|
worker: worker,
|
2019-05-12 06:38:48 +02:00
|
|
|
// You have to backtab to get to "From", since you usually don't edit it
|
2019-05-13 22:04:01 +02:00
|
|
|
focused: 1,
|
2022-08-15 21:58:42 +02:00
|
|
|
completer: nil,
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
2022-08-15 21:58:42 +02:00
|
|
|
|
|
|
|
templateData := templates.ParseTemplateData(h, orig)
|
2019-11-03 13:51:14 +01:00
|
|
|
if err := c.AddTemplate(template, templateData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
c.AddSignature()
|
2019-09-11 21:28:14 +02:00
|
|
|
|
2022-08-15 21:58:42 +02:00
|
|
|
if err := c.setupFor(acct); err != nil {
|
|
|
|
return nil, err
|
2022-07-29 22:31:54 +02:00
|
|
|
}
|
2022-08-15 21:58:42 +02:00
|
|
|
|
2019-05-26 17:58:14 +02:00
|
|
|
c.ShowTerminal()
|
2019-05-13 22:24:05 +02:00
|
|
|
|
2022-08-15 21:58:42 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) SwitchAccount(newAcct *AccountView) error {
|
|
|
|
if c.acct == newAcct {
|
|
|
|
logging.Infof("same accounts: no switch")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// sync the header with the editors
|
|
|
|
for _, editor := range c.editors {
|
|
|
|
editor.storeValue()
|
|
|
|
}
|
|
|
|
// ensure that from header is updated, so remove it
|
|
|
|
c.header.Del("from")
|
|
|
|
// update entire composer with new the account
|
|
|
|
if err := c.setupFor(newAcct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// sync the header with the editors
|
|
|
|
for _, editor := range c.editors {
|
|
|
|
editor.loadValue()
|
|
|
|
}
|
|
|
|
c.Invalidate()
|
|
|
|
c.aerc.Invalidate()
|
|
|
|
logging.Infof("account sucessfully switched")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) setupFor(acct *AccountView) error {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2022-08-15 21:58:42 +02:00
|
|
|
// set new account and accountConfig
|
|
|
|
c.acct = acct
|
|
|
|
c.acctConfig = acct.AccountConfig()
|
|
|
|
c.worker = acct.Worker()
|
|
|
|
|
|
|
|
// Set from header if not already in header
|
|
|
|
if fl, err := c.header.AddressList("from"); err != nil || fl == nil {
|
|
|
|
fl, err = mail.ParseAddressList(c.acctConfig.From)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fl != nil {
|
|
|
|
c.header.SetAddressList("from", fl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update completer
|
|
|
|
cmd := c.acctConfig.AddressBookCmd
|
|
|
|
if cmd == "" {
|
|
|
|
cmd = c.config.Compose.AddressBookCmd
|
|
|
|
}
|
|
|
|
cmpl := completer.New(cmd, func(err error) {
|
|
|
|
c.aerc.PushError(
|
|
|
|
fmt.Sprintf("could not complete header: %v", err))
|
|
|
|
logging.Errorf("could not complete header: %v", err)
|
|
|
|
})
|
|
|
|
c.completer = cmpl
|
|
|
|
|
|
|
|
// if editor already exists, we have to get it from the focusable slice
|
|
|
|
// because this will be rebuild during buildComposeHeader()
|
|
|
|
var focusEditor ui.MouseableDrawableInteractive
|
|
|
|
if c.editor != nil && len(c.focusable) > 0 {
|
|
|
|
focusEditor = c.focusable[len(c.focusable)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// rebuild editors and focusable slice
|
|
|
|
c.buildComposeHeader(c.aerc, cmpl)
|
|
|
|
|
|
|
|
// restore the editor in the focusable list
|
|
|
|
if focusEditor != nil {
|
|
|
|
c.focusable = append(c.focusable, focusEditor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// redraw the grid
|
|
|
|
c.updateGrid()
|
|
|
|
|
|
|
|
// update the crypto parts
|
|
|
|
c.crypto = nil
|
|
|
|
c.sign = false
|
2022-05-05 19:53:14 +02:00
|
|
|
if c.acctConfig.PgpAutoSign {
|
2022-08-15 21:58:42 +02:00
|
|
|
err := c.SetSign(true)
|
2022-07-29 22:31:54 +02:00
|
|
|
logging.Warnf("failed to enable message signing: %v", err)
|
2022-05-05 19:53:14 +02:00
|
|
|
}
|
2022-08-15 21:58:42 +02:00
|
|
|
c.encrypt = false
|
2022-05-05 19:53:14 +02:00
|
|
|
if c.acctConfig.PgpOpportunisticEncrypt {
|
|
|
|
c.SetEncrypt(true)
|
|
|
|
}
|
2022-08-15 21:58:42 +02:00
|
|
|
err := c.updateCrypto()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to update crypto: %v", err)
|
|
|
|
}
|
2022-05-05 19:53:14 +02:00
|
|
|
|
2022-08-15 21:58:42 +02:00
|
|
|
return nil
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
func (c *Composer) buildComposeHeader(aerc *Aerc, cmpl *completer.Completer) {
|
|
|
|
c.layout = aerc.conf.Compose.HeaderLayout
|
|
|
|
c.editors = make(map[string]*headerEditor)
|
|
|
|
c.focusable = make([]ui.MouseableDrawableInteractive, 0)
|
2022-04-28 15:28:11 +02:00
|
|
|
uiConfig := c.acct.UiConfig()
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
for i, row := range c.layout {
|
|
|
|
for j, h := range row {
|
|
|
|
h = strings.ToLower(h)
|
|
|
|
c.layout[i][j] = h // normalize to lowercase
|
2022-02-25 00:21:06 +01:00
|
|
|
e := newHeaderEditor(h, c.header, uiConfig)
|
2020-07-27 10:03:55 +02:00
|
|
|
if aerc.conf.Ui.CompletionPopovers {
|
2022-02-25 00:21:06 +01:00
|
|
|
e.input.TabComplete(cmpl.ForHeader(h), uiConfig.CompletionDelay)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
c.editors[h] = e
|
2019-07-23 01:29:07 +02:00
|
|
|
switch h {
|
2020-11-10 20:27:30 +01:00
|
|
|
case "from":
|
2019-07-23 01:29:07 +02:00
|
|
|
// Prepend From to support backtab
|
2020-11-10 20:27:30 +01:00
|
|
|
c.focusable = append([]ui.MouseableDrawableInteractive{e}, c.focusable...)
|
2019-07-23 01:29:07 +02:00
|
|
|
default:
|
2020-11-10 20:27:30 +01:00
|
|
|
c.focusable = append(c.focusable, e)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
// Add Cc/Bcc editors to layout if present in header and not already visible
|
|
|
|
for _, h := range []string{"cc", "bcc"} {
|
|
|
|
if c.header.Has(h) {
|
|
|
|
if _, ok := c.editors[h]; !ok {
|
2022-02-25 00:21:06 +01:00
|
|
|
e := newHeaderEditor(h, c.header, uiConfig)
|
2020-07-27 10:03:55 +02:00
|
|
|
if aerc.conf.Ui.CompletionPopovers {
|
2022-02-25 00:21:06 +01:00
|
|
|
e.input.TabComplete(cmpl.ForHeader(h), uiConfig.CompletionDelay)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
c.editors[h] = e
|
|
|
|
c.focusable = append(c.focusable, e)
|
|
|
|
c.layout = append(c.layout, []string{h})
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
// load current header values into all editors
|
|
|
|
for _, e := range c.editors {
|
|
|
|
e.loadValue()
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 16:59:48 +02:00
|
|
|
func (c *Composer) SetSent() {
|
|
|
|
c.sent = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) Sent() bool {
|
|
|
|
return c.sent
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:53:16 +02:00
|
|
|
func (c *Composer) SetAttachKey(attach bool) error {
|
2022-06-28 23:42:07 +02:00
|
|
|
if !attach {
|
|
|
|
name := c.crypto.signKey + ".asc"
|
|
|
|
found := false
|
|
|
|
for _, a := range c.attachments {
|
|
|
|
if a.Name() == name {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
2022-07-29 22:31:54 +02:00
|
|
|
err := c.DeleteAttachment(name)
|
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("failed to delete attachment '%s: %w", name, err)
|
2022-07-29 22:31:54 +02:00
|
|
|
}
|
2022-06-28 23:42:07 +02:00
|
|
|
} else {
|
|
|
|
attach = !attach
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if attach {
|
|
|
|
var s string
|
|
|
|
var err error
|
|
|
|
if c.crypto.signKey == "" {
|
|
|
|
if c.acctConfig.PgpKeyId != "" {
|
|
|
|
s = c.acctConfig.PgpKeyId
|
|
|
|
} else {
|
|
|
|
s, err = getSenderEmail(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.crypto.signKey, err = c.aerc.Crypto.GetSignerKeyId(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := c.aerc.Crypto.ExportKey(c.crypto.signKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.attachments = append(c.attachments,
|
|
|
|
lib.NewPartAttachment(
|
|
|
|
lib.NewPart(
|
|
|
|
"application/pgp-keys",
|
|
|
|
map[string]string{"charset": "UTF-8"},
|
|
|
|
r,
|
|
|
|
),
|
|
|
|
c.crypto.signKey+".asc",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:53:16 +02:00
|
|
|
c.attachKey = attach
|
2022-06-28 23:42:07 +02:00
|
|
|
|
2022-05-05 19:53:16 +02:00
|
|
|
c.resetReview()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) AttachKey() bool {
|
|
|
|
return c.attachKey
|
|
|
|
}
|
|
|
|
|
2022-04-29 18:19:52 +02:00
|
|
|
func (c *Composer) SetSign(sign bool) error {
|
2021-12-30 10:25:08 +01:00
|
|
|
c.sign = sign
|
2022-04-29 18:19:52 +02:00
|
|
|
err := c.updateCrypto()
|
|
|
|
if err != nil {
|
|
|
|
c.sign = !sign
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("Cannot sign message: %w", err)
|
2022-04-29 18:19:52 +02:00
|
|
|
}
|
|
|
|
return nil
|
2021-12-30 10:25:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) Sign() bool {
|
|
|
|
return c.sign
|
|
|
|
}
|
|
|
|
|
2021-12-30 10:25:09 +01:00
|
|
|
func (c *Composer) SetEncrypt(encrypt bool) *Composer {
|
2022-05-05 19:53:15 +02:00
|
|
|
if !encrypt {
|
|
|
|
c.encrypt = encrypt
|
2022-07-29 22:31:54 +02:00
|
|
|
err := c.updateCrypto()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to update crypto: %v", err)
|
|
|
|
}
|
2022-05-05 19:53:15 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
// Check on any attempt to encrypt, and any lost focus of "to", "cc", or
|
|
|
|
// "bcc" field. Use OnFocusLost instead of OnChange to limit keyring checks
|
|
|
|
c.encrypt = c.checkEncryptionKeys("")
|
|
|
|
if c.crypto.setEncOneShot {
|
|
|
|
// Prevent registering a lot of callbacks
|
|
|
|
c.OnFocusLost("to", c.checkEncryptionKeys)
|
|
|
|
c.OnFocusLost("cc", c.checkEncryptionKeys)
|
|
|
|
c.OnFocusLost("bcc", c.checkEncryptionKeys)
|
|
|
|
c.crypto.setEncOneShot = false
|
|
|
|
}
|
2021-12-30 10:25:09 +01:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) Encrypt() bool {
|
|
|
|
return c.encrypt
|
|
|
|
}
|
|
|
|
|
2022-04-29 18:19:52 +02:00
|
|
|
func (c *Composer) updateCrypto() error {
|
2022-04-29 14:48:15 +02:00
|
|
|
if c.crypto == nil {
|
2022-05-04 14:04:05 +02:00
|
|
|
uiConfig := c.acct.UiConfig()
|
2022-07-03 17:11:12 +02:00
|
|
|
c.crypto = newCryptoStatus(uiConfig)
|
2022-04-29 14:48:15 +02:00
|
|
|
}
|
2022-04-29 18:19:52 +02:00
|
|
|
var err error
|
|
|
|
// Check if signKey is empty so we only run this once
|
|
|
|
if c.sign && c.crypto.signKey == "" {
|
|
|
|
cp := c.aerc.Crypto
|
|
|
|
var s string
|
|
|
|
if c.acctConfig.PgpKeyId != "" {
|
|
|
|
s = c.acctConfig.PgpKeyId
|
|
|
|
} else {
|
|
|
|
s, err = getSenderEmail(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.crypto.signKey, err = cp.GetSignerKeyId(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-29 14:48:15 +02:00
|
|
|
crHeight := 0
|
|
|
|
st := ""
|
|
|
|
switch {
|
|
|
|
case c.sign && c.encrypt:
|
2022-04-29 18:19:52 +02:00
|
|
|
st = fmt.Sprintf("Sign (%s) & Encrypt", c.crypto.signKey)
|
2022-04-29 14:48:15 +02:00
|
|
|
crHeight = 1
|
|
|
|
case c.sign:
|
2022-04-29 18:19:52 +02:00
|
|
|
st = fmt.Sprintf("Sign (%s)", c.crypto.signKey)
|
2022-04-29 14:48:15 +02:00
|
|
|
crHeight = 1
|
|
|
|
case c.encrypt:
|
|
|
|
st = "Encrypt"
|
|
|
|
crHeight = 1
|
|
|
|
default:
|
|
|
|
st = ""
|
|
|
|
}
|
|
|
|
c.crypto.status.Text(st)
|
|
|
|
hHeight := len(c.layout)
|
|
|
|
c.grid.Rows([]ui.GridSpec{
|
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(hHeight)},
|
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(crHeight)},
|
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)},
|
|
|
|
{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)},
|
|
|
|
})
|
|
|
|
c.grid.AddChild(c.crypto).At(1, 0)
|
2022-04-29 18:19:52 +02:00
|
|
|
return nil
|
2022-04-29 14:48:15 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 18:39:22 +02:00
|
|
|
// Note: this does not reload the editor. You must call this before the first
|
|
|
|
// Draw() call.
|
|
|
|
func (c *Composer) SetContents(reader io.Reader) *Composer {
|
2022-07-29 22:31:54 +02:00
|
|
|
_, err := c.email.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to seek beginning of mail: %v", err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(c.email, reader)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to copy mail: %v", err)
|
|
|
|
}
|
|
|
|
err = c.email.Sync()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to sync mail: %v", err)
|
|
|
|
}
|
|
|
|
_, err = c.email.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to seek beginning of mail after sync: %v", err)
|
|
|
|
}
|
2019-05-16 18:39:22 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-09-11 21:28:14 +02:00
|
|
|
func (c *Composer) AppendContents(reader io.Reader) {
|
2022-07-29 22:31:54 +02:00
|
|
|
_, err := c.email.Seek(0, io.SeekEnd)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to seek beginning of mail: %v", err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(c.email, reader)
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to copy mail: %v", err)
|
|
|
|
}
|
|
|
|
err = c.email.Sync()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed to sync mail: %v", err)
|
|
|
|
}
|
2019-09-11 21:28:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 07:36:06 +02:00
|
|
|
func (c *Composer) AppendPart(mimetype string, params map[string]string, body io.Reader) error {
|
|
|
|
if !strings.HasPrefix(mimetype, "text") {
|
|
|
|
return fmt.Errorf("can only append text mimetypes")
|
|
|
|
}
|
2022-06-28 23:42:07 +02:00
|
|
|
c.textParts = append(c.textParts, lib.NewPart(mimetype, params, body))
|
|
|
|
c.resetReview()
|
2022-05-24 07:36:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
func (c *Composer) AddTemplate(template string, data interface{}) error {
|
|
|
|
if template == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-10 17:00:21 +01:00
|
|
|
templateText, err := templates.ParseTemplateFromFile(
|
|
|
|
template, c.config.Templates.TemplateDirs, data)
|
2019-11-03 13:51:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
mr, err := mail.CreateReader(templateText)
|
2019-12-05 16:23:21 +01:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("Template loading failed: %w", err)
|
2019-12-05 16:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
// copy the headers contained in the template to the compose headers
|
2020-04-23 20:55:18 +02:00
|
|
|
hf := mr.Header.Fields()
|
|
|
|
for hf.Next() {
|
2020-11-10 20:27:30 +01:00
|
|
|
c.header.Set(hf.Key(), hf.Value())
|
2019-12-05 16:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
part, err := mr.NextPart()
|
2019-12-05 16:23:21 +01:00
|
|
|
if err != nil {
|
2022-07-31 15:15:27 +02:00
|
|
|
return fmt.Errorf("Could not get body of template: %w", err)
|
2019-12-05 16:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
c.AppendContents(part.Body)
|
2019-11-03 13:51:14 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-11 21:28:14 +02:00
|
|
|
func (c *Composer) AddSignature() {
|
|
|
|
var signature []byte
|
2020-04-24 11:42:21 +02:00
|
|
|
if c.acctConfig.SignatureCmd != "" {
|
2019-09-11 21:28:14 +02:00
|
|
|
var err error
|
|
|
|
signature, err = c.readSignatureFromCmd()
|
|
|
|
if err != nil {
|
|
|
|
signature = c.readSignatureFromFile()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
signature = c.readSignatureFromFile()
|
|
|
|
}
|
|
|
|
c.AppendContents(bytes.NewReader(signature))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) readSignatureFromCmd() ([]byte, error) {
|
2020-04-24 11:42:21 +02:00
|
|
|
sigCmd := c.acctConfig.SignatureCmd
|
2019-09-11 21:28:14 +02:00
|
|
|
cmd := exec.Command("sh", "-c", sigCmd)
|
|
|
|
signature, err := cmd.Output()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return signature, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) readSignatureFromFile() []byte {
|
2020-04-24 11:42:21 +02:00
|
|
|
sigFile := c.acctConfig.SignatureFile
|
2019-09-11 21:28:14 +02:00
|
|
|
if sigFile == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
sigFile, err := homedir.Expand(sigFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-17 16:19:45 +02:00
|
|
|
signature, err := os.ReadFile(sigFile)
|
2019-09-11 21:28:14 +02:00
|
|
|
if err != nil {
|
2020-07-27 10:03:55 +02:00
|
|
|
c.aerc.PushError(
|
|
|
|
fmt.Sprintf(" Error loading signature from file: %v", sigFile))
|
2019-09-11 21:28:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return signature
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:15:34 +02:00
|
|
|
func (c *Composer) FocusTerminal() *Composer {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-05-26 17:58:14 +02:00
|
|
|
if c.editor == nil {
|
|
|
|
return c
|
|
|
|
}
|
2019-05-16 18:15:34 +02:00
|
|
|
c.focusable[c.focused].Focus(false)
|
2019-07-23 01:29:07 +02:00
|
|
|
c.focused = len(c.editors)
|
2019-05-16 18:15:34 +02:00
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-07-23 01:29:07 +02:00
|
|
|
// OnHeaderChange registers an OnChange callback for the specified header.
|
|
|
|
func (c *Composer) OnHeaderChange(header string, fn func(subject string)) {
|
2020-11-10 20:27:30 +01:00
|
|
|
if editor, ok := c.editors[strings.ToLower(header)]; ok {
|
2019-07-23 01:29:07 +02:00
|
|
|
editor.OnChange(func() {
|
|
|
|
fn(editor.input.String())
|
|
|
|
})
|
|
|
|
}
|
2019-05-14 22:18:21 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 19:53:15 +02:00
|
|
|
// OnFocusLost registers an OnFocusLost callback for the specified header.
|
|
|
|
func (c *Composer) OnFocusLost(header string, fn func(input string) bool) {
|
|
|
|
if editor, ok := c.editors[strings.ToLower(header)]; ok {
|
|
|
|
editor.OnFocusLost(func() {
|
|
|
|
fn(editor.input.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:15 +02:00
|
|
|
func (c *Composer) OnClose(fn func(composer *Composer)) {
|
|
|
|
c.onClose = append(c.onClose, fn)
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (c *Composer) Draw(ctx *ui.Context) {
|
2019-09-06 00:32:36 +02:00
|
|
|
c.width = ctx.Width()
|
2019-05-12 06:06:09 +02:00
|
|
|
c.grid.Draw(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) Invalidate() {
|
|
|
|
c.grid.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) OnInvalidate(fn func(d ui.Drawable)) {
|
|
|
|
c.grid.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
fn(c)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:05:29 +02:00
|
|
|
func (c *Composer) Close() {
|
2019-08-18 11:33:15 +02:00
|
|
|
for _, onClose := range c.onClose {
|
|
|
|
onClose(c)
|
|
|
|
}
|
2019-05-14 20:05:29 +02:00
|
|
|
if c.email != nil {
|
|
|
|
path := c.email.Name()
|
|
|
|
c.email.Close()
|
|
|
|
os.Remove(path)
|
|
|
|
c.email = nil
|
|
|
|
}
|
|
|
|
if c.editor != nil {
|
|
|
|
c.editor.Destroy()
|
|
|
|
c.editor = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:27:28 +02:00
|
|
|
func (c *Composer) Bindings() string {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2022-07-31 14:32:48 +02:00
|
|
|
switch c.editor {
|
|
|
|
case nil:
|
2019-05-14 20:27:28 +02:00
|
|
|
return "compose::review"
|
2022-07-31 14:32:48 +02:00
|
|
|
case c.focusable[c.focused]:
|
2019-05-14 20:27:28 +02:00
|
|
|
return "compose::editor"
|
2022-07-31 14:32:48 +02:00
|
|
|
default:
|
2019-05-14 20:27:28 +02:00
|
|
|
return "compose"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (c *Composer) Event(event tcell.Event) bool {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-07-16 17:33:47 +02:00
|
|
|
if c.editor != nil {
|
|
|
|
return c.focusable[c.focused].Event(event)
|
|
|
|
}
|
|
|
|
return false
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2019-11-06 04:43:45 +01:00
|
|
|
func (c *Composer) MouseEvent(localX int, localY int, event tcell.Event) {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2022-10-05 21:25:51 +02:00
|
|
|
for _, e := range c.focusable {
|
|
|
|
he, ok := e.(*headerEditor)
|
|
|
|
if ok && he.focused {
|
|
|
|
he.focused = false
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 04:43:45 +01:00
|
|
|
c.grid.MouseEvent(localX, localY, event)
|
2022-10-04 20:06:55 +02:00
|
|
|
for i, e := range c.focusable {
|
2019-11-06 04:43:45 +01:00
|
|
|
he, ok := e.(*headerEditor)
|
|
|
|
if ok && he.focused {
|
2022-10-04 20:06:55 +02:00
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused = i
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
return
|
2019-11-06 04:43:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (c *Composer) Focus(focus bool) {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
2019-05-12 06:38:48 +02:00
|
|
|
c.focusable[c.focused].Focus(focus)
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Unlock()
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 19:07:48 +02:00
|
|
|
func (c *Composer) Config() *config.AccountConfig {
|
2020-04-24 11:42:21 +02:00
|
|
|
return c.acctConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) Account() *AccountView {
|
2019-05-26 17:58:14 +02:00
|
|
|
return c.acct
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
|
|
|
|
2019-05-16 01:41:21 +02:00
|
|
|
func (c *Composer) Worker() *types.Worker {
|
|
|
|
return c.worker
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// PrepareHeader finalizes the header, adding the value from the editors
|
2020-11-10 20:27:30 +01:00
|
|
|
func (c *Composer) PrepareHeader() (*mail.Header, error) {
|
|
|
|
for _, editor := range c.editors {
|
|
|
|
editor.storeValue()
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
// control headers not normally set by the user
|
|
|
|
// repeated calls to PrepareHeader should be a noop
|
|
|
|
if !c.header.Has("Message-Id") {
|
|
|
|
err := c.header.GenerateMessageID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-21 20:33:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
if !c.header.Has("Date") {
|
2022-09-13 17:12:12 +02:00
|
|
|
if c.acctConfig.SendAsUTC {
|
|
|
|
c.header.SetDate(time.Now().UTC())
|
|
|
|
} else {
|
|
|
|
c.header.SetDate(time.Now())
|
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
}
|
|
|
|
return c.header, nil
|
2019-05-14 20:05:29 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 10:25:08 +01:00
|
|
|
func getSenderEmail(c *Composer) (string, error) {
|
|
|
|
// add the from: field also to the 'recipients' list
|
|
|
|
if c.acctConfig.From == "" {
|
|
|
|
return "", errors.New("No 'From' configured for this account")
|
|
|
|
}
|
|
|
|
from, err := mail.ParseAddress(c.acctConfig.From)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "ParseAddress(config.From)")
|
|
|
|
}
|
|
|
|
return from.Address, nil
|
|
|
|
}
|
|
|
|
|
2021-12-30 10:25:09 +01:00
|
|
|
func getRecipientsEmail(c *Composer) ([]string, error) {
|
|
|
|
h, err := c.PrepareHeader()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "PrepareHeader")
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all 'recipients' from header (to:, cc:, bcc:)
|
|
|
|
rcpts := make(map[string]bool)
|
|
|
|
for _, key := range []string{"to", "cc", "bcc"} {
|
|
|
|
list, err := h.AddressList(key)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, entry := range list {
|
|
|
|
if entry != nil {
|
|
|
|
rcpts[entry.Address] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return email addresses as string slice
|
|
|
|
results := []string{}
|
2022-07-31 22:16:40 +02:00
|
|
|
for email := range rcpts {
|
2021-12-30 10:25:09 +01:00
|
|
|
results = append(results, email)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2019-05-14 20:05:29 +02:00
|
|
|
func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
|
2019-07-23 01:29:07 +02:00
|
|
|
if err := c.reloadEmail(); err != nil {
|
|
|
|
return err
|
2019-06-27 11:06:50 +02:00
|
|
|
}
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2021-12-30 10:25:09 +01:00
|
|
|
if c.sign || c.encrypt {
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2021-12-30 10:25:08 +01:00
|
|
|
var signedHeader mail.Header
|
|
|
|
signedHeader.SetContentType("text/plain", nil)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var cleartext io.WriteCloser
|
2021-12-30 10:25:09 +01:00
|
|
|
var err error
|
2019-07-31 19:33:50 +02:00
|
|
|
|
2022-04-25 15:30:44 +02:00
|
|
|
signer := ""
|
2021-12-30 10:25:09 +01:00
|
|
|
if c.sign {
|
2022-04-25 15:30:44 +02:00
|
|
|
if c.acctConfig.PgpKeyId != "" {
|
|
|
|
signer = c.acctConfig.PgpKeyId
|
|
|
|
} else {
|
|
|
|
signer, err = getSenderEmail(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-30 10:25:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.encrypt {
|
|
|
|
rcpts, err := getRecipientsEmail(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-25 15:30:44 +02:00
|
|
|
cleartext, err = c.aerc.Crypto.Encrypt(&buf, rcpts, signer, c.aerc.DecryptKeys, header)
|
2021-12-30 10:25:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-25 15:30:44 +02:00
|
|
|
cleartext, err = c.aerc.Crypto.Sign(&buf, signer, c.aerc.DecryptKeys, header)
|
2021-12-30 10:25:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-31 19:33:50 +02:00
|
|
|
}
|
2021-12-30 10:25:08 +01:00
|
|
|
|
|
|
|
err = writeMsgImpl(c, &signedHeader, cleartext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-25 16:22:49 +02:00
|
|
|
err = cleartext.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-29 22:31:54 +02:00
|
|
|
_, err = io.Copy(writer, &buf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write message: %w", err)
|
|
|
|
}
|
2021-12-30 10:25:08 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return writeMsgImpl(c, header, writer)
|
2019-07-31 19:33:50 +02:00
|
|
|
}
|
2021-12-30 10:25:08 +01:00
|
|
|
}
|
2019-07-31 19:33:50 +02:00
|
|
|
|
2021-12-30 10:25:08 +01:00
|
|
|
func writeMsgImpl(c *Composer, header *mail.Header, writer io.Writer) error {
|
2022-06-28 23:42:07 +02:00
|
|
|
if len(c.attachments) == 0 && len(c.textParts) == 0 {
|
|
|
|
// no attachments
|
2021-12-30 10:25:08 +01:00
|
|
|
return writeInlineBody(header, c.email, writer)
|
|
|
|
} else {
|
2022-06-28 23:42:07 +02:00
|
|
|
// with attachments
|
2021-12-30 10:25:08 +01:00
|
|
|
w, err := mail.CreateWriter(writer, *header)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateWriter")
|
|
|
|
}
|
2022-06-28 23:42:07 +02:00
|
|
|
parts := []*lib.Part{
|
|
|
|
lib.NewPart(
|
|
|
|
"text/plain",
|
|
|
|
map[string]string{"Charset": "UTF-8"},
|
|
|
|
c.email,
|
|
|
|
),
|
2022-05-24 07:36:06 +02:00
|
|
|
}
|
|
|
|
if err := writeMultipartBody(append(parts, c.textParts...), w); err != nil {
|
2021-12-30 10:25:08 +01:00
|
|
|
return errors.Wrap(err, "writeMultipartBody")
|
|
|
|
}
|
|
|
|
for _, a := range c.attachments {
|
2022-06-28 23:42:07 +02:00
|
|
|
if err := a.WriteTo(w); err != nil {
|
2021-12-30 10:25:08 +01:00
|
|
|
return errors.Wrap(err, "writeAttachment")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
}
|
2019-07-31 19:33:50 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeInlineBody(header *mail.Header, body io.Reader, writer io.Writer) error {
|
|
|
|
header.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
|
|
|
w, err := mail.CreateSingleInlineWriter(writer, *header)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateSingleInlineWriter")
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
if _, err := io.Copy(w, body); err != nil {
|
|
|
|
return errors.Wrap(err, "io.Copy")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the message body to the multipart message
|
2022-06-28 23:42:07 +02:00
|
|
|
func writeMultipartBody(parts []*lib.Part, w *mail.Writer) error {
|
2019-07-16 22:48:25 +02:00
|
|
|
bi, err := w.CreateInline()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateInline")
|
|
|
|
}
|
|
|
|
defer bi.Close()
|
|
|
|
|
2022-05-24 07:36:06 +02:00
|
|
|
for _, part := range parts {
|
|
|
|
bh := mail.InlineHeader{}
|
|
|
|
bh.SetContentType(part.MimeType, part.Params)
|
|
|
|
bw, err := bi.CreatePart(bh)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreatePart")
|
|
|
|
}
|
|
|
|
defer bw.Close()
|
|
|
|
if _, err := io.Copy(bw, part.Body); err != nil {
|
|
|
|
return errors.Wrap(err, "io.Copy")
|
|
|
|
}
|
2019-05-25 17:56:56 +02:00
|
|
|
}
|
2022-05-24 07:36:06 +02:00
|
|
|
|
2019-05-25 17:56:56 +02:00
|
|
|
return nil
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
func (c *Composer) GetAttachments() []string {
|
|
|
|
var names []string
|
|
|
|
for _, a := range c.attachments {
|
|
|
|
names = append(names, a.Name())
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
2022-06-28 23:42:07 +02:00
|
|
|
return names
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
func (c *Composer) AddAttachment(path string) {
|
|
|
|
c.attachments = append(c.attachments, lib.NewFileAttachment(path))
|
|
|
|
c.resetReview()
|
2019-07-27 16:38:53 +02:00
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
func (c *Composer) AddPartAttachment(name string, mimetype string, params map[string]string, body io.Reader) {
|
|
|
|
c.attachments = append(c.attachments, lib.NewPartAttachment(
|
|
|
|
lib.NewPart(mimetype, params, body), name,
|
|
|
|
))
|
2019-07-27 16:38:53 +02:00
|
|
|
c.resetReview()
|
|
|
|
}
|
|
|
|
|
2022-06-28 23:42:07 +02:00
|
|
|
func (c *Composer) DeleteAttachment(name string) error {
|
2019-07-27 16:38:53 +02:00
|
|
|
for i, a := range c.attachments {
|
2022-06-28 23:42:07 +02:00
|
|
|
if a.Name() == name {
|
2019-07-27 16:38:53 +02:00
|
|
|
c.attachments = append(c.attachments[:i], c.attachments[i+1:]...)
|
|
|
|
c.resetReview()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("attachment does not exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) resetReview() {
|
2019-07-16 22:48:25 +02:00
|
|
|
if c.review != nil {
|
|
|
|
c.grid.RemoveChild(c.review)
|
|
|
|
c.review = newReviewMessage(c, nil)
|
2022-04-29 14:48:15 +02:00
|
|
|
c.grid.AddChild(c.review).At(3, 0)
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 04:43:45 +01:00
|
|
|
func (c *Composer) termEvent(event tcell.Event) bool {
|
2022-07-31 14:32:48 +02:00
|
|
|
if event, ok := event.(*tcell.EventMouse); ok {
|
|
|
|
if event.Buttons() == tcell.Button1 {
|
2019-11-06 04:43:45 +01:00
|
|
|
c.FocusTerminal()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
func (c *Composer) termClosed(err error) {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2022-09-15 21:39:04 +02:00
|
|
|
if c.editor == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-05-13 22:24:05 +02:00
|
|
|
c.grid.RemoveChild(c.editor)
|
2019-05-26 17:58:14 +02:00
|
|
|
c.review = newReviewMessage(c, err)
|
2022-04-29 14:48:15 +02:00
|
|
|
c.grid.AddChild(c.review).At(3, 0)
|
2019-05-13 22:24:05 +02:00
|
|
|
c.editor.Destroy()
|
2019-05-14 20:05:29 +02:00
|
|
|
c.editor = nil
|
2019-05-26 17:58:14 +02:00
|
|
|
c.focusable = c.focusable[:len(c.focusable)-1]
|
|
|
|
if c.focused >= len(c.focusable) {
|
|
|
|
c.focused = len(c.focusable) - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) ShowTerminal() {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-05-26 17:58:14 +02:00
|
|
|
if c.editor != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c.review != nil {
|
|
|
|
c.grid.RemoveChild(c.review)
|
|
|
|
}
|
|
|
|
editorName := c.config.Compose.Editor
|
|
|
|
if editorName == "" {
|
|
|
|
editorName = os.Getenv("EDITOR")
|
|
|
|
}
|
|
|
|
if editorName == "" {
|
|
|
|
editorName = "vi"
|
|
|
|
}
|
2019-06-07 16:15:35 +02:00
|
|
|
editor := exec.Command("/bin/sh", "-c", editorName+" "+c.email.Name())
|
2019-05-26 17:58:14 +02:00
|
|
|
c.editor, _ = NewTerminal(editor) // TODO: handle error
|
2019-11-06 04:43:45 +01:00
|
|
|
c.editor.OnEvent = c.termEvent
|
2019-05-26 17:58:14 +02:00
|
|
|
c.editor.OnClose = c.termClosed
|
2022-04-29 14:48:15 +02:00
|
|
|
c.grid.AddChild(c.editor).At(3, 0)
|
2019-05-26 17:58:14 +02:00
|
|
|
c.focusable = append(c.focusable, c.editor)
|
2019-05-13 22:24:05 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 17:21:28 +02:00
|
|
|
func (c *Composer) PrevField() {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-05-12 17:21:28 +02:00
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused--
|
|
|
|
if c.focused == -1 {
|
|
|
|
c.focused = len(c.focusable) - 1
|
|
|
|
}
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) NextField() {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2019-05-12 17:21:28 +02:00
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused = (c.focused + 1) % len(c.focusable)
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
}
|
|
|
|
|
2022-03-22 00:23:47 +01:00
|
|
|
func (c *Composer) FocusEditor(editor string) {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2022-10-10 11:32:12 +02:00
|
|
|
c.focusEditor(editor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) focusEditor(editor string) {
|
2022-03-22 00:23:47 +01:00
|
|
|
editor = strings.ToLower(editor)
|
2019-08-04 06:09:13 +02:00
|
|
|
c.focusable[c.focused].Focus(false)
|
2022-03-22 00:23:47 +01:00
|
|
|
for i, f := range c.focusable {
|
|
|
|
e := f.(*headerEditor)
|
|
|
|
if strings.ToLower(e.name) == editor {
|
2019-08-04 06:09:13 +02:00
|
|
|
c.focused = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:29:21 +02:00
|
|
|
// AddEditor appends a new header editor to the compose window.
|
2019-08-04 02:23:08 +02:00
|
|
|
func (c *Composer) AddEditor(header string, value string, appendHeader bool) {
|
2022-09-26 20:56:01 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2020-11-10 20:27:30 +01:00
|
|
|
var editor *headerEditor
|
|
|
|
header = strings.ToLower(header)
|
|
|
|
if e, ok := c.editors[header]; ok {
|
|
|
|
e.storeValue() // flush modifications from the user to the header
|
|
|
|
editor = e
|
|
|
|
} else {
|
2022-04-28 15:28:11 +02:00
|
|
|
uiConfig := c.acct.UiConfig()
|
2022-02-25 00:21:06 +01:00
|
|
|
e := newHeaderEditor(header, c.header, uiConfig)
|
|
|
|
if uiConfig.CompletionPopovers {
|
|
|
|
e.input.TabComplete(c.completer.ForHeader(header), uiConfig.CompletionDelay)
|
2019-08-04 02:23:08 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
c.editors[header] = e
|
|
|
|
c.layout = append(c.layout, []string{header})
|
|
|
|
// Insert focus of new editor before terminal editor
|
|
|
|
c.focusable = append(
|
|
|
|
c.focusable[:len(c.focusable)-1],
|
|
|
|
e,
|
|
|
|
c.focusable[len(c.focusable)-1],
|
|
|
|
)
|
|
|
|
editor = e
|
|
|
|
}
|
|
|
|
|
|
|
|
if appendHeader {
|
|
|
|
currVal := editor.input.String()
|
|
|
|
if currVal != "" {
|
|
|
|
value = strings.TrimSpace(currVal) + ", " + value
|
2019-08-04 06:09:13 +02:00
|
|
|
}
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
if value != "" || appendHeader {
|
|
|
|
c.editors[header].input.Set(value)
|
|
|
|
editor.storeValue()
|
|
|
|
}
|
2019-08-04 06:09:13 +02:00
|
|
|
if value == "" {
|
2022-10-10 11:32:12 +02:00
|
|
|
c.focusEditor(c.editors[header].name)
|
2019-08-04 06:09:13 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
c.updateGrid()
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateGrid should be called when the underlying header layout is changed.
|
|
|
|
func (c *Composer) updateGrid() {
|
2020-11-03 06:56:12 +01:00
|
|
|
heditors, height := c.layout.grid(
|
2020-11-10 20:27:30 +01:00
|
|
|
func(h string) ui.Drawable {
|
|
|
|
return c.editors[h]
|
|
|
|
},
|
2019-08-01 20:29:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
if c.grid == nil {
|
2020-05-31 13:37:46 +02:00
|
|
|
c.grid = ui.NewGrid().Columns([]ui.GridSpec{
|
2022-03-18 09:53:02 +01:00
|
|
|
{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)},
|
2020-05-31 13:37:46 +02:00
|
|
|
})
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
2022-04-29 14:48:15 +02:00
|
|
|
crHeight := 0
|
|
|
|
if c.sign || c.encrypt {
|
|
|
|
crHeight = 1
|
|
|
|
}
|
2019-08-01 20:29:21 +02:00
|
|
|
c.grid.Rows([]ui.GridSpec{
|
2022-03-18 09:53:02 +01:00
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(height)},
|
2022-04-29 14:48:15 +02:00
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(crHeight)},
|
2022-03-18 09:53:02 +01:00
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)},
|
|
|
|
{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)},
|
2019-08-01 20:29:21 +02:00
|
|
|
})
|
|
|
|
|
2020-11-03 06:56:12 +01:00
|
|
|
if c.heditors != nil {
|
|
|
|
c.grid.RemoveChild(c.heditors)
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
2022-04-28 15:28:11 +02:00
|
|
|
borderStyle := c.acct.UiConfig().GetStyle(config.STYLE_BORDER)
|
|
|
|
borderChar := c.acct.UiConfig().BorderCharHorizontal
|
2020-11-03 06:56:12 +01:00
|
|
|
c.heditors = heditors
|
|
|
|
c.grid.AddChild(c.heditors).At(0, 0)
|
2022-04-29 14:48:15 +02:00
|
|
|
c.grid.AddChild(ui.NewFill(borderChar, borderStyle)).At(2, 0)
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 01:29:07 +02:00
|
|
|
func (c *Composer) reloadEmail() error {
|
|
|
|
name := c.email.Name()
|
|
|
|
c.email.Close()
|
|
|
|
file, err := os.Open(name)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ReloadEmail")
|
|
|
|
}
|
|
|
|
c.email = file
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
type headerEditor struct {
|
2020-07-27 10:03:55 +02:00
|
|
|
name string
|
2020-11-10 20:27:30 +01:00
|
|
|
header *mail.Header
|
2020-07-27 10:03:55 +02:00
|
|
|
focused bool
|
|
|
|
input *ui.TextInput
|
2022-07-03 17:11:12 +02:00
|
|
|
uiConfig *config.UIConfig
|
2019-05-13 22:24:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 20:27:30 +01:00
|
|
|
func newHeaderEditor(name string, h *mail.Header,
|
2022-07-31 22:16:40 +02:00
|
|
|
uiConfig *config.UIConfig,
|
|
|
|
) *headerEditor {
|
2020-11-10 20:27:30 +01:00
|
|
|
he := &headerEditor{
|
|
|
|
input: ui.NewTextInput("", uiConfig),
|
2020-07-27 10:03:55 +02:00
|
|
|
name: name,
|
2020-11-10 20:27:30 +01:00
|
|
|
header: h,
|
2020-07-27 10:03:55 +02:00
|
|
|
uiConfig: uiConfig,
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
he.loadValue()
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// extractHumanHeaderValue extracts the human readable string for key from the
|
|
|
|
// header. If a parsing error occurs the raw value is returned
|
2020-11-10 20:27:30 +01:00
|
|
|
func extractHumanHeaderValue(key string, h *mail.Header) string {
|
|
|
|
var val string
|
|
|
|
var err error
|
|
|
|
switch strings.ToLower(key) {
|
|
|
|
case "to", "from", "cc", "bcc":
|
|
|
|
var list []*mail.Address
|
|
|
|
list, err = h.AddressList(key)
|
|
|
|
val = format.FormatAddresses(list)
|
|
|
|
default:
|
|
|
|
val, err = h.Text(key)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
// if we can't parse it, show it raw
|
|
|
|
val = h.Get(key)
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// loadValue loads the value of he.name form the underlying header
|
|
|
|
// the value is decoded and meant for human consumption.
|
|
|
|
// decoding issues are ignored and return their raw values
|
2020-11-10 20:27:30 +01:00
|
|
|
func (he *headerEditor) loadValue() {
|
|
|
|
he.input.Set(extractHumanHeaderValue(he.name, he.header))
|
|
|
|
he.input.Invalidate()
|
|
|
|
}
|
|
|
|
|
2022-07-31 22:16:40 +02:00
|
|
|
// storeValue writes the current state back to the underlying header.
|
|
|
|
// errors are ignored
|
2020-11-10 20:27:30 +01:00
|
|
|
func (he *headerEditor) storeValue() {
|
|
|
|
val := he.input.String()
|
|
|
|
switch strings.ToLower(he.name) {
|
|
|
|
case "to", "from", "cc", "bcc":
|
2022-05-25 21:24:05 +02:00
|
|
|
if strings.TrimSpace(val) == "" {
|
2022-07-07 17:43:29 +02:00
|
|
|
// if header is empty, delete it
|
|
|
|
he.header.Del(he.name)
|
2022-05-25 21:24:05 +02:00
|
|
|
return
|
|
|
|
}
|
2020-11-10 20:27:30 +01:00
|
|
|
list, err := mail.ParseAddressList(val)
|
|
|
|
if err == nil {
|
|
|
|
he.header.SetAddressList(he.name, list)
|
|
|
|
} else {
|
|
|
|
// garbage, but it'll blow up upon sending and the user can
|
|
|
|
// fix the issue
|
|
|
|
he.header.SetText(he.name, val)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
he.header.SetText(he.name, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (he *headerEditor) Draw(ctx *ui.Context) {
|
2021-01-13 08:08:09 +01:00
|
|
|
name := textproto.CanonicalMIMEHeaderKey(he.name)
|
|
|
|
// Extra character to put a blank cell between the header and the input
|
2021-11-05 10:34:10 +01:00
|
|
|
size := runewidth.StringWidth(name+":") + 1
|
2020-07-27 10:03:55 +02:00
|
|
|
defaultStyle := he.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
headerStyle := he.uiConfig.GetStyle(config.STYLE_HEADER)
|
|
|
|
ctx.Fill(0, 0, size, ctx.Height(), ' ', defaultStyle)
|
2021-11-05 10:34:10 +01:00
|
|
|
ctx.Printf(0, 0, headerStyle, "%s:", name)
|
2019-05-12 06:06:09 +02:00
|
|
|
he.input.Draw(ctx.Subcontext(size, 0, ctx.Width()-size, 1))
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (he *headerEditor) MouseEvent(localX int, localY int, event tcell.Event) {
|
2022-07-31 14:32:48 +02:00
|
|
|
if event, ok := event.(*tcell.EventMouse); ok {
|
|
|
|
if event.Buttons() == tcell.Button1 {
|
2019-11-06 04:43:45 +01:00
|
|
|
he.focused = true
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
width := runewidth.StringWidth(he.name + " ")
|
|
|
|
if localX >= width {
|
|
|
|
he.input.MouseEvent(localX-width, localY, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (he *headerEditor) Invalidate() {
|
2019-05-12 06:38:48 +02:00
|
|
|
he.input.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (he *headerEditor) OnInvalidate(fn func(ui.Drawable)) {
|
|
|
|
he.input.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
fn(he)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (he *headerEditor) Focus(focused bool) {
|
2019-11-06 04:43:45 +01:00
|
|
|
he.focused = focused
|
2019-05-12 06:38:48 +02:00
|
|
|
he.input.Focus(focused)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (he *headerEditor) Event(event tcell.Event) bool {
|
|
|
|
return he.input.Event(event)
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
2019-05-13 22:24:05 +02:00
|
|
|
|
2019-05-14 22:18:21 +02:00
|
|
|
func (he *headerEditor) OnChange(fn func()) {
|
|
|
|
he.input.OnChange(func(_ *ui.TextInput) {
|
|
|
|
fn()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-05 19:53:15 +02:00
|
|
|
func (he *headerEditor) OnFocusLost(fn func()) {
|
|
|
|
he.input.OnFocusLost(func(_ *ui.TextInput) {
|
|
|
|
fn()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
type reviewMessage struct {
|
|
|
|
composer *Composer
|
|
|
|
grid *ui.Grid
|
|
|
|
}
|
|
|
|
|
2022-02-02 20:47:54 +01:00
|
|
|
var reviewCommands = [][]string{
|
|
|
|
{":send<enter>", "Send"},
|
|
|
|
{":edit<enter>", "Edit"},
|
|
|
|
{":attach<space>", "Add attachment"},
|
|
|
|
{":detach<space>", "Remove attachment"},
|
|
|
|
{":postpone<enter>", "Postpone"},
|
|
|
|
{":abort<enter>", "Abort (discard message, no confirmation)"},
|
|
|
|
{":choose -o d discard abort -o p postpone postpone<enter>", "Abort or postpone"},
|
|
|
|
}
|
|
|
|
|
2019-05-26 17:58:14 +02:00
|
|
|
func newReviewMessage(composer *Composer, err error) *reviewMessage {
|
2022-02-02 20:47:54 +01:00
|
|
|
bindings := composer.config.MergeContextualBinds(
|
|
|
|
composer.config.Bindings.ComposeReview,
|
|
|
|
config.BIND_CONTEXT_ACCOUNT,
|
|
|
|
composer.acctConfig.Name,
|
|
|
|
"compose::review",
|
|
|
|
)
|
|
|
|
|
|
|
|
var actions []string
|
|
|
|
|
|
|
|
for _, command := range reviewCommands {
|
|
|
|
cmd := command[0]
|
|
|
|
name := command[1]
|
|
|
|
strokes, _ := config.ParseKeyStrokes(cmd)
|
|
|
|
var inputs []string
|
|
|
|
for _, input := range bindings.GetReverseBindings(strokes) {
|
|
|
|
inputs = append(inputs, config.FormatKeyStrokes(input))
|
|
|
|
}
|
2022-04-12 11:49:31 +02:00
|
|
|
actions = append(actions, fmt.Sprintf(" %-6s %-40s %s",
|
2022-07-31 14:32:48 +02:00
|
|
|
strings.Join(inputs, ", "), name, cmd))
|
2022-02-02 20:47:54 +01:00
|
|
|
}
|
|
|
|
|
2020-05-31 13:37:46 +02:00
|
|
|
spec := []ui.GridSpec{
|
2022-03-18 09:53:02 +01:00
|
|
|
{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)},
|
2020-05-31 13:37:46 +02:00
|
|
|
}
|
2022-02-02 20:47:54 +01:00
|
|
|
for i := 0; i < len(actions)-1; i++ {
|
2022-03-18 09:53:02 +01:00
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
2022-02-02 20:47:54 +01:00
|
|
|
}
|
2022-03-18 09:53:02 +01:00
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(2)})
|
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
2019-07-27 16:38:51 +02:00
|
|
|
for i := 0; i < len(composer.attachments)-1; i++ {
|
2022-03-18 09:53:02 +01:00
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
2022-05-24 07:36:06 +02:00
|
|
|
if len(composer.textParts) > 0 {
|
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
|
|
|
for i := 0; i < len(composer.textParts); i++ {
|
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_EXACT, Size: ui.Const(1)})
|
|
|
|
}
|
|
|
|
}
|
2019-07-16 22:48:25 +02:00
|
|
|
// make the last element fill remaining space
|
2022-03-18 09:53:02 +01:00
|
|
|
spec = append(spec, ui.GridSpec{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)})
|
2019-07-16 22:48:25 +02:00
|
|
|
|
|
|
|
grid := ui.NewGrid().Rows(spec).Columns([]ui.GridSpec{
|
2022-03-18 09:53:02 +01:00
|
|
|
{Strategy: ui.SIZE_WEIGHT, Size: ui.Const(1)},
|
2019-05-13 22:24:05 +02:00
|
|
|
})
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2022-04-28 15:28:11 +02:00
|
|
|
uiConfig := composer.acct.UiConfig()
|
2020-07-27 10:03:55 +02:00
|
|
|
|
2019-05-26 17:58:14 +02:00
|
|
|
if err != nil {
|
2020-07-27 10:03:55 +02:00
|
|
|
grid.AddChild(ui.NewText(err.Error(), uiConfig.GetStyle(config.STYLE_ERROR)))
|
|
|
|
grid.AddChild(ui.NewText("Press [q] to close this tab.",
|
|
|
|
uiConfig.GetStyle(config.STYLE_DEFAULT))).At(1, 0)
|
2019-05-26 17:58:14 +02:00
|
|
|
} else {
|
2022-02-02 20:47:54 +01:00
|
|
|
grid.AddChild(ui.NewText("Send this email?",
|
|
|
|
uiConfig.GetStyle(config.STYLE_TITLE))).At(0, 0)
|
|
|
|
i := 1
|
|
|
|
for _, action := range actions {
|
|
|
|
grid.AddChild(ui.NewText(action,
|
|
|
|
uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0)
|
|
|
|
i += 1
|
|
|
|
}
|
2020-07-27 10:03:55 +02:00
|
|
|
grid.AddChild(ui.NewText("Attachments:",
|
2022-02-02 20:47:54 +01:00
|
|
|
uiConfig.GetStyle(config.STYLE_TITLE))).At(i, 0)
|
|
|
|
i += 1
|
2022-06-28 23:42:07 +02:00
|
|
|
if len(composer.attachments) == 0 {
|
2020-07-27 10:03:55 +02:00
|
|
|
grid.AddChild(ui.NewText("(none)",
|
2022-02-02 20:47:54 +01:00
|
|
|
uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0)
|
2022-05-24 07:36:06 +02:00
|
|
|
i += 1
|
2019-07-16 22:48:25 +02:00
|
|
|
} else {
|
2022-02-02 20:47:54 +01:00
|
|
|
for _, a := range composer.attachments {
|
2022-06-28 23:42:07 +02:00
|
|
|
grid.AddChild(ui.NewText(a.Name(), uiConfig.GetStyle(config.STYLE_DEFAULT))).
|
2022-02-02 20:47:54 +01:00
|
|
|
At(i, 0)
|
|
|
|
i += 1
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-24 07:36:06 +02:00
|
|
|
if len(composer.textParts) > 0 {
|
|
|
|
grid.AddChild(ui.NewText("Parts:",
|
|
|
|
uiConfig.GetStyle(config.STYLE_TITLE))).At(i, 0)
|
|
|
|
i += 1
|
|
|
|
grid.AddChild(ui.NewText("text/plain", uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0)
|
|
|
|
i += 1
|
|
|
|
for _, p := range composer.textParts {
|
|
|
|
grid.AddChild(ui.NewText(p.MimeType, uiConfig.GetStyle(config.STYLE_DEFAULT))).At(i, 0)
|
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-05-26 17:58:14 +02:00
|
|
|
}
|
2019-05-13 22:24:05 +02:00
|
|
|
|
|
|
|
return &reviewMessage{
|
|
|
|
composer: composer,
|
|
|
|
grid: grid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *reviewMessage) Invalidate() {
|
|
|
|
rm.grid.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *reviewMessage) OnInvalidate(fn func(ui.Drawable)) {
|
|
|
|
rm.grid.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
fn(rm)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *reviewMessage) Draw(ctx *ui.Context) {
|
|
|
|
rm.grid.Draw(ctx)
|
|
|
|
}
|
2022-04-29 14:48:15 +02:00
|
|
|
|
|
|
|
type cryptoStatus struct {
|
2022-05-05 19:53:15 +02:00
|
|
|
title string
|
|
|
|
status *ui.Text
|
|
|
|
uiConfig *config.UIConfig
|
|
|
|
signKey string
|
|
|
|
setEncOneShot bool
|
2022-04-29 14:48:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newCryptoStatus(uiConfig *config.UIConfig) *cryptoStatus {
|
|
|
|
defaultStyle := uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
return &cryptoStatus{
|
2022-05-05 19:53:15 +02:00
|
|
|
title: "Security",
|
|
|
|
status: ui.NewText("", defaultStyle),
|
|
|
|
uiConfig: uiConfig,
|
|
|
|
signKey: "",
|
|
|
|
setEncOneShot: true,
|
2022-04-29 14:48:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *cryptoStatus) Draw(ctx *ui.Context) {
|
|
|
|
// Extra character to put a blank cell between the header and the input
|
|
|
|
size := runewidth.StringWidth(cs.title+":") + 1
|
|
|
|
defaultStyle := cs.uiConfig.GetStyle(config.STYLE_DEFAULT)
|
|
|
|
titleStyle := cs.uiConfig.GetStyle(config.STYLE_HEADER)
|
|
|
|
ctx.Fill(0, 0, size, ctx.Height(), ' ', defaultStyle)
|
|
|
|
ctx.Printf(0, 0, titleStyle, "%s:", cs.title)
|
|
|
|
cs.status.Draw(ctx.Subcontext(size, 0, ctx.Width()-size, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *cryptoStatus) Invalidate() {
|
|
|
|
cs.status.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *cryptoStatus) OnInvalidate(fn func(ui.Drawable)) {
|
|
|
|
cs.status.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
fn(cs)
|
|
|
|
})
|
|
|
|
}
|
2022-05-05 19:53:15 +02:00
|
|
|
|
|
|
|
func (c *Composer) checkEncryptionKeys(_ string) bool {
|
|
|
|
rcpts, err := getRecipientsEmail(c)
|
|
|
|
if err != nil {
|
|
|
|
// checkEncryptionKeys gets registered as a callback and must
|
|
|
|
// explicitly call c.SetEncrypt(false) when encryption is not possible
|
|
|
|
c.SetEncrypt(false)
|
|
|
|
st := fmt.Sprintf("Cannot encrypt: %v", err)
|
|
|
|
c.aerc.statusline.PushError(st)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
var mk []string
|
|
|
|
for _, rcpt := range rcpts {
|
|
|
|
key, err := c.aerc.Crypto.GetKeyId(rcpt)
|
|
|
|
if err != nil || key == "" {
|
|
|
|
mk = append(mk, rcpt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(mk) > 0 {
|
|
|
|
c.SetEncrypt(false)
|
|
|
|
st := fmt.Sprintf("Cannot encrypt, missing keys: %s", strings.Join(mk, ", "))
|
|
|
|
c.aerc.statusline.PushError(st)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// If callbacks were registered, encrypt will be set when user removes
|
|
|
|
// recipients with missing keys
|
|
|
|
c.encrypt = true
|
2022-07-29 22:31:54 +02:00
|
|
|
err = c.updateCrypto()
|
|
|
|
if err != nil {
|
|
|
|
logging.Warnf("failed update crypto: %v", err)
|
|
|
|
}
|
2022-05-05 19:53:15 +02:00
|
|
|
return true
|
|
|
|
}
|