diff --git a/lib/eml-format.js b/lib/eml-format.js index 575e557..205bfdd 100644 --- a/lib/eml-format.js +++ b/lib/eml-format.js @@ -61,19 +61,21 @@ var emlformat = { //Split around ',' char //var parts = raw.split(/,/g); //Will also split ',' inside the quotes //var parts = raw.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g); //Ignore ',' within the double quotes - var parts = raw.match(/("[^"]*")|[^,]+/g); //Ignore ',' within the double quotes + var parts = raw.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/); //Ignore ',' within the double quotes for (var i = 0; i < parts.length; i++) { + var part = parts[i].replace(/[\r\n]+/g," "); + var address = { }; //Quoted name but without the e-mail address - if (/^".*"$/g.test(parts[i])) { + if (/^".*"$/g.test(part)) { address.name = emlformat.unquoteString(parts[i]).replace(/"/g, "").trim(); i++; //Shift to another part to capture e-mail address } var regex = /^(.*?)(\s*\<(.*?)\>)$/g; - var match = regex.exec(parts[i]); + var match = regex.exec(part); if (match) { var name = emlformat.unquoteString(match[1]).replace(/"/g, "").trim(); if (name && name.length) { @@ -487,7 +489,7 @@ emlformat.read = function(eml, options, callback) { function _append(headers, content) { var contentType = headers["Content-Type"]; var charset = getCharsetName(emlformat.getCharset(contentType) || defaultCharset); - var encoding = headers["Content-Transfer-Encoding"]; + var encoding = headers["Content-Transfer-Encoding"] || "7bit"; if (typeof encoding == "string") { encoding = encoding.toLowerCase(); } @@ -537,9 +539,9 @@ emlformat.read = function(eml, options, callback) { var name = headers["Content-Disposition"] || headers["Content-Type"]; if (name) { - var match = /name="?(.+?)"?$/gi.exec(name); + var match = /name="?([\s\S]+?)"?((;[\s\S]*)|$)/gi.exec(name); if (match) { - name = match[1]; + name = match[1].replace(/[\r\n]+/g," "); } else { name = null; @@ -732,8 +734,10 @@ function parseRecursive(lines, start, parent, options) { if (isMultiHeader) { parent.headers[lastHeaderName][parent.headers[lastHeaderName].length - 1] += "\r\n" + match[1]; } - else { + else if (parent.headers[lastHeaderName].length > 0) { parent.headers[lastHeaderName] += "\r\n" + match[1]; + } else { + parent.headers[lastHeaderName] = match[1]; } continue; } @@ -757,6 +761,15 @@ function parseRecursive(lines, start, parent, options) { } continue; } + + //Header name alone + var match = /^([\w\d\-]+):$/gi.exec(line); + if (match) { + lastHeaderName = match[1]; + isMultiHeader = false; // refactor + parent.headers[lastHeaderName] = ''; + continue; + } } //Body else { diff --git a/package.json b/package.json index 5675a9a..1b3ec85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eml-format", - "version": "0.6.1", + "version": "0.6.2", "description": "RFC 822 EML file format parser and builder", "author": "Papn Kukn", "license": "MIT",