You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2019/09/24 23:27:30 UTC

[commons-net] branch master updated: Use the date from the From_ line if possible

This is an automated email from the ASF dual-hosted git repository.

sebb pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git


The following commit(s) were added to refs/heads/master by this push:
     new ab90a71  Use the date from the From_ line if possible
ab90a71 is described below

commit ab90a71cf2b43792799866c9ed78823bf34b6298
Author: Sebb <se...@apache.org>
AuthorDate: Wed Sep 25 00:27:26 2019 +0100

    Use the date from the From_ line if possible
---
 .../commons/net/examples/mail/IMAPImportMbox.java      | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java b/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
index 6c689da..eaeb0a8 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
@@ -135,7 +135,7 @@ public final class IMAPImportMbox
                 loaded++;
             }
         } catch (IOException e) {
-            System.out.println(imap.getReplyString());
+            System.out.println("Error processing msg: " + total + " " + imap.getReplyString());
             e.printStackTrace();
             System.exit(10);
             return;
@@ -151,15 +151,27 @@ public final class IMAPImportMbox
         return m.lookingAt();
     }
 
+    private static String getDate(String msg) {
+        final Pattern FROM_RE = Pattern.compile("From \\S+ +\\S+ (\\S+)  ?(\\S+) (\\S+) (\\S+)"); // From SENDER Fri Sep 13 17:04:01 2019
+        //                                                 [Fri]   Sep      13     HMS   2019
+        // output date: 13-Sep-2019 17:04:01 +0000
+        String date = null;
+        Matcher m = FROM_RE.matcher(msg);
+        if (m.lookingAt()) {
+            date = m.group(2)+"-"+m.group(1)+"-"+m.group(4)+" "+m.group(3)+" +0000";
+        }
+        return date;
+    }
+
     private static boolean process(final StringBuilder sb, final IMAPClient imap, final String folder
             ,final int msgNum) throws IOException {
         final int length = sb.length();
         boolean haveMessage = length > 2;
         if (haveMessage) {
             System.out.println("MsgNum: " + msgNum +" Length " + length);
-            sb.setLength(length-2); // drop trailing CRLF
+            sb.setLength(length-2); // drop trailing CRLF (mbox format has trailing blank line)
             String msg = sb.toString();
-            if (!imap.append(folder, null, null, msg)) {
+            if (!imap.append(folder, null, getDate(msg), msg)) {
                 throw new IOException("Failed to import message: " + msgNum + " " + imap.getReplyString());
             }
         }