def connect_account(self, email, pw, imap_endpoint, account_id=None): """Provide a connection to a IMAP account. Raises ------ socket.error If we cannot connect to the IMAP host. IMAPClient.error If the credentials are invalid. """ host, port = imap_endpoint try: conn = IMAPClient(host, port=port, use_uid=True, ssl=True) except IMAPClient.AbortError as e: log.error('account_connect_failed', account_id=account_id, email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except(IMAPClient.Error, gaierror, socket_error) as e: log.error('account_connect_failed', account_id=account_id, email=email, host=host, port=port, error='[ALERT] (Failure): {0}'.format(str(e))) raise ConnectionError(str(e)) conn.debug = False try: conn.oauth2_login(email, pw) except IMAPClient.AbortError as e: log.error('account_verify_failed', account_id=account_id, email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except IMAPClient.Error as e: log.error('IMAP Login error during connection. ' 'Account: {}, error: {}'.format(email, e), account_id=account_id) if str(e) == '[ALERT] Invalid credentials (Failure)' or \ str(e) == '[AUTHENTICATIONFAILED] OAuth authentication failed.': raise ValidationError(str(e)) else: raise ConnectionError(str(e)) except SSLError as e: log.error('account_verify_failed', account_id=account_id, email=email, host=host, port=port, error='[ALERT] (Failure) SSL Connection error') raise ConnectionError(str(e)) return conn
def connect_account(self, email, pw, imap_endpoint, account_id=None): """Provide a connection to a IMAP account. Raises ------ socket.error If we cannot connect to the IMAP host. IMAPClient.error If the credentials are invalid. """ host, port = imap_endpoint try: conn = IMAPClient(host, port=port, use_uid=True, ssl=True) except IMAPClient.AbortError as e: log.error('account_connect_failed', account_id=account_id, email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except (IMAPClient.Error, gaierror, socket_error) as e: log.error('account_connect_failed', account_id=account_id, email=email, host=host, port=port, error='[ALERT] (Failure): {0}'.format(str(e))) raise ConnectionError(str(e)) conn.debug = False try: conn.oauth2_login(email, pw) except IMAPClient.AbortError as e: log.error('account_verify_failed', account_id=account_id, email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except IMAPClient.Error as e: log.error('IMAP Login error during connection. ' 'Account: {}, error: {}'.format(email, e), account_id=account_id) if (str(e) == '[ALERT] Invalid credentials (Failure)' or str(e).startswith('[AUTHENTICATIONFAILED]')): raise ValidationError(str(e)) else: raise ConnectionError(str(e)) except SSLError as e: log.error('account_verify_failed', account_id=account_id, email=email, host=host, port=port, error='[ALERT] (Failure) SSL Connection error') raise ConnectionError(str(e)) return conn
def connect_account(account, host): """Provide a connection to a generic IMAP account. Raises ------ ConnectionError If we cannot connect to the IMAP host. ValidationError If the credentials are invalid. """ try: conn = IMAPClient(host, use_uid=True, ssl=True) except IMAPClient.Error as e: log.error('account_connect_failed', host=host, error="[ALERT] Can't connect to host (Failure)") raise ConnectionError(str(e)) conn.debug = False try: conn.login(account.email_address, account.password) except IMAPClient.Error as e: log.error('account_verify_failed', email=account.email_address, host=host, password=account.password, error="[ALERT] Invalid credentials (Failure)") raise ValidationError() return conn
def test_imap(user): credentials = decorator.credentials if credentials.access_token_expired: logging.debug('Refreshing...') credentials.refresh(httplib2.Http()) conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=True) conn.debug = 4 conn.oauth2_login(user, credentials.access_token) # status, labels = conn.list() folders = conn.list_folders() try: all_box = next(box for (flags, _, box) in folders if '\All' in flags) except StopIteration: raise Error('all message box not found') logging.debug('All message box is {}'.format(all_box)) conn.select_folder(all_box) # Once authenticated everything from the impalib.IMAP4_SSL class will # work as per usual without any modification to your code. # typ, msgnums = conn.search('X-GM-RAW vget') tid = int('14095f27c538b207', 16) # msgs = conn.search('X-GM-THRID {}'.format(tid)) msgs = conn.search('X-GM-RAW uniquetokenXXX') logging.info(msgs) logging.info(conn.fetch(msgs, 'X-GM-MSGID')) msg = conn.fetch(msgs, 'RFC822') logging.info(msg) return msg
def connect_account(self, provider, email, credential): """Provide a connection to a generic IMAP account. Raises ------ ConnectionError If we cannot connect to the IMAP host. TransientConnectionError Sometimes the server bails out on us. Retrying may fix things. ValidationError If the credentials are invalid. """ info = provider_info(provider, email) host, port = info['imap'] try: conn = IMAPClient(host, port=port, use_uid=True, ssl=True) except IMAPClient.AbortError as e: log.error('account_connect_failed', email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except(IMAPClient.Error, gaierror, socket_error) as e: log.error('account_connect_failed', email=email, host=host, port=port, error='[ALERT] (Failure): {0}'.format(str(e))) raise ConnectionError(str(e)) conn.debug = False try: conn.login(email, credential) except IMAPClient.AbortError as e: log.error('account_verify_failed', email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except IMAPClient.Error as e: log.error('account_verify_failed', email=email, host=host, port=port, error='[ALERT] Invalid credentials (Failure)') raise ValidationError(str(e)) except SSLError as e: log.error('account_verify_failed', email=email, host=host, port=port, error='[ALERT] SSL Connection error (Failure)') raise ConnectionError(str(e)) return conn
def connect_account(provider, email, pw): """Provide a connection to a IMAP account. Raises ------ socket.error If we cannot connect to the IMAP host. IMAPClient.error If the credentials are invalid. """ info = provider_info(provider) host, port = info['imap'] try: conn = IMAPClient(host, port=port, use_uid=True, ssl=True) except IMAPClient.AbortError as e: log.error('account_connect_failed', email=email, host=host, port=port, error=("[ALERT] Can't connect to host - may be transient")) raise TransientConnectionError(str(e)) except (IMAPClient.Error, gaierror, socket_error) as e: log.error('account_connect_failed', email=email, host=host, port=port, error='[ALERT] (Failure): {0}'.format(str(e))) raise ConnectionError(str(e)) conn.debug = False try: conn.oauth2_login(email, pw) except IMAPClient.AbortError as e: log.error('account_verify_failed', email=email, host=host, port=port, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except IMAPClient.Error as e: log.error('IMAP Login error during refresh auth token. ' 'Account: {}, error: {}'.format(email, e)) if str(e) == '[ALERT] Invalid credentials (Failure)' or \ str(e) == '[AUTHENTICATIONFAILED] OAuth authentication failed.': raise ValidationError(str(e)) else: raise ConnectionError(str(e)) except SSLError as e: log.error('account_verify_failed', email=email, host=host, port=port, error='[ALERT] (Failure) SSL Connection error') raise ConnectionError(str(e)) return conn
def connect_account(account): """Provide a connection to a IMAP account. Raises ------ socket.error If we cannot connect to the IMAP host. IMAPClient.error If the credentials are invalid. """ info = provider_info(account.provider) host = info['imap'] try: conn = IMAPClient(host, use_uid=True, ssl=True) except IMAPClient.AbortError as e: log.error('account_connect_failed', email=account.email_address, host=host, error=("[ALERT] Can't connect to host - may be transient")) raise TransientConnectionError(str(e)) except(IMAPClient.Error, gaierror, socket_error) as e: log.error('account_connect_failed', email=account.email_address, host=host, error='[ALERT] (Failure): {0}'.format(str(e))) raise ConnectionError(str(e)) conn.debug = False try: conn.oauth2_login(account.email_address, account.access_token) except IMAPClient.AbortError as e: log.error('account_verify_failed', email=account.email_address, host=host, error="[ALERT] Can't connect to host - may be transient") raise TransientConnectionError(str(e)) except IMAPClient.Error as e: log.error('IMAP Login error during refresh auth token. ' 'Account: {}, error: {}'.format(account.email_address, e)) if str(e) == '[ALERT] Invalid credentials (Failure)': # Access token could've expired try: conn.oauth2_login(account.email_address, account.renew_access_token()) except IMAPClient.Error as e: raise ValidationError(str(e)) else: raise ConnectionError(str(e)) except SSLError as e: log.error('account_verify_failed', email=account.email_address, host=host, error='[ALERT] (Failure) SSL Connection error') raise ConnectionError(str(e)) return conn
def connect_account(account): """Provide a connection to a IMAP account. Raises ------ socket.error If we cannot connect to the IMAP host. IMAPClient.error If the credentials are invalid. """ info = provider_info(account.provider) host = info["imap"] try: conn = IMAPClient(host, use_uid=True, ssl=True) except IMAPClient.Error as e: log.error('account_connect_failed', host=host, error="[ALERT] Can't connect to host (Failure)") raise ConnectionError(str(e)) except gaierror as e: log.error('account_connect_failed', host=host, error="[ALERT] Name resolution faiure (Failure)") raise ConnectionError(str(e)) except socket_error as e: log.error('account_connect_failed', host=host, error="[ALERT] Socket connection failure (Failure)") raise ConnectionError(str(e)) conn.debug = False try: conn.oauth2_login(account.email_address, account.access_token) except IMAPClient.Error as e: log.error("IMAP Login error, refresh auth token for: {}" .format(account.email_address)) log.error("Error was: {}".format(e)) if str(e) == '[ALERT] Invalid credentials (Failure)': # maybe the access token expired? try: conn.oauth2_login(account.email_address, account.renew_access_token()) except IMAPClient.Error as e: raise ValidationError() else: raise ValidationError() except SSLError as e: log.error('account_verify_failed', email=account.email_address, host=host, error="[ALERT] SSL Connection error (Failure)") raise ConnectionError(str(e)) return conn
def verify_account(account): try: conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.login(account.email_address, account.password) except IMAPClient.Error as e: log.error('IMAP connection error: {}'.format(e)) raise return conn
def verify_yahoo_account(account): try: conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.login(account.email_address, account.password) except IMAPClient.Error as e: print >>sys.stderr, '[ALERT] Invalid credentials (Failure)' sys.exit(1) return conn
def slurp_namespace(self, namespace, account, db): info = provider_info(account.provider) imap = IMAPClient(info['imap'], use_uid=True, ssl=True) imap.debug = self.args.debug_imap if info['auth'] == 'oauth2': imap.oauth2_login(account.email_address, account.access_token) elif info['auth'] == 'password': imap.login(account.email_address, account.password) else: raise NotImplementedError( "auth mechanism {0!r} not implemented; provider: {1!r}".format( info['auth'], account.provider)) slurp_imap_namespace_gmail(imap, namespace=namespace, account=account, db=db)
def verify_account(account): try: conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.login(account.email_address, account.password) except IMAPClient.Error as e: log.error('yahoo_account_verify_failed', error='[ALERT] Invalid credentials (Failure)') raise return conn
def connect_account(account): """Provide a connection to a generic IMAP account. Raises ------ ConnectionError If we cannot connect to the IMAP host. ValidationError If the credentials are invalid. """ info = provider_info(account.provider) host = info["imap"] try: conn = IMAPClient(host, use_uid=True, ssl=True) except IMAPClient.Error as e: log.error('account_connect_failed', host=host, error="[ALERT] Can't connect to host (Failure)") raise ConnectionError(str(e)) except gaierror as e: log.error('account_connect_failed', host=host, error="[ALERT] Name resolution faiure (Failure)") raise ConnectionError(str(e)) except socket_error as e: log.error('account_connect_failed', host=host, error="[ALERT] Socket connection failure (Failure)") raise ConnectionError(str(e)) conn.debug = False try: conn.login(account.email_address, account.password) except IMAPClient.Error as e: log.error('account_verify_failed', email=account.email_address, host=host, error="[ALERT] Invalid credentials (Failure)") raise ValidationError() except SSLError as e: log.error('account_verify_failed', email=account.email_address, host=host, error="[ALERT] SSL Connection error (Failure)") raise ConnectionError(str(e)) return conn
def slurp_namespace(self, namespace, account, db): info = account.provider_info host, port = account.imap_endpoint imap = IMAPClient(host, port=port, use_uid=True, ssl=True) imap.debug = self.args.debug_imap if info["auth"] == "oauth2": imap.oauth2_login(account.email_address, account.access_token) elif info["auth"] == "password": imap.login(account.email_address, account.password) else: raise NotImplementedError( "auth mechanism {0!r} not implemented; provider: {1!r}".format(info["auth"], account.provider) ) slurp_imap_namespace_gmail(imap, namespace=namespace, account=account, db=db)
def slurp_namespace(self, namespace, account, db): info = account.provider_info host, port = account.imap_endpoint imap = IMAPClient(host, port=port, use_uid=True, ssl=True) imap.debug = self.args.debug_imap if info['auth'] == 'oauth2': imap.oauth2_login(account.email_address, account.access_token) elif info['auth'] == 'password': imap.login(account.email_address, account.password) else: raise NotImplementedError( "auth mechanism {0!r} not implemented; provider: {1!r}".format( info['auth'], account.provider)) slurp_imap_namespace_gmail(imap, namespace=namespace, account=account, db=db)
def _new_connection(self): try: conn = IMAPClient(self.imap_host, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.oauth2_login(self.email_address, self.o_access_token) except IMAPClient.Error as e: if str(e) == "[ALERT] Invalid credentials (Failure)": self._set_account_info() conn.oauth2_login(self.email_address, self.o_access_token) return conn
def verify_gmail_account(account): try: conn = IMAPClient(IMAP_HOSTS['Gmail'], use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.oauth2_login(account.email_address, account.o_access_token) except IMAPClient.Error as e: if str(e) == '[ALERT] Invalid credentials (Failure)': # maybe refresh the access token with session_scope() as db_session: account = verify_imap_account(db_session, account) conn.oauth2_login(account.email_address, account.o_access_token) return conn
def verify_account(account): try: conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.oauth2_login(account.email_address, account.access_token) except IMAPClient.Error as e: log.info("IMAP Login error, refresh auth token for: {}" .format(account.email_address)) if str(e) == '[ALERT] Invalid credentials (Failure)': # maybe the access token expired? conn.oauth2_login(account.email_address, account.renew_access_token()) return conn
def verify_gmail_account(account): try: conn = IMAPClient(IMAP_HOST, use_uid=True, ssl=True) except IMAPClient.Error as e: raise socket.error(str(e)) conn.debug = False try: conn.oauth2_login(account.email_address, account.o_access_token) except IMAPClient.Error as e: if str(e) == '[ALERT] Invalid credentials (Failure)': # maybe refresh the access token with session_scope() as db_session: account = verify_imap_account(db_session, account) conn.oauth2_login(account.email_address, account.o_access_token) return conn