From 3f8825ae7a84435b31b985f69c0e3c0a2130d044 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 20 Aug 2019 10:21:30 +0200 Subject: [PATCH 1/8] first attempt adding imap to sync --- .hgignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.hgignore b/.hgignore index b9c430c..d07e17b 100644 --- a/.hgignore +++ b/.hgignore @@ -18,4 +18,7 @@ test/Library test/Phone test/TestInbox test/lib - +test/sync_settings.py +venv +launch.json +settings.json \ No newline at end of file From 4e3e195043eab8c065cb5a48964b1e4fe4624b7a Mon Sep 17 00:00:00 2001 From: David Date: Tue, 20 Aug 2019 10:32:28 +0200 Subject: [PATCH 2/8] imap for sync --- test/miniimap.py | 78 ++++++++++++++++++++++++++++++++++++++++++ test/sync_handshake.py | 49 ++++++++++++++++++++++---- test/sync_test.py | 18 +++++++--- 3 files changed, 134 insertions(+), 11 deletions(-) create mode 100644 test/miniimap.py diff --git a/test/miniimap.py b/test/miniimap.py new file mode 100644 index 0000000..3a1f955 --- /dev/null +++ b/test/miniimap.py @@ -0,0 +1,78 @@ +import imaplib +import pprint +import email.message +import email.charset +import time +import sync_settings as settings + + +def connect(): + "connect to the IMAP server Inbox" + server = imaplib.IMAP4_SSL(settings.IMAP_HOST) + server.login(settings.IMAP_USER, settings.IMAP_PWD) + server.select('Inbox') + + return server + +def pEpMessage_to_imap(msg): + "convert pEpMessage to python imap formatted string" + new_message = email.message.Message() + new_message["From"] = str(msg.from_).replace("\n", " ") + new_message["To"] = str(msg.to[0]) + new_message["Subject"] = msg.shortmsg + if msg.opt_fields: + for field, value in msg.opt_fields.items(): + new_message[field] = str(value).replace("\n", " ") + new_message.set_payload(msg.longmsg) + + new_message.set_charset(email.charset.Charset("utf-8")) + encoded_message = str(new_message).encode("utf-8") + + return encoded_message + +def bytesmessage_to_string(msg): + "converts bytes-like message to string" + msg = msg.decode("UTF-8").rstrip() + return msg + +def send(inbox, msg): + "send msg to inbox in MIME format" + server = connect() + msg = pEpMessage_to_imap(msg) + print('******** sent msg *******') + print(msg) + server.append('Inbox', '', imaplib.Time2Internaldate(time.time()), msg) + server.close() + + +def recv_all(inbox, start_time): + """receive a list of new MIME messages from inbox, which are newer than the + start_time""" + + server = connect() + r = [] + + tmp, data = server.search(None, 'ALL') + # tmp, data = server.search(None, 'SENTSINCE {0}'.format(start_time.strftime("%d-%b-%Y %H:%M%S"))) + + for num in data[0].split(): + tmp, data = server.fetch(num, '(RFC822)') + msg = bytesmessage_to_string(data[0][1]) + r.append((num, msg)) + print('******** recieved msg *******') + print(msg) + + server.close() + + return r + + +def clean_inbox(): + print('clean IMAP') + server = connect() + typ, data = server.search(None, 'ALL') + for num in data[0].split(): + server.store(num, '+FLAGS', '\\Deleted') + server.expunge() + + diff --git a/test/sync_handshake.py b/test/sync_handshake.py index a3a993a..fc1dddb 100644 --- a/test/sync_handshake.py +++ b/test/sync_handshake.py @@ -23,6 +23,9 @@ import sys import re import pEp import minimail +import miniimap + +import sync_settings as settings from datetime import datetime @@ -72,8 +75,12 @@ def print_msg(p): else: raise TypeError("print_msg(): pathlib.Path and pEp.Message supported, but " + str(type(p)) + " delivered") + + m = False - m = re.search("(.*)", msg.opt_fields["pEp.sync"].replace("\n", " ")) + if msg.opt_fields.get("pEp.sync"): + m = re.search("(.*)", msg.opt_fields["pEp.sync"].replace("\n", " ")) + if m: if etree: tree = objectify.fromstring(m.group(1).replace("\r", "")) @@ -82,6 +89,7 @@ def print_msg(p): text = m.group(1).replace("\r", "").strip() while text.count(" "): text = text.replace(" ", " ") + print('-- BEACON --') print(text) @@ -95,6 +103,25 @@ def messageToSend(msg): msg.opt_fields = { "pEp.sync": text } minimail.send(inbox, msg, device_name) +def messageImapToSend(msg): + if msg.enc_format: + m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) + else: + m = msg + text = "\n" + m.attachments[0].decode() + output(text) + msg.opt_fields = { "pEp.sync": text } + miniimap.send(inbox, msg) + +def getMessageToSend(msg): + if msg.enc_format: + m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) + else: + m = msg + text = "\n" + m.attachments[0].decode() + output(text) + msg.opt_fields = { "pEp.sync": text } + return msg class UserInterface(pEp.UserInterface): def notifyHandshake(self, me, partner, signal): @@ -125,7 +152,7 @@ def shutdown_sync(): pEp.shutdown_sync() -def run(name, color=None): +def run(name, color=None, imap=False): global device_name device_name = name @@ -133,9 +160,16 @@ def run(name, color=None): global output output = lambda x: print(colored(x, color)) - me = pEp.Identity("alice@peptest.ch", name + " of Alice Neuman", name) - pEp.myself(me) - pEp.messageToSend = messageToSend + if imap: + me = pEp.Identity(settings.IMAP_EMAIL, name + " of " + settings.IMAP_USER, name) + pEp.myself(me) + pEp.messageToSend = messageImapToSend + else: + me = pEp.Identity("alice@peptest.ch", name + " of Alice Neuman", name) + pEp.myself(me) + pEp.messageToSend = messageToSend + + if multithreaded: from threading import Thread @@ -153,7 +187,10 @@ def run(name, color=None): try: while not the_end: - l = minimail.recv_all(inbox, name) + if imap: + l = miniimap.recv_all(inbox, 'start_time') + else: + l = minimail.recv_all(inbox, name) for n, m in l: msg = pEp.Message(m) output("*** Reading") diff --git a/test/sync_test.py b/test/sync_test.py index 1849305..d41bd27 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -19,8 +19,9 @@ import sys import shutil import pathlib +import miniimap -def test_for(path, color=None, end_on=None, mt=False): +def test_for(path, color=None, end_on=None, mt=False, imap=False): cwd = os.getcwd(); os.chdir(path) os.environ["HOME"] = os.getcwd() @@ -31,7 +32,7 @@ def test_for(path, color=None, end_on=None, mt=False): sync_handshake.end_on = end_on sync_handshake.multithreaded = mt - sync_handshake.run(path, color) + sync_handshake.run(path, color, imap) os.chdir(cwd) @@ -96,6 +97,9 @@ if __name__ == "__main__": optParser.add_option("-j", "--multi-threaded", action="store_true", dest="multithreaded", help="use multithreaded instead of single threaded implementation") + optParser.add_option("-i", "--imap", action="store_true", + dest="imap", + help="use imap instead of minimail") options, args = optParser.parse_args() if options.cleanall: @@ -106,6 +110,10 @@ if __name__ == "__main__": rmrf("Phone") rmrf("Laptop") rmrf("Pad") + try: + miniimap.clean_inbox() + except: + pass if options.cleanall: rmrf("Backup") @@ -160,12 +168,12 @@ if __name__ == "__main__": except TypeError: end_on = (end_on,) Phone = Process(target=test_for, args=("Phone", "red", end_on, - options.multithreaded)) + options.multithreaded, options.imap)) Laptop = Process(target=test_for, args=("Laptop", "green", end_on, - options.multithreaded)) + options.multithreaded, options.imap)) if options.third: Pad = Process(target=test_for, args=("Pad", "cyan", end_on, - options.multithreaded)) + options.multithreaded, options.imap)) Phone.start() Laptop.start() From 9236031b1684122e83c275967fe527dc6f21b209 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 20 Aug 2019 11:29:05 +0200 Subject: [PATCH 3/8] update readme with IMAP settings instructions --- test/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/README.md b/test/README.md index 33bd014..5f9fc30 100644 --- a/test/README.md +++ b/test/README.md @@ -21,6 +21,15 @@ $ HOME=$PWD lldb python3 -- ../sync_handshake.py -e Phone Then this side is doing a replay in the debugger. Using touch to set a different timestamp on the marker will only partly replay. +In order to work with IMAP you need to create a sync_settings.py file with the +following variables: + +IMAP_HOST = 'domain.ch' +IMAP_PORT = '993' +IMAP_USER = 'your_username' +IMAP_PWD = 'password' +IMAP_EMAIL = 'user@domain.ch' + = Hint = installing termcolor and lxml will beautify the output From 3b073215015185b9acb25d8654a4fa136500abb1 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 18 Nov 2019 12:39:58 +0100 Subject: [PATCH 4/8] added imap module for sync_test and sync_handshake PYADPT-47 --- test/README.md | 2 +- test/imap_settings.py | 5 ++++ test/miniimap.py | 56 +++++++++++++++++------------------------- test/sync_handshake.py | 18 +++++++++++--- test/sync_test.py | 39 +++++++++++++++-------------- 5 files changed, 64 insertions(+), 56 deletions(-) create mode 100644 test/imap_settings.py diff --git a/test/README.md b/test/README.md index 5f9fc30..270c9bc 100644 --- a/test/README.md +++ b/test/README.md @@ -21,7 +21,7 @@ $ HOME=$PWD lldb python3 -- ../sync_handshake.py -e Phone Then this side is doing a replay in the debugger. Using touch to set a different timestamp on the marker will only partly replay. -In order to work with IMAP you need to create a sync_settings.py file with the +In order to work with IMAP you need to create a imap_settings.py file with the following variables: IMAP_HOST = 'domain.ch' diff --git a/test/imap_settings.py b/test/imap_settings.py new file mode 100644 index 0000000..7c288c7 --- /dev/null +++ b/test/imap_settings.py @@ -0,0 +1,5 @@ +IMAP_HOST = 'exchange.peptest.ch' +IMAP_PORT = '993' +IMAP_USER = r'testnet\test046' +IMAP_PWD = 'pEpdichauf5' +IMAP_EMAIL = 'test046@exchange.peptest.ch' \ No newline at end of file diff --git a/test/miniimap.py b/test/miniimap.py index 3a1f955..c33430e 100644 --- a/test/miniimap.py +++ b/test/miniimap.py @@ -3,33 +3,20 @@ import pprint import email.message import email.charset import time -import sync_settings as settings +import os +import imap_settings as settings def connect(): "connect to the IMAP server Inbox" server = imaplib.IMAP4_SSL(settings.IMAP_HOST) server.login(settings.IMAP_USER, settings.IMAP_PWD) - server.select('Inbox') + tmp, data = server.select('Inbox') + if os.environ.get('NUMMESSAGES') is None: + os.environ["NUMMESSAGES"] = data[0].decode("UTF-8") return server -def pEpMessage_to_imap(msg): - "convert pEpMessage to python imap formatted string" - new_message = email.message.Message() - new_message["From"] = str(msg.from_).replace("\n", " ") - new_message["To"] = str(msg.to[0]) - new_message["Subject"] = msg.shortmsg - if msg.opt_fields: - for field, value in msg.opt_fields.items(): - new_message[field] = str(value).replace("\n", " ") - new_message.set_payload(msg.longmsg) - - new_message.set_charset(email.charset.Charset("utf-8")) - encoded_message = str(new_message).encode("utf-8") - - return encoded_message - def bytesmessage_to_string(msg): "converts bytes-like message to string" msg = msg.decode("UTF-8").rstrip() @@ -37,30 +24,30 @@ def bytesmessage_to_string(msg): def send(inbox, msg): "send msg to inbox in MIME format" + print("send imap") + server = connect() - msg = pEpMessage_to_imap(msg) - print('******** sent msg *******') - print(msg) - server.append('Inbox', '', imaplib.Time2Internaldate(time.time()), msg) + tmp, data = server.append('Inbox', '', imaplib.Time2Internaldate(time.time()), str(msg).encode("UTF-8")) server.close() -def recv_all(inbox, start_time): - """receive a list of new MIME messages from inbox, which are newer than the - start_time""" +def recv_all(inbox): + """receive a list of all MIME messages from inbox newer than the last message when first connected""" + print("recieve imap") server = connect() r = [] tmp, data = server.search(None, 'ALL') - # tmp, data = server.search(None, 'SENTSINCE {0}'.format(start_time.strftime("%d-%b-%Y %H:%M%S"))) + + oldermsgid = os.environ.get('NUMMESSAGES') for num in data[0].split(): - tmp, data = server.fetch(num, '(RFC822)') - msg = bytesmessage_to_string(data[0][1]) - r.append((num, msg)) - print('******** recieved msg *******') - print(msg) + if int(num) >= int(oldermsgid): + tmp, data = server.fetch(num, '(RFC822)') + msg = bytesmessage_to_string(data[0][1]) + r.append((num, msg)) + os.environ["NUMMESSAGES"] = num.decode("UTF-8") server.close() @@ -68,11 +55,14 @@ def recv_all(inbox, start_time): def clean_inbox(): - print('clean IMAP') + """clean all messsages from IMAP inbox""" + print('cleaning IMAP...') server = connect() typ, data = server.search(None, 'ALL') for num in data[0].split(): server.store(num, '+FLAGS', '\\Deleted') - server.expunge() + server.expunge() + server.close() + print('IMAP inbox empty.') diff --git a/test/sync_handshake.py b/test/sync_handshake.py index 96c7813..26279b4 100644 --- a/test/sync_handshake.py +++ b/test/sync_handshake.py @@ -25,7 +25,7 @@ import pEp import minimail import miniimap -import sync_settings as settings +import imap_settings from datetime import datetime @@ -105,6 +105,7 @@ 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: @@ -126,6 +127,7 @@ 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)) @@ -163,6 +165,8 @@ 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)) @@ -174,7 +178,8 @@ def run(name, color=None, imap=False): pEp.debug_color(36) if imap: - me = pEp.Identity(settings.IMAP_EMAIL, name + " of " + settings.IMAP_USER, name) + 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 else: @@ -195,13 +200,14 @@ 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, 'start_time') + l = miniimap.recv_all(inbox) else: l = minimail.recv_all(inbox, name) for n, m in l: @@ -237,6 +243,10 @@ if __name__=="__main__": help="use multithreaded instead of single threaded implementation") optParser.add_option("-n", "--noend", action="store_true", dest="noend", help="do not end") + optParser.add_option("-i", "--imap", action="store_true", + dest="imap", + help="use imap instead of minimail") + options, args = optParser.parse_args() if not options.exec_for: @@ -252,5 +262,5 @@ if __name__=="__main__": end_on = (None,) multithreaded = options.multithreaded - run(options.exec_for, options.color) + run(options.exec_for, options.color, options.imap) diff --git a/test/sync_test.py b/test/sync_test.py index d7bdf4c..fe056e9 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -108,24 +108,27 @@ if __name__ == "__main__": options.clean = True if options.clean: - rmrf("TestInbox") - rmrf("Phone") - rmrf("Laptop") - rmrf("Pad") - try: - miniimap.clean_inbox() - except: - pass - - if options.cleanall: - rmrf("Backup") - if options.setup_only: - os.makedirs("TestInbox", exist_ok=True) - setup("Phone") - setup("Laptop") - if options.third: - setup("Pad") + if options.imap: + try: + miniimap.clean_inbox() + except: + pass + else: + rmrf("TestInbox") + rmrf("Phone") + rmrf("Laptop") + rmrf("Pad") + + if options.cleanall: + rmrf("Backup") + + if options.setup_only: + os.makedirs("TestInbox", exist_ok=True) + setup("Phone") + setup("Laptop") + if options.third: + setup("Pad") elif options.backup: @@ -160,7 +163,7 @@ if __name__ == "__main__": l.sort(key=(lambda p: p.stat().st_mtime)) for p in l: print_msg(p) - + else: from multiprocessing import Process From f4e755af0c58b7f0591dedf7cd67a538ef8fa0a5 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 18 Nov 2019 12:52:56 +0100 Subject: [PATCH 5/8] remove settings file --- .hgignore | 2 +- test/imap_settings.py | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 test/imap_settings.py diff --git a/.hgignore b/.hgignore index d07e17b..3333b2e 100644 --- a/.hgignore +++ b/.hgignore @@ -18,7 +18,7 @@ test/Library test/Phone test/TestInbox test/lib -test/sync_settings.py +test/imap_settings.py venv launch.json settings.json \ No newline at end of file diff --git a/test/imap_settings.py b/test/imap_settings.py deleted file mode 100644 index 7c288c7..0000000 --- a/test/imap_settings.py +++ /dev/null @@ -1,5 +0,0 @@ -IMAP_HOST = 'exchange.peptest.ch' -IMAP_PORT = '993' -IMAP_USER = r'testnet\test046' -IMAP_PWD = 'pEpdichauf5' -IMAP_EMAIL = 'test046@exchange.peptest.ch' \ No newline at end of file From 2cc27ebcbf373ab116187427b8db94512dfe13c4 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 20 Nov 2019 09:28:54 +0100 Subject: [PATCH 6/8] Add backup and restore for IMAP + fix bug + clean prints - PYADPT-47 --- .hgignore | 1 + test/miniimap.py | 41 +++++++++++++++++++++++++------- test/sync_handshake.py | 10 ++------ test/sync_test.py | 53 ++++++++++++++++++++++++++++-------------- 4 files changed, 72 insertions(+), 33 deletions(-) diff --git a/.hgignore b/.hgignore index 3333b2e..67d8ab3 100644 --- a/.hgignore +++ b/.hgignore @@ -17,6 +17,7 @@ test/Laptop test/Library test/Phone test/TestInbox +test/Backup test/lib test/imap_settings.py venv diff --git a/test/miniimap.py b/test/miniimap.py index c33430e..4ca9049 100644 --- a/test/miniimap.py +++ b/test/miniimap.py @@ -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() diff --git a/test/sync_handshake.py b/test/sync_handshake.py index 26279b4..50d7e38 100644 --- a/test/sync_handshake.py +++ b/test/sync_handshake.py @@ -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 = "\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: diff --git a/test/sync_test.py b/test/sync_test.py index fe056e9..7ce84a0 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -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 From 65a573ad6a1683383b3f72ceb92fd06372d4b14a Mon Sep 17 00:00:00 2001 From: David Date: Thu, 21 Nov 2019 12:33:05 +0100 Subject: [PATCH 7/8] handle correctly non-bytes messages - PYADAPT52 --- test/miniimap.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/miniimap.py b/test/miniimap.py index 4ca9049..b34b294 100644 --- a/test/miniimap.py +++ b/test/miniimap.py @@ -21,8 +21,11 @@ def connect(): def bytesmessage_to_string(msg): "converts bytes-like message to string" - msg = msg.decode("UTF-8").rstrip() - return msg + if type(msg) is bytes: + msg = msg.decode("UTF-8").rstrip() + return msg + else: + return str(msg) def send(inbox, msg): "send msg to inbox in MIME format" From 09abd3e5f2f0ef3780ba789b6fc3971ca7215bde Mon Sep 17 00:00:00 2001 From: David Date: Tue, 26 Nov 2019 14:25:08 +0100 Subject: [PATCH 8/8] add own-identities to imap sync_handsjake --- test/sync_handshake.py | 47 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/test/sync_handshake.py b/test/sync_handshake.py index 50d7e38..42f5720 100644 --- a/test/sync_handshake.py +++ b/test/sync_handshake.py @@ -77,11 +77,7 @@ def print_msg(p): raise TypeError("print_msg(): pathlib.Path and pEp.Message supported, but " + str(type(p)) + " delivered") - m = False - - if msg.opt_fields.get("pEp.sync"): - m = re.search("(.*)", msg.opt_fields["pEp.sync"].replace("\n", " ")) - + m = re.search("(.*)", msg.opt_fields["pEp.sync"].replace("\n", " ")) if m: if etree: tree = objectify.fromstring(m.group(1).replace("\r", "")) @@ -90,31 +86,18 @@ def print_msg(p): text = m.group(1).replace("\r", "").strip() while text.count(" "): text = text.replace(" ", " ") - print('-- BEACON --') print(text) def messageToSend(msg): - if msg.enc_format: - m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) - else: - m = msg - text = "\n" + m.attachments[0].decode() - output(text) - msg.opt_fields = { "pEp.sync": text } + msg = add_debug_info(msg) minimail.send(inbox, msg, device_name) def messageImapToSend(msg): - if msg.enc_format: - m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) - else: - m = msg - text = "\n" + m.attachments[0].decode() - output(text) - msg.opt_fields = { "pEp.sync": text } + msg = add_debug_info(msg) miniimap.send('Inbox', msg) -def getMessageToSend(msg): +def add_debug_info(msg): if msg.enc_format: m, keys, rating, flags = msg.decrypt(DONT_TRIGGER_SYNC) else: @@ -124,6 +107,7 @@ def getMessageToSend(msg): msg.opt_fields = { "pEp.sync": text } return msg + class UserInterface(pEp.UserInterface): def notifyHandshake(self, me, partner, signal): print(colored(str(signal), "yellow"), end=" ") @@ -159,7 +143,7 @@ def shutdown_sync(): pEp.shutdown_sync() -def run(name, color=None, imap=False): +def run(name, color=None, imap=False, own_ident=1): global device_name device_name = name @@ -180,9 +164,16 @@ def run(name, color=None, imap=False): else: me = pEp.Identity("alice@peptest.ch", name + " of Alice Neuman", name) pEp.myself(me) - pEp.messageToSend = messageToSend - + if own_ident >= 2: + me2 = pEp.Identity("alice@pep.security", name + " of Alice Neuman", name) + pEp.myself(me2) + + if own_ident == 3: + me3 = pEp.Identity("alice@pep.foundation", name + " of Alice Neuman", name) + pEp.myself(me3) + + pEp.messageToSend = messageToSend if multithreaded: from threading import Thread @@ -240,12 +231,17 @@ if __name__=="__main__": optParser.add_option("-i", "--imap", action="store_true", dest="imap", help="use imap instead of minimail") + optParser.add_option("-o", "--own-identities", type="int", dest="own_ident", + help="simulate having OWN_IDENT own identities (1 to 3)", default=1) options, args = optParser.parse_args() if not options.exec_for: options.exec_for = os.path.basename(os.getcwd()) + if options.own_ident < 1 or options.own_ident > 3: + raise ValueError("illegal number of own identities (allowed are 1 to 3)") + if options.notifications: end_on = eval(options.notifications) try: None in end_on @@ -255,6 +251,9 @@ if __name__=="__main__": if options.noend: end_on = (None,) + if options.imap and options.own_ident >1: + raise ValueError("Multiple own identities not supported for imap mode") + multithreaded = options.multithreaded run(options.exec_for, options.color, options.imap)