aboutsummaryrefslogtreecommitdiffstats
path: root/lib/templates/data.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2022-12-20 16:12:18 +0100
committerRobin Jarry <robin@jarry.cc>2023-01-04 22:57:31 +0100
commit2a290cf1ed6113f0d01eea3f2cff446339bed0ce (patch)
treea9a4c8c672c817b3ca963b59d7056e5b20fe3b8c /lib/templates/data.go
parent37e9a924894db7e5f232e82066155a60827c339b (diff)
downloadaerc-2a290cf1ed6113f0d01eea3f2cff446339bed0ce.tar.gz
templates: split code in multiple files
This makes room in preparation for more code in here. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib/templates/data.go')
-rw-r--r--lib/templates/data.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/templates/data.go b/lib/templates/data.go
new file mode 100644
index 00000000..5b351816
--- /dev/null
+++ b/lib/templates/data.go
@@ -0,0 +1,80 @@
+package templates
+
+import (
+ "time"
+
+ "git.sr.ht/~rjarry/aerc/models"
+ "github.com/emersion/go-message/mail"
+)
+
+type TemplateData struct {
+ To []*mail.Address
+ Cc []*mail.Address
+ Bcc []*mail.Address
+ From []*mail.Address
+ Date time.Time
+ Subject string
+ // Only available when replying with a quote
+ OriginalText string
+ OriginalFrom []*mail.Address
+ OriginalDate time.Time
+ OriginalMIMEType string
+}
+
+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")
+ }
+
+ td := TemplateData{
+ To: to,
+ Cc: cc,
+ Bcc: bcc,
+ From: from,
+ Date: time.Now(),
+ Subject: subject,
+ OriginalText: original.Text,
+ OriginalDate: original.Date,
+ OriginalMIMEType: original.MIMEType,
+ }
+ if original.RFC822Headers != nil {
+ origFrom, _ := original.RFC822Headers.AddressList("from")
+ td.OriginalFrom = origFrom
+ }
+ return td
+}
+
+// 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)
+}