Permalink
Browse files

Handle error/smtp code API better

* `MessageStream.start()` when streams are set up and plumbed together
* `MessageStream.abort(error)` when there's an error in processing (4xx error)
* `MessageStream.accept()` when the message is accepted (2xx code)
* `MessageStream.reject()` when the message is bad (5xx code)
  • Loading branch information...
1 parent 44127bf commit 15b8608a06bd91ebf711b7115708903594248f10 @aredridel committed Oct 4, 2010
Showing with 51 additions and 27 deletions.
  1. +8 −3 bin/smtpd
  2. +43 −24 lib/smtp.js
View
@@ -32,20 +32,25 @@ smtpMessage: "Internal Error"})
} else {
sys.pump(message, mbox, function(error) {
mbox.end()
- message.accept()
+ if(error) {
+ message.abort(error)
+ } else {
+ message.accept()
+ }
})
message.on('end', function() {
// This is ugly, but pump stops on
// close, not end.
message.emit('close')
})
+ message.start()
}
})
message.on('data', function(data) {
- console.log('DATA: ' + data);
+ console.log('# ' + data.trimRight());
})
message.on('end', function() {
- console.log('EOT');
+ console.log('!');
})
})
conn.on('error', function(error) {
View
@@ -18,6 +18,7 @@ var left = function (str, n) {
function MessageStream() {
events.EventEmitter.call(this)
this.hasErred = false
+ this.started = false
}
sys.inherits(MessageStream, events.EventEmitter)
@@ -27,17 +28,24 @@ MessageStream.prototype.pause = function() { this.emit('pause') }
MessageStream.prototype.resume = function() { this.emit('resume') }
MessageStream.prototype.accept = function() {
- this.status = '250 Message Accepted'
- this.emit('accept')
+ this.smtpCode = 250
+ this.smtpMessage = 'Message Accepted'
+ this.emit('accept', this.smtpCode, this.smtpMessage)
}
MessageStream.prototype.reject = function(m) {
- this.status = '551 Message Rejected'
+ this.smtpCode = 551
+ this.smtpMessage = m ? m : 'Message Rejected'
this.emit('reject')
}
-MessageStream.prototype.error = function(m) {
- this.status = m ? m : '421 Something went wrong'
+MessageStream.prototype.abort = function() {
+ this.smtpCode = 421
+ this.smtpMessage = 'Something went wrong'
this.hasErred = true
}
+MessageStream.prototype.start = function() {
+ this.emit('start')
+ this.started = true
+}
var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
@@ -85,30 +93,31 @@ function connectionListener(socket) {
} catch(e) {
Out.emit('error', e)
}
- console.info('>'+a);
+ console.info('>'+a.replace("\n", ">\n"));
}
socket.on('connect', function () {
Out.out('220 ' + self.hostname + ' ESMTP node.js');
socket.state = 'welcome';
resetSMTPState(socket)
});
In.on('data', function (line) {
- console.info('#'+line.trim());
if (socket.state == 'data') {
+ var message = socket.currentMessage
if(line.match(/^\./)) {
+ message.emit('end')
socket.state = 'welcome'
- socket.currentMessage.emit('end')
- var message = socket.currentMessage
- if(message.accepted === undefined) {
+ var f = function() { acceptOrReject(socket, message, In) }
+ if(message.smtpCode === undefined) {
In.pause()
- message.on('accept', function() { acceptOrReject(socket, message, In) })
- message.on('reject', function() { acceptOrReject(socket, message, In) })
- message.on('error', function() { acceptOrReject(socket, message, In) })
+ console.log('waiting for accept or reject')
+ message.on('accept', f)
+ message.on('reject', f)
+ message.on('error', f)
} else {
- acceptOrReject(socket, message, In)
+ f()
}
} else {
- socket.currentMessage.emit('data', line)
+ message.emit('data', line)
}
} else {
var m
@@ -157,17 +166,27 @@ function connectionListener(socket) {
message.recipients = socket.recipients
message.sender = socket.sender
message.connection = socket
+ In.pause()
message.on('pause', function() {
In.pause()
})
message.on('resume', function() {
In.resume()
})
- message.on('error', message.error);
+ message.on('error', function(e) {
+ message.error = e
+ if(!message.started) {
+ Out.out((e.smtpCode ? e.smtpCode : '451') + (e.smtpMessage ? ' ' + e.smtpMessage : ' Well, that could have gone better.'));
+ socket.state =
+'welcome'
+ }
+ })
socket.emit('DATA', socket.currentMessage)
- if(!message.hasErred) {
+ message.on('start', function() {
Out.out('354 Enter mail, end with "." on a line by itself');
- } else {
+ In.resume()
+ })
+ if(message.hasErred) {
acceptOrReject(socket, message, In)
socket.state = 'welcome'
}
@@ -214,15 +233,15 @@ function resetSMTPState(socket) {
}
function acceptOrReject(socket, message, stream) {
- if(message.accepted) {
+ if(message.smtpCode >= 200 && message.smtpCode < 300) {
console.info("Accept!")
- socket.out("250 Ok, but I don't know what to do with the message")
- } else if(message.error) {
+ socket.out(message.smtpCode + " " + message.smtpMessage)
+ } else if(message.smtpCode >= 500 && message.smtpCode < 600) {
+ console.info("Reject!")
+ socket.out(message.smtpCode + " " + message.smtpMessage)
+ } else {
console.info("Error!")
socket.out("451 Something went wrong")
- } else {
- console.info("Reject!")
- socket.out("554 Rejected")
}
if(stream.paused) stream.resume()
}

0 comments on commit 15b8608

Please sign in to comment.