From 6d38a1e9a4bb5d266c2789910676585af76359bd Mon Sep 17 00:00:00 2001
From: Mechiel Lukkien <mechiel@ueber.net>
Date: Tue, 2 Apr 2024 19:43:45 +0200
Subject: [PATCH] when reading maildirs for imports, use the file mtime as
 fallback for "received" time

more useful than the time.Time zero file in case the maildir filename isn't
properly formed with a timestamp. this is not too uncommon when people
reconstruct maildirs from other sources of message files to then import the
maildir.

based on message from abdul h
---
 store/import.go | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/store/import.go b/store/import.go
index fa73b34..c5514b2 100644
--- a/store/import.go
+++ b/store/import.go
@@ -306,11 +306,14 @@ func (mr *MaildirReader) Next() (*Message, *os.File, string, error) {
 		return nil, nil, p, fmt.Errorf("writing message: %v", err)
 	}
 
-	// Take received time from filename.
+	// Take received time from filename, falling back to mtime for maildirs
+	// reconstructed some other sources of message files.
 	var received time.Time
 	t := strings.SplitN(filepath.Base(sf.Name()), ".", 2)
 	if v, err := strconv.ParseInt(t[0], 10, 64); err == nil {
 		received = time.Unix(v, 0)
+	} else if fi, err := sf.Stat(); err == nil {
+		received = fi.ModTime()
 	}
 
 	// Parse flags. See https://cr.yp.to/proto/maildir.html.