From 37273d96842a7ffd2a64f826042f2fb47b8485b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=B6rist?= Date: Tue, 20 Nov 2018 17:48:01 +0100 Subject: [PATCH] [omemo] Publish bundle with id 'current' Fixes #362 --- omemo/omemo_connection.py | 18 ++++++----- omemo/xmpp.py | 63 ++++++++++++++------------------------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/omemo/omemo_connection.py b/omemo/omemo_connection.py index 5f388d5..1955a96 100644 --- a/omemo/omemo_connection.py +++ b/omemo/omemo_connection.py @@ -16,8 +16,8 @@ from gajim.common.connection_handlers_events import MessageNotSentEvent from gajim.plugins.plugins_i18n import _ from omemo.xmpp import ( - NS_NOTIFY, NS_OMEMO, NS_EME, NS_HINTS, BundleInformationAnnouncement, - BundleInformationQuery, DevicelistQuery, + NS_NOTIFY, NS_OMEMO, NS_EME, NS_HINTS, NS_BUNDLES, + BundleInformationQuery, DevicelistQuery, make_bundle, OmemoMessage, successful, unpack_device_bundle, unpack_device_list_update, unpack_encrypted, NS_DEVICE_LIST) from omemo.omemo.state import OmemoState @@ -811,12 +811,16 @@ class OMEMOConnection: def publish_bundle(self): """ Publish our bundle information to the PEP node """ - bundle_announce = BundleInformationAnnouncement( - self.omemo.bundle, self.omemo.own_device_id) - log.info('%s => Publishing bundle ...', self.account) - self.send_with_callback(bundle_announce, self.handle_publish_result) + bundle = make_bundle(self.omemo.bundle) + node = '%s%s' % (NS_BUNDLES, self.omemo.own_device_id) - def handle_publish_result(self, stanza): + log.info('%s => Publishing bundle ...', self.account) + + con = app.connections[self.account] + con.get_module('PubSub').send_pb_publish( + '', node, bundle, 'current', cb=self.handle_publish_result) + + def handle_publish_result(self, _con, stanza): """ Log if publishing our bundle was successful Parameters diff --git a/omemo/xmpp.py b/omemo/xmpp.py index b74752d..8d810e1 100644 --- a/omemo/xmpp.py +++ b/omemo/xmpp.py @@ -43,13 +43,6 @@ NS_HINTS = 'urn:xmpp:hints' log = logging.getLogger('gajim.plugin_system.omemo') -class PublishNode(Node): - def __init__(self, node_str, data): - assert node_str is not None and isinstance(data, Node) - Node.__init__(self, tag='publish', attrs={'node': node_str}) - self.addChild('item').addChild(node=data) - - class PubsubNode(Node): def __init__(self, data): assert isinstance(data, Node) @@ -88,39 +81,6 @@ class BundleInformationQuery(Iq): self.addChild(node=pubsub) -class BundleInformationAnnouncement(Iq): - def __init__(self, state_bundle, device_id): - id_ = app.get_an_id() - attrs = {'id': id_} - Iq.__init__(self, typ='set', attrs=attrs) - bundle_node = self.make_bundle_node(state_bundle) - publish = PublishNode(NS_BUNDLES + str(device_id), bundle_node) - pubsub = PubsubNode(publish) - self.addChild(node=pubsub) - - def make_bundle_node(self, state_bundle): - result = Node('bundle', attrs={'xmlns': NS_OMEMO}) - prekey_pub_node = result.addChild( - 'signedPreKeyPublic', - attrs={'signedPreKeyId': state_bundle['signedPreKeyId']}) - prekey_pub_node.addData(state_bundle['signedPreKeyPublic'] - .decode('utf-8')) - - prekey_sig_node = result.addChild('signedPreKeySignature') - prekey_sig_node.addData(state_bundle['signedPreKeySignature'] - .decode('utf-8')) - - identity_key_node = result.addChild('identityKey') - identity_key_node.addData(state_bundle['identityKey'].decode('utf-8')) - prekeys = result.addChild('prekeys') - - for key in state_bundle['prekeys']: - prekeys.addChild('preKeyPublic', - attrs={'preKeyId': key[0]}) \ - .addData(key[1].decode('utf-8')) - return result - - class DevicelistQuery(Iq): def __init__(self, contact_jid): id_ = app.get_an_id() @@ -139,6 +99,29 @@ class DevicelistPEP(AbstractPEP): return ({}, []) +def make_bundle(state_bundle): + result = Node('bundle', attrs={'xmlns': NS_OMEMO}) + prekey_pub_node = result.addChild( + 'signedPreKeyPublic', + attrs={'signedPreKeyId': state_bundle['signedPreKeyId']}) + prekey_pub_node.addData(state_bundle['signedPreKeyPublic'] + .decode('utf-8')) + + prekey_sig_node = result.addChild('signedPreKeySignature') + prekey_sig_node.addData(state_bundle['signedPreKeySignature'] + .decode('utf-8')) + + identity_key_node = result.addChild('identityKey') + identity_key_node.addData(state_bundle['identityKey'].decode('utf-8')) + prekeys = result.addChild('prekeys') + + for key in state_bundle['prekeys']: + prekeys.addChild('preKeyPublic', + attrs={'preKeyId': key[0]}) \ + .addData(key[1].decode('utf-8')) + return result + + @log_calls('OmemoPlugin') def unpack_device_bundle(bundle, device_id): pubsub = bundle.getTag('pubsub', namespace=NS_PUBSUB)