diff options
author | Reto Brunner <reto@labrat.space> | 2020-11-10 20:27:30 +0100 |
---|---|---|
committer | Reto Brunner <reto@labrat.space> | 2020-11-14 15:40:13 +0100 |
commit | 20ec2c8eeb2e071f28358814935a0f56672a9f49 (patch) | |
tree | 501b43a39a4152872bb6f08221da9c37fc75559c /lib | |
parent | 3ad3a5ede07c1248ae8176bdc19a623731c64056 (diff) | |
download | aerc-20ec2c8eeb2e071f28358814935a0f56672a9f49.tar.gz |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/format/format.go | 1 | ||||
-rw-r--r-- | lib/templates/template.go | 82 |
2 files changed, 50 insertions, 33 deletions
diff --git a/lib/format/format.go b/lib/format/format.go index e19ca319..2ba4d646 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -61,6 +61,7 @@ func AddressForHumans(a *mail.Address) string { var atom *regexp.Regexp = regexp.MustCompile("^[a-z0-9!#$%7'*+-/=?^_`{}|~ ]+$") +// FormatAddresses formats a list of addresses into a human readable string func FormatAddresses(l []*mail.Address) string { formatted := make([]string, len(l)) for i, a := range l { diff --git a/lib/templates/template.go b/lib/templates/template.go index f979ba23..197f159f 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 <john@example.com>", - "Cc": "Josh Doe <josh@example.com>", - "From": "Jane Smith <jane@example.com>", - "Subject": "This is only a test", - } - - original := models.OriginalMail{ - Date: time.Now(), - From: "John Doe <john@example.com>", - 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 { |