From e3b336560c0abb6edf4ef6b31d1e4999a214cbde Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 16 Nov 2012 12:20:35 -0500 Subject: [PATCH] Fix re-IDLE --- lib/imap.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/imap.js b/lib/imap.js index d8e9bc8..9e587f1 100644 --- a/lib/imap.js +++ b/lib/imap.js @@ -80,7 +80,8 @@ function ImapConnection (options) { idle: { MAX_WAIT: 1740000, // 29 mins in ms state: IDLE_NONE, - timeWaited: 0 // ms + reIDLE: false, + timeStarted: undefined } } }; @@ -543,6 +544,7 @@ ImapConnection.prototype.connect = function(loginCb) { : '[parsing incoming] saw continuation response'); if (line[0] === '+' && state.ext.idle.state === IDLE_WAIT) { state.ext.idle.state = IDLE_READY; + state.ext.idle.timeStarted = Date.now(); return process.nextTick(function() { self._send(); }); } @@ -609,11 +611,12 @@ ImapConnection.prototype.connect = function(loginCb) { // minutes to avoid disconnection by the server self._send('IDLE', undefined, true); } - state.tmrKeepalive = setTimeout(function() { + state.tmrKeepalive = setTimeout(function idleHandler() { if (state.isIdle) { if (state.ext.idle.state === IDLE_READY) { - state.ext.idle.timeWaited += state.tmoKeepalive; - if (state.ext.idle.timeWaited >= state.ext.idle.MAX_WAIT) + state.tmrKeepalive = setTimeout(idleHandler, state.tmoKeepalive); + var timeDiff = Date.now() - state.ext.idle.timeStarted; + if (timeDiff >= state.ext.idle.MAX_WAIT) self._send('IDLE', undefined, true); // restart IDLE } else if (!self._serverSupports('IDLE')) self._noop(); @@ -629,8 +632,12 @@ ImapConnection.prototype.connect = function(loginCb) { process.nextTick(function() { self._send(); }); state.isIdle = false; state.ext.idle.state = IDLE_NONE; - state.ext.idle.timeWaited = 0; + state.ext.idle.timeStated = undefined; indata.line = undefined; + if (state.ext.idle.reIDLE) { + state.ext.idle.reIDLE = false; + self._send('IDLE', undefined, true); + } } else { // unknown response self.debug&&self.debug('[parsing incoming] saw unexpected response: ' @@ -651,9 +658,12 @@ ImapConnection.prototype.isAuthenticated = function() { }; ImapConnection.prototype.logout = function(cb) { - if (this._state.status >= STATES.NOAUTH) + if (this._state.status >= STATES.NOAUTH) { this._send('LOGOUT', cb); - else + this._state.conn.end(); + if (cb === true) + this._state.conn.removeAllListeners(); + } else throw new Error('Not connected'); }; @@ -1244,7 +1254,8 @@ ImapConnection.prototype._reset = function() { this._state.tmrKeepalive = null; this._state.tmrConn = null; this._state.ext.idle.state = IDLE_NONE; - this._state.ext.idle.timeWaited = 0; + this._state.ext.idle.timeStarted = undefined; + this._state.ext.idle.reIDLE = false; this._state.indata.literals = []; this._state.indata.line = undefined; @@ -1298,6 +1309,8 @@ ImapConnection.prototype._send = function(cmdstr, cb, bypass) { clearTimeout(this._state.tmrKeepalive); if (this._state.ext.idle.state === IDLE_READY && cmd !== 'DONE') { this._state.ext.idle.state = IDLE_DONE; + if (cmd === 'IDLE') + this._state.ext.idle.reIDLE = true; return this._send('DONE', undefined, true); } else if (cmd === 'IDLE') { // we use a different prefix to differentiate and disregard the tagged