diff sat_pubsub/privilege.py @ 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 5d7c3787672e
children 6d059f07c2d3
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,