From dace19b4fdd2d8e2eb64f44953f8045c9091944d Mon Sep 17 00:00:00 2001 From: mspencer Date: Thu, 27 Feb 2020 12:29:02 -0700 Subject: [PATCH 1/2] RFC 1341: default 'Content-Transfer-Encoding' to 7bit --- lib/eml-format.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/eml-format.js b/lib/eml-format.js index 575e557..0c551ec 100644 --- a/lib/eml-format.js +++ b/lib/eml-format.js @@ -487,7 +487,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(); } From c4d296cc6a9279c75ec82ac19a8dbc6f429cd08d Mon Sep 17 00:00:00 2001 From: mspencer Date: Fri, 13 Mar 2020 14:23:06 -0700 Subject: [PATCH 2/2] change regexs --- lib/eml-format.js | 25 +++++++++++++++++++------ package.json | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/eml-format.js b/lib/eml-format.js index 0c551ec..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) { @@ -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",