changeset 321:c7fe09894952

privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
author Goffi <goffi@goffi.org>
date Tue, 05 Jan 2016 22:16:37 +0100
parents 29c2553ef863
children 54d90c73b8b5
files sat_pubsub/privilege.py sat_pubsub/tap.py
diffstat 2 files changed, 26 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/sat_pubsub/privilege.py	Sun Jan 03 18:33:23 2016 +0100
+++ b/sat_pubsub/privilege.py	Tue Jan 05 22:16:37 2016 +0100
@@ -54,12 +54,17 @@
     #FIXME: need to manage updates, and database sync
     #TODO: cache
 
-    def __init__(self):
+    def __init__(self, service_jid):
         super(PrivilegesHandler, self).__init__()
         self._permissions = {PERM_ROSTER: 'none',
                              PERM_MESSAGE: 'none',
                              PERM_PRESENCE: 'none'}
         self._pubsub_service = None
+        # FIXME: we use a hack supposing that our privilege come from hostname
+        #        and we are a component named [name].hostname
+        #        but we need to manage properly server
+        # TODO: do proper server handling
+        self.server_jid = jid.JID(service_jid.host.split('.', 1)[1])
 
     @property
     def permissions(self):
@@ -129,17 +134,20 @@
 
     ## message ##
 
-    def sendMessage(self, to_jid, priv_message):
+    def sendMessage(self, priv_message, to_jid=None):
         """ Send privileged message (in the name of the server)
 
-        @param to_jid(jid.JID): main message destinee
         @param priv_message(domish.Element): privileged message
+        @param to_jid(jid.JID, None): main message destinee
+            None to use our own server
         """
         if self._permissions[PERM_MESSAGE] not in ('outgoing',):
             log.msg("WARNING: permission not allowed to send privileged messages")
             raise failure.Failure(NotAllowedError('privileged messages are not allowed'))
 
         main_message = domish.Element((None, "message"))
+        if to_jid is None:
+            to_jid = self.server_jid
         main_message['to'] = to_jid.full()
         privilege_elt = main_message.addElement((PRIV_ENT_NS, 'privilege'))
         forwarded_elt = privilege_elt.addElement((FORWARDED_NS, 'forwarded'))
@@ -156,7 +164,7 @@
             for item in items:
                 item.uri = pubsub.NS_PUBSUB_EVENT
                 message.event.items.addChild(item)
-            self.sendMessage(jid.JID(pep_jid.host), message)
+            self.sendMessage(message)
 
 
     def notifyRetract(self, pep_jid, nodeIdentifier, notifications):
@@ -168,7 +176,7 @@
                 retract = domish.Element((None, "retract"))
                 retract['id'] = item['id']
                 message.event.items.addChild(retract)
-            self.sendMessage(jid.JID(pep_jid.host), message)
+            self.sendMessage(message)
 
 
     # def notifyDelete(self, service, nodeIdentifier, subscribers,
--- a/sat_pubsub/tap.py	Sun Jan 03 18:33:23 2016 +0100
+++ b/sat_pubsub/tap.py	Tue Jan 05 22:16:37 2016 +0100
@@ -59,9 +59,11 @@
 from wokkel.disco import DiscoHandler
 from wokkel.generic import FallbackHandler, VersionHandler
 from wokkel.iwokkel import IPubSubResource
-from wokkel import pubsub, rsm, mam as wokkel_mam
+from wokkel import pubsub, rsm, mam
 
-from sat_pubsub import __version__, const, mam
+from sat_pubsub import __version__
+from sat_pubsub import const
+from sat_pubsub import mam as pubsub_mam
 from sat_pubsub.backend import BackendService
 from sat_pubsub.privilege import PrivilegesHandler
 from sat_pubsub.delegation import DelegationsHandler
@@ -138,14 +140,10 @@
     VersionHandler(u'SàT Pubsub', __version__).setHandlerParent(cs)
     DiscoHandler().setHandlerParent(cs)
 
-    ph = PrivilegesHandler()
+    ph = PrivilegesHandler(config['jid'])
     ph.setHandlerParent(cs)
     bs.privilege = ph
 
-    dh = DelegationsHandler()
-    dh.setHandlerParent(cs)
-    bs.delegation = dh
-
     resource = IPubSubResource(bs)
     resource.hideNodes = config["hide-nodes"]
     resource.serviceJID = config["jid"]
@@ -155,8 +153,14 @@
     resource.pubsubService = ps
 
     if const.FLAG_ENABLE_MAM:
-        mam_resource = mam.MAMResource(bs)
-        mam_s = wokkel_mam.MAMService(mam_resource)
+        mam_resource = pubsub_mam.MAMResource(bs)
+        mam_s = mam.MAMService(mam_resource)
         mam_s.setHandlerParent(cs)
 
+    # XXX: delegation must be instancied at the end,
+    #      because it does some MonkeyPatching on handlers
+    dh = DelegationsHandler()
+    dh.setHandlerParent(cs)
+    bs.delegation = dh
+
     return s