Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make copy of connection in case of re-use
  • Loading branch information
DavidTanner committed Jun 16, 2015
1 parent cbc725a commit f8da537
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ node_modules
.tmp
*.swp
*.pyc
.idea
16 changes: 8 additions & 8 deletions lib/mailin.js
Expand Up @@ -153,11 +153,11 @@ Mailin.prototype.start = function (options, callback) {
logger.info('Processing message from ' + connection.from);

// Store connection data for later (the connection will reset)
var connData = _.pick(connection, ['from', 'to', 'remoteAddress', 'authentication', 'id']);
var connData = _.cloneDeep(connection);

async.auto({
retrieveRawEmail: function (cbAuto) {
fs.readFile(connection.mailPath, function (err, data) {
fs.readFile(connData.mailPath, function (err, data) {
if (err) return cbAuto(err);
cbAuto(null, data.toString());
});
Expand Down Expand Up @@ -186,8 +186,8 @@ Mailin.prototype.start = function (options, callback) {

logger.verbose('Validating spf.');
/* Get ip and host. */
mailUtilities.validateSpf(connection.remoteAddress,
connection.from, connection.host, function (err, isSpfValid) {
mailUtilities.validateSpf(connData.remoteAddress,
connData.from, connData.host, function (err, isSpfValid) {
if (err) {
logger.error('Unable to validate spf. Consider spf as failed.');
logger.error(err);
Expand Down Expand Up @@ -241,7 +241,7 @@ Mailin.prototype.start = function (options, callback) {
});

/* Stream the written email to the parser. */
var mailReadStream = fs.createReadStream(connection.mailPath);
var mailReadStream = fs.createReadStream(connData.mailPath);
mailReadStream.pipe(mailParser);
},

Expand Down Expand Up @@ -297,7 +297,7 @@ Mailin.prototype.start = function (options, callback) {
parsedEmail.envelopeFrom = mimelib.parseAddresses(connData.from);
parsedEmail.envelopeTo = mimelib.parseAddresses(connData.to);

_this.emit('message', _.merge({}, connection, connData), parsedEmail, rawEmail);
_this.emit('message', connData, parsedEmail, rawEmail);

cbAuto(null, parsedEmail);
}
Expand Down Expand Up @@ -371,15 +371,15 @@ Mailin.prototype.start = function (options, callback) {
if (err) return logger.error(err);

/* Don't forget to unlink the tmp file. */
fs.unlink(connection.mailPath, function (err) {
fs.unlink(connData.mailPath, function (err) {
if (err) return logger.error(err);
});

logger.info('End processing message.');
});

/* Close the connection. */
if (!_this.emit('dataReady', connection, callback)) {
if (!_this.emit('dataReady', connData, callback)) {
callback();
}
});
Expand Down

0 comments on commit f8da537

Please sign in to comment.