def __init__(self, host, port=None, ssl=True, use_uid=True, stream=False, silent_fail=False): # pylint: disable=R0913 """ Initialisation and setup connection to IMAPClient. Arguments: host (str): address of host port (int): port to connect to ssl (boolean): True if ssl should be used use_uid (boolean): If True unique message UIDs be used for all calls that accept message ids (defaults to True). stream (boolean): If True then *host* is used as the command to run to establish a connection to the IMAP server (defaults to False). This is useful for exotic connection or authentication setups. Raises: IMAPConnectionError: raised when connection cannot be established. """ try: self.client = IMAPClient(host, port, use_uid, ssl, stream) self.client.normalise_times = False except (error, gaierror) as e: # Could not connect to IMAPClient. self.client = None logger.warn(e) if not silent_fail: raise IMAPConnectionError(str(e))
def wrapper(self, *args, **kwargs): try: return func(self, *args, **kwargs) except IMAP4.error, e: logger.warn(e) try: self.client.logout() except: # pylint: disable=W0702 pass # ignore more exceptions: `e` is logged already raise IMAPConnectionError(str(e))
def login(self, username, password): """ IMAP login. Returns: Boolean: True if login is successful. """ if self.client: try: if self.client._imap.state in ('NONAUTH', 'LOGOUT'): self.client.login(username, password) except IMAP4.error, e: logger.warn(e) if is_login_error(e): self.auth_ok = False return False else: return True