properly check for stale IMAP sessions and reconnect

workspace
Brendan Howell 5 years ago
parent 1f645d0ab5
commit 10e51621a0

@ -7,6 +7,7 @@ import os.path
import random import random
import smtplib import smtplib
import string import string
import time
import imapclient import imapclient
import kode256 import kode256
@ -56,6 +57,7 @@ class MailRoom(Bureau):
self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl) self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl)
self.imapserv.login(self.login, self.password) self.imapserv.login(self.login, self.password)
self.imapserv.select_folder("INBOX") self.imapserv.select_folder("INBOX")
self.last_login = time.time()
else: else:
print("you need to configure an IMAP account!") print("you need to configure an IMAP account!")
print("add a user: section to PO.yml with:") print("add a user: section to PO.yml with:")
@ -74,14 +76,25 @@ class MailRoom(Bureau):
""" """
connect / reconnect to imap server connect / reconnect to imap server
""" """
try: def run_login():
self.imapserv.select_folder("INBOX")
except (self.imapserv.AbortError, SSL.SysCallError) as err:
self.log.debug("reconnecting after imap connection error: ", err)
self.imapserv.logout() self.imapserv.logout()
self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl) self.imapserv = imapclient.IMAPClient(self.host, use_uid=True, ssl=self.imap_ssl)
self.imapserv.login(self.login, self.password) self.imapserv.login(self.login, self.password)
self.imapserv.select_folder("INBOX") self.imapserv.select_folder("INBOX")
# sessions die after 10 minutes so reconnect if it's stale
now = time.time()
if (now - self.last_login) > 600.0:
run_login()
self.last_login = now
return
# if we're already logged in just open the inbox
try:
self.imapserv.select_folder("INBOX")
except (self.imapserv.AbortError, SSL.SysCallError) as err:
self.log.debug("reconnecting after imap connection error: ", err)
run_login()
except self.imapserv.Error as err: except self.imapserv.Error as err:
self.log.debug("IMAP connection error: %err", err) self.log.debug("IMAP connection error: %err", err)
self.smprint("IMAP connection error: " + str(err)) self.smprint("IMAP connection error: " + str(err))
@ -190,7 +203,6 @@ class MailRoom(Bureau):
kode256.image("POr." + shortcode).save(msg.r_bc) kode256.image("POr." + shortcode).save(msg.r_bc)
self.print_full("email.html", msg=msg, shortcode=shortcode) self.print_full("email.html", msg=msg, shortcode=shortcode)
self.imapserv.logout()
@add_command("d", "Delete email") @add_command("d", "Delete email")
def delete(self, data): def delete(self, data):
@ -204,7 +216,6 @@ class MailRoom(Bureau):
self.imapserv.copy((imap_id), self.trashfolder) self.imapserv.copy((imap_id), self.trashfolder)
self.imapserv.delete_messages((imap_id)) self.imapserv.delete_messages((imap_id))
self.imapserv.expunge() self.imapserv.expunge()
self.imapserv.logout()
@add_command("sp", "Mark as spam") @add_command("sp", "Mark as spam")
def mark_spam(self, data): def mark_spam(self, data):
@ -219,7 +230,6 @@ class MailRoom(Bureau):
self.imapserv.copy((imap_id), self.spamfolder) self.imapserv.copy((imap_id), self.spamfolder)
self.imapserv.delete_messages((imap_id)) self.imapserv.delete_messages((imap_id))
self.imapserv.expunge() self.imapserv.expunge()
self.imapserv.logout()
@add_command("un", "Mark as unread") @add_command("un", "Mark as unread")
def mark_unread(self, data): def mark_unread(self, data):
@ -230,7 +240,6 @@ class MailRoom(Bureau):
imap_id = self.get_imap_id(shortcode) imap_id = self.get_imap_id(shortcode)
self._connect_imap() self._connect_imap()
self.imapserv.remove_flags(imap_id, [imapclient.SEEN]) self.imapserv.remove_flags(imap_id, [imapclient.SEEN])
self.imapserv.logout()
@add_command("re", "Reply with scan") @add_command("re", "Reply with scan")
def reply_scan(self, data): def reply_scan(self, data):
@ -284,7 +293,6 @@ class MailRoom(Bureau):
# flag as replied # flag as replied
self.imapserv.add_flags(imap_id, [imapclient.ANSWERED]) self.imapserv.add_flags(imap_id, [imapclient.ANSWERED])
self.imapserv.logout()
@add_api("unread", "Get unread mails") @add_api("unread", "Get unread mails")
def unread(self): def unread(self):
@ -325,7 +333,6 @@ class MailRoom(Bureau):
msgs.append(msg.__dict__) msgs.append(msg.__dict__)
self.imapserv.logout()
return msgs return msgs

Loading…
Cancel
Save