Skip to content

Commit

Permalink
Added new imapConnection event 'idleResponse' for more complete chang…
Browse files Browse the repository at this point in the history
…e notification.
  • Loading branch information
Bruno Morency committed Oct 26, 2011
1 parent 28ef611 commit a8dba9a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -199,6 +199,8 @@ ImapConnection Events

* **error**(Error) - Fires when an exception/error occurs (similar to net.Stream's error event). The given Error object represents the error raised.

* **idleResponse**(Integer, String, Mixed) - Fires when an idling connection (or NOOP if server doesn't support IDLE) gets a response. Params supplied are: sequence id (Integer), response (String) and flags as an Array if the response is 'FETCH' or false otherwise.


ImapConnection Properties
-------------------------
Expand Down
23 changes: 23 additions & 0 deletions imap.js
Expand Up @@ -402,6 +402,29 @@ ImapConnection.prototype.connect = function(loginCb) {
}
}
}
if ((self._state.ext.idle.sentIdle || self._state.requests[0].command == 'NOOP') && /^(EXISTS|EXPUNGE|RECENT|FETCH)/.test(data[2])) {
// Emit 'idleResponse' event for untagged server responses received from a NOOP
// or while idling.
//
// In the case on new message arriving in mailbox, both the 'mail' event
// (see 'EXISTS' case above) and this 'idleResponse' event will be triggered.
//
// In the case on flags changing on an existing message and the response
// is not from an IDLE command, both the 'message' event (see default
// case above) and this 'idleResponse' event will be triggered.

// parse flags on FETCH response into data structure
var rData = data[2].trim().explode(' ',2);
if (rData[0] == 'FETCH') {
var flags = false;
try {
flags = rData[1].match(/^\(FLAGS \((.*)\)\)/).pop().split(' ');
} catch (e) {}
}

self.emit('idleResponse', parseInt(data[1]), rData[0], flags);
}

}
}
} else if (data[0].indexOf('A') === 0) { // Tagged server response
Expand Down

0 comments on commit a8dba9a

Please sign in to comment.