From 2d74514dd0c88c489a1d95c0965812acddb2120e Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sun, 23 Feb 2020 02:21:51 +0100 Subject: [PATCH] add some incoming/outgoing message hooks --- python/src/deltachat/account.py | 13 ++++++++++++- python/src/deltachat/hookspec.py | 8 ++++++++ python/tests/test_account.py | 22 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/python/src/deltachat/account.py b/python/src/deltachat/account.py index 2be16a2ed..6089a5362 100644 --- a/python/src/deltachat/account.py +++ b/python/src/deltachat/account.py @@ -63,11 +63,22 @@ class Account(object): @hookspec.account_hookimpl def process_ffi_event(self, ffi_event): - if ffi_event.name == "DC_EVENT_CONFIGURE_PROGRESS": + name = ffi_event.name + if name == "DC_EVENT_CONFIGURE_PROGRESS": data1 = ffi_event.data1 if data1 == 0 or data1 == 1000: success = data1 == 1000 self._pm.hook.configure_completed(success=success) + elif name == "DC_EVENT_INCOMING_MSG": + msg = self.get_message_by_id(ffi_event.data2) + self._pm.hook.process_incoming_message(message=msg) + elif name == "DC_EVENT_MSGS_CHANGED": + if ffi_event.data2 != 0: + msg = self.get_message_by_id(ffi_event.data2) + self._pm.hook.process_incoming_message(message=msg) + elif name == "DC_EVENT_MSG_DELIVERED": + msg = self.get_message_by_id(ffi_event.data2) + self._pm.hook.process_message_delivered(message=msg) def add_account_plugin(self, plugin): """ add an account plugin whose hookimpls are called. """ diff --git a/python/src/deltachat/hookspec.py b/python/src/deltachat/hookspec.py index dc9bd028a..a697028f2 100644 --- a/python/src/deltachat/hookspec.py +++ b/python/src/deltachat/hookspec.py @@ -39,6 +39,14 @@ class PerAccount: def configure_completed(self, success): """ Called when a configure process completed. """ + @account_hookspec + def process_incoming_message(self, message): + """ Called on any incoming message (to deaddrop or chat). """ + + @account_hookspec + def process_message_delivered(self, message): + """ Called when an outgoing message has been delivered to SMTP. """ + @account_hookspec def after_shutdown(self): """ Called after the account has been shutdown. """ diff --git a/python/tests/test_account.py b/python/tests/test_account.py index b91b9078a..c279e0bd5 100644 --- a/python/tests/test_account.py +++ b/python/tests/test_account.py @@ -5,6 +5,7 @@ import queue import time from deltachat import const, Account from deltachat.message import Message +from deltachat.hookspec import account_hookimpl from datetime import datetime, timedelta from conftest import (wait_configuration_progress, wait_securejoin_inviter_progress) @@ -968,6 +969,23 @@ class TestOnlineAccount: ac1, ac2 = acfactory.get_two_online_accounts() chat = self.get_chat(ac1, ac2) + message_queue = queue.Queue() + + class InPlugin: + @account_hookimpl + def process_incoming_message(self, message): + message_queue.put(message) + + delivered = queue.Queue() + + class OutPlugin: + @account_hookimpl + def process_message_delivered(self, message): + delivered.put(message) + + ac1.add_account_plugin(OutPlugin()) + ac2.add_account_plugin(InPlugin()) + lp.sec("sending image message from ac1 to ac2") path = data.get_path("d.png") msg_out = chat.send_image(path) @@ -975,6 +993,8 @@ class TestOnlineAccount: assert ev.data1 == chat.id assert ev.data2 == msg_out.id assert msg_out.is_out_delivered() + m = delivered.get() + assert m == msg_out lp.sec("wait for ac2 to receive message") ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") @@ -983,6 +1003,8 @@ class TestOnlineAccount: assert msg_in.is_image() assert os.path.exists(msg_in.filename) assert os.stat(msg_in.filename).st_size == os.stat(path).st_size + m = message_queue.get() + assert m == msg_in def test_import_export_online_all(self, acfactory, tmpdir, lp): ac1 = acfactory.get_online_configuring_account()