forked from protz/thunderbird-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msgHdrUtils.js
382 lines (348 loc) · 13.1 KB
/
msgHdrUtils.js
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
/* ***** 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',
]
const {classes: Cc, interfaces: Ci, utils: Cu, results : Cr} = Components;
// from mailnews/base/public/nsMsgFolderFlags.idl
const nsMsgFolderFlags_SentMail = 0x00000200;
const nsMsgFolderFlags_Drafts = 0x00000400;
const nsMsgFolderFlags_Archive = 0x00004000;
const nsMsgFolderFlags_Inbox = 0x00001000;
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); // for defineLazyServiceGetter
Cu.import("resource:///modules/iteratorUtils.jsm"); // for toXPCOMArray
Cu.import("resource:///modules/mailServices.js");
// 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)
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)
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)
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)
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)
msgHdr.folder.getFlag(nsMsgFolderFlags_Archive)
/**
* Get a nsIMsgDbHdr from a Necko URL.
* @param {String} The URL
* @return {nsIMsgDbHdr} The message header.
*/
function msgHdrFromNeckoUrl(aUrl)
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] in Iterator(keywordList)) {
keywordMap[keyword] = true;
}
let tagArray = MailServices.tags.getAllTags({});
let tags = [];
for (let [iTag, tag] in Iterator(tagArray)) {
let tag = tagArray[iTag];
if (tag.key in keywordMap)
tags.push(tag);
}
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 each (let [, tag] in Iterator(oldTagList))
oldTags[tag.key] = null;
let newTags = {};
let newTagList = aTags;
for each (let [, tag] in Iterator(newTagList))
newTags[tag.key] = null;
let toAdd = [x.key for each ([, x] in Iterator(newTagList))
if (!(x.key in oldTags))];
let toRemove = [x.key for each ([, x] in Iterator(oldTagList))
if (!(x.key in newTags))];
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 each (msgHdr in 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, false);
}
for each (let { folder, msgs } in 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 each (msgHdr in 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, false);
}
for each (let { folder, msgs } in 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(aForce) {
return Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator)
.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(
function (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)
(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)
(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)
aMsgHdr.getStringProperty("junkscore") == Ci.nsIJunkMailPlugin.IS_SPAM_SCORE
// XXX implement some day
function msgHdrMarkAsJunk(msgHdr) {
//starts here http://mxr.mozilla.org/comm-central/source/mailnews/base/content/junkCommands.js#384
/* 2733 nsCOMPtr<nsIJunkMailPlugin> junkPlugin;
2734
2735 // if this is a junk command, get the junk plugin.
2736 if (command == nsMsgViewCommandType::junk ||
2737 command == nsMsgViewCommandType::unjunk)
2738 {
2739 // get the folder from the first item; we assume that
2740 // all messages in the view are from the same folder (no
2741 // more junk status column in the 'search messages' dialog
2742 // like in earlier versions...)
2743
2744 nsCOMPtr<nsIMsgIncomingServer> server;
2745 rv = folder->GetServer(getter_AddRefs(server));
2746 NS_ENSURE_SUCCESS(rv, rv);
2747
2748 nsCOMPtr<nsIMsgFilterPlugin> filterPlugin;
2749 rv = server->GetSpamFilterPlugin(getter_AddRefs(filterPlugin));
2750 NS_ENSURE_SUCCESS(rv, rv);
2751
2752 junkPlugin = do_QueryInterface(filterPlugin, &rv);
2753 NS_ENSURE_SUCCESS(rv, rv);
2754 if (!mJunkHdrs)
2755 {
2756 mJunkHdrs = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
2757 NS_ENSURE_SUCCESS(rv,rv);
2758 }
2759 }
2817 case nsMsgViewCommandType::junk:
2818 mNumMessagesRemainingInBatch++;
2819 mJunkHdrs->AppendElement(msgHdr, PR_FALSE);
2820 rv = SetMsgHdrJunkStatus(junkPlugin.get(), msgHdr,
2821 nsIJunkMailPlugin::JUNK);
2822 break;
2837 // Provide junk-related batch notifications
2838 if ((command == nsMsgViewCommandType::junk) &&
2839 (command == nsMsgViewCommandType::unjunk)) {
2840 nsCOMPtr<nsIMsgFolderNotificationService>
2841 notifier(do_GetService(NS_MSGNOTIFICATIONSERVICE_CONTRACTID));
2842 if (notifier)
2843 notifier->NotifyItemEvent(messages,
2844 NS_LITERAL_CSTRING("JunkStatusChanged"),
2845 (command == nsMsgViewCommandType::junk) ?
2846 kJunkMsgAtom : kNotJunkMsgAtom);
2847 } */
//check OnMessageClassified for the rest of the actions
//http://mxr.mozilla.org/comm-central/source/mailnews/base/content/junkCommands.js#241
//the listener is for automatic classification, for manual marking, the
//junkstatusorigin needs to be changed
}