-from ConfigParser import SafeConfigParser, NoOptionError
+from __future__ import unicode_literals
+
+try:
+ from ConfigParser import SafeConfigParser, NoOptionError
+except ImportError:
+ from configparser import SafeConfigParser, NoOptionError
+
import imapclient
+ import urllib
+
+ try:
+ import json
+ except ImportError:
+ json = None
def parse_config_file(path):
Used by livetest.py and interact.py
"""
- parser = SafeConfigParser(dict(ssl='false',
- username=None,
- password=None,
- oauth='false',
- oauth_url=None,
- oauth_token=None,
- oauth_token_secret=None))
+ parser = SafeConfigParser(dict(
+ username=None,
+ password=None,
+ ssl='false',
+ stream='false',
+ oauth='false',
+ oauth_token=None,
+ oauth_token_secret=None,
+ oauth_url=None,
+ oauth2='false',
+ oauth2_client_id=None,
+ oauth2_client_secret=None,
+ oauth2_refresh_token=None,
+ ))
- fh = file(path)
- parser.readfp(fh)
- fh.close()
+ with open(path, 'r') as fh:
+ parser.readfp(fh)
section = 'main'
assert parser.sections() == [section], 'Only expected a [main] section'
from datetime import datetime
from operator import itemgetter
-import imaplib
-import response_lexer
+from . import response_lexer
+ # Confusingly, this module is for OAUTH v1, not v2
try:
- import oauth2
+ import oauth2 as oauth_module
except ImportError:
- oauth2 = None
+ oauth_module = None
- from .imap_utf7 import encode as encode_utf7, from_bytes, decode as decode_utf7
-import imap_utf7
-from fixed_offset import FixedOffset
++from .imap_utf7 import encode as encode_utf7, from_bytes
+from .fixed_offset import FixedOffset
+from .six import moves, iteritems, text_type, integer_types, PY3, binary_type
+xrange = moves.xrange
+
+if PY3:
+ long = int # long is just int in python3
__all__ = ['IMAPClient', 'DELETED', 'SEEN', 'ANSWERED', 'FLAGGED', 'DRAFT', 'RECENT']
def capabilities(self):
"""Returns the server capability list.
+
+ If the session is authenticated and the server has returned a
+ CAPABILITY response at authentication time, this response
+ will be returned. Otherwise, the CAPABILITY command will be
+ issued to the server, with the results cached for future calls.
+
+ If the session is not yet authenticated, the cached
+ capabilities determined at connection time will be returned.
"""
- capabilities = from_bytes(self._imap.capabilities, self.folder_encode)
- return capabilities
+ # if a capability response has been cached, use that
+ if self._cached_capabilities:
+ return self._cached_capabilities
+
+ # If server returned an untagged CAPABILITY response (during
+ # authentication), cache it and return that.
+ response = self._imap.untagged_responses.pop('CAPABILITY', None)
+ if response:
+ return self._save_capabilities(response[0])
+
+ # if authenticated, but don't have a capability reponse, ask for one
+ if self._imap.state in ('SELECTED', 'AUTH'):
+ response = self._command_and_check('capability', unpack=True)
+ return self._save_capabilities(response)
+
+ # Just return capabilities that imaplib grabbed at connection
+ # time (pre-auth)
- return self._imap.capabilities
++ return from_bytes(self._imap.capabilities, folder_encode=False)
+
+ def _save_capabilities(self, raw_response):
++ raw_response = from_bytes(raw_response, folder_encode=False)
+ self._cached_capabilities = tuple(raw_response.upper().split())
+ return self._cached_capabilities
def has_capability(self, capability):
"""Return ``True`` if the IMAP server has the given *capability*.
args = ['CHARSET', charset]
else:
args = []
- args.extend(crit_list)
+ args.extend(criteria)
typ, data = self._imap.uid('SEARCH', *args)
else:
- typ, data = self._imap.search(charset, *crit_list)
+ typ, data = self._imap.search(charset, *criteria)
+ data = from_bytes(data, self.folder_encode)
+
self._checkok('search', typ, data)
data = data[0]
if data is None: # no untagged responses...
messages = (messages,)
elif not isinstance(messages, (tuple, list)):
raise ValueError('invalid message list: %r' % messages)
- return ','.join([str(m) for m in messages])
-
-def seq_to_parenlist(flags):
- """Convert a sequence of strings into parenthised list string for
- use with IMAP commands.
- """
- if isinstance(flags, str):
- flags = (flags,)
- elif not isinstance(flags, (tuple, list)):
- raise ValueError('invalid flags list: %r' % flags)
- return '(%s)' % ' '.join(flags)
+ return ','.join([text_type(m) for m in messages])
+ def normalise_search_criteria(criteria):
+ if not criteria:
+ raise ValueError('no criteria specified')
+ if isinstance(criteria, basestring):
+ criteria = (criteria,)
+ return ['(%s)' % c for c in criteria]
+
def datetime_to_imap(dt):
"""Convert a datetime instance to a IMAP datetime string.