From 153edfb564fe38c0b01962504a85b94f3772f40a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Mon, 2 Sep 2019 02:21:45 +0200 Subject: [PATCH] Echo_bot: Use expect_problems and handle MissingBundleException 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 | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/examples/echo_client.py b/examples/echo_client.py index 954280a..3d7343b 100644 --- a/examples/echo_client.py +++ b/examples/echo_client.py @@ -18,10 +18,12 @@ from getpass import getpass from argparse import ArgumentParser from slixmpp import ClientXMPP, JID +from slixmpp.exceptions import IqTimeout, IqError from slixmpp.stanza import Message +import slixmpp_omemo from slixmpp_omemo import PluginCouldNotLoad, MissingOwnKey, EncryptionPrepareException from slixmpp_omemo import UndecidedException, UntrustedException, NoAvailableSession -import slixmpp_omemo +from omemo.exceptions import MissingBundleException log = logging.getLogger(__name__) @@ -158,6 +160,8 @@ class EchoBot(ClientXMPP): msg['eme']['namespace'] = self.eme_ns msg['eme']['name'] = self['xep_0380'].mechanisms[self.eme_ns] + expect_problems = {} # type: Optional[Dict[JID, List[int]]] + while True: try: # `encrypt_message` excepts the plaintext to be sent, a list of @@ -171,7 +175,7 @@ class EchoBot(ClientXMPP): # # TODO: Document expect_problems recipients = [mto] - encrypt = await self['xep_0384'].encrypt_message(body, recipients) + encrypt = await self['xep_0384'].encrypt_message(body, recipients, expect_problems) msg.append(encrypt) return msg.send() except UndecidedException as exn: @@ -180,7 +184,25 @@ class EchoBot(ClientXMPP): # This is where you prompt your user to ask what to do. In # this bot we will automatically trust undecided recipients. self['xep_0384'].trust(exn.bare_jid, exn.device, exn.ik) - # TODO: catch NoEligibleDevicesException and MissingBundleException + # TODO: catch NoEligibleDevicesException + except EncryptionPrepareException as exn: + # TODO: We might need to bail out here if errors are the same? + for error in exn.errors: + if isinstance(error, MissingBundleException): + self.plain_reply( + original_msg, + 'Could not find keys for device "%d" of recipient "%s". Skipping.' % + (error.device, error.bare_jid), + ) + jid = JID(error.bare_jid) + device_list = expect_problems.setdefault(jid, []) + device_list.append(error.device) + except (IqError, IqTimeout) as exn: + self.plain_reply( + original_msg, + 'An error occured while fetching information on a recipient.\n%r' % exn, + ) + return None except Exception as exn: await self.plain_reply( original_msg, @@ -190,6 +212,7 @@ class EchoBot(ClientXMPP): return None + if __name__ == '__main__': # Setup the command line arguments. parser = ArgumentParser(description=EchoBot.__doc__)