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