diff sat_pubsub/delegation.py @ 459:cebcb7f56889

backend, delegation: update to XEP-0355 v0.5 (namespace bump) + disco: - delegation now uses namespace `urn:xmpp:delegation:2` - restored node metadata and made it work with PEP - `delegated` attribute is also set on recipient when available (needed to know when a disco query is delegated as the original stanza is lost by wokkel)
author Goffi <goffi@goffi.org>
date Fri, 15 Oct 2021 09:32:04 +0200
parents ccb2a22ea0fc
children 607616f9ef5b
line wrap: on
line diff
--- a/sat_pubsub/delegation.py	Thu Oct 14 21:30:33 2021 +0200
+++ b/sat_pubsub/delegation.py	Fri Oct 15 09:32:04 2021 +0200
@@ -1,7 +1,6 @@
 #!/usr/bin/env python3
-#-*- coding: utf-8 -*-
 #
-# Copyright (c) 2015 Jérôme Poisson
+# Copyright (c) 2015-2021 Jérôme Poisson
 
 
 # This program is free software: you can redistribute it and/or modify
@@ -24,16 +23,16 @@
 from wokkel.subprotocols import XMPPHandler
 from wokkel import pubsub
 from wokkel import data_form
-from wokkel import disco, iwokkel
+from wokkel import disco, iwokkel, generic
 from wokkel.iwokkel import IPubSubService
 from wokkel import mam
 from twisted.python import log
-from twisted.words.protocols.jabber import jid, error
+from twisted.words.protocols.jabber import ijabber, jid, error
 from twisted.words.protocols.jabber.xmlstream import toResponse
 from twisted.words.xish import domish
 from zope.interface import implementer
 
-DELEGATION_NS = 'urn:xmpp:delegation:1'
+DELEGATION_NS = 'urn:xmpp:delegation:2'
 FORWARDED_NS = 'urn:xmpp:forward:0'
 DELEGATION_ADV_XPATH = '/message/delegation[@xmlns="{}"]'.format(DELEGATION_NS)
 DELEGATION_FWD_XPATH = '/iq[@type="set"]/delegation[@xmlns="{}"]/forwarded[@xmlns="{}"]'.format(DELEGATION_NS, FORWARDED_NS)
@@ -42,7 +41,8 @@
 DELEGATION_BARE_SEP = ":bare:"
 
 TO_HACK = ((IPubSubService, pubsub, "PubSubRequest"),
-           (mam.IMAMService, mam, "MAMRequest"))
+           (mam.IMAMService, mam, "MAMRequest"),
+           (None, disco, "_DiscoRequest"))
 
 
 class InvalidStanza(Exception):
@@ -62,9 +62,10 @@
         #      As PubSubRequest from sat.tmp.wokkel.pubsub use _request_class while
         #      original wokkel.pubsub use directly pubsub.PubSubRequest, we need to
         #      check which version is used before monkeypatching
-        for handler in self.parent.handlers:
-            for service, module, default_base_cls in TO_HACK:
-                if service.providedBy(handler):
+        for service, module, default_base_cls in TO_HACK:
+            module_patched = False
+            for handler in self.parent.handlers:
+                if not service or service.providedBy(handler):
                     if hasattr(handler, '_request_class'):
                         request_base_class = handler._request_class
                     else:
@@ -87,12 +88,17 @@
                                 delegated = False
                             instance = cls.__base__.fromElement(element)
                             instance.delegated = delegated
+                            try:
+                                instance.recipient.delegated = delegated
+                            except (AttributeError, TypeError):
+                                pass
                             return instance
 
                     if hasattr(handler, '_request_class'):
                         handler._request_class = RequestWithDelegation
-                    else:
+                    elif not module_patched:
                         setattr(module, default_base_cls, RequestWithDelegation)
+                        module_patched = True
         DelegationsHandler._service_hacked = True
 
     def connectionInitialized(self):