Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions lib/eml-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down