add some incoming/outgoing message hooks

This commit is contained in:
holger krekel
2020-02-23 02:21:51 +01:00
parent 84012e760e
commit 2d74514dd0
3 changed files with 42 additions and 1 deletions

View File

@@ -63,11 +63,22 @@ class Account(object):
@hookspec.account_hookimpl @hookspec.account_hookimpl
def process_ffi_event(self, ffi_event): 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 data1 = ffi_event.data1
if data1 == 0 or data1 == 1000: if data1 == 0 or data1 == 1000:
success = data1 == 1000 success = data1 == 1000
self._pm.hook.configure_completed(success=success) 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): def add_account_plugin(self, plugin):
""" add an account plugin whose hookimpls are called. """ """ add an account plugin whose hookimpls are called. """

View File

@@ -39,6 +39,14 @@ class PerAccount:
def configure_completed(self, success): def configure_completed(self, success):
""" Called when a configure process completed. """ """ 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 @account_hookspec
def after_shutdown(self): def after_shutdown(self):
""" Called after the account has been shutdown. """ """ Called after the account has been shutdown. """

View File

@@ -5,6 +5,7 @@ import queue
import time import time
from deltachat import const, Account from deltachat import const, Account
from deltachat.message import Message from deltachat.message import Message
from deltachat.hookspec import account_hookimpl
from datetime import datetime, timedelta from datetime import datetime, timedelta
from conftest import (wait_configuration_progress, from conftest import (wait_configuration_progress,
wait_securejoin_inviter_progress) wait_securejoin_inviter_progress)
@@ -968,6 +969,23 @@ class TestOnlineAccount:
ac1, ac2 = acfactory.get_two_online_accounts() ac1, ac2 = acfactory.get_two_online_accounts()
chat = self.get_chat(ac1, ac2) 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") lp.sec("sending image message from ac1 to ac2")
path = data.get_path("d.png") path = data.get_path("d.png")
msg_out = chat.send_image(path) msg_out = chat.send_image(path)
@@ -975,6 +993,8 @@ class TestOnlineAccount:
assert ev.data1 == chat.id assert ev.data1 == chat.id
assert ev.data2 == msg_out.id assert ev.data2 == msg_out.id
assert msg_out.is_out_delivered() assert msg_out.is_out_delivered()
m = delivered.get()
assert m == msg_out
lp.sec("wait for ac2 to receive message") lp.sec("wait for ac2 to receive message")
ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED") ev = ac2._evtracker.get_matching("DC_EVENT_MSGS_CHANGED")
@@ -983,6 +1003,8 @@ class TestOnlineAccount:
assert msg_in.is_image() assert msg_in.is_image()
assert os.path.exists(msg_in.filename) assert os.path.exists(msg_in.filename)
assert os.stat(msg_in.filename).st_size == os.stat(path).st_size 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): def test_import_export_online_all(self, acfactory, tmpdir, lp):
ac1 = acfactory.get_online_configuring_account() ac1 = acfactory.get_online_configuring_account()