Browse Source

imap for sync

PYADPT-55
David 6 years ago
parent
commit
4e3e195043
  1. 78
      test/miniimap.py
  2. 49
      test/sync_handshake.py
  3. 18
      test/sync_test.py

78
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()

49
test/sync_handshake.py

@ -23,6 +23,9 @@ import sys
import re import re
import pEp import pEp
import minimail import minimail
import miniimap
import sync_settings as settings
from datetime import datetime from datetime import datetime
@ -72,8 +75,12 @@ def print_msg(p):
else: else:
raise TypeError("print_msg(): pathlib.Path and pEp.Message supported, but " raise TypeError("print_msg(): pathlib.Path and pEp.Message supported, but "
+ str(type(p)) + " delivered") + str(type(p)) + " delivered")
m = False
m = re.search("<keysync>(.*)</keysync>", msg.opt_fields["pEp.sync"].replace("\n", " ")) if msg.opt_fields.get("pEp.sync"):
m = re.search("<keysync>(.*)</keysync>", msg.opt_fields["pEp.sync"].replace("\n", " "))
if m: if m:
if etree: if etree:
tree = objectify.fromstring(m.group(1).replace("\r", "")) tree = objectify.fromstring(m.group(1).replace("\r", ""))
@ -82,6 +89,7 @@ def print_msg(p):
text = m.group(1).replace("\r", "").strip() text = m.group(1).replace("\r", "").strip()
while text.count(" "): while text.count(" "):
text = text.replace(" ", " ") text = text.replace(" ", " ")
print('-- BEACON --')
print(text) print(text)
@ -95,6 +103,25 @@ def messageToSend(msg):
msg.opt_fields = { "pEp.sync": text } msg.opt_fields = { "pEp.sync": text }
minimail.send(inbox, msg, device_name) 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 = "<!-- sending from " + device_name + " -->\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 = "<!-- sending from " + device_name + " -->\n" + m.attachments[0].decode()
output(text)
msg.opt_fields = { "pEp.sync": text }
return msg
class UserInterface(pEp.UserInterface): class UserInterface(pEp.UserInterface):
def notifyHandshake(self, me, partner, signal): def notifyHandshake(self, me, partner, signal):
@ -125,7 +152,7 @@ def shutdown_sync():
pEp.shutdown_sync() pEp.shutdown_sync()
def run(name, color=None): def run(name, color=None, imap=False):
global device_name global device_name
device_name = name device_name = name
@ -133,9 +160,16 @@ def run(name, color=None):
global output global output
output = lambda x: print(colored(x, color)) output = lambda x: print(colored(x, color))
me = pEp.Identity("alice@peptest.ch", name + " of Alice Neuman", name) if imap:
pEp.myself(me) me = pEp.Identity(settings.IMAP_EMAIL, name + " of " + settings.IMAP_USER, name)
pEp.messageToSend = messageToSend 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: if multithreaded:
from threading import Thread from threading import Thread
@ -153,7 +187,10 @@ def run(name, color=None):
try: try:
while not the_end: 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: for n, m in l:
msg = pEp.Message(m) msg = pEp.Message(m)
output("*** Reading") output("*** Reading")

18
test/sync_test.py

@ -19,8 +19,9 @@ import sys
import shutil import shutil
import pathlib 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(); cwd = os.getcwd();
os.chdir(path) os.chdir(path)
os.environ["HOME"] = os.getcwd() 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.end_on = end_on
sync_handshake.multithreaded = mt sync_handshake.multithreaded = mt
sync_handshake.run(path, color) sync_handshake.run(path, color, imap)
os.chdir(cwd) os.chdir(cwd)
@ -96,6 +97,9 @@ if __name__ == "__main__":
optParser.add_option("-j", "--multi-threaded", action="store_true", optParser.add_option("-j", "--multi-threaded", action="store_true",
dest="multithreaded", dest="multithreaded",
help="use multithreaded instead of single threaded implementation") 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() options, args = optParser.parse_args()
if options.cleanall: if options.cleanall:
@ -106,6 +110,10 @@ if __name__ == "__main__":
rmrf("Phone") rmrf("Phone")
rmrf("Laptop") rmrf("Laptop")
rmrf("Pad") rmrf("Pad")
try:
miniimap.clean_inbox()
except:
pass
if options.cleanall: if options.cleanall:
rmrf("Backup") rmrf("Backup")
@ -160,12 +168,12 @@ if __name__ == "__main__":
except TypeError: except TypeError:
end_on = (end_on,) end_on = (end_on,)
Phone = Process(target=test_for, args=("Phone", "red", 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, Laptop = Process(target=test_for, args=("Laptop", "green", end_on,
options.multithreaded)) options.multithreaded, options.imap))
if options.third: if options.third:
Pad = Process(target=test_for, args=("Pad", "cyan", end_on, Pad = Process(target=test_for, args=("Pad", "cyan", end_on,
options.multithreaded)) options.multithreaded, options.imap))
Phone.start() Phone.start()
Laptop.start() Laptop.start()

Loading…
Cancel
Save