From d1600e4666e9efdb3ae45ef2cc01182950a41bb5 Mon Sep 17 00:00:00 2001 From: elumbella Date: Tue, 5 May 2020 11:26:43 +0200 Subject: Guess date from received if not present --- worker/lib/parse.go | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/worker/lib/parse.go b/worker/lib/parse.go index ac20ac8..bbea49d 100644 --- a/worker/lib/parse.go +++ b/worker/lib/parse.go @@ -2,9 +2,12 @@ package lib import ( "bytes" + "errors" "fmt" "io" + "regexp" "strings" + "time" "git.sr.ht/~sircmpwn/aerc/models" "github.com/emersion/go-message" @@ -12,6 +15,11 @@ import ( "github.com/emersion/go-message/mail" ) +// RFC 1123Z regexp +var dateRe = regexp.MustCompile(`(((Mon|Tue|Wed|Thu|Fri|Sat|Sun))[,]?\s[0-9]{1,2})\s` + + `(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s` + + `([0-9]{4})\s([0-9]{2}):([0-9]{2})(:([0-9]{2}))?\s([\+|\-][0-9]{4})\s?`) + func FetchEntityPartReader(e *message.Entity, index []int) (io.Reader, error) { if len(index) < 1 { return nil, fmt.Errorf("no part to read") @@ -97,7 +105,7 @@ func ParseEntityStructure(e *message.Entity) (*models.BodyStructure, error) { } func parseEnvelope(h *mail.Header) (*models.Envelope, error) { - date, err := h.Date() + date, err := parseDate(h) if err != nil { return nil, fmt.Errorf("could not parse date header: %v", err) } @@ -141,6 +149,38 @@ func parseEnvelope(h *mail.Header) (*models.Envelope, error) { }, nil } +// parseDate extends the built-in date parser with additional layouts which are +// non-conforming but appear in the wild. +func parseDate(h *mail.Header) (time.Time, error) { + t, parseErr := h.Date() + if parseErr == nil { + return t, nil + } + text, err := h.Text("date") + if err != nil { + return time.Time{}, errors.New("no date header") + } + // sometimes, no error occurs but the date is empty. In this case, guess time from received header field + if text == "" { + guess, err := h.Text("received") + if err != nil { + return time.Time{}, errors.New("no received header") + } + t, _ := time.Parse(time.RFC1123Z, dateRe.FindString(guess)) + return t, nil + } + layouts := []string{ + // X-Mailer: EarthLink Zoo Mail 1.0 + "Mon, _2 Jan 2006 15:04:05 -0700 (GMT-07:00)", + } + for _, layout := range layouts { + if t, err := time.Parse(layout, text); err == nil { + return t, nil + } + } + return time.Time{}, fmt.Errorf("unrecognized date format: %s", t) +} + func parseAddressList(h *mail.Header, key string) ([]*models.Address, error) { var converted []*models.Address addrs, err := h.AddressList(key) -- cgit 1.4.1-2-gfad0 t'>blob: bf4af1d9a8c9bcd44fb97e5088cd5002ad78b879 (plain) (tree)
1
2
3
4
5
6
7
8
9