Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'mail' event fires when attempting to open box box after having opened another box #585

Open
jstejada opened this issue Dec 19, 2016 · 0 comments

Comments

@jstejada
Copy link

Whenever I attempt to open [Gmail]/All Mail, after having opened either [Gmail]/Spam or [Gmail]/Trash I am getting a 'mail' event emitted by the connection object.

This is the sequence of events:

-> Open box `[Gmail]/Trash`
-> `[Gmail]/Trash` is opened
-> Open box `[Gmail]/All Mail`
-> ***'mail' event is emitted for `[Gmail]/Trash`*** (why?)
-> `[Gmail]/All Mail` is opened

I can work around this in my own code, but it would be great to have this fixed


Here's the debug output

1. Opening [Gmail]/Trash

2016-12-18 17:28:54.593 Connection.js:1787 => DONE
2016-12-18 17:28:54.752 Parser.js:132 <= 'IDLE OK IDLE terminated (Success)'
2016-12-18 17:28:54.753 Connection.js:1733 => 'A23 SELECT "[Gmail]/Trash" (CONDSTORE)'
2016-12-18 17:28:54.877 Parser.js:132 <= '* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing)'
2016-12-18 17:28:54.877 Parser.js:132 <= '* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing \\*)] Flags permitted.'
2016-12-18 17:28:54.878 Parser.js:132 <= '* OK [UIDVALIDITY 2] UIDs valid.'
2016-12-18 17:28:54.878 Parser.js:132 <= '* 1 EXISTS'
2016-12-18 17:28:54.879 Parser.js:132 <= '* 0 RECENT'
2016-12-18 17:28:54.879 Parser.js:132 <= '* OK [UIDNEXT 27] Predicted next UID.'
2016-12-18 17:28:54.880 Parser.js:132 <= '* OK [HIGHESTMODSEQ 42706]'
2016-12-18 17:28:54.880 Parser.js:132 <= 'A23 OK [READ-WRITE] [Gmail]/Trash selected. (Success)'
2016-12-18 17:28:54.881 Connection.js:1733 => 'IDLE IDLE'
2016-12-18 17:28:54.968 Parser.js:132 <= '+ idling'

2. Opening [Gmail]/All Mail

2016-12-18 17:30:37.185 Connection.js:1787 => DONE
2016-12-18 17:30:37.272 Parser.js:132 <= 'IDLE OK IDLE terminated (Success)'
2016-12-18 17:30:37.272 Connection.js:1733 => 'A24 SELECT "[Gmail]/All Mail" (CONDSTORE)'
2016-12-18 17:30:37.378 Parser.js:132 <= '* FLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing)'
2016-12-18 17:30:37.378 Parser.js:132 <= '* OK [PERMANENTFLAGS (\\Answered \\Flagged \\Draft \\Deleted \\Seen $NotPhishing $Phishing \\*)] Flags permitted.'
2016-12-18 17:30:37.379 Parser.js:132 <= '* OK [UIDVALIDITY 11] UIDs valid.'
2016-12-18 17:30:37.380 Parser.js:132 <= '* 858 EXISTS'

2016-12-18 17:30:37.381 ***'mail' event emitted here***

2016-12-18 17:30:37.382 Parser.js:132 <= '* 0 RECENT'
2016-12-18 17:30:37.382 Parser.js:132 <= '* OK [UIDNEXT 946] Predicted next UID.'
2016-12-18 17:30:37.382 Parser.js:132 <= '* OK [HIGHESTMODSEQ 42706]'
2016-12-18 17:30:37.383 Parser.js:132 <= 'A24 OK [READ-WRITE] [Gmail]/All Mail selected. (Success)'
2016-12-18 17:30:37.384 Connection.js:1733 => 'IDLE IDLE'
2016-12-18 17:30:37.510 Parser.js:132 <= '+ idling'

After some more digging, the problem seems to be caused here: https://github.com/mscdex/node-imap/blob/master/lib/Connection.js#L1285

It looks like before emitting the 'mail' event we are checking if the new message count is greater than the previous message count, and if it is, we will emit the event. However, this code is not taking into account when we are opening a new box, in which case its comparing the message count of the old box against the new box, instead of ensuring to compare the message counts within the same box.

In my case specifically, All Mail always has more messages than Trash or Spam, so if the current box is Spam or Trash, and I switch to All Mail, it will parse the EXISTS message and think that because the new count is greater than the last one it should emit the 'mail' event, when in fact it shouldn't because the new count corresponds to a different mailbox.

This seems like it would be a straightforward fix, but I am hesitant to try to submit a PR because I might be unaware of other constraints or assumptions in the code. I'd be happy to if you point me in the right direction.

Thanks @mscdex !

@jstejada jstejada changed the title 'mail' event fires when attempting to open [Gmail]/All Mail box after having opened another box 'mail' event fires when attempting to open box box after having opened another box Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant