emersion / go-imap

📥 An IMAP library for clients and servers
MIT License
2.1k stars 296 forks source link

Ignore space before first item in flag list #633

Closed quzhi1 closed 3 months ago

quzhi1 commented 3 months ago

Hi @emersion ,

Sometimes we see IMAP server return invalid flag list.

For example, when examining a folder with EXAMINE INBOX, this is the server response:

* FLAGS (\Seen \Answered \Flagged $Completed \Deleted \Draft \Recent $Forwarded $Redirected Junk NonJunk)
* 119 EXISTS
* 119 RECENT
* OK [UIDVALIDITY 1421117124] UID validity
* OK [UNSEEN 111] Message 111 is first unseen
* OK [UIDNEXT 246] Predicted next UID
* OK [PERMANENTFLAGS ( \*)]
a OK [READ-ONLY] EXAMINE Completed

In the line OK [PERMANENTFLAGS ( \*)], there is a leading space inside the flag list. This causes this library to return an error:

T5 EXAMINE INBOX
* FLAGS (\Seen \Answered \Flagged $Completed \Deleted \Draft \Recent $Forwarded $Redirected Junk NonJunk)
* 119 EXISTS
* 119 RECENT
* OK [UIDVALIDITY 1421117124] UID validity
* OK [UNSEEN 111] Message 111 is first unseen
* OK [UIDNEXT 246] Predicted next UID
* OK [PERMANENTFLAGS ( \*)]
T5 OK [READ-ONLY] EXAMINE Completed
2024/08/08 17:30:57 failed to select INBOX: in response-data: in flag: imapwire: expected atom, got " "
exit status 1

Can we just ignore the invalid flag instead of erroring out? If not, is there an elegant way to handle such server error?

Thank you @emersion , and hope you have a good day!

emersion commented 3 months ago

Interesting. Can you explain which server behaves like this?

I don't think ignoring the error is great, because we loose the flag. I'd prefer to add a dec.SP call before ExpectFlag, with a comment explaining that some servers start the list with a space and link to this PR.

quzhi1 commented 3 months ago

@emersion No problem. The imap server is mail.mcspowermail.com:993. I updated this PR to use dec.SP instead of ignoring error. Thank you 🙏