09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 15 ■ IMAP<br />

Message Ranges<br />

Most IMAP commands that work with messages can work with one or more messages. This can make<br />

processing far faster if you need a whole group <strong>of</strong> messages. Instead <strong>of</strong> issuing separate commands and<br />

receiving separate responses for each individual message, you can operate on a group <strong>of</strong> messages as a<br />

whole. The operation works faster since you no longer have to deal with a network round-trip for every<br />

single command.<br />

When you supply a message number, you can instead supply a comma-separated list <strong>of</strong> message<br />

numbers. And, if you want all messages whose numbers are in a range but you do not want to have to list<br />

all <strong>of</strong> their numbers (or if you do not even know their numbers—maybe you want “everything starting<br />

with message one” without having to fetch their numbers first), you can use a colon to separate the start<br />

and end message numbers. An asterisk means “and all <strong>of</strong> the rest <strong>of</strong> the messages.” Here is an example<br />

specification:<br />

2,4:6,20:*<br />

It means “message 2,” “messages 4 through 6,” and “message 20 through the end <strong>of</strong> the mail folder.”<br />

Summary Information<br />

When you first select a folder, the IMAP server provides some summary information about it—about the<br />

folder itself and also about its messages.<br />

The summary is returned by IMAPClient as a dictionary. Here are the keys that most IMAP servers<br />

will return when you run select_folder():<br />

• EXISTS: An integer giving the number <strong>of</strong> messages in the folder<br />

• FLAGS: A list <strong>of</strong> the flags that can be set on messages in this folder<br />

• RECENT: Specifies the server’s approximation <strong>of</strong> the number <strong>of</strong> messages that have<br />

appeared in the folder since the last time an IMAP client ran select_folder() on<br />

it.<br />

• PERMANENTFLAGS: Specifies the list <strong>of</strong> custom flags that can be set on messages; this<br />

is usually empty.<br />

• UIDNEXT: The server’s guess about the UID that will be assigned to the next<br />

incoming (or uploaded) message<br />

• UIDVALIDITY: A string that can be used by clients to verify that the UID numbering<br />

has not changed; if you come back to a folder and this is a different value than the<br />

last time you connected, then the UID number has started over and your stored<br />

UID values are no longer valid.<br />

• UNSEEN: Specifies the message number <strong>of</strong> the first unseen message (one without<br />

the \Seen flag) in the folder<br />

Of these flags, servers are only required to return FLAGS, EXISTS, and RECENT, though most will<br />

include at least UIDVALIDITY as well. Listing 15–5 shows an example program that reads and displays the<br />

summary information <strong>of</strong> my INBOX mail folder.<br />

249

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!