From e77eb8af26a09881b44043a6f174e8ec7ba42865 Mon Sep 17 00:00:00 2001 From: Brian White Date: Tue, 22 Nov 2011 12:03:19 -0500 Subject: [PATCH] Watch out for unsolicited FETCH responses Fixes #11 --- imap.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/imap.js b/imap.js index 94e2c23..565785a 100644 --- a/imap.js +++ b/imap.js @@ -369,6 +369,9 @@ ImapConnection.prototype.connect = function(loginCb) { break; default: if (/^\d+$/.test(data[1])) { + var isUnsolicited = (self._state.requests[0] && + self._state.requests[0].command.indexOf('NOOP') > -1) || + (self._state.isIdle && self._state.ext.idle.sentIdle); switch (data[2]) { case 'EXISTS': // mailbox total message count @@ -386,19 +389,25 @@ ImapConnection.prototype.connect = function(loginCb) { break; case 'EXPUNGE': // confirms permanent deletion of a single message + // TODO: emit 'deleted' event for unsolicited expunge responses if (self._state.box.messages.total > 0) self._state.box.messages.total--; break; default: // fetches without header or body (part) retrievals + // TODO: emit event to notify flags for a message have changed + // for unsolicited fetch responses if (/^FETCH/.test(data[2])) { - var curReq = self._state.requests[0], - msg = new ImapMessage(); - parseFetch(data[2].substring(data[2].indexOf("(")+1, - data[2].lastIndexOf(")")), - "", msg); - curReq._fetcher.emit('message', msg); - msg.emit('end'); + if (self._state.requests.length && + self._state.requests[0].command.indexOf('FETCH') > -1) { + var curReq = self._state.requests[0], + msg = new ImapMessage(); + parseFetch(data[2].substring(data[2].indexOf("(")+1, + data[2].lastIndexOf(")")), + "", msg); + curReq._fetcher.emit('message', msg); + msg.emit('end'); + } } } }