Reputation: 31
One way of ensuring an IMAP client is in sync with its server is to leverage the SEEN flag (e.g., Library for IMAP IDLE).
I have not yet used this myself, but I was wondering if setting the SEEN flag basically sets the message to "read" on the server.
If so, this is obviously a problem when there are multiple readers involved or when the user logs into the server directly (e.g., logs into their Gmail account) and reads the message there (so that it is "marked read", and - thus - flagged as SEEN).
Or, I could be misunderstanding this completely and SEEN is something that is unique between a particular client and the server. However, not clear how to maintain state in that case.
Upvotes: 2
Views: 3496
Reputation: 22261
"Leveraging the SEEN flag" sounds like a bad way to synchronize with the server. As you surmise, setting the SEEN flag basically sets the message to "read" on the server. All the other IMAP clients will see that the message has been read. The flag is not "private" between the server and each client. Your client should not mark the message SEEN unless the user has seen it.
To synchronize, you need to keep track of the UIDs of the messages your client has already seen, and compare the list with the ones available on the server whenever you poll the folder. You then locally discard ones that aren't on the server anymore (they're messages that have been deleted from other clients) and download the ones you didn't have in your local list (they're new messages).
It gets more complicated if you want to be robust and handle the case where the server has forgotten the UIDs of all messages and rebuild the folder with new UIDs (can happen if the index is corrupted and rebuilt on the server, the server software is changed, the server has become a different hosting provider, etc...) but that's the basic idea.
Upvotes: 2