Fix re-IDLE

fork
Brian White 12 years ago
parent d39b7e72ac
commit e3b336560c

@ -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

Loading…
Cancel
Save