From 9e70954a0e5562ae6608ec64c4df86a72851cacf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 23 Feb 2019 18:29:00 +0000 Subject: [PATCH] example-bot: attempt at returning encrypted messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- examples/echo_client.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/examples/echo_client.py b/examples/echo_client.py index 73699a7..ded07cd 100644 --- a/examples/echo_client.py +++ b/examples/echo_client.py @@ -36,6 +36,8 @@ class EchoBot(ClientXMPP): slixmpp repository. """ + eme_ns = 'eu.siacs.conversations.axolotl' + def __init__(self, jid, password): ClientXMPP.__init__(self, jid, password) @@ -140,9 +142,30 @@ class EchoBot(ClientXMPP): async def encrypted_reply(self, original_msg, body) -> None: """Helper to reply with encrypted messages""" - # TODO: Send the message encrypted. - return await self.plain_reply(msg, body) + mto = original_msg['from'] + mtype = original_msg['type'] + msg = self.make_message(mto=mto, mtype=mtype) + msg['eme']['namespace'] = self.eme_ns + msg['eme']['name'] = self['xep_0380'].mechanisms[self.eme_ns] + while True: + try: + # `encrypt_message` excepts the plaintext to be sent, a list of + # bare JIDs to encrypt to, and optionally a dict of problems to + # expect per bare JID. + recipients = [mto] + encrypt = await self['xep_0384'].encrypt_message(body, recipients) + msg.append(encrypt) + return msg.send() + except UndecidedException as exn: + # The library prevents us from sending a message to an + # untrusted/undecided barejid, so we need to make a decision here. + # This is where you prompt your user to ask what to do. + self['xep_0384'].trust(exn.bare_jid, exn.device, exn.ik) + except Exception as exn: + break + + return None if __name__ == '__main__': # Setup the command line arguments.