1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
/* -*- 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 = FeedUtils.getValidRFC5322Date();
this.mUnicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
createInstance(Ci.nsIScriptableUnicodeConverter);
this.mParserUtils = Cc["@mozilla.org/parserutils;1"].
getService(Ci.nsIParserUtils);
}
FeedItem.prototype =
{
// Only for IETF Atom.
xmlContentBase: null,
id: null,
feed: null,
description: null,
content: null,
enclosures: [],
title: null,
// Author must be angle bracket enclosed to function as an addr-spec, in the
// absence of an addr-spec portion of an RFC5322 email address, as other
// functionality (gloda search) depends on this.
author: "<anonymous>",
inReplyTo: "",
keywords: [],
mURL: null,
characterSet: "UTF-8",
ENCLOSURE_BOUNDARY_PREFIX: "--------------", // 14 dashes
ENCLOSURE_HEADER_BOUNDARY_PREFIX: "------------", // 12 dashes
MESSAGE_TEMPLATE: '\n' +
'<html>\n' +
' <head>\n' +
' <title>%TITLE%</title>\n' +
' <base href="%BASE%">\n' +
' </head>\n' +
' <body id="msgFeedSummaryBody" selected="false">\n' +
' %CONTENT%\n' +
' </body>\n' +
'</html>\n',
get url()
{
return this.mURL;
},
set url(aVal)
{
try
{
this.mURL = Services.io.newURI(aVal, null, null).spec;
}
catch(ex)
{
// The url as published or constructed can be a non url. It's used as a
// feeditem identifier in feeditems.rdf, as a messageId, and as an href
// and for the content-base header. Save as is; ensure not null.
this.mURL = aVal ? aVal : "";
}
},
get date()
{
return this.mDate;
},
set date (aVal)
{
this.mDate = aVal;
},
get identity ()
{
return this.feed.name + ": " + this.title + " (" + this.id + ")"
},
normalizeMessageID: function(messageID)
{
// Escape occurrences of message ID meta characters <, >, and @.
messageID.replace(/</g, "%3C");
messageID.replace(/>/g, "%3E");
messageID.replace(/@/g, "%40");
messageID = "<" + messageID.trim() + "@" + "localhost.localdomain" + ">";
FeedUtils.log.trace("FeedItem.normalizeMessageID: messageID - " + messageID);
return messageID;
},
get itemUniqueURI()
{
return this.createURN(this.id);
},
get contentBase()
{
if(this.xmlContentBase)
return this.xmlContentBase
else
return this.mURL;
},
store: function()
{
// this.title and this.content contain HTML.
// this.mUrl and this.contentBase contain plain text.
let stored = false;
let resource = this.findStoredResource();
if (!this.feed.folder)
return stored;
if (resource == null)
{
resource = FeedUtils.rdf.GetResource(this.itemUniqueURI);
if (!this.content)
{
FeedUtils.log.trace("FeedItem.store: " + this.identity +
" no content; storing description or title");
this.content = this.description || this.title;
}
let content = this.MESSAGE_TEMPLATE;
content = content.replace(/%TITLE%/, this.title);
content = content.replace(/%BASE%/, this.htmlEscape(this.contentBase));
content = content.replace(/%CONTENT%/, this.content);
this.content = content;
this.writeToFolder();
this.markStored(resource);
stored = true;
}
this.markValid(resource);
return stored;
},
findStoredResource: function()
{
// Checks to see if the item has already been stored in its feed's
// message folder.
FeedUtils.log.trace("FeedItem.findStoredResource: checking if stored - " +
this.identity);
let server = this.feed.server;
let folder = this.feed.folder;
if (!folder)
{
FeedUtils.log.debug("FeedItem.findStoredResource: folder '" +
this.feed.folderName +
"' doesn't exist; creating as child of " +
server.rootMsgFolder.prettyName + "\n");
this.feed.createFolder();
return null;
}
let ds = FeedUtils.getItemsDS(server);
let itemURI = this.itemUniqueURI;
let itemResource = FeedUtils.rdf.GetResource(itemURI);
let downloaded = ds.GetTarget(itemResource, FeedUtils.FZ_STORED, true);
if (!downloaded ||
downloaded.QueryInterface(Ci.nsIRDFLiteral).Value == "false")
{
FeedUtils.log.trace("FeedItem.findStoredResource: not stored");
return null;
}
FeedUtils.log.trace("FeedItem.findStoredResource: already stored");
return itemResource;
},
markValid: function(resource)
{
let ds = FeedUtils.getItemsDS(this.feed.server);
let newTimeStamp = FeedUtils.rdf.GetLiteral(new Date().getTime());
let currentTimeStamp = ds.GetTarget(resource,
FeedUtils.FZ_LAST_SEEN_TIMESTAMP,
true);
if (currentTimeStamp)
ds.Change(resource, FeedUtils.FZ_LAST_SEEN_TIMESTAMP,
currentTimeStamp, newTimeStamp);
else
ds.Assert(resource, FeedUtils.FZ_LAST_SEEN_TIMESTAMP,
newTimeStamp, true);
if (!ds.HasAssertion(resource, FeedUtils.FZ_FEED,
FeedUtils.rdf.GetResource(this.feed.url), true))
ds.Assert(resource, FeedUtils.FZ_FEED,
FeedUtils.rdf.GetResource(this.feed.url), true);
if (ds.hasArcOut(resource, FeedUtils.FZ_VALID))
{
let currentValue = ds.GetTarget(resource, FeedUtils.FZ_VALID, true);
ds.Change(resource, FeedUtils.FZ_VALID,
currentValue, FeedUtils.RDF_LITERAL_TRUE);
}
else
ds.Assert(resource, FeedUtils.FZ_VALID, FeedUtils.RDF_LITERAL_TRUE, true);
},
markStored: function(resource)
{
let ds = FeedUtils.getItemsDS(this.feed.server);
if (!ds.HasAssertion(resource, FeedUtils.FZ_FEED,
FeedUtils.rdf.GetResource(this.feed.url), true))
ds.Assert(resource, FeedUtils.FZ_FEED,
FeedUtils.rdf.GetResource(this.feed.url), true);
let currentValue;
if (ds.hasArcOut(resource, FeedUtils.FZ_STORED))
{
currentValue = ds.GetTarget(resource, FeedUtils.FZ_STORED, true);
ds.Change(resource, FeedUtils.FZ_STORED,
currentValue, FeedUtils.RDF_LITERAL_TRUE);
}
else
ds.Assert(resource, FeedUtils.FZ_STORED,
FeedUtils.RDF_LITERAL_TRUE, true);
},
mimeEncodeSubject: function(aSubject, aCharset)
{
// This routine sometimes throws exceptions for mis-encoded data so
// wrap it with a try catch for now.
let newSubject;
try
{
newSubject = mailServices.mimeConverter.encodeMimePartIIStr_UTF8(aSubject,
false,
aCharset, 9, 72);
}
catch (ex)
{
newSubject = aSubject;
}
return newSubject;
},
writeToFolder: function()
{
FeedUtils.log.trace("FeedItem.writeToFolder: " + this.identity +
" writing to message folder " + this.feed.name);
// The subject may contain HTML entities. Convert these to their unencoded
// state. i.e. & becomes '&'.
let title = this.title;
title = this.mParserUtils.convertToPlainText(
title,
Ci.nsIDocumentEncoder.OutputSelectionOnly |
Ci.nsIDocumentEncoder.OutputAbsoluteLinks,
0);
// Compress white space in the subject to make it look better. Trim
// leading/trailing spaces to prevent mbox header folding issue at just
// the right subject length.
title = title.replace(/[\t\r\n]+/g, " ").trim();
this.title = this.mimeEncodeSubject(title, this.characterSet);
// If the date looks like it's in W3C-DTF format, convert it into
// an IETF standard date. Otherwise assume it's in IETF format.
if (this.mDate.search(/^\d\d\d\d/) != -1)
this.mDate = new Date(this.mDate).toUTCString();
// If there is an inreplyto value, create the headers.
let inreplytoHdrsStr = this.inReplyTo ?
("References: " + this.inReplyTo + "\n" +
"In-Reply-To: " + this.inReplyTo + "\n") : "";
// Support multiple authors in From.
let fromStr = this.createHeaderStrFromArray("From: ", this.author);
// If there are keywords (categories), create the headers.
let keywordsStr = this.createHeaderStrFromArray("Keywords: ", this.keywords);
// Escape occurrences of "From " at the beginning of lines of
// content per the mbox standard, since "From " denotes a new
// message, and add a line break so we know the last line has one.
this.content = this.content.replace(/([\r\n]+)(>*From )/g, "$1>$2");
this.content += "\n";
// The opening line of the message, mandated by standards to start
// with "From ". It's useful to construct this separately because
// we not only need to write it into the message, we also need to
// use it to calculate the offset of the X-Mozilla-Status lines from
// the front of the message for the statusOffset property of the
// 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' +
fromStr +
'MIME-Version: 1.0\n' +
'Subject: ' + this.title + '\n' +
inreplytoHdrsStr +
keywordsStr +
'Content-Transfer-Encoding: 8bit\n' +
'Content-Base: ' + this.mURL + '\n';
if (this.enclosures.length)
{
let boundaryID = source.length;
source += 'Content-Type: multipart/mixed; boundary="' +
this.ENCLOSURE_HEADER_BOUNDARY_PREFIX + boundaryID + '"' + '\n\n' +
'This is a multi-part message in MIME format.\n' +
this.ENCLOSURE_BOUNDARY_PREFIX + boundaryID + '\n' +
'Content-Type: text/html; charset=' + this.characterSet + '\n' +
'Content-Transfer-Encoding: 8bit\n' +
this.content;
this.enclosures.forEach(function(enclosure) {
source += enclosure.convertToAttachment(boundaryID);
});
source += this.ENCLOSURE_BOUNDARY_PREFIX + boundaryID + '--' + '\n\n\n';
}
else
source += 'Content-Type: text/html; charset=' + this.characterSet + '\n' +
this.content;
FeedUtils.log.trace("FeedItem.writeToFolder: " + this.identity +
" is " + source.length + " characters long");
// Get the folder and database storing the feed's messages and headers.
let folder = this.feed.folder.QueryInterface(Ci.nsIMsgLocalMailFolder);
let msgFolder = folder.QueryInterface(Ci.nsIMsgFolder);
msgFolder.gettingNewMessages = true;
// Source is a unicode string, we want to save a char * string in
// the original charset. So convert back.
this.mUnicodeConverter.charset = this.characterSet;
let msgDBHdr = folder.addMessage(this.mUnicodeConverter.ConvertFromUnicode(source));
msgDBHdr.OrFlags(Ci.nsMsgMessageFlags.FeedMsg);
msgFolder.gettingNewMessages = false;
this.tagItem(msgDBHdr, this.keywords);
},
/**
* Create a header string from an array. Intended for comma separated headers
* like From or Keywords. In the case of a longer than RFC5322 recommended
* line length, create multiple folded lines (easier to parse than multiple
* headers).
*
* @param {String} headerName - Name of the header.
* @param {String[]} headerItemsArray - An Array of strings to concatenate.
*
* @returns {String} - The header string.
*/
createHeaderStrFromArray(headerName, headerItemsArray) {
let headerStr = "";
if (!headerItemsArray || headerItemsArray.length == 0) {
return headerStr;
}
const HEADER = headerName;
const LINELENGTH = 78;
const MAXLINELENGTH = 990;
let items = [].concat(headerItemsArray);
let lines = [];
headerStr = HEADER;
while (items.length) {
let item = items.shift();
if (headerStr.length + item.length > LINELENGTH &&
headerStr.length > HEADER.length) {
lines.push(headerStr);
headerStr = " ".repeat(HEADER.length);
}
headerStr += headerStr.length + item.length > MAXLINELENGTH ?
item.substr(0, MAXLINELENGTH - headerStr.length) + "…, " :
item + ", ";
}
headerStr = headerStr.replace(/,\s$/, "\n");
lines.push(headerStr);
headerStr = lines.join("\n");
return headerStr;
},
/**
* Autotag messages.
*
* @param nsIMsgDBHdr aMsgDBHdr - message to tag
* @param array aKeywords - keywords (tags)
*/
tagItem: function(aMsgDBHdr, aKeywords)
{
let categoryPrefs = this.feed.categoryPrefs();
if (!aKeywords.length || !categoryPrefs.enabled)
return;
let msgArray = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
msgArray.appendElement(aMsgDBHdr, false);
let prefix = categoryPrefs.prefixEnabled ? categoryPrefs.prefix : "";
let rtl = Services.prefs.getIntPref("bidi.direction") == 2;
let keys = [];
for (let keyword of aKeywords)
{
keyword = rtl ? keyword + prefix : prefix + keyword;
let keyForTag = MailServices.tags.getKeyForTag(keyword);
if (!keyForTag)
{
// Add the tag if it doesn't exist.
MailServices.tags.addTag(keyword, "", FeedUtils.AUTOTAG);
keyForTag = MailServices.tags.getKeyForTag(keyword);
}
// Add the tag key to the keys array.
keys.push(keyForTag);
}
if (keys.length)
// Add the keys to the message.
aMsgDBHdr.folder.addKeywordsToMessages(msgArray, keys.join(" "));
},
htmlEscape: function(s)
{
s = s.replace(/&/g, "&");
s = s.replace(/>/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/'/g, "'");
s = s.replace(/"/g, """);
return s;
},
createURN: function(aName)
{
// Returns name as a URN in the 'feeditem' namespace. The returned URN is
// (or is intended to be) RFC2141 compliant.
// The builtin encodeURI provides nearly the exact encoding functionality
// required by the RFC. The exceptions are that NULL characters should not
// appear, and that #, /, ?, &, and ~ should be escaped.
// NULL characters are removed before encoding.
let name = aName.replace(/\0/g, "");
let encoded = encodeURI(name);
encoded = encoded.replace(/\#/g, "%23");
encoded = encoded.replace(/\//g, "%2f");
encoded = encoded.replace(/\?/g, "%3f");
encoded = encoded.replace(/\&/g, "%26");
encoded = encoded.replace(/\~/g, "%7e");
return FeedUtils.FZ_ITEM_NS + encoded;
}
};
// A feed enclosure is to RSS what an attachment is for e-mail. We make
// enclosures look like attachments in the UI.
function FeedEnclosure(aURL, aContentType, aLength, aTitle)
{
this.mURL = aURL;
// Store a reasonable mimetype if content-type is not present.
this.mContentType = aContentType || "application/unknown";
this.mLength = aLength;
this.mTitle = aTitle;
// Generate a fileName from the URL.
if (this.mURL)
{
try
{
this.mFileName = Services.io.newURI(this.mURL, null, null).
QueryInterface(Ci.nsIURL).
fileName;
}
catch(ex)
{
this.mFileName = this.mURL;
}
}
}
FeedEnclosure.prototype =
{
mURL: "",
mContentType: "",
mLength: 0,
mFileName: "",
mTitle: "",
ENCLOSURE_BOUNDARY_PREFIX: "--------------", // 14 dashes
// Returns a string that looks like an e-mail attachment which represents
// the enclosure.
convertToAttachment: function(aBoundaryID)
{
return '\n' +
this.ENCLOSURE_BOUNDARY_PREFIX + aBoundaryID + '\n' +
'Content-Type: ' + this.mContentType +
'; name="' + (this.mTitle || this.mFileName) +
(this.mLength ? '"; size=' + this.mLength : '"') + '\n' +
'X-Mozilla-External-Attachment-URL: ' + this.mURL + '\n' +
'Content-Disposition: attachment; filename="' + this.mFileName + '"\n\n' +
FeedUtils.strings.GetStringFromName("externalAttachmentMsg") + '\n';
}
};
|