mailparser
Advanced tools
Comparing version 0.2.21 to 0.2.22
@@ -5,3 +5,3 @@ | ||
* @author <a href="mailto:andris@node.ee">Andris Reinman</a> | ||
* @version 0.2.21 | ||
* @version 0.2.22 | ||
*/ | ||
@@ -37,2 +37,3 @@ | ||
* <li><b>defaultCharset</b> - the default charset for text/plain, text/html content, if not set reverts to Latin-1 | ||
* <li><b>showAttachmentLinks</b></li> - if set to true, show inlined attachment links | ||
* </ul> | ||
@@ -68,2 +69,5 @@ * | ||
* @private */ this._currentNode = this.mimeTree; | ||
// default values for the root node | ||
this._currentNode.priority = "normal"; | ||
@@ -447,2 +451,3 @@ /** | ||
value = this._parsePriority(value); | ||
this._currentNode.priority = value; | ||
break; | ||
@@ -682,4 +687,4 @@ case "message-id": | ||
value = value.toLowerCase().trim(); | ||
if(!isNaN(value)){ | ||
value = Number(value) || 0; | ||
if(!isNaN(parseInt(value,10))){ // support "X-Priority: 1 (Highest)" | ||
value = parseInt(value, 10) || 0; | ||
if(value == 3){ | ||
@@ -892,2 +897,6 @@ return "normal"; | ||
if(this.mimeTree.priority){ | ||
returnValue.priority = this.mimeTree.priority; | ||
} | ||
if(this.mimeTree.from){ | ||
@@ -927,5 +936,4 @@ returnValue.from = this.mimeTree.from; | ||
level = level || 1; | ||
for(var i=0, len = node.childNodes.length; i<len; i++){ | ||
this._processMimeNode(node.childNodes[i], level); | ||
this._processMimeNode(node.childNodes[i], level, node.meta.mimeMultipart); | ||
this._walkMimeTree(node.childNodes[i], level+1); | ||
@@ -942,4 +950,7 @@ } | ||
* @param {Number} [level=0] current depth | ||
* @param {String} mimeMultipart Type of multipart we are dealing with (if any) | ||
*/ | ||
MailParser.prototype._processMimeNode = function(node, level){ | ||
MailParser.prototype._processMimeNode = function(node, level, mimeMultipart){ | ||
var i, len; | ||
level = level || 0; | ||
@@ -950,7 +961,15 @@ | ||
case "text/html": | ||
if(mimeMultipart == "mixed" && this.mailData.html.length){ | ||
for(i=0, len = this.mailData.html.length; i<len; i++){ | ||
if(this.mailData.html[i].level == level){ | ||
this._joinHTMLNodes(this.mailData.html[i], node.content); | ||
return; | ||
} | ||
} | ||
} | ||
this.mailData.html.push({content: this._updateHTMLCharset(node.content || ""), level: level}); | ||
break; | ||
return; | ||
case "text/plain": | ||
this.mailData.text.push({content: node.content || "", level: level}); | ||
break; | ||
return; | ||
} | ||
@@ -963,2 +982,11 @@ }else{ | ||
this.mailData.attachments.push({content: node.meta || {}, level: level}); | ||
if(this.options.showAttachmentLinks && mimeMultipart == "mixed" && this.mailData.html.length){ | ||
for(i=0, len = this.mailData.html.length; i<len; i++){ | ||
if(this.mailData.html[i].level == level){ | ||
this._joinHTMLAttachment(this.mailData.html[i], node.meta); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
@@ -968,2 +996,65 @@ }; | ||
/** | ||
* <p>Joins two HTML blocks by removing the header of the added element<p> | ||
* | ||
* @param {Object} htmlNode Original HTML contents node object | ||
* @param {String} newHTML HTML text to add to the original object node | ||
*/ | ||
MailParser.prototype._joinHTMLNodes = function(htmlNode, newHTML){ | ||
var inserted = false; | ||
// process new HTML | ||
newHTML = (newHTML || "").toString("utf-8").trim(); | ||
// remove doctype from the beginning | ||
newHTML = newHTML.replace(/^\s*<\!doctype( [^>]*)?>/gi, ""); | ||
// remove <head> and <html> blocks | ||
newHTML = newHTML.replace(/<head( [^>]*)?>(.*)<\/head( [^>]*)?>/gi, ""). | ||
replace(/<\/?html( [^>]*)?>/gi, ""). | ||
trim(); | ||
// keep only text between <body> tags (if <body exists) | ||
newHTML.replace(/<body(?: [^>]*)?>(.*)<\/body( [^>]*)?>/gi, function(match, body){ | ||
newHTML = body.trim(); | ||
}); | ||
htmlNode.content = (htmlNode.content || "").toString("utf-8").trim(); | ||
htmlNode.content = htmlNode.content.replace(/<\/body( [^>]*)?>/i, function(match){ | ||
inserted = true; | ||
return "<br/>\n" + newHTML + match; | ||
}); | ||
if(!inserted){ | ||
htmlNode.content += "<br/>\n" + newHTML; | ||
} | ||
}; | ||
/** | ||
* <p>Adds filename placeholder to the HTML if needed</p> | ||
* | ||
* @param {Object} htmlNode Original HTML contents node object | ||
* @param {String} attachment Attachment meta object | ||
*/ | ||
MailParser.prototype._joinHTMLAttachment = function(htmlNode, attachment){ | ||
var inserted = false, | ||
fname = attachment.generatedFileName.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """), | ||
cid, newHTML; | ||
cid = attachment.cid || (attachment.cid = attachment.generatedFileName+"@node"); | ||
newHTML = "\n<div class=\"mailparser-attachment\"><a href=\"cid:"+cid+"\"><" + fname + "></a></div>"; | ||
htmlNode.content = (htmlNode.content || "").toString("utf-8").trim(); | ||
htmlNode.content = htmlNode.content.replace(/<\/body( [^>]*)?>/i, function(match){ | ||
inserted = true; | ||
return "<br/>\n" + newHTML + match; | ||
}); | ||
if(!inserted){ | ||
htmlNode.content += "<br/>\n" + newHTML; | ||
} | ||
}; | ||
/** | ||
* <p>Converts a string from one charset to another</p> | ||
@@ -970,0 +1061,0 @@ * |
{ | ||
"name": "mailparser", | ||
"description": "Asynchronous and non-blocking parser for mime encoded e-mail messages", | ||
"version": "0.2.21", | ||
"version": "0.2.22", | ||
"author" : "Andris Reinman", | ||
@@ -17,3 +17,3 @@ "maintainers":[ | ||
"scripts":{ | ||
"test": "node ./run_tests.js" | ||
"test": "nodeunit test/" | ||
}, | ||
@@ -20,0 +20,0 @@ "main" : "./lib/mailparser", |
@@ -80,2 +80,3 @@ MailParser | ||
* **subject** - the subject line | ||
* **priority** - priority of the e-mail, always one of the following: *normal* (default), *high*, *low* | ||
* **text** - text body | ||
@@ -82,0 +83,0 @@ * **html** - html body |
@@ -103,2 +103,35 @@ var MailParser = require("../lib/mailparser").MailParser, | ||
}); | ||
}, | ||
"No priority": function(test){ | ||
var encodedText = "Content-type: text/plain; charset=utf-8\r" + | ||
"Subject: ÕÄÖÜ\n" + | ||
"\r" + | ||
"1234", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
test.expect(1); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.priority, "normal"); | ||
test.done(); | ||
}); | ||
}, | ||
"MS Style priority": function(test){ | ||
var encodedText = "Content-type: text/plain; charset=utf-8\r" + | ||
"Subject: ÕÄÖÜ\n" + | ||
"X-Priority: 1 (Highest)\n" + | ||
"\r" + | ||
"1234", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
test.expect(1); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.priority, "high"); | ||
test.done(); | ||
}); | ||
} | ||
@@ -105,0 +138,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
601790
3266
225
28