2019-05-12 06:06:09 +02:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-07-16 22:48:25 +02:00
|
|
|
"bufio"
|
2019-09-11 21:28:14 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2019-05-14 19:07:48 +02:00
|
|
|
"io"
|
2019-05-13 22:04:01 +02:00
|
|
|
"io/ioutil"
|
2019-07-16 22:48:25 +02:00
|
|
|
"mime"
|
|
|
|
"net/http"
|
2019-05-14 19:07:48 +02:00
|
|
|
gomail "net/mail"
|
2019-05-13 22:04:01 +02:00
|
|
|
"os"
|
2019-05-12 06:06:09 +02:00
|
|
|
"os/exec"
|
2019-07-16 22:48:25 +02:00
|
|
|
"path/filepath"
|
2019-08-04 02:23:08 +02:00
|
|
|
"strings"
|
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"
|
2019-05-12 06:06:09 +02:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"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
|
|
|
|
2019-12-20 19:21:35 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/completer"
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/config"
|
2019-11-03 13:51:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/templates"
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
2020-01-08 21:44:14 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/models"
|
2019-05-18 02:57:10 +02:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/worker/types"
|
2019-05-12 06:06:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Composer struct {
|
2019-07-23 01:29:07 +02:00
|
|
|
editors map[string]*headerEditor
|
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
|
|
|
|
2019-12-12 16:22:28 +01:00
|
|
|
attachments []string
|
|
|
|
date time.Time
|
2019-07-16 22:48:25 +02:00
|
|
|
defaults map[string]string
|
|
|
|
editor *Terminal
|
|
|
|
email *os.File
|
|
|
|
grid *ui.Grid
|
2019-08-01 20:29:21 +02:00
|
|
|
header *ui.Grid
|
2019-12-12 16:22:28 +01:00
|
|
|
msgId string
|
2019-07-16 22:48:25 +02:00
|
|
|
review *reviewMessage
|
|
|
|
worker *types.Worker
|
2019-12-20 19:21:35 +01:00
|
|
|
completer *completer.Completer
|
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
|
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,
|
2020-01-08 21:44:14 +01:00
|
|
|
defaults map[string]string, original models.OriginalMail) (*Composer, error) {
|
2019-07-23 01:29:07 +02:00
|
|
|
|
|
|
|
if defaults == nil {
|
|
|
|
defaults = make(map[string]string)
|
|
|
|
}
|
|
|
|
if from := defaults["From"]; from == "" {
|
2020-04-24 11:42:21 +02:00
|
|
|
defaults["From"] = acctConfig.From
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 21:44:14 +01:00
|
|
|
templateData := templates.ParseTemplateData(defaults, original)
|
2019-12-20 19:21:35 +01:00
|
|
|
cmpl := completer.New(conf.Compose.AddressBookCmd, func(err error) {
|
2020-05-28 16:32:42 +02:00
|
|
|
aerc.PushError(fmt.Sprintf("could not complete header: %v", err))
|
2019-12-20 19:21:35 +01:00
|
|
|
worker.Logger.Printf("could not complete header: %v", err)
|
|
|
|
}, aerc.Logger())
|
2020-05-28 16:32:42 +02:00
|
|
|
layout, editors, focusable := buildComposeHeader(conf, cmpl, defaults)
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2019-05-13 22:04:01 +02:00
|
|
|
email, err := ioutil.TempFile("", "aerc-compose-*.eml")
|
|
|
|
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,
|
|
|
|
date: time.Now(),
|
|
|
|
defaults: defaults,
|
|
|
|
editors: editors,
|
|
|
|
email: email,
|
|
|
|
layout: layout,
|
|
|
|
msgId: mail.GenerateMessageID(),
|
|
|
|
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,
|
2019-07-23 01:29:07 +02:00
|
|
|
focusable: focusable,
|
2019-12-20 19:21:35 +01:00
|
|
|
completer: cmpl,
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
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
|
|
|
|
2019-08-01 20:29:21 +02:00
|
|
|
c.updateGrid()
|
2019-05-26 17:58:14 +02:00
|
|
|
c.ShowTerminal()
|
2019-05-13 22:24:05 +02:00
|
|
|
|
2019-11-03 13:51:14 +01:00
|
|
|
return c, nil
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:32:42 +02:00
|
|
|
func buildComposeHeader(conf *config.AercConfig, cmpl *completer.Completer,
|
2019-12-20 19:21:35 +01:00
|
|
|
defaults map[string]string) (
|
2019-07-23 18:52:33 +02:00
|
|
|
newLayout HeaderLayout,
|
|
|
|
editors map[string]*headerEditor,
|
2019-09-06 00:32:36 +02:00
|
|
|
focusable []ui.MouseableDrawableInteractive,
|
2019-07-23 18:52:33 +02:00
|
|
|
) {
|
2020-05-28 16:32:42 +02:00
|
|
|
layout := conf.Compose.HeaderLayout
|
2019-07-23 01:29:07 +02:00
|
|
|
editors = make(map[string]*headerEditor)
|
2019-09-06 00:32:36 +02:00
|
|
|
focusable = make([]ui.MouseableDrawableInteractive, 0)
|
2019-07-23 01:29:07 +02:00
|
|
|
|
|
|
|
for _, row := range layout {
|
|
|
|
for _, h := range row {
|
2020-05-28 16:32:42 +02:00
|
|
|
e := newHeaderEditor(h, "")
|
|
|
|
if conf.Ui.CompletionPopovers {
|
|
|
|
e.input.TabComplete(cmpl.ForHeader(h), conf.Ui.CompletionDelay)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
editors[h] = e
|
|
|
|
switch h {
|
|
|
|
case "From":
|
|
|
|
// Prepend From to support backtab
|
2019-09-06 00:32:36 +02:00
|
|
|
focusable = append([]ui.MouseableDrawableInteractive{e}, focusable...)
|
2019-07-23 01:29:07 +02:00
|
|
|
default:
|
|
|
|
focusable = append(focusable, e)
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
|
|
|
// Add Cc/Bcc editors to layout if in defaults and not already visible
|
|
|
|
for _, h := range []string{"Cc", "Bcc"} {
|
|
|
|
if val, ok := defaults[h]; ok && val != "" {
|
|
|
|
if _, ok := editors[h]; !ok {
|
2020-05-28 16:32:42 +02:00
|
|
|
e := newHeaderEditor(h, "")
|
|
|
|
if conf.Ui.CompletionPopovers {
|
|
|
|
e.input.TabComplete(cmpl.ForHeader(h), conf.Ui.CompletionDelay)
|
2019-12-20 19:21:35 +01:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
editors[h] = e
|
|
|
|
focusable = append(focusable, e)
|
|
|
|
layout = append(layout, []string{h})
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
|
|
|
// Set default values for all editors
|
|
|
|
for key := range editors {
|
|
|
|
if val, ok := defaults[key]; ok {
|
|
|
|
editors[key].input.Set(val)
|
|
|
|
delete(defaults, key)
|
|
|
|
}
|
2019-05-16 16:49:50 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
return layout, editors, focusable
|
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
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-09-03 21:34:08 +02:00
|
|
|
c.email.Seek(0, io.SeekStart)
|
2019-05-16 18:39:22 +02:00
|
|
|
io.Copy(c.email, reader)
|
2019-05-16 20:09:57 +02:00
|
|
|
c.email.Sync()
|
2019-09-03 21:34:08 +02:00
|
|
|
c.email.Seek(0, io.SeekStart)
|
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) {
|
|
|
|
c.email.Seek(0, io.SeekEnd)
|
|
|
|
io.Copy(c.email, reader)
|
|
|
|
c.email.Sync()
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-04-23 20:55:18 +02:00
|
|
|
return fmt.Errorf("Template loading failed: %v", err)
|
2019-12-05 16:23:21 +01:00
|
|
|
}
|
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
// add the headers contained in the template to the default headers
|
|
|
|
hf := mr.Header.Fields()
|
|
|
|
for hf.Next() {
|
|
|
|
var val string
|
|
|
|
var err error
|
|
|
|
if val, err = hf.Text(); err != nil {
|
|
|
|
val = hf.Value()
|
2019-12-05 16:23:21 +01:00
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
c.defaults[hf.Key()] = val
|
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 {
|
2020-04-23 20:55:18 +02:00
|
|
|
return fmt.Errorf("Could not get body of template: %v", 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
|
|
|
|
}
|
|
|
|
signature, err := ioutil.ReadFile(sigFile)
|
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +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 {
|
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-19 20:15:48 +02:00
|
|
|
func (c *Composer) FocusSubject() *Composer {
|
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused = 2
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-08-18 11:33:14 +02:00
|
|
|
func (c *Composer) FocusRecipient() *Composer {
|
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused = 1
|
|
|
|
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)) {
|
|
|
|
if editor, ok := c.editors[header]; ok {
|
|
|
|
editor.OnChange(func() {
|
|
|
|
fn(editor.input.String())
|
|
|
|
})
|
|
|
|
}
|
2019-05-14 22:18:21 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if c.editor == nil {
|
|
|
|
return "compose::review"
|
|
|
|
} else if c.editor == c.focusable[c.focused] {
|
|
|
|
return "compose::editor"
|
|
|
|
} else {
|
|
|
|
return "compose"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (c *Composer) Event(event tcell.Event) bool {
|
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) {
|
|
|
|
c.grid.MouseEvent(localX, localY, event)
|
|
|
|
for _, e := range c.focusable {
|
|
|
|
he, ok := e.(*headerEditor)
|
|
|
|
if ok && he.focused {
|
|
|
|
c.FocusEditor(he)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 06:06:09 +02:00
|
|
|
func (c *Composer) Focus(focus bool) {
|
2019-05-12 06:38:48 +02:00
|
|
|
c.focusable[c.focused].Focus(focus)
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:49:50 +02:00
|
|
|
func (c *Composer) PrepareHeader() (*mail.Header, []string, error) {
|
2020-04-23 20:55:18 +02:00
|
|
|
header := &mail.Header{}
|
|
|
|
for h, val := range c.defaults {
|
|
|
|
if val == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
header.SetText(h, val)
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
header.SetText("Message-Id", c.msgId)
|
|
|
|
header.SetDate(c.date)
|
2019-07-23 01:29:07 +02:00
|
|
|
|
|
|
|
headerKeys := make([]string, 0, len(c.editors))
|
|
|
|
for key := range c.editors {
|
|
|
|
headerKeys = append(headerKeys, key)
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
2019-07-23 01:29:07 +02:00
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
var rcpts []string
|
|
|
|
for h, editor := range c.editors {
|
|
|
|
val := editor.input.String()
|
|
|
|
if val == "" {
|
|
|
|
continue
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
switch h {
|
2020-04-23 20:55:18 +02:00
|
|
|
case "From", "To", "Cc", "Bcc": // Address headers
|
|
|
|
hdrRcpts, err := gomail.ParseAddressList(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrapf(err, "ParseAddressList(%s)", val)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
edRcpts := make([]*mail.Address, len(hdrRcpts))
|
|
|
|
for i, addr := range hdrRcpts {
|
|
|
|
edRcpts[i] = (*mail.Address)(addr)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
header.SetAddressList(h, edRcpts)
|
|
|
|
if h != "From" {
|
|
|
|
for _, addr := range edRcpts {
|
|
|
|
rcpts = append(rcpts, addr.Address)
|
2019-07-23 01:29:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2020-04-23 20:55:18 +02:00
|
|
|
header.SetText(h, val)
|
2019-06-21 20:33:09 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 20:55:18 +02:00
|
|
|
return header, rcpts, 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
|
|
|
|
|
|
|
if len(c.attachments) == 0 {
|
|
|
|
// don't create a multipart email if we only have text
|
2020-04-23 20:55:18 +02:00
|
|
|
return writeInlineBody(header, c.email, writer)
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise create a multipart email,
|
|
|
|
// with a multipart/alternative part for the text
|
|
|
|
w, err := mail.CreateWriter(writer, *header)
|
2019-05-14 19:07:48 +02:00
|
|
|
if err != nil {
|
2019-07-16 22:48:25 +02:00
|
|
|
return errors.Wrap(err, "CreateWriter")
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
2019-05-14 20:05:29 +02:00
|
|
|
defer w.Close()
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2020-04-23 20:55:18 +02:00
|
|
|
if err := writeMultipartBody(c.email, w); err != nil {
|
2019-07-31 19:33:50 +02:00
|
|
|
return errors.Wrap(err, "writeMultipartBody")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, a := range c.attachments {
|
|
|
|
if err := writeAttachment(a, w); err != nil {
|
|
|
|
return errors.Wrap(err, "writeAttachment")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func writeMultipartBody(body io.Reader, w *mail.Writer) error {
|
2019-07-16 22:48:25 +02:00
|
|
|
bh := mail.InlineHeader{}
|
|
|
|
bh.SetContentType("text/plain", map[string]string{"charset": "UTF-8"})
|
|
|
|
|
|
|
|
bi, err := w.CreateInline()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateInline")
|
|
|
|
}
|
|
|
|
defer bi.Close()
|
|
|
|
|
|
|
|
bw, err := bi.CreatePart(bh)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreatePart")
|
|
|
|
}
|
|
|
|
defer bw.Close()
|
2019-07-31 19:33:50 +02:00
|
|
|
if _, err := io.Copy(bw, body); err != nil {
|
2019-05-25 17:56:56 +02:00
|
|
|
return errors.Wrap(err, "io.Copy")
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-14 19:07:48 +02:00
|
|
|
}
|
|
|
|
|
2019-07-16 22:48:25 +02:00
|
|
|
// write the attachment specified by path to the message
|
|
|
|
func writeAttachment(path string, writer *mail.Writer) error {
|
|
|
|
filename := filepath.Base(path)
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "os.Open")
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
reader := bufio.NewReader(f)
|
|
|
|
|
|
|
|
// determine the MIME type
|
|
|
|
// http.DetectContentType only cares about the first 512 bytes
|
|
|
|
head, err := reader.Peek(512)
|
2019-07-31 20:21:13 +02:00
|
|
|
if err != nil && err != io.EOF {
|
2019-07-16 22:48:25 +02:00
|
|
|
return errors.Wrap(err, "Peek")
|
|
|
|
}
|
|
|
|
|
|
|
|
mimeString := http.DetectContentType(head)
|
|
|
|
// mimeString can contain type and params (like text encoding),
|
|
|
|
// so we need to break them apart before passing them to the headers
|
|
|
|
mimeType, params, err := mime.ParseMediaType(mimeString)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ParseMediaType")
|
|
|
|
}
|
|
|
|
params["name"] = filename
|
|
|
|
|
|
|
|
// set header fields
|
|
|
|
ah := mail.AttachmentHeader{}
|
|
|
|
ah.SetContentType(mimeType, params)
|
|
|
|
// setting the filename auto sets the content disposition
|
|
|
|
ah.SetFilename(filename)
|
|
|
|
|
|
|
|
aw, err := writer.CreateAttachment(ah)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "CreateAttachment")
|
|
|
|
}
|
|
|
|
defer aw.Close()
|
|
|
|
|
|
|
|
if _, err := reader.WriteTo(aw); err != nil {
|
|
|
|
return errors.Wrap(err, "reader.WriteTo")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-27 16:38:53 +02:00
|
|
|
func (c *Composer) GetAttachments() []string {
|
|
|
|
return c.attachments
|
|
|
|
}
|
|
|
|
|
2019-07-16 22:48:25 +02:00
|
|
|
func (c *Composer) AddAttachment(path string) {
|
|
|
|
c.attachments = append(c.attachments, path)
|
2019-07-27 16:38:53 +02:00
|
|
|
c.resetReview()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Composer) DeleteAttachment(path string) error {
|
|
|
|
for i, a := range c.attachments {
|
|
|
|
if a == path {
|
|
|
|
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)
|
2020-03-03 22:57:21 +01:00
|
|
|
c.grid.AddChild(c.review).At(2, 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 {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventMouse:
|
|
|
|
switch event.Buttons() {
|
|
|
|
case tcell.Button1:
|
|
|
|
c.FocusTerminal()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
func (c *Composer) termClosed(err error) {
|
|
|
|
c.grid.RemoveChild(c.editor)
|
2019-05-26 17:58:14 +02:00
|
|
|
c.review = newReviewMessage(c, err)
|
2020-03-03 22:57:21 +01:00
|
|
|
c.grid.AddChild(c.review).At(2, 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() {
|
|
|
|
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
|
2020-03-03 22:57:21 +01:00
|
|
|
c.grid.AddChild(c.editor).At(2, 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() {
|
|
|
|
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() {
|
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
c.focused = (c.focused + 1) % len(c.focusable)
|
|
|
|
c.focusable[c.focused].Focus(true)
|
|
|
|
}
|
|
|
|
|
2019-08-04 06:09:13 +02:00
|
|
|
func (c *Composer) FocusEditor(editor *headerEditor) {
|
|
|
|
c.focusable[c.focused].Focus(false)
|
|
|
|
for i, e := range c.focusable {
|
|
|
|
if e == editor {
|
|
|
|
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) {
|
2019-08-01 20:29:21 +02:00
|
|
|
if _, ok := c.editors[header]; ok {
|
2019-08-04 02:23:08 +02:00
|
|
|
if appendHeader {
|
|
|
|
header := c.editors[header].input.String()
|
|
|
|
value = strings.TrimSpace(header) + ", " + value
|
|
|
|
}
|
2019-08-01 20:29:21 +02:00
|
|
|
c.editors[header].input.Set(value)
|
2019-08-04 06:09:13 +02:00
|
|
|
if value == "" {
|
|
|
|
c.FocusEditor(c.editors[header])
|
|
|
|
}
|
2019-08-01 20:29:21 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-28 16:32:42 +02:00
|
|
|
e := newHeaderEditor(header, value)
|
2019-12-20 19:21:35 +01:00
|
|
|
if c.config.Ui.CompletionPopovers {
|
|
|
|
e.input.TabComplete(c.completer.ForHeader(header), c.config.Ui.CompletionDelay)
|
|
|
|
}
|
2019-08-01 20:29:21 +02: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],
|
|
|
|
)
|
|
|
|
c.updateGrid()
|
2019-08-04 06:09:13 +02:00
|
|
|
if value == "" {
|
|
|
|
c.FocusEditor(c.editors[header])
|
|
|
|
}
|
2019-08-01 20:29:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// updateGrid should be called when the underlying header layout is changed.
|
|
|
|
func (c *Composer) updateGrid() {
|
|
|
|
header, height := c.layout.grid(
|
|
|
|
func(h string) ui.Drawable { return c.editors[h] },
|
|
|
|
)
|
|
|
|
|
|
|
|
if c.grid == nil {
|
|
|
|
c.grid = ui.NewGrid().Columns([]ui.GridSpec{{ui.SIZE_WEIGHT, 1}})
|
|
|
|
}
|
|
|
|
|
|
|
|
c.grid.Rows([]ui.GridSpec{
|
|
|
|
{ui.SIZE_EXACT, height},
|
2020-03-03 22:57:21 +01:00
|
|
|
{ui.SIZE_EXACT, 1},
|
2019-08-01 20:29:21 +02:00
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
|
|
|
|
|
|
|
if c.header != nil {
|
|
|
|
c.grid.RemoveChild(c.header)
|
|
|
|
}
|
|
|
|
c.header = header
|
|
|
|
c.grid.AddChild(c.header).At(0, 0)
|
2020-03-03 22:57:21 +01:00
|
|
|
c.grid.AddChild(ui.NewFill(' ')).At(1, 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-05-28 16:32:42 +02:00
|
|
|
name string
|
|
|
|
focused bool
|
|
|
|
input *ui.TextInput
|
2019-05-13 22:24:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 16:32:42 +02:00
|
|
|
func newHeaderEditor(name string, value string) *headerEditor {
|
2019-05-12 06:06:09 +02:00
|
|
|
return &headerEditor{
|
2020-05-28 16:32:42 +02:00
|
|
|
input: ui.NewTextInput(value),
|
|
|
|
name: name,
|
2019-05-12 06:06:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (he *headerEditor) Draw(ctx *ui.Context) {
|
|
|
|
name := he.name + " "
|
|
|
|
size := runewidth.StringWidth(name)
|
2020-05-28 16:32:42 +02:00
|
|
|
ctx.Fill(0, 0, size, ctx.Height(), ' ', tcell.StyleDefault)
|
|
|
|
ctx.Printf(0, 0, tcell.StyleDefault.Bold(true), "%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) {
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventMouse:
|
2019-11-06 04:43:45 +01:00
|
|
|
switch event.Buttons() {
|
|
|
|
case tcell.Button1:
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-13 22:24:05 +02:00
|
|
|
type reviewMessage struct {
|
|
|
|
composer *Composer
|
|
|
|
grid *ui.Grid
|
|
|
|
}
|
|
|
|
|
2019-05-26 17:58:14 +02:00
|
|
|
func newReviewMessage(composer *Composer, err error) *reviewMessage {
|
2019-07-16 22:48:25 +02:00
|
|
|
spec := []ui.GridSpec{{ui.SIZE_EXACT, 2}, {ui.SIZE_EXACT, 1}}
|
2019-07-27 16:38:51 +02:00
|
|
|
for i := 0; i < len(composer.attachments)-1; i++ {
|
2019-07-16 22:48:25 +02:00
|
|
|
spec = append(spec, ui.GridSpec{ui.SIZE_EXACT, 1})
|
|
|
|
}
|
|
|
|
// make the last element fill remaining space
|
|
|
|
spec = append(spec, ui.GridSpec{ui.SIZE_WEIGHT, 1})
|
|
|
|
|
|
|
|
grid := ui.NewGrid().Rows(spec).Columns([]ui.GridSpec{
|
2019-05-13 22:24:05 +02:00
|
|
|
{ui.SIZE_WEIGHT, 1},
|
|
|
|
})
|
2019-07-16 22:48:25 +02:00
|
|
|
|
2019-05-26 17:58:14 +02:00
|
|
|
if err != nil {
|
2020-05-28 16:32:42 +02:00
|
|
|
grid.AddChild(ui.NewText(err.Error()).
|
|
|
|
Color(tcell.ColorRed, tcell.ColorDefault))
|
|
|
|
grid.AddChild(ui.NewText("Press [q] to close this tab.")).At(1, 0)
|
2019-05-26 17:58:14 +02:00
|
|
|
} else {
|
|
|
|
// TODO: source this from actual keybindings?
|
2020-05-28 16:32:42 +02:00
|
|
|
grid.AddChild(ui.NewText(
|
|
|
|
"Send this email? [y]es/[n]o/[p]ostpone/[e]dit/[a]ttach")).At(0, 0)
|
|
|
|
grid.AddChild(ui.NewText("Attachments:").
|
|
|
|
Reverse(true)).At(1, 0)
|
2019-07-16 22:48:25 +02:00
|
|
|
if len(composer.attachments) == 0 {
|
2020-05-28 16:32:42 +02:00
|
|
|
grid.AddChild(ui.NewText("(none)")).At(2, 0)
|
2019-07-16 22:48:25 +02:00
|
|
|
} else {
|
|
|
|
for i, a := range composer.attachments {
|
2020-05-28 16:32:42 +02:00
|
|
|
grid.AddChild(ui.NewText(a)).At(i+2, 0)
|
2019-07-16 22:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
}
|