From 57addc3b390bf2424645b2729991eeb6a44a7968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sat, 29 Dec 2018 19:00:20 +0100 Subject: [PATCH] xep_0384: add _fetch_device_list method for manual fetching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- plugin.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugin.py b/plugin.py index 9b0552b..9342c13 100644 --- a/plugin.py +++ b/plugin.py @@ -17,6 +17,7 @@ import asyncio from slixmpp.plugins.xep_0384.stanza import OMEMO_BASE_NS from slixmpp.plugins.xep_0384.stanza import OMEMO_DEVICES_NS, OMEMO_BUNDLES_NS from slixmpp.plugins.xep_0384.stanza import Bundle, Devices, Device, Encrypted, Key, PreKeyPublic +from slixmpp.plugins.xep_0060.stanza import Items, EventItems from slixmpp.plugins.base import BasePlugin from slixmpp.exceptions import IqError, IqTimeout from slixmpp.stanza import Message, Iq @@ -220,26 +221,32 @@ class XEP_0384(BasePlugin): return _parse_bundle(self._omemo_backend, bundle) + async def _fetch_device_list(self, jid: JID) -> None: + jid = JID(jid) + iq = await self.xmpp['xep_0060'].get_items(jid, OMEMO_DEVICES_NS) + return await self._read_device_list(jid, iq['pubsub']['items']) + def _store_device_ids(self, jid: str, items) -> None: device_ids = [] # type: List[int] items = list(items) device_ids = [int(d['id']) for d in items[0]['devices']] return self._omemo.newDeviceList(str(jid), device_ids) - def _receive_device_list(self, msg: Message) -> None: - if msg['pubsub_event']['items']['node'] != OMEMO_DEVICES_NS: - return + async def _receive_device_list(self, msg: Message) -> None: + return await self._read_device_list(msg['from'], msg['pubsub_event']['items']) - jid = msg['from'].bare - items = msg['pubsub_event']['items'] - self._store_device_ids(jid, items) + async def _read_device_list(self, jid: JID, items: Union[Items, EventItems]) -> None: + bare_jid = jid.bare + self._store_device_ids(bare_jid, items) - device_ids = self.get_device_list(jid) + device_ids = self.get_device_list(bare_jid) active_devices = device_ids['active'] - if jid == self.xmpp.boundjid.bare and \ + if bare_jid == self.xmpp.boundjid.bare and \ self._device_id not in active_devices: - asyncio.ensure_future(self._set_device_list()) + await self._set_device_list() + + return None async def _set_device_list(self) -> None: jid = self.xmpp.boundjid.bare