about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Burwell <ben@benburwell.com>2019-07-15 10:30:20 -0400
committerDrew DeVault <sir@cmpwn.com>2019-07-17 15:58:15 -0400
commiteb0481b9cc7d7cfff9d9c2e163bbe106eb2a0de4 (patch)
treec51650d3c53e380c3105d8da9aa18747a3c03fc1
parent8bb115dbae928111be52ed422dafb1caf72a744c (diff)
downloadaerc-eb0481b9cc7d7cfff9d9c2e163bbe106eb2a0de4.tar.gz
Fix header decoding
Email headers can be encoded with different charsets, which is signalled
using a special character sequence. The go-message package provides two
different methods for accessing header values, Get(key) (actually
inherited from the embedded textproto.Header) which returns the raw
header value and Text(key), which returns the header's value decoded as
UTF-8.

Before, in the maildir backend, we were using the Get method which
sometimes resulted in encoded headers being displayed in the UI. This
patch replaces the incorrect usage of Get() with Text().
-rw-r--r--worker/maildir/message.go29
1 files changed, 16 insertions, 13 deletions
diff --git a/worker/maildir/message.go b/worker/maildir/message.go
index b95ec98..b732324 100644
--- a/worker/maildir/message.go
+++ b/worker/maildir/message.go
@@ -7,7 +7,6 @@ import (
 	"io"
 	"io/ioutil"
 	"mime/quotedprintable"
-	gomail "net/mail"
 	"strings"
 
 	"github.com/emersion/go-maildir"
@@ -88,7 +87,7 @@ func (m Message) MessageInfo() (*models.MessageInfo, error) {
 	if err != nil {
 		return nil, fmt.Errorf("could not get structure: %v", err)
 	}
-	env, err := parseEnvelope(&msg.Header)
+	env, err := parseEnvelope(&mail.Header{msg.Header})
 	if err != nil {
 		return nil, fmt.Errorf("could not get envelope: %v", err)
 	}
@@ -237,8 +236,8 @@ func parseEntityStructure(e *message.Entity) (*models.BodyStructure, error) {
 	return &body, nil
 }
 
-func parseEnvelope(h *message.Header) (*models.Envelope, error) {
-	date, err := gomail.ParseDate(h.Get("date"))
+func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
+	date, err := h.Date()
 	if err != nil {
 		return nil, fmt.Errorf("could not parse date header: %v", err)
 	}
@@ -258,10 +257,18 @@ func parseEnvelope(h *message.Header) (*models.Envelope, error) {
 	if err != nil {
 		return nil, fmt.Errorf("could not read bcc address: %v", err)
 	}
+	subj, err := h.Subject()
+	if err != nil {
+		return nil, fmt.Errorf("could not read subject: %v", err)
+	}
+	msgID, err := h.Text("message-id")
+	if err != nil {
+		return nil, fmt.Errorf("could not read message id: %v", err)
+	}
 	return &models.Envelope{
 		Date:      date,
-		Subject:   h.Get("subject"),
-		MessageId: h.Get("message-id"),
+		Subject:   subj,
+		MessageId: msgID,
 		From:      from,
 		To:        to,
 		Cc:        cc,
@@ -269,15 +276,11 @@ func parseEnvelope(h *message.Header) (*models.Envelope, error) {
 	}, nil
 }
 
-func parseAddressList(h *message.Header, key string) ([]*models.Address, error) {
+func parseAddressList(h *mail.Header, key string) ([]*models.Address, error) {
 	var converted []*models.Address
-	hdr := h.Get(key)
-	if strings.TrimSpace(hdr) == "" {
-		return converted, nil
-	}
-	addrs, err := gomail.ParseAddressList(hdr)
+	addrs, err := h.AddressList(key)
 	if err != nil {
-		if strings.Index(hdr, "@") < 0 {
+		if hdr, err := h.Text(key); err != nil && strings.Index(hdr, "@") < 0 {
 			return []*models.Address{&models.Address{
 				Name: hdr,
 			}}, nil
mes to lowercase' href='/akkartik/mu/commit/examples/ex10.subx?h=main&id=333525360b22f3d3ea31db46a4d2f1b4edbfebdb'>33352536 ^
9d27e966 ^
33352536 ^
ee9a9237 ^
6030d7e2 ^
ecfbbfb5 ^
33352536 ^
ecfbbfb5 ^
ddeed58f ^


71eb22a5 ^
33352536 ^


03d50cc8 ^
33352536 ^




ddeed58f ^
6323661c ^
6070c23e ^
ddeed58f ^
33352536 ^
6070c23e ^
ddeed58f ^
33352536 ^

6030d7e2 ^

03d50cc8 ^
6030d7e2 ^
6323661c ^
6070c23e ^
03d50cc8 ^
33352536 ^
6030d7e2 ^

03d50cc8 ^
33352536 ^
6030d7e2 ^
ed0e64a9 ^
ee9a9237 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69