From 2b014528e62624210319011ee14a76eebfcea843 Mon Sep 17 00:00:00 2001 From: Edouard Tisserant Date: Wed, 7 Sep 2016 16:29:42 +0200 Subject: [PATCH] Now handles consumed messaged --- src/message_api.cc | 3 +- src/pEpmodule.cc | 5 ++- test/multipEp.py | 92 ++++++++++++++++++++++++++++++---------------- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/src/message_api.cc b/src/message_api.cc index bdec3df..d52faf4 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -51,9 +51,10 @@ namespace pEp { int rating = (int) _rating; int flags = (int) _flags; + bool consumed = status == PEP_MESSAGE_CONSUMED; Message dst = _dst ? Message(_dst) : Message(src); - return boost::python::make_tuple(dst, keylist, rating, flags); + return boost::python::make_tuple(dst, keylist, rating, consumed, flags); } int _color(int rating) diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 4e098c4..5e279af 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -32,6 +32,8 @@ namespace pEp { return; if (status >= 0x400 && status <= 0x4ff) return; + if (status == PEP_MESSAGE_CONSUMED) + return; if (status == PEP_OUT_OF_MEMORY) throw bad_alloc(); if (status == PEP_ILLEGAL_VALUE) @@ -249,13 +251,14 @@ BOOST_PYTHON_MODULE(pEp) " flags 1 is force encryption\n" ) .def("decrypt", &Message::decrypt, - "msg2, keys, rating, flags = msg1.decrypt()\n" + "msg2, keys, rating, consumed, flags = msg1.decrypt()\n" "\n" "decrypts a p≡p message and returns a tuple with data\n" "\n" " msg the decrypted p≡p message\n" " keys a list of keys being used\n" " rating the rating of the message as integer\n" + " consumed boolean denoting message is consumed by sync\n" " flags flags set while decryption (reserved)\n" ) .add_property("outgoing_rating", &Message::outgoing_rating, "rating outgoing message will have") diff --git a/test/multipEp.py b/test/multipEp.py index a1c8f02..4bc6d40 100644 --- a/test/multipEp.py +++ b/test/multipEp.py @@ -8,6 +8,8 @@ from collections import OrderedDict pEp = None handler = None own_addresses = [] +indent = 0 +i_name = "" def create_account(address, name): global own_addresses @@ -15,41 +17,56 @@ def create_account(address, name): pEp.myself(i) own_addresses.append(address) -def header(blah=None): +def printi(*args): + global indent + print(i_name + ">" * indent, *args) + +def printheader(blah=None): + global indent if blah is None: - return "-" * 80 + printi("-" * 80) + indent = indent - 1 else: - return ("-" * (39 - int(len(blah)/2)) + - " " + blah + " " + - "-" * (39 - len(blah) + int(len(blah)/2))) - + indent = indent + 1 + printi("-" * (39 - int(len(blah)/2)) + + " " + blah + " " + + "-" * (39 - len(blah) + int(len(blah)/2))) + +def printmsg(msg): + printi("from :", msg.from_) + printi("to :", msg.to) + printi("short :", msg.shortmsg) + printi("opt_fields :", msg.opt_fields) + lng = msg.longmsg.splitlines() + lngcut = lng[:20]+["[...]"] if len(lng)>20 else lng + pfx = "long : " + for l in lngcut : + printi(pfx + l) + pfx = " " + printi("attachments : ", msg.attachments) def pEp_instance_run(iname, conn, msgs_folders): - global pEp, handler, own_addresses + global pEp, handler, own_addresses, i_name + + i_name = iname pEp = importlib.import_module("pEp") class Handler(pEp.SyncMixIn): def messageToSend(self, msg): - print(header("SYNC MESSAGE from instance"+iname)) - print("from :", msg.from_) - print("to :", msg.to) - print("short :", msg.shortmsg) - print("long :", (msg.longmsg[:250]+" [...]" - if len(msg.longmsg)>250 - else msg.longmsg)) - print(msg.attachments) - print(header()) + printheader("SYNC MESSAGE to send") + printmsg(msg) + printheader() for rcpt in msg.to + msg.cc + msg.bcc: - # list inside dict from MP manager are not mutable, apparently. + # list inside dict from MP manager are not proxified. msgs = msgs_folders.get(rcpt.address,[]) msgs.append(str(msg)) msgs_folders[rcpt.address] = msgs def showHandshake(self, me, partner): - print(header("HANDSHAKE from instance"+iname)) - print("handshake needed between " + repr(me) + " and " + repr(partner)) - print(header()) + printheader("show HANDSHAKE dialog") + printi("handshake needed between " + repr(me) + " and " + repr(partner)) + printheader() # TODO: accept or reject handler = Handler() @@ -61,27 +78,38 @@ def pEp_instance_run(iname, conn, msgs_folders): func = order[0] - print(header("DECRYPT messages for instance "+iname)) + printheader("DECRYPT messages") # decrypt every non-consumed message for all instance accounts for own_address in own_addresses: msgs_for_me = msgs_folders[own_address] - for idx, msgstr in enumerate(msgs_for_me): + for msgstr in msgs_for_me: msg = pEp.incoming_message(msgstr) - decrypt_result = msg.decrypt() - # TODO get status instead of an exception when consumed... - #if decrypt_result == 0xff02: #PEP_MESSAGE_CONSUMED - # msgs_for_me.pop(idx) - print(header()) + printi("--- decrypt()") + printmsg(msg) + msg2, keys, rating, consumed, flags = msg.decrypt() + + if consumed: #PEP_MESSAGE_CONSUMED + printi("--- PEP_MESSAGE_CONSUMED") + # folder may have changed in the meantime, + # remove item directly from latest version of it. + folder = msgs_folders[own_address] + folder.remove(msgstr) + msgs_folders[own_address] = folder + else : + printi("->-") + printmsg(msg2) + printi("---") + printheader() res = None if func is not None: - print(header("Instance " + iname + " : function " + func.__name__)) + printheader("Executing function " + func.__name__) args, kwargs = order[1:] + [[], {}][len(order) - 1:] - print("args :", args) - print("kwargs :", kwargs) + printi("args :", args) + printi("kwargs :", kwargs) res = func(*args,**kwargs) - print(" -> ", res) - print(header()) + printi("function " + func.__name__ + " returned :", res) + printheader() conn.send(res)