Foundations of Python Network Programming

(WallPaper) #1

Chapter 15 ■ IMap


272


Second, you can see that each result from the LIST command—which in this library is offered as the
list_folders() method instead of the list() method offered by imaplib—has already been parsed into Python
data types. Each line of data comes back as a tuple, giving you the folder flags, folder name delimiter, and folder name,
and the flags themselves are a sequence of strings.
Look at Listing 15-4 to see what the output of this second script looks like.


Listing 15-4. Properly Parsed Flags and Folder Names


Capabilities: ('IMAP4REV1', 'UNSELECT', 'IDLE', 'NAMESPACE', 'QUOTA', 'XLIST', 'CHILDREN', 'XYZZY',
'SASL-IR', 'AUTH=XOAUTH')
Listing mailboxes:
\HasNoChildren / INBOX
\HasNoChildren / Personal
\HasNoChildren / Receipts
\HasNoChildren / Travel
\HasNoChildren / Work
\Noselect \HasChildren / [Gmail]
\HasChildren \HasNoChildren / [Gmail]/All Mail
\HasNoChildren / [Gmail]/Drafts
\HasChildren \HasNoChildren / [Gmail]/Sent Mail
\HasNoChildren / [Gmail]/Spam
\HasNoChildren / [Gmail]/Starred
\HasChildren \HasNoChildren / [Gmail]/Trash


The standard flags listed for each folder may be zero or more of the following:

•    \Noinferiors: This means that the folder does not contain any subfolders and that it is
impossible for it to contain subfolders in the future. Your IMAP client will receive an error if it
tries to create a subfolder within this folder.

•    \Noselect: This means that it is not possible to run select_folder() on this folder; that
is, this folder does not and cannot contain any messages. (Perhaps it exists just to allow
subfolders beneath it, as one possibility.)

•    \Marked: This means that the server considers this box to be interesting in some way.
Generally, this indicates that new messages have been delivered since the last time the folder
was selected. However, the absence of \Marked does not guarantee that the folder does not
contain new messages; some servers simply do not implement \Marked at all.

•    \Unmarked: This guarantees that the folder doesn’t contain new messages.

Some servers return additional flags not covered in the standard. Your code must be able to accept and ignore
those additional flags.


Examining Folders

Before you can actually download, search, or modify any messages, you must “select” a particular folder to look
at. This means that the IMAP protocol is stateful: it remembers which folder you are currently looking at, and its
commands operate on the current folder without making you repeat its name over and over again. Only when your
connection is closed and you reconnect are you starting fresh with a clean slate. This can make interaction more
pleasant, but it also means that your program has to be careful that it always knows what folder is selected, or it might
wind up doing something to the wrong folder.

Free download pdf