Add backup and restore for IMAP + fix bug + clean prints - PYADPT-47

imap
David 4 years ago
parent f4e755af0c
commit 2cc27ebcbf

@ -17,6 +17,7 @@ test/Laptop
test/Library
test/Phone
test/TestInbox
test/Backup
test/lib
test/imap_settings.py
venv

@ -1,10 +1,9 @@
import imaplib
import pprint
import email.message
import email.charset
import pathlib
import time
import os
import imap_settings as settings
from secrets import token_urlsafe
def connect():
@ -12,6 +11,9 @@ def connect():
server = imaplib.IMAP4_SSL(settings.IMAP_HOST)
server.login(settings.IMAP_USER, settings.IMAP_PWD)
tmp, data = server.select('Inbox')
#When you connect to the inbox one of the parameters returned is the current
#number of messages in it
if os.environ.get('NUMMESSAGES') is None:
os.environ["NUMMESSAGES"] = data[0].decode("UTF-8")
@ -24,16 +26,14 @@ def bytesmessage_to_string(msg):
def send(inbox, msg):
"send msg to inbox in MIME format"
print("send imap")
server = connect()
tmp, data = server.append('Inbox', '', imaplib.Time2Internaldate(time.time()), str(msg).encode("UTF-8"))
tmp, data = server.append(inbox, flags='', date_time=time.time(), message=str(msg).encode("UTF-8"))
server.close()
def recv_all(inbox):
def recv_all():
"""receive a list of all MIME messages from inbox newer than the last message when first connected"""
print("recieve imap")
server = connect()
r = []
@ -58,7 +58,7 @@ def clean_inbox():
"""clean all messsages from IMAP inbox"""
print('cleaning IMAP...')
server = connect()
typ, data = server.search(None, 'ALL')
tmp, data = server.search(None, 'ALL')
for num in data[0].split():
server.store(num, '+FLAGS', '\\Deleted')
server.expunge()
@ -66,3 +66,28 @@ def clean_inbox():
print('IMAP inbox empty.')
def backup_inbox():
"""copy all messsages from IMAP to local backup folder"""
server = connect()
tmp, data = server.search(None, 'ALL')
for num in data[0].split():
tmp, data = server.fetch(num, '(RFC822 BODY[HEADER])')
device = str(data[0][1]).split('From: "')[1].split(' of')[0]
name = device + "_" + token_urlsafe(16) + ".eml"
msg = bytesmessage_to_string(data[0][1])
with open(os.path.join('Backup/TestInbox',name), "wb") as f:
f.write(str(msg).encode())
server.close()
def restore_inbox():
"""copy all the messages from the Backup folder to the IMAP inbox"""
server = connect()
backups = pathlib.Path("./Backup/TestInbox")
emails = backups.glob("*.eml")
l = [ path for path in emails ]
for p in l:
with open(p, "rb") as f:
tmp, data = server.append("Inbox", flags='', date_time=p.stat().st_ctime, message=f.read(-1))
server.close()

@ -105,7 +105,6 @@ def messageToSend(msg):
minimail.send(inbox, msg, device_name)
def messageImapToSend(msg):
print("send imap message")
if msg.enc_format:
m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC)
else:
@ -113,7 +112,7 @@ def messageImapToSend(msg):
text = "<!-- sending from " + device_name + " -->\n" + m.attachments[0].decode()
output(text)
msg.opt_fields = { "pEp.sync": text }
miniimap.send(inbox, msg)
miniimap.send('Inbox', msg)
def getMessageToSend(msg):
if msg.enc_format:
@ -127,7 +126,6 @@ def getMessageToSend(msg):
class UserInterface(pEp.UserInterface):
def notifyHandshake(self, me, partner, signal):
print('ui.notifyHandshake')
print(colored(str(signal), "yellow"), end=" ")
output("on " + device_name + "" if not me.fpr else
"for identities " + str(me.fpr) + " " + str(partner.fpr))
@ -165,8 +163,6 @@ def run(name, color=None, imap=False):
global device_name
device_name = name
print("run sync_handhske")
if color:
global output
output = lambda x: print(colored(x, color))
@ -178,7 +174,6 @@ def run(name, color=None, imap=False):
pEp.debug_color(36)
if imap:
print("run handshake using imap")
me = pEp.Identity(imap_settings.IMAP_EMAIL, name + " of " + imap_settings.IMAP_USER, name)
pEp.myself(me)
pEp.messageToSend = messageImapToSend
@ -200,14 +195,13 @@ def run(name, color=None, imap=False):
sync = Thread(target=sync_thread)
sync.start()
else:
print('no threading')
sync = None
ui = UserInterface()
try:
while not the_end:
if imap:
l = miniimap.recv_all(inbox)
l = miniimap.recv_all()
else:
l = minimail.recv_all(inbox, name)
for n, m in l:

@ -110,10 +110,11 @@ if __name__ == "__main__":
if options.clean:
if options.imap:
try:
miniimap.clean_inbox()
except:
pass
miniimap.clean_inbox()
if options.cleanall:
rmrf("Backup")
else:
rmrf("TestInbox")
rmrf("Phone")
@ -139,21 +140,39 @@ if __name__ == "__main__":
except FileExistsError:
pass
shutil.copytree("Phone", "Backup/Phone", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Laptop", "Backup/Laptop", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Pad", "Backup/Pad", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("TestInbox", "Backup/TestInbox", symlinks=True, copy_function=shutil.copy2)
if options.imap:
try:
os.mkdir("Backup/TestInbox")
except FileExistsError:
pass
miniimap.backup_inbox()
else:
shutil.copytree("Phone", "Backup/Phone", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Laptop", "Backup/Laptop", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("TestInbox", "Backup/TestInbox", symlinks=True, copy_function=shutil.copy2)
try:
shutil.copytree("Pad", "Backup/Pad", symlinks=True, copy_function=shutil.copy2)
except FileNotFoundError:
pass
elif options.restore:
rmrf("TestInbox")
rmrf("Phone")
rmrf("Laptop")
rmrf("Pad")
shutil.copytree("Backup/Phone", "Phone", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Backup/Laptop", "Laptop", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Backup/Pad", "Pad", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Backup/TestInbox", "TestInbox", symlinks=True, copy_function=shutil.copy2)
if options.imap:
miniimap.clean_inbox()
miniimap.restore_inbox()
else:
rmrf("TestInbox")
rmrf("Phone")
rmrf("Laptop")
rmrf("Pad")
shutil.copytree("Backup/Phone", "Phone", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Backup/Laptop", "Laptop", symlinks=True, copy_function=shutil.copy2)
shutil.copytree("Backup/TestInbox", "TestInbox", symlinks=True, copy_function=shutil.copy2)
try:
shutil.copytree("Backup/Pad", "Pad", symlinks=True, copy_function=shutil.copy2)
except FileNotFoundError:
pass
elif options.print:
from sync_handshake import print_msg

Loading…
Cancel
Save