def _open_crispin_connection(self, db_session): account = db_session.query(Account).get(self.account_id) try: conn = account.auth_handler.connect_account(account) except (IMAPClient.Error, socket.error, IMAP4.error): raise SearchBackendException(('Unable to connect to the IMAP ' 'server. Please retry in a ' 'couple minutes.'), 503) except ValidationError: raise SearchBackendException(("This search can't be performed " "because the account's credentials " "are out of date. Please " "reauthenticate and try again."), 403) try: acct_provider_info = provider_info(account.provider) except NotSupportedError: self.log.warn('Account provider not supported', provider=account.provider) raise self.crispin_client = CrispinClient(self.account_id, acct_provider_info, account.email_address, conn, readonly=True)
def search_messages(self, db_session, search_query): account = db_session.query(Account).get(self.account_id) conn = account.auth_handler.connect_account(account) try: acct_provider_info = provider_info(account.provider) except NotSupportedError: self.log.warn('Account provider {} not supported.'.format( account.provider)) raise crispin_client = CrispinClient(self.account_id, acct_provider_info, account.email_address, conn, readonly=True) self.log.info('Searching {} for `{}`'.format(account.email_address, search_query)) if ':' not in search_query: criteria = 'TEXT {}'.format(search_query) else: criteria = re.sub('(\w+:[ ]?)', format_key, search_query) all_messages = set() folders = db_session.query(Folder).filter( Folder.account_id == self.account_id).all() for folder in folders: all_messages.update( self.search_folder(db_session, crispin_client, folder, criteria)) crispin_client.logout() return sorted(all_messages, key=lambda msg: msg.received_date)
def patch_generic_client(monkeypatch, folders): monkeypatch.setattr(CrispinClient, '_fetch_folder_list', lambda x: folders) conn = MockedIMAPClient(host='somehost') return CrispinClient(account_id=1, provider_info={}, email_address='*****@*****.**', conn=conn)
def generic_client(): conn = MockedIMAPClient(host="somehost") return CrispinClient( account_id=1, provider_info=None, email_address="*****@*****.**", conn=conn, )
def _open_crispin_connection(self, db_session): account = db_session.query(Account).get(self.account_id) conn = account.auth_handler.connect_account(account) try: acct_provider_info = provider_info(account.provider) except NotSupportedError: self.log.warn('Account provider not supported', provider=account.provider) raise self.crispin_client = CrispinClient(self.account_id, acct_provider_info, account.email_address, conn, readonly=True)
def verify_account(self, account): """ Verifies a generic IMAP account by logging in and logging out to both the IMAP/ SMTP servers. Note: Raises exceptions from connect_account(), SMTPClient._get_connection() on error. Returns ------- True: If the client can successfully connect to both. """ # Verify IMAP login conn = self.connect_account(account) crispin = CrispinClient(account.id, account.provider_info, account.email_address, conn) info = account.provider_info if "condstore" not in info: if self._supports_condstore(conn): account.supports_condstore = True try: conn.list_folders() account.folder_separator = crispin.folder_separator account.folder_prefix = crispin.folder_prefix except Exception as e: log.error("account_folder_list_failed", account_id=account.id, error=e.message) error_message = ( "Full IMAP support is not enabled for this account. " "Please contact your domain " "administrator and try again.") raise UserRecoverableConfigError(error_message) finally: conn.logout() # Verify SMTP login try: # Check that SMTP settings work by establishing and closing and # SMTP session. smtp_client = SMTPClient(account) with smtp_client._get_connection(): pass except socket.gaierror as exc: log.error('Failed to resolve SMTP server domain', account_id=account.id, error=exc) error_message = ( "Couldn't resolve the SMTP server domain name. " "Please check that your SMTP settings are correct.") raise UserRecoverableConfigError(error_message) except socket.timeout as exc: log.error('TCP timeout when connecting to SMTP server', account_id=account.id, error=exc) error_message = ( "Connection timeout when connecting to SMTP server. " "Please check that your SMTP settings are correct.") raise UserRecoverableConfigError(error_message) except Exception as exc: log.error('Failed to establish an SMTP connection', smtp_endpoint=account.smtp_endpoint, account_id=account.id, error=exc) raise UserRecoverableConfigError("Please check that your SMTP " "settings are correct.") # Reset the sync_state to 'running' on a successful re-auth. # Necessary for API requests to proceed and an account modify delta to # be returned to delta/ streaming clients. # NOTE: Setting this does not restart the sync. Sync scheduling occurs # via the sync_should_run bit (set to True in update_account() above). account.sync_state = ('running' if account.sync_state else account.sync_state) return True