refine member/add remove further, and introduce ac_outgoing_message

This commit is contained in:
holger krekel
2020-04-19 12:31:15 +02:00
parent a1c82eaea6
commit 02cda1e611
6 changed files with 51 additions and 23 deletions

View File

@@ -17,25 +17,31 @@ class GroupTrackingPlugin:
text = message.text
message.chat.send_text("echoing from {}:\n{}".format(addr, text))
@account_hookimpl
def ac_outgoing_message(self, message):
print("ac_outgoing_message:", message)
@account_hookimpl
def ac_configure_completed(self, success):
print("*** ac_configure_completed:", 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(
contact.addr, chat.id, sender.addr))
print("ac_chat_modified:", chat.id, chat.get_name())
for member in chat.get_contacts():
print("chat member: {}".format(member.addr))
@account_hookimpl
def ac_member_removed(self, chat, contact, sender):
print("*** ac_member_removed {} from chat {} by {}".format(
contact.addr, chat.id, sender.addr))
def ac_member_added(self, chat, contact, message):
print("ac_member_added {} to chat {} from {}".format(
contact.addr, chat.id, message.get_sender_contact().addr))
for member in chat.get_contacts():
print("chat member: {}".format(member.addr))
@account_hookimpl
def ac_member_removed(self, chat, contact, message):
print("ac_member_removed {} from chat {} by {}".format(
contact.addr, chat.id, message.get_sender_contact().addr))
def main(argv=None):

View File

@@ -30,7 +30,7 @@ class Chat(object):
return not (self == other)
def __repr__(self):
return "<Chat id={} name={} dc_context={}>".format(self.id, self.get_name(), self._dc_context)
return "<Chat id={} name={}>".format(self.id, self.get_name())
@property
def _dc_chat(self):

View File

@@ -48,6 +48,10 @@ class PerAccount:
def ac_incoming_message(self, message):
""" Called on any incoming message (to deaddrop or chat). """
@account_hookspec
def ac_outgoing_message(self, message):
""" Called on each outgoing message (both system and "normal")."""
@account_hookspec
def ac_message_delivered(self, message):
""" Called when an outgoing message has been delivered to SMTP. """
@@ -57,12 +61,12 @@ class PerAccount:
""" Chat was created or modified regarding membership, avatar, title. """
@account_hookspec
def ac_member_added(self, chat, contact, sender):
def ac_member_added(self, chat, contact, message):
""" Called for each contact added to an accepted chat. """
@account_hookspec
def ac_member_removed(self, chat, contact, sender):
""" Called for each contact removed from a chat. """
def ac_member_removed(self, chat, contact, message):
""" Called for each contact removed from a chat. """
class Global:

View File

@@ -28,7 +28,9 @@ class Message(object):
return self.account == other.account and self.id == other.id
def __repr__(self):
return "<Message id={} dc_context={}>".format(self.id, self._dc_context)
c = self.get_sender_contact()
return "<Message id={} sender={}/{} outgoing={} chat={}/{}>".format(
self.id, c.id, c.addr, self.is_outgoing(), self.chat.id, self.chat.get_name())
@classmethod
def from_db(cls, account, id):
@@ -322,14 +324,17 @@ def get_viewtype_code_from_name(view_type_name):
"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())
d = dict(chat=msg.chat, contact=contact, message=msg)
return "ac_member_" + res[0], d

View File

@@ -347,15 +347,21 @@ class BotProcess:
patterns = [x.strip() for x in Source(pattern_lines.rstrip()).lines if x.strip()]
for next_pattern in patterns:
print("+++FNMATCH:", next_pattern)
ignored = []
while 1:
line = self.stdout_queue.get(timeout=15)
if line is None:
if ignored:
print("BOT stdout terminated after these lines")
for line in ignored:
print(line)
raise IOError("BOT stdout-thread terminated")
if fnmatch.fnmatch(line, next_pattern):
print("+++MATCHED:", line)
break
else:
print("+++IGN:", line)
ignored.append(line)
@pytest.fixture

View File

@@ -1052,12 +1052,17 @@ class TestOnlineAccount:
message_queue.put(message)
delivered = queue.Queue()
out = queue.Queue()
class OutPlugin:
@account_hookimpl
def ac_message_delivered(self, message):
delivered.put(message)
@account_hookimpl
def ac_outgoing_message(self, message):
out.put(message)
ac1.add_account_plugin(OutPlugin())
ac2.add_account_plugin(InPlugin())
@@ -1068,6 +1073,8 @@ class TestOnlineAccount:
assert ev.data1 == chat.id
assert ev.data2 == msg_out.id
assert msg_out.is_out_delivered()
m = out.get()
assert m == msg_out
m = delivered.get()
assert m == msg_out
@@ -1304,12 +1311,12 @@ class TestOnlineAccount:
in_list.put(EventHolder(action="chat-modified", chat=chat))
@account_hookimpl
def ac_member_added(self, chat, contact, sender):
in_list.put(EventHolder(action="added", chat=chat, contact=contact, sender=sender))
def ac_member_added(self, chat, contact, message):
in_list.put(EventHolder(action="added", chat=chat, contact=contact, message=message))
@account_hookimpl
def ac_member_removed(self, chat, contact, sender):
in_list.put(EventHolder(action="removed", chat=chat, contact=contact, sender=sender))
def ac_member_removed(self, chat, contact, message):
in_list.put(EventHolder(action="removed", chat=chat, contact=contact, message=message))
ac2.add_account_plugin(InPlugin())
@@ -1335,7 +1342,7 @@ class TestOnlineAccount:
assert ev.action == "chat-modified"
ev = in_list.get(timeout=10)
assert ev.action == "added"
assert ev.sender.addr == ac1_addr
assert ev.message.get_sender_contact().addr == ac1_addr
assert ev.contact.addr == "notexistingaccountihope@testrun.org"
lp.sec("ac1: remove address2")
@@ -1345,7 +1352,7 @@ class TestOnlineAccount:
ev = in_list.get(timeout=10)
assert ev.action == "removed"
assert ev.contact.addr == contact2.addr
assert ev.sender.addr == ac1_addr
assert ev.message.get_sender_contact().addr == ac1_addr
lp.sec("ac1: remove ac2 contact from chat")
chat.remove_contact(contact)
@@ -1353,7 +1360,7 @@ class TestOnlineAccount:
assert ev.action == "chat-modified"
ev = in_list.get(timeout=10)
assert ev.action == "removed"
assert ev.sender.addr == ac1_addr
assert ev.message.get_sender_contact().addr == ac1_addr
def test_set_get_group_image(self, acfactory, data, lp):
ac1, ac2 = acfactory.get_two_online_accounts()