Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
try:
if self.source.message_ok_to_import(message_string):
(email_object, dummy, error) = self.source.parse_email(message_string)
if error:
raise ReaderError(error)
self.source.db.add(email_object)
else:
print "Skipped message with imap id %s (bounce or vacation message)" % (email_id)
# print "Setting self.source.last_imported_email_uid to "+email_id
self.source.last_imported_email_uid = email_id
self.source.db.commit()
finally:
self.source = ContentSource.get(self.source.id)
except IMAP4.abort as e:
raise IrrecoverableError(e)
except IMAP4.error as e:
raise ClientError(e)
@staticmethod
def do_import_content(mbox, only_new=True):
mbox = mbox.db.merge(mbox)
session = mbox.db
session.add(mbox)
if mbox.use_ssl:
mailbox = IMAP4_SSL(host=mbox.host.encode('utf-8'), port=mbox.port)
else:
mailbox = IMAP4(host=mbox.host.encode('utf-8'), port=mbox.port)
if 'STARTTLS' in mailbox.capabilities:
# Always use starttls if server supports it
mailbox.starttls()
mailbox.login(mbox.username, mbox.password)
mailbox.select(mbox.folder)
command = "ALL"
search_status = None
email_ids = None
if only_new and mbox.last_imported_email_uid:
command = "(UID %s:*)" % mbox.last_imported_email_uid
search_status, search_result = mailbox.uid('search', None, command)
# print "UID searched with: "+ command + ", got result "+repr(search_status)+" and found "+repr(search_result)
email_ids = search_result[0].split()
if not message_string:
raise ClientError()
try:
if self.source.message_ok_to_import(message_string):
(email_object, dummy, error) = self.source.parse_email(message_string)
if error:
raise ReaderError(error)
self.source.db.add(email_object)
else:
print "Skipped message with imap id %s (bounce or vacation message)" % (email_id)
# print "Setting self.source.last_imported_email_uid to "+email_id
self.source.last_imported_email_uid = email_id
self.source.db.commit()
finally:
self.source = ContentSource.get(self.source.id)
except IMAP4.abort as e:
raise IrrecoverableError(e)
except IMAP4.error as e:
raise ClientError(e)
def login(self):
try:
if self.source.use_ssl:
mailbox = IMAP4_SSL(host=self.source.host.encode('utf-8'), port=self.source.port)
else:
mailbox = IMAP4(host=self.source.host.encode('utf-8'), port=self.source.port)
if 'STARTTLS' in mailbox.capabilities:
#Always use starttls if server supports it
res = mailbox.starttls()
if not is_ok(res):
# TODO: Require bad login from client error
raise ReaderError(res)
if 'IDLE' in mailbox.capabilities:
self.can_push = True
res = mailbox.login(self.source.username, self.source.password)
if not is_ok(res):
# TODO: Require bad login from client error
raise ClientError(res)
res = mailbox.select(self.source.folder)
if not is_ok(res):
# TODO: Require bad login from client error
raise ClientError(res)
res = self.mailbox.idle(int(self.max_idle_period.total_seconds()))
if not is_ok(res):
raise ClientError(res)
# was it a timeout?
res = self.mailbox.response('IDLE')
if res and len(res) > 1 and len(res[1]) > 1 and res[1][1] == 'TIMEOUT':
self.set_status(ReaderStatus.PAUSED)
return
if not self.is_connected():
return
if self.status == ReaderStatus.WAIT_FOR_PUSH:
self.do_read()
if self.is_connected():
# am I still in IDLE state (WAIT_FOR_PUSH), or PAUSED?
self.set_status(ReaderStatus.WAIT_FOR_PUSH)
except IMAP4.abort as e:
raise IrrecoverableError(e)
except IMAP4.error as e:
raise ClientError(e)