create OriginalMail struct

This commit is contained in:
Leszek Cimała 2020-01-08 21:44:14 +01:00 committed by Drew DeVault
parent da6fb1a155
commit bf28e23933
8 changed files with 44 additions and 24 deletions

View file

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
"git.sr.ht/~sircmpwn/getopt" "git.sr.ht/~sircmpwn/getopt"
) )
@ -31,7 +32,8 @@ func (Compose) Execute(aerc *widgets.Aerc, args []string) error {
acct := aerc.SelectedAccount() acct := aerc.SelectedAccount()
composer, err := widgets.NewComposer(aerc, composer, err := widgets.NewComposer(aerc,
aerc.Config(), acct.AccountConfig(), acct.Worker(), template, nil) aerc.Config(), acct.AccountConfig(), acct.Worker(),
template, nil, models.OriginalMail{})
if err != nil { if err != nil {
return err return err
} }

View file

@ -71,14 +71,16 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
"Subject": subject, "Subject": subject,
} }
original := models.OriginalMail{}
addTab := func() (*widgets.Composer, error) { addTab := func() (*widgets.Composer, error) {
if template != "" { if template != "" {
defaults["OriginalFrom"] = models.FormatAddresses(msg.Envelope.From) original.From = models.FormatAddresses(msg.Envelope.From)
defaults["OriginalDate"] = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM") original.Date = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")
} }
composer, err := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(), composer, err := widgets.NewComposer(aerc, aerc.Config(), acct.AccountConfig(),
acct.Worker(), template, defaults) acct.Worker(), template, defaults, original)
if err != nil { if err != nil {
aerc.PushError("Error: " + err.Error()) aerc.PushError("Error: " + err.Error())
return nil, err return nil, err
@ -138,7 +140,7 @@ func (forward) Execute(aerc *widgets.Aerc, args []string) error {
store.FetchBodyPart(msg.Uid, msg.BodyStructure, []int{1}, func(reader io.Reader) { store.FetchBodyPart(msg.Uid, msg.BodyStructure, []int{1}, func(reader io.Reader) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(reader) buf.ReadFrom(reader)
defaults["Original"] = buf.String() original.Text = buf.String()
addTab() addTab()
}) })
} }

View file

@ -116,15 +116,16 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
"Subject": subject, "Subject": subject,
"In-Reply-To": msg.Envelope.MessageId, "In-Reply-To": msg.Envelope.MessageId,
} }
original := models.OriginalMail{}
addTab := func() error { addTab := func() error {
if template != "" { if template != "" {
defaults["OriginalFrom"] = models.FormatAddresses(msg.Envelope.From) original.From = models.FormatAddresses(msg.Envelope.From)
defaults["OriginalDate"] = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM") original.Date = msg.Envelope.Date.Format("Mon Jan 2, 2006 at 3:04 PM")
} }
composer, err := widgets.NewComposer(aerc, aerc.Config(), composer, err := widgets.NewComposer(aerc, aerc.Config(),
acct.AccountConfig(), acct.Worker(), template, defaults) acct.AccountConfig(), acct.Worker(), template, defaults, original)
if err != nil { if err != nil {
aerc.PushError("Error: " + err.Error()) aerc.PushError("Error: " + err.Error())
return err return err
@ -155,7 +156,7 @@ func (reply) Execute(aerc *widgets.Aerc, args []string) error {
store.FetchBodyPart(msg.Uid, msg.BodyStructure, []int{1}, func(reader io.Reader) { store.FetchBodyPart(msg.Uid, msg.BodyStructure, []int{1}, func(reader io.Reader) {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(reader) buf.ReadFrom(reader)
defaults["Original"] = buf.String() original.Text = buf.String()
addTab() addTab()
}) })
return nil return nil

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"git.sr.ht/~sircmpwn/aerc/lib" "git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/widgets" "git.sr.ht/~sircmpwn/aerc/widgets"
) )
@ -94,6 +95,7 @@ func unsubscribeMailto(aerc *widgets.Aerc, u *url.URL) error {
acct.Worker(), acct.Worker(),
"", "",
defaults, defaults,
models.OriginalMail{},
) )
if err != nil { if err != nil {
return err return err

View file

@ -10,6 +10,7 @@ import (
"text/template" "text/template"
"time" "time"
"git.sr.ht/~sircmpwn/aerc/models"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
) )
@ -28,20 +29,23 @@ type TemplateData struct {
func TestTemplateData() TemplateData { func TestTemplateData() TemplateData {
defaults := map[string]string{ defaults := map[string]string{
"To": "John Doe <john@example.com>", "To": "John Doe <john@example.com>",
"Cc": "Josh Doe <josh@example.com>", "Cc": "Josh Doe <josh@example.com>",
"From": "Jane Smith <jane@example.com>", "From": "Jane Smith <jane@example.com>",
"Subject": "This is only a test", "Subject": "This is only a test",
"OriginalText": "This is only a test text",
"OriginalFrom": "John Doe <john@example.com>",
"OriginalDate": time.Now().Format("Mon Jan 2, 2006 at 3:04 PM"),
} }
return ParseTemplateData(defaults) original := models.OriginalMail{
Date: time.Now().Format("Mon Jan 2, 2006 at 3:04 PM"),
From: "John Doe <john@example.com>",
Text: "This is only a test text",
}
return ParseTemplateData(defaults, original)
} }
func ParseTemplateData(defaults map[string]string) TemplateData { func ParseTemplateData(defaults map[string]string, original models.OriginalMail) TemplateData {
originalDate, _ := time.Parse("Mon Jan 2, 2006 at 3:04 PM", defaults["OriginalDate"]) originalDate, _ := time.Parse("Mon Jan 2, 2006 at 3:04 PM", original.Date)
td := TemplateData{ td := TemplateData{
To: parseAddressList(defaults["To"]), To: parseAddressList(defaults["To"]),
Cc: parseAddressList(defaults["Cc"]), Cc: parseAddressList(defaults["Cc"]),
@ -49,8 +53,8 @@ func ParseTemplateData(defaults map[string]string) TemplateData {
From: parseAddressList(defaults["From"]), From: parseAddressList(defaults["From"]),
Date: time.Now(), Date: time.Now(),
Subject: defaults["Subject"], Subject: defaults["Subject"],
OriginalText: defaults["Original"], OriginalText: original.Text,
OriginalFrom: parseAddressList(defaults["OriginalFrom"]), OriginalFrom: parseAddressList(original.From),
OriginalDate: originalDate, OriginalDate: originalDate,
} }
return td return td

View file

@ -164,3 +164,10 @@ func FormatAddresses(addrs []*Address) string {
} }
return val.String() return val.String()
} }
// OriginalMail is helper struct used for reply/forward
type OriginalMail struct {
Date string
From string
Text string
}

View file

@ -14,6 +14,7 @@ import (
"git.sr.ht/~sircmpwn/aerc/config" "git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/lib" "git.sr.ht/~sircmpwn/aerc/lib"
"git.sr.ht/~sircmpwn/aerc/lib/ui" "git.sr.ht/~sircmpwn/aerc/lib/ui"
"git.sr.ht/~sircmpwn/aerc/models"
) )
type Aerc struct { type Aerc struct {
@ -432,7 +433,7 @@ func (aerc *Aerc) Mailto(addr *url.URL) error {
} }
} }
composer, err := NewComposer(aerc, aerc.Config(), composer, err := NewComposer(aerc, aerc.Config(),
acct.AccountConfig(), acct.Worker(), "", defaults) acct.AccountConfig(), acct.Worker(), "", defaults, models.OriginalMail{})
if err != nil { if err != nil {
return nil return nil
} }

View file

@ -26,6 +26,7 @@ import (
"git.sr.ht/~sircmpwn/aerc/config" "git.sr.ht/~sircmpwn/aerc/config"
"git.sr.ht/~sircmpwn/aerc/lib/templates" "git.sr.ht/~sircmpwn/aerc/lib/templates"
"git.sr.ht/~sircmpwn/aerc/lib/ui" "git.sr.ht/~sircmpwn/aerc/lib/ui"
"git.sr.ht/~sircmpwn/aerc/models"
"git.sr.ht/~sircmpwn/aerc/worker/types" "git.sr.ht/~sircmpwn/aerc/worker/types"
) )
@ -59,7 +60,7 @@ type Composer struct {
func NewComposer(aerc *Aerc, conf *config.AercConfig, func NewComposer(aerc *Aerc, conf *config.AercConfig,
acct *config.AccountConfig, worker *types.Worker, template string, acct *config.AccountConfig, worker *types.Worker, template string,
defaults map[string]string) (*Composer, error) { defaults map[string]string, original models.OriginalMail) (*Composer, error) {
if defaults == nil { if defaults == nil {
defaults = make(map[string]string) defaults = make(map[string]string)
@ -68,7 +69,7 @@ func NewComposer(aerc *Aerc, conf *config.AercConfig,
defaults["From"] = acct.From defaults["From"] = acct.From
} }
templateData := templates.ParseTemplateData(defaults) templateData := templates.ParseTemplateData(defaults, original)
cmpl := completer.New(conf.Compose.AddressBookCmd, func(err error) { cmpl := completer.New(conf.Compose.AddressBookCmd, func(err error) {
aerc.PushError(fmt.Sprintf("could not complete header: %v", err)) aerc.PushError(fmt.Sprintf("could not complete header: %v", err))
worker.Logger.Printf("could not complete header: %v", err) worker.Logger.Printf("could not complete header: %v", err)