Bug 32216 - If the 'Date' header is invalid, use date from 'Received' header instead. r=jorgk
authorAlfred Peters <infofrommozilla@gmail.com>
Sat, 20 Oct 2018 11:50:00 +0200
changeset 33547 6876e8c9ea75ac91a2408ba42a644447c8888bef
parent 33546 af1295243bcf777e3d832bbc48d2ab4edf816db6
child 33548 3f7e3c78bdd8f998dbbcb0a8d5f366d3623f6364
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersjorgk
bugs32216
Bug 32216 - If the 'Date' header is invalid, use date from 'Received' header instead. r=jorgk
mailnews/local/src/nsParseMailbox.cpp
--- a/mailnews/local/src/nsParseMailbox.cpp
+++ b/mailnews/local/src/nsParseMailbox.cpp
@@ -1284,43 +1284,40 @@ nsresult nsParseMailMessageState::Finali
 
   struct message_header to;
   GetAggregateHeader (m_toList, &to);
   struct message_header cc;
   GetAggregateHeader (m_ccList, &cc);
   // we don't aggregate bcc, as we only generate it locally,
   // and we don't use multiple lines
 
-  sender     = (m_from.length          ? &m_from :
-  m_sender.length        ? &m_sender :
-  m_envelope_from.length ? &m_envelope_from :
-  0);
-  recipient  = (to.length         ? &to :
-  cc.length         ? &cc :
-  m_newsgroups.length ? &m_newsgroups :
-  0);
-  ccList     = (cc.length ? &cc : 0);
-  bccList    = (m_bccList.length    ? &m_bccList    : 0);
-  subject    = (m_subject.length    ? &m_subject    : 0);
-  id         = (m_message_id.length ? &m_message_id : 0);
-  references = (m_references.length ? &m_references : 0);
-  statush    = (m_status.length     ? &m_status     : 0);
-  mozstatus  = (m_mozstatus.length  ? &m_mozstatus  : 0);
-  mozstatus2 = (m_mozstatus2.length  ? &m_mozstatus2  : 0);
-  date       = (m_date.length       ? &m_date :
-  m_envelope_date.length ? &m_envelope_date :
-  0);
+  sender       = (m_from.length          ? &m_from          :
+                  m_sender.length        ? &m_sender        :
+                  m_envelope_from.length ? &m_envelope_from : 0);
+  recipient    = (to.length              ? &to              :
+                  cc.length              ? &cc              :
+                  m_newsgroups.length    ? &m_newsgroups    : 0);
+  ccList       = (cc.length              ? &cc              : 0);
+  bccList      = (m_bccList.length       ? &m_bccList       : 0);
+  subject      = (m_subject.length       ? &m_subject       : 0);
+  id           = (m_message_id.length    ? &m_message_id    : 0);
+  references   = (m_references.length    ? &m_references    : 0);
+  statush      = (m_status.length        ? &m_status        : 0);
+  mozstatus    = (m_mozstatus.length     ? &m_mozstatus     : 0);
+  mozstatus2   = (m_mozstatus2.length    ? &m_mozstatus2    : 0);
+  date         = (m_date.length          ? &m_date          :
+                  m_envelope_date.length ? &m_envelope_date : 0);
   deliveryDate = (m_delivery_date.length ? &m_delivery_date : 0);
-  priority   = (m_priority.length   ? &m_priority   : 0);
-  keywords   =  (m_keywords.length   ? &m_keywords  : 0);
-  mdn_dnt     = (m_mdn_dnt.length    ? &m_mdn_dnt    : 0);
-  inReplyTo = (m_in_reply_to.length ? &m_in_reply_to : 0);
-  replyTo = (m_replyTo.length ? &m_replyTo : 0);
-  content_type = (m_content_type.length ? &m_content_type : 0);
-  account_key = (m_account_key.length ? &m_account_key :0);
+  priority     = (m_priority.length      ? &m_priority      : 0);
+  keywords     = (m_keywords.length      ? &m_keywords      : 0);
+  mdn_dnt      = (m_mdn_dnt.length       ? &m_mdn_dnt       : 0);
+  inReplyTo    = (m_in_reply_to.length   ? &m_in_reply_to   : 0);
+  replyTo      = (m_replyTo.length       ? &m_replyTo       : 0);
+  content_type = (m_content_type.length  ? &m_content_type  : 0);
+  account_key  = (m_account_key.length   ? &m_account_key   : 0);
 
   if (mozstatus)
   {
     if (mozstatus->length == 4)
     {
       NS_ASSERTION(MsgIsHex(mozstatus->value, 4), "Expected 4 hex digits for flags.");
       flags = MsgUnhex(mozstatus->value, 4);
       // strip off and remember priority bits.
@@ -1524,60 +1521,73 @@ nsresult nsParseMailMessageState::Finali
           m_newMsgHdr->SetReferences(inReplyTo->value);
 
         // 'Received' should be as reliable an indicator of the receipt
         // date+time as possible, whilst always giving something *from
         // the message*.  It won't use PR_Now() under any circumstance.
         // Therefore, the fall-thru order for 'Received' is:
         // Received: -> Delivery-date: -> date
         // 'Date' uses:
-        // date -> PR_Now()
+        // date -> 'Received' -> PR_Now()
         //
         // date is:
         // Date: -> m_envelope_date
 
         uint32_t rcvTimeSecs = 0;
+        PRTime datePRTime = 0;
         if (date)
-        {  // Date:
-          PRTime resultTime;
-          PRStatus timeStatus = PR_ParseTimeString (date->value, false, &resultTime);
-          if (PR_SUCCESS == timeStatus)
+        {
+          // Date:
+          if (PR_ParseTimeString(date->value, false, &datePRTime) == PR_SUCCESS)
           {
-            m_newMsgHdr->SetDate(resultTime);
-            PRTime2Seconds(resultTime, &rcvTimeSecs);
+            // Convert to seconds as default value for 'Received'.
+            PRTime2Seconds(datePRTime, &rcvTimeSecs);
           }
           else
+          {
             NS_WARNING("PR_ParseTimeString of date failed in FinalizeHeader().");
+          }
         }
-        else
-        {  // PR_Now()
-          // If there was some problem parsing the Date header *AND* we
-          // couldn't get a valid envelope date, use now as the time.
-          // PR_ParseTimeString won't touch resultTime unless it succeeds.
-          // This doesn't affect local (POP3) messages, because we use the envelope
-          // date if there's no Date: header, but it will affect IMAP msgs
-          // w/o a Date: hdr or Received: headers.
-          PRTime resultTime = PR_Now();
-          m_newMsgHdr->SetDate(resultTime);
-        }
-        if (m_receivedTime != 0)
-        {  // Upgrade 'Received' to Received: ?
+        if (m_receivedTime)
+        {
+          // Upgrade 'Received' to Received: ?
           PRTime2Seconds(m_receivedTime, &rcvTimeSecs);
+          if (datePRTime == 0)
+            datePRTime = m_receivedTime;
         }
         else if (deliveryDate)
-        {  // Upgrade 'Received' to Delivery-date: ?
+        {
+          // Upgrade 'Received' to Delivery-date: ?
           PRTime resultTime;
-          PRStatus timeStatus = PR_ParseTimeString (deliveryDate->value, false, &resultTime);
-          if (PR_SUCCESS == timeStatus)
+          if (PR_ParseTimeString(deliveryDate->value, false, &resultTime) == PR_SUCCESS)
+          {
             PRTime2Seconds(resultTime, &rcvTimeSecs);
-          else // TODO/FIXME: We need to figure out what to do in this case!
+            if (datePRTime == 0)
+              datePRTime = resultTime;
+          }
+          else
+          {
+            // TODO/FIXME: We need to figure out what to do in this case!
             NS_WARNING("PR_ParseTimeString of delivery date failed in FinalizeHeader().");
+          }
         }
         m_newMsgHdr->SetUint32Property("dateReceived", rcvTimeSecs);
 
+        if (datePRTime == 0)
+        {
+          // If there was some problem parsing the Date header *AND* we
+          // couldn't get a valid envelope date *AND* we couldn't get a valid
+          // Received: header date, use now as the time.
+          // This doesn't affect local (POP3) messages, because we use the
+          // envelope date if there's no Date: header, but it will affect IMAP
+          // msgs w/o a Date: header or Received: headers.
+          datePRTime = PR_Now();
+        }
+        m_newMsgHdr->SetDate(datePRTime);
+
         if (priority)
           m_newMsgHdr->SetPriorityString(priority->value);
         else if (priorityFlags == nsMsgPriority::notSet)
           m_newMsgHdr->SetPriority(nsMsgPriority::none);
         if (keywords)
         {
           // When there are many keywords, some may not have been written
           // to the message file, so add extra keywords from the backup