changeset 116fd297511f in /home/hg/repos/gajim-plugins

author: lovetox <[email protected]>
branches: 
details:gajim-plugins?cmd=changeset;node=116fd297511f
description: Query the devicelist after BundleAnnouncement

        The Devicelist ist queried after the BundleAnnouncement and
        afterwards handled with handle_devicelist_result()

diffstat:

 omemo/__init__.py |  67 +++++++++++++++++++++++++++++++++++++++++-------------
 omemo/xmpp.py     |  23 +++++++++++++----
 2 files changed, 67 insertions(+), 23 deletions(-)

diffs (153 lines):

diff -r aae1de9a8e68 -r 116fd297511f omemo/__init__.py
--- a/omemo/__init__.py Sun May 29 11:45:46 2016 +0200
+++ b/omemo/__init__.py Mon Jun 06 02:33:41 2016 +0200
@@ -33,7 +33,7 @@
 from .ui import Ui
 from .xmpp import (
     NS_NOTIFY, NS_OMEMO, BundleInformationAnnouncement, BundleInformationQuery,
-    DeviceListAnnouncement, DevicelistPEP, OmemoMessage, successful,
+    DeviceListAnnouncement, DevicelistQuery, DevicelistPEP, OmemoMessage, 
successful,
     unpack_device_bundle, unpack_device_list_update, unpack_encrypted)
 
 
@@ -191,7 +191,7 @@
         if event.pep_type != 'headline':
             return False
 
-        devices_list = unpack_device_list_update(event)
+        devices_list = unpack_device_list_update(event.stanza, event.conn.name)
         if len(devices_list) == 0:
             return False
         account_name = event.conn.name
@@ -407,14 +407,14 @@
         id_ = str(iq.getAttr("id"))
         log.debug(account + " → Announcing OMEMO support via PEP")
         iq_ids_to_callbacks[id_] = lambda stanza: \
-            self.handle_announcement_result(account, stanza, state)
+            self.handle_announcement_result(account, stanza)
 
     @log_calls('OmemoPlugin')
-    def handle_announcement_result(self, account, stanza, state):
-        """ Updates own device list if announcement was successfull.
+    def handle_announcement_result(self, account, stanza):
+        """ Query own device list if announcement was successfull.
 
-            If the OMEMO support announcement was successfull update own device
-            list if needed.
+            If the OMEMO support announcement was successfull own device
+            list is queried.
 
             Parameters
             ----------
@@ -422,20 +422,53 @@
                 the account name
             stanza
                 The stanza object received from callback
-            state : (OmemoState)
-                The OmemoState used
+        """
+        my_jid = gajim.get_jid_from_account(account)
+        iq = DevicelistQuery(my_jid)
+        if successful(stanza):
+            log.debug(account + ' → Publishing bundle was successful')
+            gajim.connections[account].connection.send(iq)
+            log.debug(account + ' → Querry own Devicelist')
+            id_ = str(iq.getAttr("id"))
+            iq_ids_to_callbacks[id_] = lambda stanza: \
+                self.handle_devicelist_result(account, stanza)
+        else:
+            log.error(account + ' → Publishing bundle was NOT successful')
+
+    @log_calls('OmemoPlugin')
+    def handle_devicelist_result(self, account, stanza):
+        """ If query was successful add own device to the list.
+
+            Parameters
+            ----------
+            account : str
+                the account name
+            stanza
+                The stanza object received from callback
         """
 
-        state = self.get_omemo_state(account)
         if successful(stanza):
-            log.debug(account + ' → Publishing bundle was successful')
-            if not state.own_device_id_published():
-                log.warn(account + ' → Device list needs updating')
-                self.publish_own_devices_list(account, state)
-            else:
-                log.debug(account + ' → Device list up to date')
+            log.debug(account + ' → Devicelistquery was successful')
+            devices_list = unpack_device_list_update(stanza, account)
+            if len(devices_list) == 0:
+                return False
+
+            my_jid = gajim.get_jid_from_account(account)
+            state = self.get_omemo_state(account)
+            contact_jid = stanza.getAttr('from')
+            if contact_jid == my_jid:
+                state.set_own_devices(devices_list)
+                if not state.own_device_id_published() or anydup(
+                        state.own_devices):
+                    # Our own device_id is not in the list, it could be
+                    # overwritten by some other client?
+                    # also remove duplicates
+                    devices_list = list(set(state.own_devices))
+                    devices_list.append(state.own_device_id)
+                    self.publish_own_devices_list(account, state)
         else:
-            log.error(account + ' → Publishing bundle was NOT successful')
+            log.error(account + ' → Devicelistquery was NOT successful')
+            self.publish_own_devices_list(account, state)
 
     @log_calls('OmemoPlugin')
     def clear_device_list(self, contact):
diff -r aae1de9a8e68 -r 116fd297511f omemo/xmpp.py
--- a/omemo/xmpp.py     Sun May 29 11:45:46 2016 +0200
+++ b/omemo/xmpp.py     Mon Jun 06 02:33:41 2016 +0200
@@ -128,6 +128,16 @@
         return result
 
 
+class DevicelistQuery(Iq):
+    def __init__(self, contact_jid,):
+        id_ = gajim.get_an_id()
+        attrs = {'id': id_}
+        Iq.__init__(self, typ='get', attrs=attrs, to=contact_jid)
+        items = Node('items', attrs={'node': NS_DEVICE_LIST})
+        pubsub = PubsubNode(items)
+        self.addChild(node=pubsub)
+
+
 class DevicelistPEP(AbstractPEP):
     type_ = 'headline'
     namespace = NS_DEVICE_LIST
@@ -270,20 +280,21 @@
     return result
 
 
-def unpack_device_list_update(event):
-    """ Unpacks the device list update received in a MessageReceivedEvent.
+def unpack_device_list_update(stanza, account):
+    """ Unpacks the device list from a stanza
 
         Parameters
         ----------
-        event : MessageReceivedEvent
-                The event received from gajim
+        stanza
+
         Returns
         -------
         [int]
             List of device ids or empty list if nothing found
     """
-    event_node = event.stanza.getTag('event', namespace=NS_PUBSUB_EVENT)
-    account = event.conn.name
+    event_node = stanza.getTag('event', namespace=NS_PUBSUB_EVENT)
+    if not event_node:
+        event_node = stanza.getTag('pubsub', namespace=NS_PUBSUB)
     result = []
 
     if not event_node:
_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to