__all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT',
'RECENT']
-from response_parser import parse_fetch_response
+from response_parser import parse_response, parse_fetch_response
+
# System flags
DELETED = r'\Deleted'
such as search and fetch will act on the selected folder.
@param folder: The folder name.
- @return: Number of messages in the folder.
- @rtype: long int
+ @return: A dictionary containing the SELECT response
+ values. At least the EXISTS, FLAGS and RECENT keys are
+ guaranteed to exist. Example:
+ {'EXISTS': 3,
+ 'FLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ),
+ 'RECENT': 0,
+ 'PERMANENTFLAGS': ('\\Answered', '\\Flagged', '\\Deleted', ... ),
+ 'READ-WRITE': True,
+ 'UIDNEXT': 11,
+ 'UIDVALIDITY': 1239278212}
"""
typ, data = self._imap.select(self._encode_folder_name(folder))
self._checkok('select', typ, data)
- return long(data[0])
+ return self._process_select_response(self._imap.untagged_responses)
+
+
+ def _process_select_response(self, resp):
+ out = {}
+ for key, value in resp.iteritems():
+ key = key.upper()
+ if key == 'OK':
+ continue
+ elif key in ('EXISTS', 'RECENT', 'UIDNEXT', 'UIDVALIDITY'):
+ value = int(value[0])
+ elif key in ('FLAGS', 'PERMANENTFLAGS'):
+ value = parse_response(value)[0]
+ elif key == 'READ-WRITE':
+ value = True
+
+ out[key] = value
+ return out
def folder_status(self, folder, what=None):
class ResponseTokeniser(object):
CTRL_CHARS = ''.join([chr(ch) for ch in range(32)])
- ATOM_SPECIALS = r'()%*"' + CTRL_CHARS
+ SPECIALS = r'()%"' + CTRL_CHARS
ALL_CHARS = [chr(ch) for ch in range(256)]
- ATOM_NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in ATOM_SPECIALS]
+ NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in SPECIALS]
def __init__(self, resp_chunks):
# initialize the lexer with all the chunks we read.
self.lex.quotes = '"'
self.lex.commenters = ''
- self.lex.wordchars = self.ATOM_NON_SPECIALS
+ self.lex.wordchars = self.NON_SPECIALS
def __iter__(self):
return iter(self.lex)
def test_select_and_close(client):
- num_msgs = client.select_folder('INBOX')
- assert isinstance(num_msgs, long)
- assert num_msgs >= 0
+ resp = client.select_folder('INBOX')
+ assert isinstance(resp['EXISTS'], int)
+ assert resp['EXISTS'] > 1
+ assert isinstance(resp['RECENT'], int)
+ assert isinstance(resp['FLAGS'], tuple)
+ assert len(resp['FLAGS']) > 1
client.close_folder()
assert isinstance(resp, str)
# Retrieve the just added message and check that all looks well
- num_msgs = client.select_folder('INBOX')
- assert num_msgs == 1
+ assert client.select_folder('INBOX')['EXISTS'] == 1
resp = client.fetch(
client.search()[0],