def verify_account(self, account): """ Verify the credentials provided by logging in. Verify the account configuration -- specifically checks for the presence of the 'All Mail' folder. Raises ------ An inbox.crispin.GmailSettingError if the 'All Mail' folder is not present and is required (account.sync_email == True). """ try: # Verify login. conn = self.connect_account(account) # Verify configuration. client = GmailCrispinClient(account.id, provider_info('gmail'), account.email_address, conn, readonly=True) client.sync_folders() conn.logout() except ImapSupportDisabledError: if account.sync_email: raise # 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. account.sync_state = ('running' if account.sync_state in ('running', 'invalid') else account.sync_state) return True
def verify_account(self, account): """ Verify the credentials provided by logging in. Verify the account configuration -- specifically checks for the presence of the 'All Mail' folder. Raises ------ An inbox.crispin.GmailSettingError if the 'All Mail' folder is not present and is required (account.sync_email == True). """ try: # Verify login. conn = self.connect_account(account) # Verify configuration. client = GmailCrispinClient(account.id, provider_info('gmail'), account.email_address, conn, readonly=True) client.sync_folders() conn.logout() except ImapSupportDisabledError: if account.sync_email: raise # 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
def _open_crispin_connection(self, db_session): account = db_session.query(Account).get(self.account_id) conn = account.auth_handler.connect_account(account) self.crispin_client = GmailCrispinClient(self.account_id, provider_info('gmail'), account.email_address, conn, readonly=True)
def verify_config(self, account): """Verifies configuration, specifically presence of 'All Mail' folder. Will raise an inbox.crispin.GmailSettingError if not present. """ conn = self.connect_account(account) # make a crispin client and check the folders client = GmailCrispinClient(account.id, provider_info('gmail'), account.email_address, conn, readonly=True) client.sync_folders() conn.logout() return True
def patch_gmail_client(monkeypatch, folders): monkeypatch.setattr(GmailCrispinClient, '_fetch_folder_list', lambda x: folders) conn = MockedIMAPClient(host='somehost') return GmailCrispinClient(account_id=1, provider_info=None, email_address='*****@*****.**', conn=conn)
def gmail_client(): conn = MockedIMAPClient(host="somehost") return GmailCrispinClient( account_id=1, provider_info=None, email_address="*****@*****.**", conn=conn, )
class GmailSearchClient(IMAPSearchClient): def _open_crispin_connection(self, db_session): account = db_session.query(Account).get(self.account_id) conn = account.auth_handler.connect_account(account) self.crispin_client = GmailCrispinClient(self.account_id, provider_info('gmail'), account.email_address, conn, readonly=True) def _search_folder(self, db_session, folder, search_query): self.crispin_client.select_folder(folder.name, uidvalidity_cb) try: try: query = search_query.encode('ascii') matching_uids = self.crispin_client.conn.gmail_search(query) except UnicodeEncodeError: matching_uids = \ self.crispin_client.conn.gmail_search(search_query, charset="UTF-8") except Exception as e: self.log.debug('Search error', error=e) raise self.log.debug('Search found message for folder', folder_name=folder.name, matching_uids=len(matching_uids)) return matching_uids def _search(self, db_session, search_query): self._open_crispin_connection(db_session) folders = db_session.query(Folder).filter( Folder.account_id == self.account_id).all() imap_uids = set() for folder in folders: imap_uids.update(self._search_folder(db_session, folder, search_query)) self._close_crispin_connection() return imap_uids
def search_messages(self, db_session, search_query): account = db_session.query(Account).get(self.account_id) conn = account.auth_handler.connect_account(account) crispin_client = GmailCrispinClient(self.account_id, provider_info('gmail'), account.email_address, conn, readonly=True) self.log.debug('Searching {} for `{}`' .format(account.email_address, 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, search_query)) crispin_client.logout() return sorted(all_messages, key=lambda msg: msg.received_date)