forked from protz/thunderbird-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.js
428 lines (399 loc) · 15.9 KB
/
compose.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
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
/* ***** 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 Thunderbird Conversations
*
* 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 Composition-related utils: quoting, wrapping text before
* sending a message, converting back and forth between HTML and plain text...
* @author Jonathan Protzenko
*/
var EXPORTED_SYMBOLS = [
'quoteMsgHdr', 'citeString',
'htmlToPlainText', 'simpleWrap',
'plainTextToHtml', 'replyAllParams',
'determineComposeHtml', 'composeMessageTo',
]
const {classes: Cc, interfaces: Ci, utils: Cu, results : Cr} = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); // for generateQI, defineLazyServiceGetter
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource:///modules/gloda/mimemsg.js"); // For MsgHdrToMimeMessage
Cu.import("resource:///modules/mailServices.js");
XPCOMUtils.importRelative(this, "misc.js");
XPCOMUtils.importRelative(this, "msgHdrUtils.js");
XPCOMUtils.importRelative(this, "../log.js");
let Log = setupLogging(logRoot+".Stdlib");
/**
* Use the mailnews component to stream a message, and process it in a way
* that's suitable for quoting (strip signature, remove images, stuff like
* that).
* @param {nsIMsgDBHdr} aMsgHdr The message header that you want to quote
* @param {Function} k The continuation. This function will be passed quoted
* text suitable for insertion in an HTML editor. You can pass this to
* htmlToPlainText if you're running a plaintext editor.
* @return
*/
function quoteMsgHdr(aMsgHdr, k) {
let chunks = [];
// Everyone knows that nsICharsetConverterManager and nsIUnicodeDecoder
// are not to be used from scriptable code, right? And the error you'll
// get if you try to do so is really meaningful, and that you'll have no
// trouble figuring out where the error comes from...
let unicodeConverter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Ci.nsIScriptableUnicodeConverter);
unicodeConverter.charset = "UTF-8";
let listener = {
/**@ignore*/
setMimeHeaders: function () {
},
/**@ignore*/
onStartRequest: function (/* nsIRequest */ aRequest, /* nsISupports */ aContext) {
},
/**@ignore*/
onStopRequest: function (/* nsIRequest */ aRequest, /* nsISupports */ aContext, /* int */ aStatusCode) {
let data = chunks.join("");
k(data);
},
/**@ignore*/
onDataAvailable: function (/* nsIRequest */ aRequest, /* nsISupports */ aContext,
/* nsIInputStream */ aStream, /* int */ aOffset, /* int */ aCount) {
// Fortunately, we have in Gecko 2.0 a nice wrapper
let data = NetUtil.readInputStreamToString(aStream, aCount);
// Now each character of the string is actually to be understood as a byte
// of a UTF-8 string.
// So charCodeAt is what we want here...
let array = [];
for (let i = 0; i < data.length; ++i)
array[i] = data.charCodeAt(i);
// Yay, good to go!
chunks.push(unicodeConverter.convertFromByteArray(array, array.length));
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, Ci.nsIStreamListener,
Ci.nsIMsgQuotingOutputStreamListener, Ci.nsIRequestObserver])
};
// Here's what we want to stream...
let msgUri = msgHdrGetUri(aMsgHdr);
/**
* Quote a particular message specified by its URI.
*
* @param charset optional parameter - if set, force the message to be
* quoted using this particular charset
*/
// void quoteMessage(in string msgURI, in boolean quoteHeaders,
// in nsIMsgQuotingOutputStreamListener streamListener,
// in string charset, in boolean headersOnly);
let quoter = Cc["@mozilla.org/messengercompose/quoting;1"]
.createInstance(Ci.nsIMsgQuote);
quoter.quoteMessage(msgUri, false, listener, "", false);
}
/**
* A function that properly quotes a plaintext email.
* @param {String} aStr The mail body that we're expected to quote.
* @return {String} The quoted mail body with >'s properly taken care of.
*/
function citeString(aStr) {
let l = aStr.length;
return aStr.replace("\n", function (match, offset, str) {
// http://mxr.mozilla.org/comm-central/source/mozilla/editor/libeditor/text/nsInternetCiter.cpp#96
if (offset < l - 1) {
if (str[offset+1] != ">" && str[offset+1] != "\n" && str[offset+1] != "\r")
return "\n> ";
else
return "\n>";
} else {
return match;
}
}, "g");
}
/**
* Wrap some text. Beware, that function doesn't do rewrapping, and only
* operates on non-quoted lines. This is only useful in our very specific case
* where the quoted lines have been properly wrapped for format=flowed already,
* and the non-quoted lines are the only ones that need wrapping for
* format=flowed.
* Beware, this function will treat all lines starting with >'s as quotations,
* even user-inserted ones. We would need support from the editor to proceed
* otherwise, and the current textarea doesn't provide this.
* This function, when breaking lines, will do space-stuffing per the RFC if
* after the break the text starts with From or >.
* @param {String} txt The text that should be wrapped.
* @param {Number} width (optional) The width we should wrap to. Default to 72.
* @return {String} The text with non-quoted lines wrapped. This is suitable for
* sending as format=flowed.
*/
function simpleWrap(txt, width) {
if (!width)
width = 72;
function maybeEscape(line) {
if (line.indexOf("From") === 0 || line.indexOf(">") === 0)
return (" " + line);
else
return line;
}
/**
* That function takes a (long) line, and splits it into many lines.
* @param soFar {Array String} an accumulator of the lines we've wrapped already
* @param remaining {String} the remaining string to wrap
*/
function splitLongLine(soFar, remaining) {
if (remaining.length > width) {
// Start at the end of the line, and move back until we find a word
// boundary.
let i = width - 1;
while (remaining[i] != " " && i > 0)
i--;
// We found a word boundary, break there
if (i > 0) {
// This includes the trailing space that indicates that we are wrapping
// a long line with format=flowed.
soFar.push(maybeEscape(remaining.substring(0, i+1)));
return splitLongLine(soFar, remaining.substring(i+1, remaining.length));
} else {
// No word boundary, break at the first space
let j = remaining.indexOf(" ");
if (j > 0) {
// Same remark about the trailing space.
soFar.push(maybeEscape(remaining.substring(0, j+1)));
return splitLongLine(soFar, remaining.substring(j+1, remaining.length));
} else {
// Make sure no one interprets this as a line continuation.
soFar.push(remaining.trimRight());
return soFar.join("\n");
}
}
} else {
// Same remark about the trailing space.
soFar.push(maybeEscape(remaining.trimRight()));
return soFar.join("\n");
}
}
let lines = txt.split(/\r?\n/);
for each (let [i, line] in Iterator(lines)) {
if (line.length > width && line[0] != ">")
lines[i] = splitLongLine([], line);
}
return lines.join("\n");
}
/**
* Convert HTML into text/plain suitable for insertion right away in the mail
* body. If there is text with >'s at the beginning of lines, these will be
* space-stuffed, and the same goes for Froms. <blockquote>s will be converted
* with the suitable >'s at the beginning of the line, and so on...
* This function also takes care of rewrapping at 72 characters, so your quoted
* lines will be properly wrapped too. This means that you can add some text of
* your own, and then pass this to simpleWrap, it should "just work" (unless
* the user has edited a quoted line and made it longer than 990 characters, of
* course).
* @param {String} aHtml A string containing the HTML that's to be converted.
* @return {String} A text/plain string suitable for insertion in a mail body.
*/
function htmlToPlainText(aHtml) {
// Yes, this is ridiculous, we're instanciating composition fields just so
// that they call ConvertBufPlainText for us. But ConvertBufToPlainText
// really isn't easily scriptable, so...
let fields = Cc["@mozilla.org/messengercompose/composefields;1"]
.createInstance(Ci.nsIMsgCompFields);
fields.body = aHtml;
fields.forcePlainText = true;
fields.ConvertBodyToPlainText();
return fields.body;
}
/**
* @ignore
*/
function citeLevel (line) {
let i;
for (i = 0; line[i] == ">" && i < line.length; ++i)
; // nop
return i;
};
/**
* Just try to convert quoted lines back to HTML markup (<blockquote>s).
* @param {String} txt
* @return {String}
*/
function plainTextToHtml(txt) {
let lines = txt.split(/\r?\n/);
let newLines = [];
let level = 0;
for each (let [, line] in Iterator(lines)) {
let newLevel = citeLevel(line);
if (newLevel > level)
for (let i = level; i < newLevel; ++i)
newLines.push('<blockquote type="cite">');
if (newLevel < level)
for (let i = newLevel; i < level; ++i)
newLines.push('</blockquote>');
let newLine = line[newLevel] == " "
? escapeHtml(line.substring(newLevel + 1, line.length))
: escapeHtml(line.substring(newLevel, line.length))
;
newLines.push(newLine);
level = newLevel;
}
return newLines.join("\n");
}
function parse(aMimeLine) {
let emails = {};
let fullNames = {};
let names = {};
let numAddresses = MailServices.headerParser.parseHeadersWithArray(aMimeLine, emails, names, fullNames);
return [names.value, emails.value];
}
/**
* Analyze a message header, and then return all the compose parameters for the
* reply-all case.
* @param {nsIIdentity} The identity you've picked for the reply.
* @param {nsIMsgDbHdr} The message header.
* @param {k} The function to call once we've determined all parameters. Take an
* argument like
* {{ to: [[name, email]], cc: [[name, email]], bcc: [[name, email]]}}
*/
function replyAllParams(aIdentity, aMsgHdr, k) {
// Do the whole shebang to find out who to send to...
let [[author], [authorEmailAddress]] = parse(aMsgHdr.mime2DecodedAuthor);
let [recipients, recipientsEmailAddresses] = parse(aMsgHdr.mime2DecodedRecipients);
let [ccList, ccListEmailAddresses] = parse(aMsgHdr.ccList);
let [bccList, bccListEmailAddresses] = parse(aMsgHdr.bccList);
authorEmailAddress = authorEmailAddress.toLowerCase();
recipientsEmailAddresses = [x.toLowerCase()
for each ([, x] in Iterator(recipientsEmailAddresses))];
ccListEmailAddresses = [x.toLowerCase() for each ([, x] in Iterator(ccListEmailAddresses))];
bccListEmailAddresses = [x.toLowerCase() for each ([, x] in Iterator(bccListEmailAddresses))];
let identity = aIdentity;
let identityEmail = identity.email.toLowerCase();
let to = [], cc = [], bcc = [];
let isReplyToOwnMsg = false;
for each (let [i, identity] in Iterator(gIdentities)) {
// It happens that gIdentities.default is null!
if (!identity) {
Log.debug("This identity is null, pretty weird...");
continue;
}
let email = identity.email.toLowerCase();
if (email == authorEmailAddress)
isReplyToOwnMsg = true;
if (recipientsEmailAddresses.some(function (x) x == email))
isReplyToOwnMsg = false;
if (ccListEmailAddresses.some(function (x) x == email))
isReplyToOwnMsg = false;
}
// Actually we are implementing the "Reply all" logic... that's better, no one
// wants to really use reply anyway ;-)
if (isReplyToOwnMsg) {
to = [[r, recipientsEmailAddresses[i]]
for each ([i, r] in Iterator(recipients))];
} else {
to = [[author, authorEmailAddress]];
}
cc = [[cc, ccListEmailAddresses[i]]
for each ([i, cc] in Iterator(ccList))
if (ccListEmailAddresses[i] != identityEmail)];
if (!isReplyToOwnMsg)
cc = cc.concat
([[r, recipientsEmailAddresses[i]]
for each ([i, r] in Iterator(recipients))
if (recipientsEmailAddresses[i] != identityEmail)]);
bcc = [[bcc, bccListEmailAddresses[i]]
for each ([i, bcc] in Iterator(bccList))];
let finish = function (to, cc, bcc) {
let hashMap = {};
hashMap[to[0][1]] = null;
cc = cc.filter(function ([name, email])
let (r = (email in hashMap))
(hashMap[email] = null,
!r)
);
bcc = bcc.filter(function ([name, email])
let (r = (email in hashMap))
(hashMap[email] = null,
!r)
);
k({ to: to, cc: cc, bcc: bcc });
}
// Do we have a Reply-To header?
MsgHdrToMimeMessage(aMsgHdr, null, function (aMsgHdr, aMimeMsg) {
if ("reply-to" in aMimeMsg.headers) {
let [[name], [email]] = parse(aMimeMsg.headers["reply-to"]);
email = email.toLowerCase();
if (email) {
cc = cc.concat([to[0]]); // move the to in cc
to = [[name, email]];
}
}
finish(to, cc, bcc);
}, true, {
partsOnDemand: true,
}); // do download
}
/**
* This function replaces nsMsgComposeService::determineComposeHTML, which is
* marked as [noscript], just to make our lives complicated. [insert random rant
* here].
*
* @param aIdentity (optional) You can specify the identity which you would like
* to get the preference for.
* @return a bool which is true if you should compose in HTML
*/
function determineComposeHtml(aIdentity) {
if (!aIdentity)
aIdentity = gIdentities.default;
if (aIdentity) {
return (aIdentity.composeHtml == Ci.nsIMsgCompFormat.HTML);
} else {
return Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
.getBranch(null)
.getBoolPref("mail.compose_html");
}
}
/**
* Open a composition window for the given email address.
* @param aEmail {String}
* @param aDisplayedFolder {nsIMsgFolder} pass gFolderDisplay.displayedFolder
*/
function composeMessageTo(aEmail, aDisplayedFolder) {
let fields = Cc["@mozilla.org/messengercompose/composefields;1"]
.createInstance(Ci.nsIMsgCompFields);
let params = Cc["@mozilla.org/messengercompose/composeparams;1"]
.createInstance(Ci.nsIMsgComposeParams);
fields.to = aEmail
params.type = Ci.nsIMsgCompType.New;
params.format = Ci.nsIMsgCompFormat.Default;
if (aDisplayedFolder) {
params.identity = MailServices.accounts
.getFirstIdentityForServer(aDisplayedFolder.server);
}
params.composeFields = fields;
MailServices.compose.OpenComposeWindowWithParams(null, params);
}