forked from protz/thunderbird-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgHdrUtils.js
More file actions
507 lines (460 loc) · 15.7 KB
/
msgHdrUtils.js
File metadata and controls
507 lines (460 loc) · 15.7 KB
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mail utility functions for GMail Conversation View
*
* The Initial Developer of the Original Code is
* Jonathan Protzenko
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/**
* @fileoverview A whole bunch of utility functions that will abstract away
* various low-level nsIMsgDbHdr operations. The idea is to save time by not
* having to lookup how to do simple actions.
* @author Jonathan Protzenko
*/
var EXPORTED_SYMBOLS = [
// Low-level XPCOM boring stuff
"msgHdrToMessageBody", "msgHdrToNeckoURL", "msgHdrGetTags", "msgUriToMsgHdr",
"msgHdrGetUri", "msgHdrFromNeckoUrl", "msgHdrSetTags",
// Quickly identify a message
"msgHdrIsDraft", "msgHdrIsSent", "msgHdrIsArchive", "msgHdrIsInbox",
"msgHdrIsRss", "msgHdrIsNntp", "msgHdrIsJunk",
// Actions on a set of message headers
"msgHdrsMarkAsRead", "msgHdrsArchive", "msgHdrsDelete",
// Doesn't really belong here
"getMail3Pane",
// Higher-level functions
"msgHdrGetHeaders",
// Modify messages, raw.
"msgHdrsModifyRaw",
];
// from mailnews/base/public/nsMsgFolderFlags.idl
const nsMsgFolderFlags_SentMail = 0x00000200;
const nsMsgFolderFlags_Drafts = 0x00000400;
const nsMsgFolderFlags_Archive = 0x00004000;
const nsMsgFolderFlags_Inbox = 0x00001000;
const PR_WRONLY = 0x02;
const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", null);
const {MimeMessage, MsgHdrToMimeMessage} = ChromeUtils.import("resource:///modules/gloda/mimemsg.js", null);
const {toXPCOMArray} = ChromeUtils.import("resource:///modules/iteratorUtils.jsm", null);
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm", null);
const {MailServices} = ChromeUtils.import("resource:///modules/mailServices.js", null);
Cu.importGlobalProperties(["URL"]);
const {entries, generateQI} = ChromeUtils.import(new URL("misc.js", this.__URI__), null);
// Adding a messenger lazy getter to the MailServices even though it's not a service
XPCOMUtils.defineLazyGetter(MailServices, "messenger", function() {
return Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
});
/**
* Get a given message header's uri.
* @param {nsIMsgDbHdr} aMsg The message
* @return {String}
*/
function msgHdrGetUri(aMsg) {
return aMsg.folder.getUriForMsg(aMsg);
}
/**
* Get a msgHdr from a message URI (msgHdr.URI).
* @param {String} aUri The URI of the message
* @return {nsIMsgDbHdr}
*/
function msgUriToMsgHdr(aUri) {
try {
let messageService = MailServices.messenger.messageServiceFromURI(aUri);
return messageService.messageURIToMsgHdr(aUri);
} catch (e) {
dump("Unable to get " + aUri + " — returning null instead");
return null;
}
}
/**
* Tells if the message is in the account's inbox
* @param {nsIMsgDbHdr} msgHdr The message header to examine
* @return {bool}
*/
function msgHdrIsInbox(msgHdr) {
return msgHdr.folder.getFlag(nsMsgFolderFlags_Inbox);
}
/**
* Tells if the message is a draft message
* @param {nsIMsgDbHdr} msgHdr The message header to examine
* @return {bool}
*/
function msgHdrIsDraft(msgHdr) {
return msgHdr.folder.getFlag(nsMsgFolderFlags_Drafts);
}
/**
* Tells if the message is a sent message
* @param {nsIMsgDbHdr} msgHdr The message header to examine
* @return {bool}
*/
function msgHdrIsSent(msgHdr) {
return msgHdr.folder.getFlag(nsMsgFolderFlags_SentMail);
}
/**
* Tells if the message is an archived message
* @param {nsIMsgDbHdr} msgHdr The message header to examine
* @return {bool}
*/
function msgHdrIsArchive(msgHdr) {
return msgHdr.folder.getFlag(nsMsgFolderFlags_Archive);
}
/**
* Get a nsIMsgDbHdr from a Necko URL.
* @param {String} The URL
* @return {nsIMsgDbHdr} The message header.
*/
function msgHdrFromNeckoUrl(aUrl) {
return aUrl.QueryInterface(Ci.nsIMsgMessageUrl).messageHeader;
}
/**
* Get a string containing the body of a messsage.
* @param {nsIMsgDbHdr} aMessageHeader The message header
* @param {bool} aStripHtml Keep html?
* @return {string}
*/
function msgHdrToMessageBody(aMessageHeader, aStripHtml, aLength) {
let messenger = Cc["@mozilla.org/messenger;1"].createInstance(Ci.nsIMessenger);
let listener = Cc["@mozilla.org/network/sync-stream-listener;1"].createInstance(Ci.nsISyncStreamListener);
let uri = aMessageHeader.folder.getUriForMsg(aMessageHeader);
messenger.messageServiceFromURI(uri).streamMessage(uri, listener, null, null, false, "");
let folder = aMessageHeader.folder;
/*
* AUTF8String getMsgTextFromStream(in nsIInputStream aStream, in ACString aCharset,
in unsigned long aBytesToRead, in unsigned long aMaxOutputLen,
in boolean aCompressQuotes, in boolean aStripHTMLTags,
out ACString aContentType);
*/
return folder.getMsgTextFromStream(
listener.inputStream, aMessageHeader.Charset, 2 * aLength, aLength, false, aStripHtml, { });
}
/**
* Get a nsIURI from a nsIMsgDBHdr
* @param {nsIMsgDbHdr} aMsgHdr The message header
* @return {nsIURI}
*/
function msgHdrToNeckoURL(aMsgHdr) {
let uri = aMsgHdr.folder.getUriForMsg(aMsgHdr);
let neckoURL = {};
let msgService = MailServices.messenger.messageServiceFromURI(uri);
msgService.GetUrlForUri(uri, neckoURL, null);
return neckoURL.value;
}
/**
* Given a msgHdr, return a list of tag objects. This function
* just does the messy work of understanding how tags are
* stored in nsIMsgDBHdrs.
*
* @param {nsIMsgDbHdr} aMsgHdr the msgHdr whose tags we want
* @return {nsIMsgTag array} a list of tag objects
*/
function msgHdrGetTags(aMsgHdr) {
let keywords = aMsgHdr.getStringProperty("keywords");
let keywordList = keywords.split(" ");
let keywordMap = {};
for (let keyword of keywordList) {
keywordMap[keyword] = true;
}
let tagArray = MailServices.tags.getAllTags({});
let tags = tagArray.filter(tag => tag.key in keywordMap);
return tags;
}
/**
* Set the tags for a given msgHdr.
*
* @param {nsIMsgDBHdr} aMsgHdr
* @param {nsIMsgTag array} aTags
*/
function msgHdrSetTags(aMsgHdr, aTags) {
let oldTagList = msgHdrGetTags(aMsgHdr);
let oldTags = {}; // hashmap
for (let tag of oldTagList)
oldTags[tag.key] = null;
let newTags = {};
let newTagList = aTags;
for (let tag of newTagList)
newTags[tag.key] = null;
let toAdd = newTagList.filter(x => !(x.key in oldTags)).map(x => x.key);
let toRemove = oldTagList.filter(x => !(x.key in newTags)).map(x => x.key);
let folder = aMsgHdr.folder;
let msgHdr = toXPCOMArray([aMsgHdr], Ci.nsIMutableArray);
folder.addKeywordsToMessages(msgHdr, toAdd.join(" "));
folder.removeKeywordsFromMessages(msgHdr, toRemove.join(" "));
aMsgHdr.folder.msgDatabase = null;
}
/**
* Mark an array of msgHdrs read (or unread)
* @param {nsIMsgDbHdr array} msgHdrs The message headers
* @param {bool} read True to mark them read, false to mark them unread
*/
function msgHdrsMarkAsRead(msgHdrs, read) {
let pending = {};
for (let msgHdr of msgHdrs) {
if (msgHdr.isRead == read)
continue;
if (!pending[msgHdr.folder.URI]) {
pending[msgHdr.folder.URI] = {
folder: msgHdr.folder,
msgs: Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray),
};
}
pending[msgHdr.folder.URI].msgs.appendElement(msgHdr);
}
for (let [, { folder, msgs } ] of entries(pending)) {
folder.markMessagesRead(msgs, read);
folder.msgDatabase = null; /* don't leak */
}
}
/**
* Delete a set of messages.
* @param {nsIMsgDbHdr array} msgHdrs The message headers
*/
function msgHdrsDelete(msgHdrs) {
let pending = {};
for (let msgHdr of msgHdrs) {
if (!pending[msgHdr.folder.URI]) {
pending[msgHdr.folder.URI] = {
folder: msgHdr.folder,
msgs: Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray),
};
}
pending[msgHdr.folder.URI].msgs.appendElement(msgHdr);
}
for (let [, { folder, msgs } ] of entries(pending)) {
folder.deleteMessages(msgs, getMail3Pane().msgWindow, false, false, null, true);
folder.msgDatabase = null; /* don't leak */
}
}
/**
* Get the main Thunderbird window. Used heavily to get a reference to globals
* that are defined in mail/base/content/.
* @return The window object for the main window.
*/
function getMail3Pane() {
return Services.wm.getMostRecentWindow("mail:3pane");
}
/**
* Archive a set of messages
* @param {nsIMsgDbHdr array} msgHdrs The message headers
*/
function msgHdrsArchive(msgHdrs) {
/* See
* http://mxr.mozilla.org/comm-central/source/suite/mailnews/mailWindowOverlay.js#1337
*
* The window is here because otherwise we don't have access to
* BatchMessageMover.
* */
let mail3PaneWindow = getMail3Pane();
let batchMover = new mail3PaneWindow.BatchMessageMover();
batchMover.archiveMessages(msgHdrs.filter(
x => !msgHdrIsArchive(x) && getMail3Pane().getIdentityForHeader(x).archiveEnabled
));
}
/**
* Tell if a message is an RSS feed iteme
* @param {nsIMsgDbHdr} msgHdr The message header
* @return {Bool}
*/
function msgHdrIsRss(msgHdr) {
return msgHdr.folder.server instanceof Ci.nsIRssIncomingServer;
}
/**
* Tell if a message is a NNTP message
* @param {nsIMsgDbHdr} msgHdr The message header
* @return {Bool}
*/
function msgHdrIsNntp(msgHdr) {
return msgHdr.folder.server instanceof Ci.nsINntpIncomingServer;
}
/**
* Tell if a message has been marked as junk.
* @param {nsIMsgDbHdr} msgHdr The message header
* @return {Bool}
*/
function msgHdrIsJunk(aMsgHdr) {
return aMsgHdr.getStringProperty("junkscore") == Ci.nsIJunkMailPlugin.IS_SPAM_SCORE;
}
/**
* Recycling the HeaderHandlerBase from mimemsg.js
*/
function HeaderHandler(aHeaders) {
this.headers = aHeaders;
}
HeaderHandler.prototype = {
__proto__: MimeMessage.prototype.__proto__, // == HeaderHandlerBase
};
/**
* Creates a stream listener that will call k once done, passing it the string
* that has been read.
*/
function createStreamListener(k) {
return {
_data: "",
_stream: null,
QueryInterface:
generateQI([Ci.nsIStreamListener, Ci.nsIRequestObserver]),
// nsIRequestObserver
onStartRequest(aRequest, aContext) {
},
onStopRequest(aRequest, aContext, aStatusCode) {
try {
k(this._data);
} catch (e) {
dump("Error inside stream listener:\n" + e + "\n");
}
},
// nsIStreamListener
onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount) {
if (this._stream == null) {
this._stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
this._stream.init(aInputStream);
}
this._data += this._stream.read(aCount);
},
};
}
/**
* @param aMsgHdr The message header whose headers you want
* @param k A function that takes a HeaderHandler object (see mimemsg.js).
* Such an object has a get function, a has function. It has a header property,
* whose keys are lowercased header names, and whose values are list of
* strings corresponding to the multiple entries found for that header.
*/
function msgHdrGetHeaders(aMsgHdr, k) {
let uri = msgHdrGetUri(aMsgHdr);
let messageService = MailServices.messenger.messageServiceFromURI(uri);
let fallback = () =>
MsgHdrToMimeMessage(aMsgHdr, null, function(aMsgHdr, aMimeMsg) {
k(aMimeMsg);
}, true, {
partsOnDemand: true,
});
// This is intentionally disabled because there's a bug in Thunderbird that
// renders the supposedly-useful streamHeaders function unusable.
if (false && "streamHeaders" in messageService) {
try {
messageService.streamHeaders(uri, createStreamListener(aRawString => {
let re = /\r?\n\s+/g;
let str = aRawString.replace(re, " ");
let lines = str.split(/\r?\n/);
let obj = {};
for (let line of lines) {
let i = line.indexOf(":");
if (i < 0)
continue;
let k = line.substring(0, i).toLowerCase();
let v = line.substring(i + 1).trim();
if (!(k in obj))
obj[k] = [];
obj[k].push(v);
}
k(new HeaderHandler(obj));
}), null, true);
} catch (e) {
fallback();
}
} else {
fallback();
}
}
/**
* @param aMsgHdrs The messages to modify
* @param aTransformer A function which takes the input data, modifies it, and
* returns the corresponding data. This is the _raw_ contents of the message.
*/
function msgHdrsModifyRaw(aMsgHdrs, aTransformer) {
let toCopy = [];
let toDelete = [];
let copyNext = () => {
dump("msgHdrModifyRaw: copying next\n");
let obj = toCopy.pop();
if (!obj) {
msgHdrsDelete(toDelete);
return;
}
let { msgHdr, tempFile } = obj;
MailServices.copy.CopyFileMessage(
tempFile,
msgHdr.folder,
null,
false,
msgHdr.flags,
msgHdr.getStringProperty("keywords"),
{
QueryInterface: generateQI([Ci.nsIMsgCopyServiceListener]),
OnStartCopy() {
},
OnProgress(aProgress, aProgressMax) {
},
SetMessageKey(aKey) {
},
GetMessageId(aMessageId) {
},
OnStopCopy(aStatus) {
if (Components.isSuccessCode(aStatus)) {
dump("msgHdrModifyRaw: copied successfully\n");
toDelete.push(msgHdr);
tempFile.remove(false);
}
copyNext();
},
},
null
);
};
let count = aMsgHdrs.length;
let tick = function() {
if (--count == 0)
copyNext();
};
for (let aMsgHdr of aMsgHdrs) {
let msgHdr = aMsgHdr;
let uri = msgHdrGetUri(msgHdr);
let messageService = MailServices.messenger.messageServiceFromURI(uri);
messageService.streamMessage(uri, createStreamListener(function(aRawString) {
let data = aTransformer(aRawString);
if (!data) {
dump("msgHdrModifyRaw: no data, aborting\n");
return;
}
let tempFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
tempFile.append("rethread.eml");
tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, parseInt("0600", 8));
let stream = Cc["@mozilla.org/network/file-output-stream;1"]
.createInstance(Ci.nsIFileOutputStream);
stream.init(tempFile, PR_WRONLY, parseInt("0600", 8), 0);
stream.write(data, data.length);
stream.close();
dump("msgHdrModifyRaw: wrote to file\n");
toCopy.push({ tempFile, msgHdr });
tick();
}), null, null, false, "");
}
}