From 5e6c3d86ea3a02f5915a299d6eddd56ead22dc0c Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Tue, 27 Oct 2015 12:44:12 +1300 Subject: [PATCH] Clarify how IMAP message ids work in the docs Fixes #181. --- doc/src/index.rst | 30 ++++++++++++++++-------------- imapclient/imapclient.py | 36 +++++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/doc/src/index.rst b/doc/src/index.rst index 919436d..7be4e94 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -70,32 +70,34 @@ Concepts Message Identifiers ~~~~~~~~~~~~~~~~~~~ -There are two ways to refer to messages using the IMAP protocol. +In the IMAP protocol, messages are identified using an integer. These +message ids are specific to a given folder. -One way is by message sequence number where the messages in a mailbox -are numbered from 1 to N where N is the number of messages. These -numbers don't persist between sessions and may be reassigned after -some operations such as a folder expunge. +There are two types of message identifiers in the IMAP protocol. + +One type is the message sequence number where the messages in a folder +are numbered from 1 to N where N is the number of messages in the +folder. These numbers don't persist between sessions and may be +reassigned after some operations such as an expunge. A more convenient approach is Unique Identifiers (UIDs). Unique Identifiers are integers assigned to each message by the IMAP server that will persist across sessions. They do not change when folders are -expunged. +expunged. Almost all IMAP servers support UIDs. Each call to the IMAP server can use either message sequence numbers or UIDs in the command arguments and return values. The client -specifies to the server which type of identifier should be -used. IMAPClient uses UIDs by default. +specifies to the server which type of identifier should be used. You +can set whether IMAPClient should use UIDs or message sequence number +via the *use_uid* argument passed when an IMAPClient instance is +created and the *use_uid* attribute. The *use_uid* attribute can be +used to change the message id type between calls to the +server. IMAPClient uses UIDs by default. Any method that accepts message ids takes either a sequence containing message ids (eg. ``[1,2,3]``), or a single message id integer, or a string representing sets and ranges of messages as supported by the -IMAP protocol (e.g. ``'50-65'``, ``'2:*'`` or -``'2,4:7,9,12:*'``). Whether these are interpreted as message sequence -numbers or UIDs depends on the *use_uid* argument passed when an -IMAPClient instance is created and the *use_uid* attribute. The -*use_uid* attribute can be used to change the message id type between -calls to the server. +IMAP protocol (e.g. ``'50-65'``, ``'2:*'`` or ``'2,4:7,9,12:*'``). Message Flags ~~~~~~~~~~~~~ diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 2f46621..e32dd7e 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -663,7 +663,8 @@ class IMAPClient(object): return self._command_and_check('unsubscribe', self._normalise_folder(folder)) def search(self, criteria='ALL', charset=None): - """Return a list of messages ids matching *criteria*. + """Return a list of messages ids from the currently selected + folder matching *criteria*. *criteria* should be a sequence of one or more criteria items. Each criteria item may be either unicode or @@ -741,8 +742,9 @@ class IMAPClient(object): return parse_message_list(data) def sort(self, sort_criteria, criteria='ALL', charset='UTF-8'): - """Return a list of message ids sorted by *sort_criteria* and - optionally filtered by *criteria*. + """Return a list of message ids from the currently selected + folder, sorted by *sort_criteria* and optionally filtered by + *criteria*. *sort_criteria* may be specified as a sequence of strings or a single string. IMAPClient will take care any required @@ -773,7 +775,8 @@ class IMAPClient(object): return [long(i) for i in ids.split()] def thread(self, algorithm='REFERENCES', criteria='ALL', charset='UTF-8'): - """Return a list of messages threads matching *criteria*. + """Return a list of messages threads from the currently + selected folder which match *criteria*. Each returned thread is a list of messages ids. An example return value containing three message threads:: @@ -799,7 +802,8 @@ class IMAPClient(object): return parse_response(data) def get_flags(self, messages): - """Return the flags set for each message in *messages*. + """Return the flags set for each message in *messages* from + the currently selected folder. The return value is a dictionary structured like this: ``{ msgid1: (flag1, flag2, ... ), }``. @@ -808,7 +812,7 @@ class IMAPClient(object): return self._filter_fetch_dict(response, b'FLAGS') def add_flags(self, messages, flags): - """Add *flags* to *messages*. + """Add *flags* to *messages* in the currently selected folder. *flags* should be a sequence of strings. @@ -818,7 +822,8 @@ class IMAPClient(object): return self._store(b'+FLAGS', messages, flags, b'FLAGS') def remove_flags(self, messages, flags): - """Remove one or more *flags* from *messages*. + """Remove one or more *flags* from *messages* in the currently + selected folder. *flags* should be a sequence of strings. @@ -828,7 +833,8 @@ class IMAPClient(object): return self._store(b'-FLAGS', messages, flags, b'FLAGS') def set_flags(self, messages, flags): - """Set the *flags* for *messages*. + """Set the *flags* for *messages* in the currently selected + folder. *flags* should be a sequence of strings. @@ -838,7 +844,8 @@ class IMAPClient(object): return self._store(b'FLAGS', messages, flags, b'FLAGS') def get_gmail_labels(self, messages): - """Return the label set for each message in *messages*. + """Return the label set for each message in *messages* in the + currently selected folder. The return value is a dictionary structured like this: ``{ msgid1: (label1, label2, ... ), }``. @@ -850,7 +857,7 @@ class IMAPClient(object): return self._filter_fetch_dict(response, b'X-GM-LABELS') def add_gmail_labels(self, messages, labels): - """Add *labels* to *messages*. + """Add *labels* to *messages* in the currently selected folder. *labels* should be a sequence of strings. @@ -864,7 +871,8 @@ class IMAPClient(object): self._normalise_labels(labels), b'X-GM-LABELS') def remove_gmail_labels(self, messages, labels): - """Remove one or more *labels* from *messages*. + """Remove one or more *labels* from *messages* in the + currently selected folder. *labels* should be a sequence of strings. @@ -878,7 +886,8 @@ class IMAPClient(object): self._normalise_labels(labels), b'X-GM-LABELS') def set_gmail_labels(self, messages, labels): - """Set the *labels* for *messages*. + """Set the *labels* for *messages* in the currently selected + folder. *labels* should be a sequence of strings. @@ -901,7 +910,8 @@ class IMAPClient(object): return self.add_flags(messages, DELETED) def fetch(self, messages, data, modifiers=None): - """Retrieve selected *data* associated with one or more *messages*. + """Retrieve selected *data* associated with one or more + *messages* in the currently selected folder. *data* should be specified as a sequnce of strings, one item per data selector, for example ``['INTERNALDATE', -- 2.43.0