From 86efc68998f98393c3c02c686d108642552625e0 Mon Sep 17 00:00:00 2001 From: mscdex Date: Thu, 17 Jan 2013 01:20:47 -0500 Subject: [PATCH] improve handling of negative welcome response upon connection --- lib/imap.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/imap.js b/lib/imap.js index d282a10..ac5a18c 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -140,6 +140,7 @@ ImapConnection.prototype.connect = function(loginCb) { state.connected = false; state.authenticated = false; self.debug&&self.debug('[connection] FIN packet received. Disconnecting...'); + clearTimeout(state.tmrConn); self.emit('end'); }); @@ -154,9 +155,11 @@ ImapConnection.prototype.connect = function(loginCb) { state.conn.on('error', function(err) { clearTimeout(state.tmrConn); + err.level = 'socket'; if (state.status === STATES.NOCONNECT) - loginCb(new Error('Unable to connect. Reason: ' + err)); - self.emit('error', err); + loginCb(err); + else + self.emit('error', err); self.debug&&self.debug('[connection] Error occurred: ' + err); }); @@ -569,8 +572,16 @@ ImapConnection.prototype.connect = function(loginCb) { break; case 'NO': case 'BAD': - if (state.status === STATES.NOAUTH) + if (state.status === STATES.NOAUTH) { + clearTimeout(state.tmrConn); + var err = new Error('Received negative welcome (' + m[3] + ')'); + err.level = 'protocol'; + if (state.status === STATES.NOCONNECT) + loginCb(err); + else + self.emit('error', err); state.conn.end(); + } break; } } else {