mailparser
Advanced tools
Comparing version 0.2.16 to 0.2.17
@@ -5,3 +5,3 @@ | ||
* @author <a href="mailto:andris@node.ee">Andris Reinman</a> | ||
* @version 0.2.16 | ||
* @version 0.2.17 | ||
*/ | ||
@@ -546,2 +546,3 @@ | ||
MailParser.prototype._parseContentType = function(value){ | ||
var fileName; | ||
value = this._parseHeaderLineWithParams(value); | ||
@@ -580,5 +581,7 @@ if(value){ | ||
} | ||
if(value.name && !this._currentNode.meta.fileName){ | ||
this._currentNode.meta.fileName = this._replaceMimeWords(value.name); | ||
if(!this._currentNode.meta.fileName && (fileName = this._detectFilename(value))){ | ||
this._currentNode.meta.fileName = fileName; | ||
} | ||
if(value.boundary){ | ||
@@ -596,2 +599,48 @@ this._currentNode.meta.mimeBoundary = value.boundary; | ||
/** | ||
* <p>Parses file name from a Content-Type or Content-Disposition field</p> | ||
* | ||
* <p>Supports <a href="http://tools.ietf.org/html/rfc2231">RFC2231</a> for | ||
* folded filenames</p> | ||
* | ||
* @param {Object} value Parsed Content-(Type|Disposition) object | ||
* @return {String} filename | ||
*/ | ||
MailParser.prototype._detectFilename = function(value){ | ||
var fileName="", i=0, parts, encoding, name; | ||
if(value.name){ | ||
return this._replaceMimeWords(value.name); | ||
} | ||
if(value.filename){ | ||
return this._replaceMimeWords(value.filename); | ||
} | ||
// RFC2231 | ||
if(value["name*"]){ | ||
fileName = value["name*"]; | ||
}else if(value["filename*"]){ | ||
fileName = value["filename*"]; | ||
}else if(value["name*0*"]){ | ||
while(value["name*"+(i)+"*"]){ | ||
fileName += value["name*"+(i++)+"*"]; | ||
} | ||
}else if(value["filename*0*"]){ | ||
while(value["filename*"+(i)+"*"]){ | ||
fileName += value["filename*"+(i++)+"*"]; | ||
} | ||
} | ||
if(fileName){ | ||
parts = fileName.split("'"); | ||
encoding = parts.shift(); | ||
name = parts.pop(); | ||
if(name){ | ||
return this._replaceMimeWords(this._replaceMimeWords("=?"+(encoding || "us-ascii")+"?Q?" + name.replace(/%/g,"=")+"?=")); | ||
} | ||
} | ||
return ""; | ||
}; | ||
/** | ||
* <p>Parses Content-Disposition header field value</p> | ||
@@ -604,3 +653,4 @@ * | ||
MailParser.prototype._parseContentDisposition = function(value){ | ||
var returnValue = {}; | ||
var returnValue = {}, | ||
fileName; | ||
@@ -613,4 +663,4 @@ value = this._parseHeaderLineWithParams(value); | ||
} | ||
if(value.filename){ | ||
this._currentNode.meta.fileName = this._replaceMimeWords(value.filename); | ||
if((fileName = this._detectFilename(value))){ | ||
this._currentNode.meta.fileName = fileName; | ||
} | ||
@@ -617,0 +667,0 @@ } |
{ | ||
"name": "mailparser", | ||
"description": "Asynchronous and non-blocking parser for mime encoded e-mail messages", | ||
"version": "0.2.16", | ||
"version": "0.2.17", | ||
"author" : "Andris Reinman", | ||
@@ -6,0 +6,0 @@ "maintainers":[ |
@@ -8,4 +8,4 @@ MailParser | ||
[![Build Status](https://secure.travis-ci.org/andris9/mailparser.png)](http://travis-ci.org/andris9/mailparser) | ||
**MailParser** is an asynchronous and non-blocking parser for | ||
@@ -12,0 +12,0 @@ [node.js](http://nodejs.org) to parse mime encoded e-mail messages. |
@@ -151,3 +151,6 @@ var MailParser = require("../lib/mailparser").MailParser, | ||
"Quoted-Printable": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: QUOTED-PRINTABLE\r\n\r\n=00=01=02=03=FD=FE=FF", | ||
var encodedText = "Content-Type: application/octet-stream\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
@@ -187,2 +190,100 @@ | ||
exports["Attachment filename"] = { | ||
"Content-Disposition filename": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"Content-Disposition: attachment; filename=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
}, | ||
"Content-Disposition filename*": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"Content-Disposition: attachment; filename*=\"UTF-8''%C3%95%C3%84%C3%96%C3%9C\"\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
}, | ||
"Content-Disposition filename*X*": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"Content-Disposition: attachment;\r\n"+ | ||
" filename*0*=UTF-8''%C3%95%C3%84;\r\n"+ | ||
" filename*1*=%C3%96%C3%9C\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
}, | ||
"Content-Type name": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream; name=\"=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?=\"\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
}, | ||
"Content-Type name*": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream;\r\n"+ | ||
" name*=UTF-8''%C3%95%C3%84%C3%96%C3%9C\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
}, | ||
"Content-Type name*X*": function(test){ | ||
var encodedText = "Content-Type: application/octet-stream;\r\n"+ | ||
" name*0*=UTF-8''%C3%95%C3%84;\r\n"+ | ||
" name*1*=%C3%96%C3%9C\r\n"+ | ||
"Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n"+ | ||
"\r\n"+ | ||
"=00=01=02=03=FD=FE=FF", | ||
mail = new Buffer(encodedText, "utf-8"); | ||
var mailparser = new MailParser(); | ||
mailparser.end(mail); | ||
mailparser.on("end", function(mail){ | ||
test.equal(mail.attachments && mail.attachments[0] && mail.attachments[0].content && mail.attachments[0].fileName, "ÕÄÖÜ"); | ||
test.done(); | ||
}); | ||
} | ||
}; | ||
exports["Plaintext format"] = { | ||
@@ -189,0 +290,0 @@ "Default": function(test){ |
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
551267
2919