Skip to content

Commit

Permalink
Fixed #112. Bumped version to v0.4.9
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Feb 25, 2015
1 parent 68cacee commit ef18b6f
Show file tree
Hide file tree
Showing 7 changed files with 802 additions and 745 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ language: node_js
node_js:
- 0.8
- "0.10"
- 0.11
- 0.12
- iojs

notifications:
email:
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2012 Andris Reinman
Copyright (c) 2012-2015 Andris Reinman

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
54 changes: 28 additions & 26 deletions lib/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,27 @@ module.exports.strtotime = function(str, now) {
// * example 4: strtotime('2009-05-04 08:30:00');
// * returns 4: 1241418600

var i, match, s, strTmp = '', parse = '';
var i, match, s, strTmp = '',
parse = '';

strTmp = str;
strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
strTmp = strTmp.replace(/[ \r\n]/g, ''); // unecessary chars

if (strTmp == 'now') {
return (new Date()).getTime()/1000; // Return seconds, not milli-seconds
return (new Date()).getTime() / 1000; // Return seconds, not milli-seconds
} else if (!isNaN(parse = Date.parse(strTmp))) {
return (parse/1000);
return (parse / 1000);
} else if (now) {
now = new Date(now*1000); // Accept PHP-style seconds
now = new Date(now * 1000); // Accept PHP-style seconds
} else {
now = new Date();
}

strTmp = strTmp.toLowerCase();

var __is =
{
day:
{
var __is = {
day: {
'sun': 0,
'mon': 1,
'tue': 2,
Expand All @@ -163,8 +162,7 @@ module.exports.strtotime = function(str, now) {
'fri': 5,
'sat': 6
},
mon:
{
mon: {
'jan': 0,
'feb': 1,
'mar': 2,
Expand All @@ -180,7 +178,7 @@ module.exports.strtotime = function(str, now) {
}
};

var process = function (m) {
var process = function(m) {
var ago = (m[2] && m[2] == 'ago');
var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);

Expand Down Expand Up @@ -211,14 +209,18 @@ module.exports.strtotime = function(str, now) {
break;
default:
var day;
if (typeof (day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
if (typeof(day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
var diff = day - now.getDay();
if (diff === 0) {
diff = 7 * num;
} else if (diff > 0) {
if (m[0] == 'last') {diff -= 7;}
if (m[0] == 'last') {
diff -= 7;
}
} else {
if (m[0] == 'next') {diff += 7;}
if (m[0] == 'next') {
diff += 7;
}
}
now.setDate(now.getDate() + diff);
}
Expand Down Expand Up @@ -277,18 +279,18 @@ module.exports.strtotime = function(str, now) {
}
s[0] = parseInt(s[0], 10);

s[0] = (s[0] >= 0 && s[0] <= 69) ? '20'+(s[0] < 10 ? '0'+s[0] : s[0]+'') : (s[0] >= 70 && s[0] <= 99) ? '19'+s[0] : s[0]+'';
return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2])+(match[4] ? match[4]/1000 : ''), 10);
s[0] = (s[0] >= 0 && s[0] <= 69) ? '20' + (s[0] < 10 ? '0' + s[0] : s[0] + '') : (s[0] >= 70 && s[0] <= 99) ? '19' + s[0] : s[0] + '';
return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2]) + (match[4] ? match[4] / 1000 : ''), 10);
}

var regex = '([+-]?\\d+\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)'+
'|(last|next)\\s'+
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'+
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday'+
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))'+
var regex = '([+-]?\\d+\\s' +
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?' +
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday' +
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)' +
'|(last|next)\\s' +
'(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?' +
'|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday' +
'|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))' +
'(\\sago)?';

match = strTmp.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
Expand All @@ -302,5 +304,5 @@ module.exports.strtotime = function(str, now) {
}
}

return (now.getTime()/1000);
};
return (now.getTime() / 1000);
};
150 changes: 75 additions & 75 deletions lib/mailparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,75 +43,75 @@ var STATES = {
*/
function MailParser(options) {

// Make MailParser a Stream object
Stream.call(this);
this.writable = true;

/**
* Options object
* @public */
this.options = options || {};

/**
* Indicates current state the parser is in
* @private */
this._state = STATES.header;

/**
* The remaining data from the previos chunk which is waiting to be processed
* @private */
this._remainder = "";

/**
* The complete tree structure of the e-mail
* @public */
this.mimeTree = this._createMimeNode();

/**
* Current node of the multipart mime tree that is being processed
* @private */
this._currentNode = this.mimeTree;

// default values for the root node
this._currentNode.priority = "normal";

/**
* An object of already used attachment filenames
* @private */
this._fileNames = {};

/**
* An array of multipart nodes
* @private */
this._multipartTree = [];


/**
* This is the final mail structure object that is returned to the client
* @public */
this.mailData = {};

/**
* Line counter for debugging
* @private */
this._lineCounter = 0;

/**
* Did the last chunk end with \r
* @private */
this._lineFeed = false;

/**
* Is the "headers" event already emitted
* @private */
this._headersSent = false;

/**
* If the e-mail is in mbox format, unescape ">From " to "From " in body
* @private */
this._isMbox = -1;
}
// inherit methods and properties of Stream
// Make MailParser a Stream object
Stream.call(this);
this.writable = true;

/**
* Options object
* @public */
this.options = options || {};

/**
* Indicates current state the parser is in
* @private */
this._state = STATES.header;

/**
* The remaining data from the previos chunk which is waiting to be processed
* @private */
this._remainder = "";

/**
* The complete tree structure of the e-mail
* @public */
this.mimeTree = this._createMimeNode();

/**
* Current node of the multipart mime tree that is being processed
* @private */
this._currentNode = this.mimeTree;

// default values for the root node
this._currentNode.priority = "normal";

/**
* An object of already used attachment filenames
* @private */
this._fileNames = {};

/**
* An array of multipart nodes
* @private */
this._multipartTree = [];


/**
* This is the final mail structure object that is returned to the client
* @public */
this.mailData = {};

/**
* Line counter for debugging
* @private */
this._lineCounter = 0;

/**
* Did the last chunk end with \r
* @private */
this._lineFeed = false;

/**
* Is the "headers" event already emitted
* @private */
this._headersSent = false;

/**
* If the e-mail is in mbox format, unescape ">From " to "From " in body
* @private */
this._isMbox = -1;
}
// inherit methods and properties of Stream
utillib.inherits(MailParser, Stream);

/**
Expand Down Expand Up @@ -331,7 +331,7 @@ MailParser.prototype._processStateHeader = function(line) {
}
attachment.stream = this._currentNode.stream;

this.emit("attachment", attachment, this._currentNode.parentNode);
this.emit("attachment", attachment, this._currentNode.parentNode || this._currentNode);
} else {
this._currentNode.content = undefined;
}
Expand Down Expand Up @@ -589,7 +589,7 @@ MailParser.prototype._createMimeNode = function(parentNode) {
MailParser.prototype._parseHeaderLineWithParams = function(value) {
var key, parts, returnValue = {};

parts = value.match(/(?:[^;"]+|"[^"]*")+/g) || [value];
parts = value.match(/(?:[^;"]+|"[^"]*")+/g) ||  [value];
returnValue.defaultValue = parts.shift().toLowerCase();

for (var i = 0, len = parts.length; i < len; i++) {
Expand Down Expand Up @@ -626,7 +626,7 @@ MailParser.prototype._parseDateString = function(value) {
}

return date;
}
};

/**
* <p>Parses Received and X-Received header field value</p>
Expand Down Expand Up @@ -670,7 +670,7 @@ MailParser.prototype._parseReceived = function(value) {
}

return date;
}
};

/**
* <p>Parses a Content-Type header field value</p>
Expand Down Expand Up @@ -1415,7 +1415,7 @@ MailParser.prototype._generateFileName = function(fileName, contentType) {
fileRootName = fileName.replace(/(?:\-\d+)+(\.[^.]*)$/, "$1") || "attachment";

if (fileRootName in this._fileNames) {
this._fileNames[fileRootName]++;
this._fileNames[fileRootName] ++;
ext = fileName.substr((fileName.lastIndexOf(".") || 0) + 1);
if (ext == fileName) {
fileName += "-" + this._fileNames[fileRootName];
Expand Down Expand Up @@ -1482,4 +1482,4 @@ MailParser.prototype._detectHTMLCharset = function(html) {
}

return charset;
};
};
Loading

0 comments on commit ef18b6f

Please sign in to comment.