From 20ec2c8eeb2e071f28358814935a0f56672a9f49 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Tue, 10 Nov 2020 20:27:30 +0100 Subject: compose: use a proper header instead of a string map Prior to this commit, the composer was based on a map[string]string. While this approach was very versatile, it lead to a constant encoding / decoding of addresses and other headers. This commit switches to a different model, where the composer is based on a header. Commands which want to interact with it can simply set some defaults they would like to have. Users can overwrite them however they like. In order to get access to the functions generating / getting the msgid go-message was upgraded. --- lib/templates/template.go | 82 ++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 33 deletions(-) (limited to 'lib/templates') diff --git a/lib/templates/template.go b/lib/templates/template.go index f979ba2..197f159 100644 --- a/lib/templates/template.go +++ b/lib/templates/template.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "net/mail" "os" "os/exec" "path" @@ -12,6 +11,8 @@ import ( "text/template" "time" + "github.com/emersion/go-message/mail" + "git.sr.ht/~sircmpwn/aerc/models" "github.com/mitchellh/go-homedir" ) @@ -37,47 +38,34 @@ type TemplateData struct { OriginalMIMEType string } -func TestTemplateData() TemplateData { - defaults := map[string]string{ - "To": "John Doe ", - "Cc": "Josh Doe ", - "From": "Jane Smith ", - "Subject": "This is only a test", - } - - original := models.OriginalMail{ - Date: time.Now(), - From: "John Doe ", - Text: "This is only a test text", - MIMEType: "text/plain", +func ParseTemplateData(h *mail.Header, original models.OriginalMail) TemplateData { + // we ignore errors as this shouldn't fail the sending / replying even if + // something is wrong with the message we reply to + to, _ := h.AddressList("to") + cc, _ := h.AddressList("cc") + bcc, _ := h.AddressList("bcc") + from, _ := h.AddressList("from") + subject, err := h.Text("subject") + if err != nil { + subject = h.Get("subject") } - return ParseTemplateData(defaults, original) -} - -func ParseTemplateData(defaults map[string]string, original models.OriginalMail) TemplateData { td := TemplateData{ - To: parseAddressList(defaults["To"]), - Cc: parseAddressList(defaults["Cc"]), - Bcc: parseAddressList(defaults["Bcc"]), - From: parseAddressList(defaults["From"]), + To: to, + Cc: cc, + Bcc: bcc, + From: from, Date: time.Now(), - Subject: defaults["Subject"], + Subject: subject, OriginalText: original.Text, - OriginalFrom: parseAddressList(original.From), OriginalDate: original.Date, OriginalMIMEType: original.MIMEType, } - return td -} - -func parseAddressList(list string) []*mail.Address { - addrs, err := mail.ParseAddressList(list) - if err != nil { - return nil + if original.RFC822Headers != nil { + origFrom, _ := original.RFC822Headers.AddressList("from") + td.OriginalFrom = origFrom } - - return addrs + return td } // wrap allows to chain wrapText @@ -194,6 +182,34 @@ func findTemplate(templateName string, templateDirs []string) (string, error) { "Can't find template %q in any of %v ", templateName, templateDirs) } +//DummyData provides dummy data to test template validity +func DummyData() interface{} { + from := &mail.Address{ + Name: "John Doe", + Address: "john@example.com", + } + to := &mail.Address{ + Name: "Alice Doe", + Address: "alice@example.com", + } + h := &mail.Header{} + h.SetAddressList("from", []*mail.Address{from}) + h.SetAddressList("to", []*mail.Address{to}) + + oh := &mail.Header{} + oh.SetAddressList("from", []*mail.Address{to}) + oh.SetAddressList("to", []*mail.Address{from}) + + original := models.OriginalMail{ + Date: time.Now(), + From: from.String(), + Text: "This is only a test text", + MIMEType: "text/plain", + RFC822Headers: oh, + } + return ParseTemplateData(h, original) +} + func ParseTemplateFromFile(templateName string, templateDirs []string, data interface{}) (io.Reader, error) { templateFile, err := findTemplate(templateName, templateDirs) if err != nil { -- cgit 1.4.1-2-gfad0