summaryrefslogtreecommitdiff
path: root/lib/format
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2020-07-27 02:48:22 +0300
committerReto Brunner <reto@labrat.space>2020-08-10 08:00:52 +0200
commitf1a0fd20d6d30fb1203ffa3cb9f4004476acfdb3 (patch)
tree2b67a7e61abc02a0a30ae07005c35df8576f321f /lib/format
parent2d7a8707257eec2b9f0ff41772cfd8dab4f1201e (diff)
downloadaerc-f1a0fd20d6d30fb1203ffa3cb9f4004476acfdb3.zip
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 <lyudmilangelov@gmail.com> But tries to handle a parsing error a bit more gracefully instead of just returning the zero date.
Diffstat (limited to 'lib/format')
-rw-r--r--lib/format/format.go20
1 files changed, 18 insertions, 2 deletions
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)
+}