From c085fcacf7745f1e9516ca2c5a3194b2ba1db5f4 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 20 Aug 2021 09:07:38 +0200 Subject: [PATCH] POP3: get received time before resend time --- app/src/main/java/eu/faircode/email/Core.java | 2 +- .../java/eu/faircode/email/MessageHelper.java | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 966294aba3..efd1f1caf8 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -2476,7 +2476,7 @@ class Core { try { Long sent = helper.getSent(); - Long received = helper.getReceivedHeader(); + Long received = helper.getReceivedHeader(helper.getResent()); if (received == null) received = sent; if (received == null) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index 0aea783148..4889b24293 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -1557,6 +1557,10 @@ public class MessageHelper { } Long getReceivedHeader() throws MessagingException { + return getReceivedHeader(null); + } + + Long getReceivedHeader(Long before) throws MessagingException { ensureHeaders(); // https://tools.ietf.org/html/rfc5321#section-4.4 @@ -1565,17 +1569,24 @@ public class MessageHelper { if (received == null || received.length == 0) return null; - String last = MimeUtility.unfold(received[0]); - int semi = last.lastIndexOf(';'); - if (semi < 0) - return null; + // First header is last added header + for (int i = 0; i < received.length; i++) { + String header = MimeUtility.unfold(received[i]); + int semi = header.lastIndexOf(';'); + if (semi < 0) + return null; - MailDateFormat mdf = new MailDateFormat(); - Date date = mdf.parse(last, new ParsePosition(semi + 1)); - if (date == null) - return null; + MailDateFormat mdf = new MailDateFormat(); + Date date = mdf.parse(header, new ParsePosition(semi + 1)); + if (date == null) + return null; - return date.getTime(); + long time = date.getTime(); + if (before == null || time < before) + return time; + } + + return null; } Long getSent() throws MessagingException { @@ -1588,6 +1599,21 @@ public class MessageHelper { return sent.getTime(); } + Long getResent() throws MessagingException { + ensureHeaders(); + + String resent = imessage.getHeader("Resent-Date", null); + if (resent == null) + return null; + + MailDateFormat mdf = new MailDateFormat(); + Date date = mdf.parse(resent, new ParsePosition(0)); + if (date == null) + return null; + + return date.getTime(); + } + String getHeaders() throws MessagingException { ensureHeaders();