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 {