Bug 934241 - Feeds should support the received date, ie store a Received trace header. r=mkmelin
authoralta88@gmail.com
Thu, 07 Nov 2013 07:53:00 -0700
changeset 16874 31b2b8a22a7a2d0a8ba4ee060740426f71dbef46
parent 16873 e7fd81e33b3d1f8adbdbe8517ff73f3f0a36be2e
child 16875 4bd351dfc76a1a6eddc08d959f65fb3f3e84a9ee
push id1074
push userbugzilla@standard8.plus.com
push dateMon, 03 Feb 2014 22:47:23 +0000
treeherdercomm-beta@6b791b5369ed [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs934241
Bug 934241 - Feeds should support the received date, ie store a Received trace header. r=mkmelin
mailnews/extensions/newsblog/content/FeedItem.js
mailnews/extensions/newsblog/content/feed-parser.js
mailnews/extensions/newsblog/content/feed-subscriptions.js
mailnews/extensions/newsblog/content/utils.js
--- a/mailnews/extensions/newsblog/content/FeedItem.js
+++ b/mailnews/extensions/newsblog/content/FeedItem.js
@@ -1,16 +1,16 @@
 /* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 function FeedItem()
 {
-  this.mDate = new Date().toString();
+  this.mDate = FeedUtils.getValidRFC5322Date();
   this.mUnicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
                            createInstance(Ci.nsIScriptableUnicodeConverter);
   this.mParserUtils = Cc["@mozilla.org/parserutils;1"].
                       getService(Ci.nsIParserUtils);
 }
 
 FeedItem.prototype =
 {
@@ -301,16 +301,17 @@ FeedItem.prototype =
     // DB header object.
     let openingLine = 'From - ' + this.mDate + '\n';
 
     let source =
       openingLine +
       'X-Mozilla-Status: 0000\n' +
       'X-Mozilla-Status2: 00000000\n' +
       'X-Mozilla-Keys: ' + " ".repeat(80) + '\n' +
+      'Received: by localhost; ' + FeedUtils.getValidRFC5322Date() + '\n' +
       'Date: ' + this.mDate + '\n' +
       'Message-Id: ' + this.normalizeMessageID(this.id) + '\n' +
       'From: ' + this.author + '\n' +
       'MIME-Version: 1.0\n' +
       'Subject: ' + this.title + '\n' +
       inreplytoHdrsStr +
       'Content-Transfer-Encoding: 8bit\n' +
       'Content-Base: ' + this.mURL + '\n';
--- a/mailnews/extensions/newsblog/content/feed-parser.js
+++ b/mailnews/extensions/newsblog/content/feed-parser.js
@@ -205,17 +205,17 @@ FeedParser.prototype =
       item.date = this.getNodeValue(tags ? tags[0] : null) || item.date;
 
       // If the date is invalid, users will see the beginning of the epoch
       // unless we reset it here, so they'll see the current time instead.
       // This is typical aggregator behavior.
       if (item.date)
       {
         item.date = item.date.trim();
-        if (!this.isValidRFC822Date(item.date))
+        if (!FeedUtils.isValidRFC822Date(item.date))
         {
           // XXX Use this on the other formats as well.
           item.date = this.dateRescue(item.date);
         }
       }
 
       tags = this.childrenByTagNameNS(itemNode, FeedUtils.RSS_CONTENT_NS, "encoded");
       item.content = this.getNodeValue(tags ? tags[0] : null);
@@ -747,44 +747,28 @@ FeedParser.prototype =
   xmlEscape: function(s)
   {
     s = s.replace(/&/g, "&");
     s = s.replace(/>/g, ">");
     s = s.replace(/</g, "&lt;");
     return s;
   },
 
-  // Date validator for RSS feeds
-  FZ_RFC822_RE: "^(((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?\\d\\d?" +
-    " +((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))" +
-    " +\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? +(([+-]?\\d\\d\\d\\d)|(UT)|(GMT)" +
-    "|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)$",
-
-  isValidRFC822Date: function(pubDate)
-  {
-    let regex = new RegExp(this.FZ_RFC822_RE);
-    return regex.test(pubDate);
-  },
-
   dateRescue: function(dateString)
   {
     // Deal with various kinds of invalid dates.
     if (!isNaN(parseInt(dateString)))
     {
       // It's an integer, so maybe it's a timestamp.
       let d = new Date(parseInt(dateString) * 1000);
       let now = new Date();
       let yeardiff = now.getFullYear() - d.getFullYear();
       FeedUtils.log.trace("FeedParser.dateRescue: Rescue Timestamp date - " +
                           d.toString() + " ,year diff - " + yeardiff);
       if (yeardiff >= 0 && yeardiff < 3)
         // It's quite likely the correct date.
         return d.toString();
     }
 
-    if (dateString.search(/^\d\d\d\d/) != -1)
-      //Could be an ISO8601/W3C date.
-      return new Date(dateString).toUTCString();
-
-    // Can't help.  Set to current time.
-    return (new Date()).toString();
+    // Could be an ISO8601/W3C date.  If not, get the current time.
+    return FeedUtils.getValidRFC5322Date(dateString);
   }
 };
--- a/mailnews/extensions/newsblog/content/feed-subscriptions.js
+++ b/mailnews/extensions/newsblog/content/feed-subscriptions.js
@@ -1929,17 +1929,17 @@ var FeedSubscriptions = {
       let titleText = FeedUtils.strings.formatStringFromName(
                         "subscribe-OPMLExportFileDialogTitle",
                         [this.brandShortName, rootFolder.prettyName], 2);
       let title = opmlDoc.createElement("title");
       title.appendChild(opmlDoc.createTextNode(titleText));
       head.appendChild(title);
       this.generatePPSpace(head, SPACES4);
       let dt = opmlDoc.createElement("dateCreated");
-      dt.appendChild(opmlDoc.createTextNode((new Date()).toGMTString()));
+      dt.appendChild(opmlDoc.createTextNode((new Date()).toUTCString()));
       head.appendChild(dt);
       this.generatePPSpace(head, SPACES2);
       opmlRoot.appendChild(head);
 
       this.generatePPSpace(opmlRoot, SPACES2);
 
       // Add <outline>s to the <body>.
       let body = opmlDoc.createElement("body");
--- a/mailnews/extensions/newsblog/content/utils.js
+++ b/mailnews/extensions/newsblog/content/utils.js
@@ -282,20 +282,17 @@ var FeedUtils = {
                      aFolder.server.serverURI + " : " + aFolder.server.prettyName);
     }
 
     return feedUrlArray.length ? feedUrlArray : null;
   },
 
 /**
  * Update the feeds.rdf database with the new folder's location on name changes
- * on rename and move/copy. Note that for nested folders to also be reflected
- * correctly, the feedUrl property *must* be derived from the individual
- * folder's db, and not from panacea (ie using getStringProperty, which contains
- * the old name at the time of the notification).
+ * on rename and move/copy.
  *
  * @param  nsIMsgFolder aFolder      - the folder, new if rename or move/copy
  * @param  nsIMsgFolder aOrigFolder  - original folder, if move/copy
  */
   updateFolderChangeInFeedsDS: function(aFolder, aOrigFolder) {
     let sourceFolder = aFolder;
     if (aOrigFolder && aFolder.server != aOrigFolder.server)
       // Copied from another account, get the feed urls from that account's
@@ -712,16 +709,48 @@ var FeedUtils = {
       pathParts.push(msgFolder.name);
     }
 
     // Leaf folder last.
     pathParts.push(aFolder.name);
     return pathParts.join("/");
   },
 
+/**
+ * Date validator for feeds.
+ *
+ * @param  string aDate - date string
+ * @return boolean      - true if passes regex test, false if not
+ */
+  isValidRFC822Date: function(aDate)
+  {
+    const FZ_RFC822_RE = "^(((Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)|(Sun)), *)?\\d\\d?" +
+    " +((Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))" +
+    " +\\d\\d(\\d\\d)? +\\d\\d:\\d\\d(:\\d\\d)? +(([+-]?\\d\\d\\d\\d)|(UT)|(GMT)" +
+    "|(EST)|(EDT)|(CST)|(CDT)|(MST)|(MDT)|(PST)|(PDT)|\\w)$";
+    let regex = new RegExp(FZ_RFC822_RE);
+    return regex.test(aDate);
+  },
+
+/**
+ * Create rfc5322 date.
+ *
+ * @param  [string] aDateString - optional date string; if null or invalid
+ *                                 date, get the current datetime.
+ * @return string               - an rfc5322 date string
+ */
+  getValidRFC5322Date: function(aDateString)
+  {
+    let d = new Date(aDateString || new Date().getTime());
+    d = isNaN(d.getTime()) ? new Date() : d;
+    let utcDate = d.toUTCString();
+    let tzOffset = d.toTimeString().split("GMT")[1].split(" ")[0];
+    return utcDate.replace(/GMT/, tzOffset);
+  },
+
   // Progress glue code.  Acts as a go between the RSS back end and the mail
   // window front end determined by the aMsgWindow parameter passed into
   // nsINewsBlogFeedDownloader.
   progressNotifier: {
     mSubscribeMode: false,
     mMsgWindow: null,
     mStatusFeedback: null,
     mFeeds: {},