From a1c82eaea60a54965456f2c7848904d145b9b331 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 19 Apr 2020 11:08:08 +0200 Subject: [PATCH] refine bot testing and ac_member* handling --- python/examples/group_tracking.py | 4 +++ python/examples/test_examples.py | 6 ++-- python/src/deltachat/account.py | 47 ++++++++---------------------- python/src/deltachat/message.py | 23 +++++++++++++++ python/src/deltachat/testplugin.py | 8 +++-- python/tests/test_account.py | 2 +- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/python/examples/group_tracking.py b/python/examples/group_tracking.py index dc66c1f2e..e3962c868 100644 --- a/python/examples/group_tracking.py +++ b/python/examples/group_tracking.py @@ -21,6 +21,10 @@ class GroupTrackingPlugin: def ac_configure_completed(self, success): print("*** ac_configure_completed:", success) + @account_hookimpl + def ac_chat_modified(self, chat): + print("*** ac_chat_modified:", chat.id, chat.get_name()) + @account_hookimpl def ac_member_added(self, chat, contact, sender): print("*** ac_member_added {} to chat {} from {}".format( diff --git a/python/examples/test_examples.py b/python/examples/test_examples.py index d3a16fe2d..5e6d363fe 100644 --- a/python/examples/test_examples.py +++ b/python/examples/test_examples.py @@ -33,12 +33,12 @@ def test_echo_quit_plugin(acfactory): def test_group_tracking_plugin(acfactory, lp): lp.sec("creating one group-tracking bot and two temp accounts") - botproc = acfactory.run_bot_process(group_tracking) + botproc = acfactory.run_bot_process(group_tracking, ffi=False) ac1, ac2 = acfactory.get_two_online_accounts(quiet=True) botproc.fnmatch_lines(""" - *ac_configure_completed: True* + *ac_configure_completed* """) ac1.add_account_plugin(FFIEventLogger(ac1, "ac1")) ac2.add_account_plugin(FFIEventLogger(ac2, "ac2")) @@ -50,7 +50,7 @@ def test_group_tracking_plugin(acfactory, lp): ch.send_text("hello") botproc.fnmatch_lines(""" - *ac_chat_modified* + *ac_chat_modified*bot test group* """.format(ac1.get_config("addr"))) lp.sec("adding third member {}".format(ac2.get_config("addr"))) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index da3588c9d..514884207 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -13,7 +13,7 @@ from . import const from .capi import ffi, lib from .cutil import as_dc_charpointer, from_dc_charpointer, iter_array, DCLot from .chat import Chat -from .message import Message +from .message import Message, map_system_message from .contact import Contact from .tracker import ImexTracker from . import hookspec, iothreads @@ -65,8 +65,7 @@ class Account(object): @hookspec.account_hookimpl def ac_process_ffi_event(self, ffi_event): - name, kwargs = self._map_ffi_event(ffi_event) - if name is not None: + for name, kwargs in self._map_ffi_event(ffi_event): ev = HookEvent(self, name=name, kwargs=kwargs) self._hook_event_queue.put(ev) @@ -622,48 +621,26 @@ class Account(object): data1 = ffi_event.data1 if data1 == 0 or data1 == 1000: success = data1 == 1000 - return "ac_configure_completed", dict(success=success) + yield "ac_configure_completed", dict(success=success) elif name == "DC_EVENT_INCOMING_MSG": msg = self.get_message_by_id(ffi_event.data2) - return self._map_incoming(msg) + yield map_system_message(msg) or ("ac_incoming_message", dict(message=msg)) elif name == "DC_EVENT_MSGS_CHANGED": if ffi_event.data2 != 0: msg = self.get_message_by_id(ffi_event.data2) if msg.is_outgoing(): - evname, kwargs = self._map_incoming(msg) - if evname.startswith("ac_member"): - return evname, kwargs - if msg.is_in_fresh(): - return self._map_incoming(msg) + res = map_system_message(msg) + if res and res[0].startswith("ac_member"): + yield res + yield "ac_outgoing_message", dict(message=msg) + elif msg.is_in_fresh(): + yield map_system_message(msg) or ("ac_incoming_message", dict(message=msg)) elif name == "DC_EVENT_MSG_DELIVERED": msg = self.get_message_by_id(ffi_event.data2) - return "ac_message_delivered", dict(message=msg) + yield "ac_message_delivered", dict(message=msg) elif name == "DC_EVENT_CHAT_MODIFIED": chat = self.get_chat_by_id(ffi_event.data1) - return "ac_chat_modified", dict(chat=chat) - return None, {} - - def _map_incoming(self, msg): - if msg.is_system_message(): - res = parse_system_add_remove(msg.text) - if res: - contact = msg.account.get_contact_by_addr(res[1]) - if contact: - d = dict(chat=msg.chat, contact=contact, sender=msg.get_sender_contact()) - return "ac_member_" + res[0], d - return "ac_incoming_message", dict(message=msg) - - -def parse_system_add_remove(text): - # Member Me (x@y) removed by a@b. - # Member x@y removed by a@b - text = text.lower() - parts = text.split() - if parts[0] == "member": - if parts[2] in ("removed", "added"): - return parts[2], parts[1] - if parts[3] in ("removed", "added"): - return parts[3], parts[2].strip("()") + yield "ac_chat_modified", dict(chat=chat) def _destroy_dc_context(dc_context, dc_context_unref=lib.dc_context_unref): diff --git a/python/src/deltachat/message.py b/python/src/deltachat/message.py index d79ca2523..907ffe4c2 100644 --- a/python/src/deltachat/message.py +++ b/python/src/deltachat/message.py @@ -320,3 +320,26 @@ def get_viewtype_code_from_name(view_type_name): return code raise ValueError("message typecode not found for {!r}, " "available {!r}".format(view_type_name, list(_view_type_mapping.values()))) + + +# some helper code for turning system messages into hook events +def map_system_message(msg): + if msg.is_system_message(): + res = parse_system_add_remove(msg.text) + if res: + contact = msg.account.get_contact_by_addr(res[1]) + if contact: + d = dict(chat=msg.chat, contact=contact, sender=msg.get_sender_contact()) + return "ac_member_" + res[0], d + + +def parse_system_add_remove(text): + # Member Me (x@y) removed by a@b. + # Member x@y removed by a@b + text = text.lower() + parts = text.split() + if parts[0] == "member": + if parts[2] in ("removed", "added"): + return parts[2], parts[1] + if parts[3] in ("removed", "added"): + return parts[3], parts[2].strip("()") diff --git a/python/src/deltachat/testplugin.py b/python/src/deltachat/testplugin.py index c3e92f0eb..7719ff157 100644 --- a/python/src/deltachat/testplugin.py +++ b/python/src/deltachat/testplugin.py @@ -280,26 +280,28 @@ def acfactory(pytestconfig, tmpdir, request, session_liveconfig, data): ac.start() return ac - def run_bot_process(self, module): + def run_bot_process(self, module, ffi=True): fn = module.__file__ bot_ac, bot_cfg = self.get_online_config() args = [ sys.executable, + "-u", fn, - "--show-ffi", "--email", bot_cfg["addr"], "--password", bot_cfg["mail_pw"], bot_ac.db_path, ] + if ffi: + args.insert(-1, "--show-ffi") print("$", " ".join(args)) popen = subprocess.Popen( args=args, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, # combine stdout/stderr in one stream - bufsize=1, # line buffering + bufsize=0, # line buffering close_fds=True, # close all FDs other than 0/1/2 universal_newlines=True # give back text ) diff --git a/python/tests/test_account.py b/python/tests/test_account.py index bcf1ad20a..7563f5bf5 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -16,7 +16,7 @@ from conftest import (wait_configuration_progress, ("Member tmp1@x.org added by tmp2@x.org.", ("added", "tmp1@x.org")), ]) def test_parse_system_add_remove(msgtext, res): - from deltachat.account import parse_system_add_remove + from deltachat.message import parse_system_add_remove out = parse_system_add_remove(msgtext) assert out == res