def fetch(self, messages, parts, modifiers=None): """Retrieve selected data items for one or more messages. @param messages: Message IDs to fetch. @param parts: A sequence of data items to retrieve. @param modifiers: An optional sequence of modifiers (where supported by the server, eg. ['CHANGEDSINCE 123']). @return: A dictionary indexed by message number. Each item is itself a dictionary containing the requested message parts. INTERNALDATE parts will be returned as datetime objects converted to the local machine's time zone. """ if not messages: return {} msg_list = messages_to_str(messages) parts_list = seq_to_parenlist([p.upper() for p in parts]) modifiers_list = None if modifiers is not None: modifiers_list = seq_to_parenlist([m.upper() for m in modifiers]) if self.use_uid: tag = self._imap._command('UID', 'FETCH', msg_list, parts_list, modifiers_list) else: tag = self._imap._command('FETCH', msg_list, parts_list, modifiers_list) typ, data = self._imap._command_complete('FETCH', tag) self._checkok('fetch', typ, data) typ, data = self._imap._untagged_response(typ, data, 'FETCH') return parse_fetch_response(data)
def _store(self, cmd, messages, flags): """Worker function for the various flag manipulation methods. *cmd* is the STORE command to use (eg. '+FLAGS'). """ if not messages: return {} data = self._command_and_check("store", messages_to_str(messages), cmd, seq_to_parenlist(flags), uid=True) return self._flatten_dict(parse_fetch_response((data)))
def fetch(self, messages, data, modifiers=None): """Retrieve selected *data* associated with one or more *messages*. *data* should be specified as a sequnce of strings, one item per data selector, for example ``['INTERNALDATE', 'RFC822']``. *modifiers* are required for some extensions to the IMAP protocol (eg. RFC 4551). These should be a sequnce of strings if specified, for example ``['CHANGEDSINCE 123']``. A dictionary is returned, indexed by message number. Each item in this dictionary is also a dictionary, with an entry corresponding to each item in *data*. In addition to an element for each *data* item, the dict returned for each message also contains a *SEQ* key containing the sequence number for the message. This allows for mapping between the UID and sequence number (when the *use_uid* property is ``True``). Example:: >> c.fetch([3293, 3230], ['INTERNALDATE', 'FLAGS']) {3230: {'FLAGS': ('\\Seen',), 'INTERNALDATE': datetime.datetime(2011, 1, 30, 13, 32, 9), 'SEQ': 84}, 3293: {'FLAGS': (), 'INTERNALDATE': datetime.datetime(2011, 2, 24, 19, 30, 36), 'SEQ': 110}} """ if not messages: return {} msg_list = messages_to_str(messages) data_list = seq_to_parenlist([p.upper() for p in data]) modifiers_list = None if modifiers is not None: modifiers_list = seq_to_parenlist([m.upper() for m in modifiers]) args = ["FETCH", msg_list, data_list, modifiers_list] if self.use_uid: args.insert(0, "UID") tag = self._imap._command(*args) typ, data = self._imap._command_complete("FETCH", tag) self._checkok("fetch", typ, data) typ, data = self._imap._untagged_response(typ, data, "FETCH") return parse_fetch_response(data, self.normalise_times, self.use_uid)
def _store(self, cmd, messages, flags): """Worker function for flag manipulation functions @param cmd: STORE command to use (eg. '+FLAGS') @param messages: Sequence of message IDs @param flags: Sequence of flags to set. @return: The flags set for each message ID as a dictionary { msgid1: [flag1, flag2, ... ], } """ if not messages: return {} msg_list = messages_to_str(messages) flag_list = seq_to_parenlist(flags) if self.use_uid: typ, data = self._imap.uid('STORE', msg_list, cmd, flag_list) else: typ, data = self._imap.store(msg_list, cmd, flag_list) self._checkok('store', typ, data) return self._flatten_dict(parse_fetch_response((data)))