mailparser
Advanced tools
Comparing version 0.2.33 to 0.3.0
@@ -865,4 +865,3 @@ | ||
* <p>Finds text parts and attachments from the tree. If there's several text/plain | ||
* or text/html parts, push the ones from the lower parts of the tree to the | ||
* alternatives array</p> | ||
* or text/html parts, join these into one</p> | ||
* | ||
@@ -875,3 +874,3 @@ * <p>Emits "end" when finished</p> | ||
this.mailData = {html:[], text:[], alternatives:[], attachments:[]}; | ||
this.mailData = {html:[], text:[], attachments:[]}; | ||
@@ -886,22 +885,6 @@ if(!this.mimeTree.meta.mimeMultipart){ | ||
for(i=0, len=this.mailData.html.length; i<len; i++){ | ||
if(!returnValue.html || this.mailData.html[i].level < htmlLevel){ | ||
if(returnValue.html){ | ||
if(!returnValue.alternatives){ | ||
returnValue.alternatives = []; | ||
} | ||
returnValue.alternatives.push({ | ||
contentType: "text/html", | ||
content: returnValue.html | ||
}); | ||
} | ||
htmlLevel = this.mailData.html[i].level; | ||
if(!returnValue.html && this.mailData.html[i].content){ | ||
returnValue.html = this.mailData.html[i].content; | ||
}else{ | ||
if(!returnValue.alternatives){ | ||
returnValue.alternatives = []; | ||
} | ||
returnValue.alternatives.push({ | ||
contentType: "text/html", | ||
content: this.mailData.html[i].content | ||
}); | ||
}else if(this.mailData.html[i].content){ | ||
returnValue.html = this._concatHTML(returnValue.html, this.mailData.html[i].content); | ||
} | ||
@@ -913,22 +896,6 @@ } | ||
for(i=0, len=this.mailData.text.length; i<len; i++){ | ||
if(!returnValue.text || this.mailData.text[i].level < textLevel){ | ||
if(returnValue.text){ | ||
if(!returnValue.alternatives){ | ||
returnValue.alternatives = []; | ||
} | ||
returnValue.alternatives.push({ | ||
contentType: "text/plain", | ||
content: returnValue.text | ||
}); | ||
} | ||
textLevel = this.mailData.text[i].level; | ||
if(!returnValue.text && this.mailData.text[i].content){ | ||
returnValue.text = this.mailData.text[i].content; | ||
}else{ | ||
if(!returnValue.alternatives){ | ||
returnValue.alternatives = []; | ||
} | ||
returnValue.alternatives.push({ | ||
contentType: "text/plain", | ||
content: this.mailData.text[i].content | ||
}); | ||
}else if(this.mailData.text[i].content){ | ||
returnValue.text += this.mailData.text[i].content; | ||
} | ||
@@ -1109,2 +1076,60 @@ } | ||
/** | ||
* <p>Joins two HTML blocks by removing the header of the added element<p> | ||
* | ||
* @param {Sting} htmlNode Original HTML contents | ||
* @param {String} newHTML HTML text to add to the original object node | ||
* @return {String} Joined HTML | ||
*/ | ||
MailParser.prototype._concatHTML = function(firstNode, secondNode){ | ||
var inserted = false, | ||
headerNode = "", | ||
htmlHeader = ""; | ||
firstNode = (firstNode || "").toString("utf-8"); | ||
secondNode = (secondNode || "").toString("utf-8"); | ||
if(!secondNode){ | ||
return firstNode; | ||
} | ||
if(!firstNode){ | ||
return secondNode; | ||
} | ||
if(firstNode.substr(0, 1024).replace(/\r?\n/g,"\u0000").match(/^[\s\u0000]*(<\!doctype\b[^>]*?>)?[\s\u0000]*<(html|head)\b[^>]*?>/i)){ | ||
headerNode = firstNode; | ||
}else if(secondNode.substr(0, 1024).replace(/\r?\n/g,"\u0000").match(/^[\s\u0000]*(<\!doctype\b[^>]*?>)?[\s\u0000]*<(html|head)\b[^>]*?>/i)){ | ||
headerNode = secondNode; | ||
} | ||
if(headerNode){ | ||
headerNode.replace(/\r?\n/g, "\u0000").replace(/^[\s\u0000]*(<\!doctype\b[^>]*?>)?[\s\u0000]*<(html|head)\b[^>]*>.*?<\/(head)\b[^>]*>(.*?<body\b[^>]*>)?/i, function(h){ | ||
var doctype = h.match(/^[\s\u0000]*(<\!doctype\b[^>]*?>)/i), | ||
html = h.match(/<html\b[^>]*?>/i), | ||
head = h.match(/<head\b[^>]*?>/i), | ||
body = h.match(/<body\b[^>]*?>/i); | ||
doctype = doctype && doctype[1] && doctype[1] + "\n" || ""; | ||
html = html && html[0] || "<head>"; | ||
head = head && head[0] || "<head>"; | ||
body = body && body[0] || "<body>"; | ||
h = h.replace(/<[\!\/]?(doctype|html|head|body)\b[^>]*?>/ig, "\u0000").replace(/\u0000+/g, "\n").trim(); | ||
htmlHeader = doctype + html + "\n" + head + (h ? h + "\n" : "") + "</head>\n" + body + "\n"; | ||
}); | ||
} | ||
firstNode = firstNode.replace(/\r?\n/g, "\u0000"). | ||
replace(/[\s\u0000]*<head\b[^>]*>.*?<\/(head|body)\b[^>]*>/gi, ""). | ||
replace(/[\s\u0000]*<[\!\/]?(doctype|html|body)\b[^>]*>[\s\u0000]*/gi, ""). | ||
replace(/\u0000/g, "\n"); | ||
secondNode = secondNode.replace(/\r?\n/g, "\u0000"). | ||
replace(/[\s\u0000]*<head\b[^>]*>.*?<\/(head|body)\b[^>]*>/gi, ""). | ||
replace(/[\s\u0000]*<[\!\/]?(doctype|html|body)\b[^>]*>[\s\u0000]*/gi, ""). | ||
replace(/\u0000/g, "\n"); | ||
return htmlHeader + firstNode + secondNode + (htmlHeader? (firstNode || secondNode ? "\n" : "") + "</body>\n</html>" : ""); | ||
}; | ||
/** | ||
* <p>Converts a string from one charset to another</p> | ||
@@ -1111,0 +1136,0 @@ * |
{ | ||
"name": "mailparser", | ||
"description": "Asynchronous and non-blocking parser for mime encoded e-mail messages", | ||
"version": "0.2.33", | ||
"version": "0.3.0", | ||
"author" : "Andris Reinman", | ||
@@ -6,0 +6,0 @@ "maintainers":[ |
MailParser | ||
========== | ||
**NB!** MailParser is now out of the box compatible with Windows - instead of compiled [Iconv](https://npmjs.org/package/iconv) module, [iconv-lite](https://npmjs.org/package/iconv-lite) is used instead when in Windows. | ||
[![Build Status](https://secure.travis-ci.org/andris9/mailparser.png)](http://travis-ci.org/andris9/mailparser) | ||
@@ -80,3 +78,2 @@ | ||
* **html** - html body | ||
* **alternatives** - an array of alternative bodies in addition to the default `html` and `text` - `[{contentType:"text/plain", content: "..."}]` | ||
* **attachments** - an array of attachments | ||
@@ -83,0 +80,0 @@ |
var MailParser = require("../lib/mailparser").MailParser, | ||
testCase = require('nodeunit').testCase, | ||
utillib = require("util"), | ||
encodinglib = require("encoding"); | ||
encodinglib = require("encoding"), | ||
fs = require("fs"); | ||
@@ -889,106 +890,2 @@ exports["General tests"] = { | ||
}); | ||
}, | ||
"Alternative content - Main TEXT first": function(test){ | ||
var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n"+ | ||
"\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: text/plain; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ1\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: multipart/related; boundary=DEF\r\n"+ | ||
"\r\n"+ | ||
"--DEF\r\n"+ | ||
"Content-type: text/plain; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ2\r\n"+ | ||
"--DEF--\r\n"+ | ||
"--ABC--", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.text, "ÕÄÖÜ1"); | ||
test.done(); | ||
}); | ||
}, | ||
"Alternative content - Main TEXT last": function(test){ | ||
var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n"+ | ||
"\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: multipart/related; boundary=DEF\r\n"+ | ||
"\r\n"+ | ||
"--DEF\r\n"+ | ||
"Content-type: text/plain; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ2\r\n"+ | ||
"--DEF--\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: text/plain; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ1\r\n"+ | ||
"--ABC--", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.text, "ÕÄÖÜ1"); | ||
test.done(); | ||
}); | ||
}, | ||
"Alternative content - Main HTML first": function(test){ | ||
var encodedHTML = "Content-type: multipart/mixed; boundary=ABC\r\n"+ | ||
"\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: text/html; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ1\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: multipart/related; boundary=DEF\r\n"+ | ||
"\r\n"+ | ||
"--DEF\r\n"+ | ||
"Content-type: text/html; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ2\r\n"+ | ||
"--DEF--\r\n"+ | ||
"--ABC--", | ||
mail = new Buffer(encodedHTML, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.html, "ÕÄÖÜ1"); | ||
test.done(); | ||
}); | ||
}, | ||
"Alternative content - Main HTML last": function(test){ | ||
var encodedHTML = "Content-type: multipart/mixed; boundary=ABC\r\n"+ | ||
"\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: multipart/related; boundary=DEF\r\n"+ | ||
"\r\n"+ | ||
"--DEF\r\n"+ | ||
"Content-type: text/html; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ2\r\n"+ | ||
"--DEF--\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-type: text/html; charset=utf-8\r\n"+ | ||
"\r\n"+ | ||
"ÕÄÖÜ1\r\n"+ | ||
"--ABC--", | ||
mail = new Buffer(encodedHTML, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.html, "ÕÄÖÜ1"); | ||
test.done(); | ||
}); | ||
} | ||
@@ -1220,3 +1117,53 @@ }; | ||
}); | ||
}, | ||
"Inline attachments": function(test){ | ||
var encodedText = "Content-type: multipart/mixed; boundary=ABC\r\n"+ | ||
"X-Test: =?UTF-8?Q?=C3=95=C3=84?= =?UTF-8?Q?=C3=96=C3=9C?=\r\n"+ | ||
"Subject: ABCDEF\r\n"+ | ||
"\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-Type: text/html\r\n"+ | ||
"\r\n"+ | ||
"<p>test 1</p>\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-Type: application/octet-stream\r\n"+ | ||
"Content-Transfer-Encoding: base64\r\n"+ | ||
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n"+ | ||
"\r\n"+ | ||
"AAECAwQFBg==\r\n"+ | ||
"--ABC\r\n"+ | ||
"Content-Type: text/html\r\n"+ | ||
"\r\n"+ | ||
"<p>test 2</p>\r\n"+ | ||
"--ABC--", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
test.expect(1); | ||
var mailparser = new MailParser({showAttachmentLinks: true}); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.html, '<p>test 1</p><br/>\n\n<div class="mailparser-attachment"><a href="cid:test.pdf@node"><test.pdf></a></div><br/>\n<p>test 2</p>'); | ||
test.done(); | ||
}); | ||
} | ||
}; | ||
exports["Advanced nested HTML"] = function(test){ | ||
var mail = fs.readFileSync(__dirname + "/nested.eml"); | ||
test.expect(2); | ||
var mailparser = new MailParser(); | ||
for(var i=0, len = mail.length; i<len; i++){ | ||
mailparser.write(new Buffer([mail[i]])); | ||
} | ||
mailparser.end(); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.text, "\nDear Sir,\n\nGood evening.\n\n\n \n\n\n\nThe footer\n"); | ||
test.equal(mail.html, "<p>Dear Sir</p>\n<p>Good evening.</p>\n<p></p><p>The footer</p>\n"); | ||
test.done(); | ||
}); | ||
} |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
11
119523
2576
217
1