From f1a0fd20d6d30fb1203ffa3cb9f4004476acfdb3 Mon Sep 17 00:00:00 2001 From: Reto Brunner Date: Mon, 27 Jul 2020 02:48:22 +0300 Subject: improve date parsing for notmuch/maildir If a message date would fail to parse, the worker would never receive the MessageInfo it asked for, and so it wouldn't display the message. The problem is the spec for date formats is too lax, so trying to ensure we can parse every weird date format out there is not a strategy we want to pursue. On the other hand, preventing the user from reading and working with a message due to the error format is also not a solution. The maildir and notmuch workers will now fallback to the internal date, which is based on the received header if we can't parse the format of the Date header. The UI will also fallback to the received header whenever the date header can't be parsed. This patch is based on the work done by Lyudmil Angelov But tries to handle a parsing error a bit more gracefully instead of just returning the zero date. --- lib/format/format.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/format') diff --git a/lib/format/format.go b/lib/format/format.go index 5f45e58..cc46da8 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -5,6 +5,7 @@ import ( "fmt" gomail "net/mail" "strings" + "time" "unicode" "git.sr.ht/~sircmpwn/aerc/models" @@ -111,13 +112,21 @@ func ParseMessageFormat( retval = append(retval, 'd') args = append(args, number) case 'd': + date := msg.Envelope.Date + if date.IsZero() { + date = msg.InternalDate + } retval = append(retval, 's') args = append(args, - msg.InternalDate.Format(timestampformat)) + dummyIfZeroDate(date, timestampformat)) case 'D': + date := msg.Envelope.Date + if date.IsZero() { + date = msg.InternalDate + } retval = append(retval, 's') args = append(args, - msg.InternalDate.Local().Format(timestampformat)) + dummyIfZeroDate(date, timestampformat)) case 'f': if msg.Envelope == nil { return "", nil, @@ -335,3 +344,10 @@ handle_end_error: return "", nil, errors.New("reached end of string while parsing message format") } + +func dummyIfZeroDate(date time.Time, format string) string { + if date.IsZero() { + return strings.Repeat("?", len(format)) + } + return date.Format(format) +} -- cgit 1.4.1-2-gfad0