vladimarius / imapy

Imapy: Imap for Humans
230 stars 22 forks source link

Fixes several mail parsing and folder list bugs #29

Closed briancline closed 5 years ago

briancline commented 7 years ago

Hello, I couldn't tell whether this project is still maintained or not, but figured I would submit this PR anyway just in case -- and for others to find easily if they run into the same bugs.

While running this over my inbox folder at work with nearly 10k messages for the last few years, I found a few bugs in the way imapy parses some folder list data and message data.

I'm only partially confident in the way I've handled the fixes around servers that use a folder delimiter of \. While it now successfully builds the folder tree, moving messages into a non-root (child) folder still does not work. I'm not exactly sure why yet, but it seems related to the \ delimiter -- just didn't have ample time to finish figuring out why that is.

That being said, it would be great if you or others could test these changes with other imap servers, as I don't have a lot I can test against at the moment.

Thank you!

Summary of fixes from commit msg:

* Don't explode if a MIME attachment does not specify a filename.
  Default to a blank string instead of None.

* Don't explode if a 'From' header is not given. Default to a blank
  string instead of None.

* Properly handle an IMAP server sending a long-form folder name
  by specifying its length followed by the actual name separately.

  imaplib returns this as a tuple of two strings, rather than a single
  string. It's also followed by a blank string for the next element
  in the folder list, which needs skipping (see next bullet).
  For example:
    normal:    '(\\HasNoChildren) "\\\\" dev.github'
    long form: ('(\\HasNoChildren) "\\\\" {11}', 'lists\\nanog'),
               '',

* Don't explode if the server sends a blank value where we might
  normally expect to see a folder name (happens when receiving a
  folder name in long form - see prior bullet).

* Properly handle folder hierarchy delimiters of '\', which appear on
  each folder's delimiter field as b'\\\\', and need to be unescaped
  to b'\\', which is how they appear in the actual folder name.