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.
This commit is contained in:
parent
2d7a870725
commit
f1a0fd20d6
2 changed files with 67 additions and 26 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
gomail "net/mail"
|
gomail "net/mail"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"git.sr.ht/~sircmpwn/aerc/models"
|
"git.sr.ht/~sircmpwn/aerc/models"
|
||||||
|
@ -111,13 +112,21 @@ func ParseMessageFormat(
|
||||||
retval = append(retval, 'd')
|
retval = append(retval, 'd')
|
||||||
args = append(args, number)
|
args = append(args, number)
|
||||||
case 'd':
|
case 'd':
|
||||||
|
date := msg.Envelope.Date
|
||||||
|
if date.IsZero() {
|
||||||
|
date = msg.InternalDate
|
||||||
|
}
|
||||||
retval = append(retval, 's')
|
retval = append(retval, 's')
|
||||||
args = append(args,
|
args = append(args,
|
||||||
msg.InternalDate.Format(timestampformat))
|
dummyIfZeroDate(date, timestampformat))
|
||||||
case 'D':
|
case 'D':
|
||||||
|
date := msg.Envelope.Date
|
||||||
|
if date.IsZero() {
|
||||||
|
date = msg.InternalDate
|
||||||
|
}
|
||||||
retval = append(retval, 's')
|
retval = append(retval, 's')
|
||||||
args = append(args,
|
args = append(args,
|
||||||
msg.InternalDate.Local().Format(timestampformat))
|
dummyIfZeroDate(date, timestampformat))
|
||||||
case 'f':
|
case 'f':
|
||||||
if msg.Envelope == nil {
|
if msg.Envelope == nil {
|
||||||
return "", nil,
|
return "", nil,
|
||||||
|
@ -335,3 +344,10 @@ handle_end_error:
|
||||||
return "", nil,
|
return "", nil,
|
||||||
errors.New("reached end of string while parsing message format")
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -102,11 +102,9 @@ func ParseEntityStructure(e *message.Entity) (*models.BodyStructure, error) {
|
||||||
return &body, nil
|
return &body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var DateParseError = errors.New("date parsing failed")
|
||||||
|
|
||||||
func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
|
func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
|
||||||
date, err := parseDate(h)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not parse date header: %v", err)
|
|
||||||
}
|
|
||||||
from, err := parseAddressList(h, "from")
|
from, err := parseAddressList(h, "from")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read from address: %v", err)
|
return nil, fmt.Errorf("could not read from address: %v", err)
|
||||||
|
@ -135,6 +133,12 @@ func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not read message id: %v", err)
|
return nil, fmt.Errorf("could not read message id: %v", err)
|
||||||
}
|
}
|
||||||
|
date, err := parseDate(h)
|
||||||
|
if err != nil {
|
||||||
|
// still return a valid struct plus a sentinel date parsing error
|
||||||
|
// if only the date parsing failed
|
||||||
|
err = fmt.Errorf("%w: %v", DateParseError, err)
|
||||||
|
}
|
||||||
return &models.Envelope{
|
return &models.Envelope{
|
||||||
Date: date,
|
Date: date,
|
||||||
Subject: subj,
|
Subject: subj,
|
||||||
|
@ -144,29 +148,25 @@ func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
|
||||||
To: to,
|
To: to,
|
||||||
Cc: cc,
|
Cc: cc,
|
||||||
Bcc: bcc,
|
Bcc: bcc,
|
||||||
}, nil
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseDate extends the built-in date parser with additional layouts which are
|
// parseDate tries to parse the date from the Date header with non std formats
|
||||||
// non-conforming but appear in the wild.
|
// if this fails it tries to parse the received header as well
|
||||||
func parseDate(h *mail.Header) (time.Time, error) {
|
func parseDate(h *mail.Header) (time.Time, error) {
|
||||||
t, parseErr := h.Date()
|
t, err := h.Date()
|
||||||
if parseErr == nil {
|
if err == nil {
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
text, err := h.Text("date")
|
text, err := h.Text("date")
|
||||||
if err != nil {
|
// sometimes, no error occurs but the date is empty.
|
||||||
return time.Time{}, errors.New("no date header")
|
// In this case, guess time from received header field
|
||||||
}
|
if err != nil || text == "" {
|
||||||
// sometimes, no error occurs but the date is empty. In this case, guess time from received header field
|
t, err := parseReceivedHeader(h)
|
||||||
if text == "" {
|
if err == nil {
|
||||||
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
|
return t, nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
layouts := []string{
|
layouts := []string{
|
||||||
// X-Mailer: EarthLink Zoo Mail 1.0
|
// X-Mailer: EarthLink Zoo Mail 1.0
|
||||||
"Mon, _2 Jan 2006 15:04:05 -0700 (GMT-07:00)",
|
"Mon, _2 Jan 2006 15:04:05 -0700 (GMT-07:00)",
|
||||||
|
@ -176,7 +176,21 @@ func parseDate(h *mail.Header) (time.Time, error) {
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// still no success, try the received header as a last resort
|
||||||
|
t, err = parseReceivedHeader(h)
|
||||||
|
if err != nil {
|
||||||
return time.Time{}, fmt.Errorf("unrecognized date format: %s", text)
|
return time.Time{}, fmt.Errorf("unrecognized date format: %s", text)
|
||||||
|
}
|
||||||
|
return t, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseReceivedHeader(h *mail.Header) (time.Time, error) {
|
||||||
|
guess, err := h.Text("received")
|
||||||
|
if err != nil {
|
||||||
|
return time.Time{}, fmt.Errorf("received header not parseable: %v",
|
||||||
|
err)
|
||||||
|
}
|
||||||
|
return time.Parse(time.RFC1123Z, dateRe.FindString(guess))
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseAddressList(h *mail.Header, key string) ([]*models.Address, error) {
|
func parseAddressList(h *mail.Header, key string) ([]*models.Address, error) {
|
||||||
|
@ -231,9 +245,20 @@ func MessageInfo(raw RawMessage) (*models.MessageInfo, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not get structure: %v", err)
|
return nil, fmt.Errorf("could not get structure: %v", err)
|
||||||
}
|
}
|
||||||
env, err := parseEnvelope(&mail.Header{msg.Header})
|
h := &mail.Header{msg.Header}
|
||||||
if err != nil {
|
env, err := parseEnvelope(h)
|
||||||
return nil, fmt.Errorf("could not get envelope: %v", err)
|
if err != nil && !errors.Is(err, DateParseError) {
|
||||||
|
return nil, fmt.Errorf("could not parse envelope: %v", err)
|
||||||
|
// if only the date parsing failed we still get the rest of the
|
||||||
|
// envelop structure in a valid state.
|
||||||
|
// Date parsing errors are fairly common and it's better to be
|
||||||
|
// slightly off than to not be able to read the mails at all
|
||||||
|
// hence we continue here
|
||||||
|
}
|
||||||
|
recDate, _ := parseReceivedHeader(h)
|
||||||
|
if recDate.IsZero() {
|
||||||
|
// better than nothing, if incorrect
|
||||||
|
recDate = env.Date
|
||||||
}
|
}
|
||||||
flags, err := raw.ModelFlags()
|
flags, err := raw.ModelFlags()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -248,7 +273,7 @@ func MessageInfo(raw RawMessage) (*models.MessageInfo, error) {
|
||||||
Envelope: env,
|
Envelope: env,
|
||||||
Flags: flags,
|
Flags: flags,
|
||||||
Labels: labels,
|
Labels: labels,
|
||||||
InternalDate: env.Date,
|
InternalDate: recDate,
|
||||||
RFC822Headers: &mail.Header{msg.Header},
|
RFC822Headers: &mail.Header{msg.Header},
|
||||||
Size: 0,
|
Size: 0,
|
||||||
Uid: raw.UID(),
|
Uid: raw.UID(),
|
||||||
|
|
Loading…
Reference in a new issue