Permalink
Browse files

Handle errors storing a message

  • Loading branch information...
1 parent d0f990b commit 2131487788ad7459a73c7d042279eea306dcadec @aredridel committed Oct 3, 2010
Showing with 44 additions and 20 deletions.
  1. +4 −2 bin/smtpd
  2. +39 −17 lib/smtp.js
  3. +1 −1 package.json
View
@@ -26,7 +26,9 @@ var server = smtp.createServer(function(conn) {
message.pause()
destination.appendMessage(function(error, mbox) {
if(error) {
- console.log(error)
+ console.log("Error: " + error)
+ message.emit('error', new Error("451 Internal Error"))
+ message.resume()
} else {
sys.pump(message, mbox, function() {
mbox.end()
@@ -47,7 +49,7 @@ var server = smtp.createServer(function(conn) {
})
})
conn.on('error', function(error) {
- console.error(error)
+ console.error("Error: " + error)
})
})
View
@@ -17,6 +17,7 @@ var left = function (str, n) {
function MessageStream() {
events.EventEmitter.call(this)
+ this.hasErred = false
}
sys.inherits(MessageStream, events.EventEmitter)
@@ -26,13 +27,17 @@ MessageStream.prototype.pause = function() { this.emit('pause') }
MessageStream.prototype.resume = function() { this.emit('resume') }
MessageStream.prototype.accept = function() {
- this.accepted = true
+ this.status = '250 Message Accepted'
this.emit('accept')
}
-MessageStream.prototype.reject = function() {
- this.accepted = false
+MessageStream.prototype.reject = function(m) {
+ this.status = '551 Message Rejected'
this.emit('reject')
}
+MessageStream.prototype.error = function(m) {
+ this.status = m ? m : '421 Something went wrong'
+ this.hasErred = true
+}
var debug;
var debugLevel = parseInt(process.env.NODE_DEBUG, 16);
@@ -98,6 +103,7 @@ function connectionListener(socket) {
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) })
} else {
acceptOrReject(socket, message, In)
}
@@ -145,19 +151,30 @@ function connectionListener(socket) {
}
} else if(line.match(/^DATA\b/i)) {
socket.state = 'data';
- Out.out('354 Enter mail, end with "." on a line by itself');
- var message = new MessageStream()
- socket.currentMessage = message
- message.recipients = socket.recipients
- message.sender = socket.sender
- message.connection = socket
- message.on('pause', function() {
- In.pause()
- })
- message.on('resume', function() {
- In.resume()
- })
- socket.emit('DATA', socket.currentMessage)
+ try {
+ var message = new MessageStream()
+ socket.currentMessage = message
+ message.recipients = socket.recipients
+ message.sender = socket.sender
+ message.connection = socket
+ message.on('pause', function() {
+ In.pause()
+ })
+ message.on('resume', function() {
+ In.resume()
+ })
+ message.on('error', message.error);
+ socket.emit('DATA', socket.currentMessage)
+ if(!message.hasErred) {
+ Out.out('354 Enter mail, end with "." on a line by itself');
+ } else {
+ acceptOrReject(socket, message, In)
+ socket.state = 'welcome'
+ }
+ } catch(e) {
+ Out.out('451 Internal Error')
+ socket.state = 'welcome'
+ }
} else if(line.match(/^NOOP\b/i)) {
Out.out('250 OK');
} else if(line.match(/^QUIT\b/i)) {
@@ -198,9 +215,14 @@ function resetSMTPState(socket) {
function acceptOrReject(socket, message, stream) {
if(message.accepted) {
+ console.info("Accept!")
socket.out("250 Ok, but I don't know what to do with the message")
+ } else if(message.error) {
+ console.info("Error!")
+ socket.out("451 Something went wrong")
} else {
- socket.out("421 Something went wrong")
+ console.info("Reject!")
+ socket.out("554 Rejected")
}
if(stream.paused) stream.resume()
}
View
@@ -2,6 +2,6 @@
, "version" : "0.0.3"
, "description" : "SMTP daemon (and eventually client) library"
, "engines" : [ "node" ]
-, "dependencies" : ["linebuffer"]
+, "dependencies" : ["linebuffer >= 0.0.4"]
, "directories" : { "lib" : "./lib", "bin": "./bin" }
}

0 comments on commit 2131487

Please sign in to comment.