~sircmpwn/aerc

Allow working with messages when their dates cannot be parsed v2 PROPOSED

Lyudmil Angelov: 1
 Allow working with messages when their dates cannot be parsed

 3 files changed, 25 insertions(+), 13 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/aerc/patches/11753/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH v2] Allow working with messages when their dates cannot be parsed Export this patch

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.

We therefore log and display any date parsing errors, but still display
the message. Instead of the broken date, we display repeated dashes to
ensure the message fields stay aligned.
---
This is a follow-up to the following thread:
https://lists.sr.ht/~sircmpwn/aerc/patches/11535

We had decided not to merge the second part of the patch, where I had
added support for the broken date format I'd encountered, and to instead
handle the date parse error better.

 lib/format/format.go     | 12 ++++++++++--
 worker/lib/parse.go      | 18 +++++++++---------
 worker/maildir/worker.go |  8 ++++++--
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/lib/format/format.go b/lib/format/format.go
index 5f45e58..dbed9bc 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"
@@ -113,11 +114,11 @@ func ParseMessageFormat(
		case 'd':
			retval = append(retval, 's')
			args = append(args,
				msg.InternalDate.Format(timestampformat))
				formattedDate(msg.InternalDate, timestampformat))
		case 'D':
			retval = append(retval, 's')
			args = append(args,
				msg.InternalDate.Local().Format(timestampformat))
				formattedDate(msg.InternalDate.Local(), timestampformat))
		case 'f':
			if msg.Envelope == nil {
				return "", nil,
@@ -335,3 +336,10 @@ handle_end_error:
	return "", nil,
		errors.New("reached end of string while parsing message format")
}

func formattedDate(date time.Time, format string) string {
	if date.IsZero() {
		return strings.Repeat("-", len(format))
	}
	return date.Format(format)
}
diff --git a/worker/lib/parse.go b/worker/lib/parse.go
index ded7a83..6db0a8c 100644
--- a/worker/lib/parse.go
+++ b/worker/lib/parse.go
@@ -105,10 +105,7 @@ func ParseEntityStructure(e *message.Entity) (*models.BodyStructure, 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)
	}
	date, dateParseError := parseDate(h)
	from, err := parseAddressList(h, "from")
	if err != nil {
		return nil, fmt.Errorf("could not read from address: %v", err)
@@ -146,7 +143,7 @@ func parseEnvelope(h *mail.Header) (*models.Envelope, error) {
		To:        to,
		Cc:        cc,
		Bcc:       bcc,
	}, nil
	}, dateParseError
}

// parseDate extends the built-in date parser with additional layouts which are
@@ -233,9 +230,12 @@ func MessageInfo(raw RawMessage) (*models.MessageInfo, error) {
	if err != nil {
		return nil, fmt.Errorf("could not get structure: %v", err)
	}
	env, err := parseEnvelope(&mail.Header{msg.Header})
	if err != nil {
		return nil, fmt.Errorf("could not get envelope: %v", err)
	env, envelopeParseError := parseEnvelope(&mail.Header{msg.Header})
	if envelopeParseError != nil {
		envelopeParseError = fmt.Errorf("could not get envelope: %v", envelopeParseError)
		if env == nil {
			return nil, envelopeParseError
		}
	}
	flags, err := raw.ModelFlags()
	if err != nil {
@@ -254,5 +254,5 @@ func MessageInfo(raw RawMessage) (*models.MessageInfo, error) {
		RFC822Headers: &mail.Header{msg.Header},
		Size:          0,
		Uid:           raw.UID(),
	}, nil
	}, envelopeParseError
}
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index ce548ff..d68bea2 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -325,7 +325,9 @@ func (w *Worker) sort(uids []uint32, criteria []*types.SortCriterion) ([]uint32,
		info, err := m.MessageInfo()
		if err != nil {
			w.worker.Logger.Printf("could not get message info: %v", err)
			continue
			if info == nil {
				continue
			}
		}
		msgInfos = append(msgInfos, info)
	}
@@ -360,7 +362,9 @@ func (w *Worker) handleFetchMessageHeaders(
		if err != nil {
			w.worker.Logger.Printf("could not get message info: %v", err)
			w.err(msg, err)
			continue
			if info == nil {
				continue
			}
		}
		w.worker.PostMessage(&types.MessageInfo{
			Message: types.RespondTo(msg),
-- 
2.27.0