From 9591cf0ced8b6e83730b910cb715a95c18cdf105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Tue, 29 Jan 2019 18:03:01 +0100 Subject: [PATCH] xep_0384: Modify device list handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When receiving the device list, do not read from storage before checking if our JID is included, because the OMEMO library always includes it. The method now verifies directly from the items and calls the set method if necessary. Also fix an issue where the `device_ids` parameter of ̀_set_device_list` was never used. Signed-off-by: Maxime “pep” Buquet --- plugin.py | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/plugin.py b/plugin.py index 9ce2348..5ff6166 100644 --- a/plugin.py +++ b/plugin.py @@ -227,24 +227,29 @@ class XEP_0384(BasePlugin): return _parse_bundle(self._omemo, bundle) async def _fetch_device_list(self, jid: JID) -> None: + """Manually query PEP OMEMO_DEVICES_NS nodes""" 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: + def _store_device_ids(self, jid: str, items: Union[Items, EventItems]) -> None: + """Store Device list""" 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) async def _receive_device_list(self, msg: Message) -> None: + """Handler for received PEP OMEMO_DEVICES_NS payloads""" return await self._read_device_list(msg['from'], msg['pubsub_event']['items']) async def _read_device_list(self, jid: JID, items: Union[Items, EventItems]) -> None: + """Read items and devices if we need to set the device list again or not""" bare_jid = jid.bare self._store_device_ids(bare_jid, items) - device_ids = self.get_device_list(bare_jid) + items = list(items) + device_ids = [int(d['id']) for d in items[0]['devices']] if bare_jid == self.xmpp.boundjid.bare and \ self._device_id not in device_ids: @@ -252,28 +257,23 @@ class XEP_0384(BasePlugin): return None - async def _set_device_list(self) -> None: - jid = self.xmpp.boundjid.bare + async def _set_device_list(self, device_ids: Optional[Set[int]] = None) -> None: + own_jid = self.xmpp.boundjid try: iq = await self.xmpp['xep_0060'].get_items( - self.xmpp.boundjid.bare, OMEMO_DEVICES_NS, + own_jid.bare, OMEMO_DEVICES_NS, ) items = iq['pubsub']['items'] - self._store_device_ids(jid, items) + self._store_device_ids(own_jid.bare, items) except IqError as iq_err: if iq_err.condition == "item-not-found": - self._store_device_ids(jid, []) + self._store_device_ids(own_jid.bare, []) else: return # XXX: Handle this! - device_ids = self.get_device_list(jid) - - # Verify that this device in the list and set it if necessary - if self._device_id in device_ids: - return - - device_ids['active'].add(self._device_id) + if device_ids is None: + device_ids = self.get_device_list(own_jid) devices = [] for i in device_ids: @@ -284,12 +284,12 @@ class XEP_0384(BasePlugin): payload['devices'] = devices await self.xmpp['xep_0060'].publish( - jid, OMEMO_DEVICES_NS, payload=payload, + own_jid.bare, OMEMO_DEVICES_NS, payload=payload, ) - def get_device_list(self, jid: str) -> List[str]: - """Return active device ids""" - return self._omemo.getDevices(jid).get('active', []) + def get_device_list(self, jid: JID) -> List[str]: + """Return active device ids. Always contains our own device id.""" + return self._omemo.getDevices(jid.bare).get('active', []) def trust(self, jid: JID, device_id: int, ik: bytes) -> None: self._omemo.trust(jid.bare, device_id, ik)