karastojko / mailio

mailio is a cross platform C++ library for MIME format and SMTP, POP3, IMAP protocols. It is based on the standard C++ 17 and Boost library.
Other
387 stars 104 forks source link

imap fetch command response parsing #135

Open yjm6560 opened 1 year ago

yjm6560 commented 1 year ago

As you know, if imap client send UID FETCH command, then imap server must send UID data in the response. https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8

image

However there is no rule which specifies the order between UID and other data(RFC822 or RFC822.HEADER). For example, if client send 123 UID FETCH 10 (RFC822)\r\n command, imap server can send following responses. (123 is tag, 10 is uid)

RFC822 data after UID data

  1. * 3 FETCH (UID 10 RFC822 {6}\r\n
    • 3 is message sequence number
  2. abcd\r\n
  3. )\r\n
  4. 123 OK Fetch completed.\r\n

UID data after RFC822 data

  1. * 3 FETCH (RFC822 {6}\r\n
    • 3 is message sequence number
  2. abcd\r\n
  3. UID 10)\r\n
  4. 123 OK Fetch completed.\r\n

The second case cannot be covered by the current mailio:imap::fetch function logic because it gets out of for loop(checking if requested tokens exist or not) when it met RFC822 token. So even though it gets UID data after receiving literal response, it can't parse UID data.

This case actually happens often.

So... i recommend to parse response after receiving all responses is completed(until parenthesis_list_counter_ becomes 0).

karastojko commented 1 year ago

For the test purposes, I am using Dovecot, Gmail, Outlook, Zoho, Yahoo, Vkontakte servers. So far, I did not see such case, but I will take a more detailed look with UIDs.

yjm6560 commented 1 year ago

Thank you for replying! Naver(the parent company of LINE) mail service is the most famous in Korea, and it gives UID FETCH response like following example.

$ openssl s_client -connect imap.naver.com:993 -quiet
... # login, select
3 UID FETCH 38 (RFC822) # request UID FETCH command
# receive data
* 1 FETCH (RFC822 {1998} # untagged response
... # mime data
 UID 38) # receive UID data after RFC822 data
3 OK Fetch completed. # tagged response

i searched rfc document but i cannot find that UID should come before RFC822. I have used mailio::imap after fixing it, it works well whether UID comes before RFC822 or not. (I tested in more than 10 imap servers including Gmail, Outlook, Naver and minor imap server) If you don't have time, I can help.