comparison 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
comparison
equal deleted inserted replaced
320:29c2553ef863 321:c7fe09894952
52 52
53 class PrivilegesHandler(XMPPHandler): 53 class PrivilegesHandler(XMPPHandler):
54 #FIXME: need to manage updates, and database sync 54 #FIXME: need to manage updates, and database sync
55 #TODO: cache 55 #TODO: cache
56 56
57 def __init__(self): 57 def __init__(self, service_jid):
58 super(PrivilegesHandler, self).__init__() 58 super(PrivilegesHandler, self).__init__()
59 self._permissions = {PERM_ROSTER: 'none', 59 self._permissions = {PERM_ROSTER: 'none',
60 PERM_MESSAGE: 'none', 60 PERM_MESSAGE: 'none',
61 PERM_PRESENCE: 'none'} 61 PERM_PRESENCE: 'none'}
62 self._pubsub_service = None 62 self._pubsub_service = None
63 # FIXME: we use a hack supposing that our privilege come from hostname
64 # and we are a component named [name].hostname
65 # but we need to manage properly server
66 # TODO: do proper server handling
67 self.server_jid = jid.JID(service_jid.host.split('.', 1)[1])
63 68
64 @property 69 @property
65 def permissions(self): 70 def permissions(self):
66 return self._permissions 71 return self._permissions
67 72
127 d.addCallback(processRoster) 132 d.addCallback(processRoster)
128 return d 133 return d
129 134
130 ## message ## 135 ## message ##
131 136
132 def sendMessage(self, to_jid, priv_message): 137 def sendMessage(self, priv_message, to_jid=None):
133 """ Send privileged message (in the name of the server) 138 """ Send privileged message (in the name of the server)
134 139
135 @param to_jid(jid.JID): main message destinee
136 @param priv_message(domish.Element): privileged message 140 @param priv_message(domish.Element): privileged message
141 @param to_jid(jid.JID, None): main message destinee
142 None to use our own server
137 """ 143 """
138 if self._permissions[PERM_MESSAGE] not in ('outgoing',): 144 if self._permissions[PERM_MESSAGE] not in ('outgoing',):
139 log.msg("WARNING: permission not allowed to send privileged messages") 145 log.msg("WARNING: permission not allowed to send privileged messages")
140 raise failure.Failure(NotAllowedError('privileged messages are not allowed')) 146 raise failure.Failure(NotAllowedError('privileged messages are not allowed'))
141 147
142 main_message = domish.Element((None, "message")) 148 main_message = domish.Element((None, "message"))
149 if to_jid is None:
150 to_jid = self.server_jid
143 main_message['to'] = to_jid.full() 151 main_message['to'] = to_jid.full()
144 privilege_elt = main_message.addElement((PRIV_ENT_NS, 'privilege')) 152 privilege_elt = main_message.addElement((PRIV_ENT_NS, 'privilege'))
145 forwarded_elt = privilege_elt.addElement((FORWARDED_NS, 'forwarded')) 153 forwarded_elt = privilege_elt.addElement((FORWARDED_NS, 'forwarded'))
146 priv_message['xmlns'] = 'jabber:client' 154 priv_message['xmlns'] = 'jabber:client'
147 forwarded_elt.addChild(priv_message) 155 forwarded_elt.addChild(priv_message)
154 nodeIdentifier, subscriber, 162 nodeIdentifier, subscriber,
155 subscriptions) 163 subscriptions)
156 for item in items: 164 for item in items:
157 item.uri = pubsub.NS_PUBSUB_EVENT 165 item.uri = pubsub.NS_PUBSUB_EVENT
158 message.event.items.addChild(item) 166 message.event.items.addChild(item)
159 self.sendMessage(jid.JID(pep_jid.host), message) 167 self.sendMessage(message)
160 168
161 169
162 def notifyRetract(self, pep_jid, nodeIdentifier, notifications): 170 def notifyRetract(self, pep_jid, nodeIdentifier, notifications):
163 for subscriber, subscriptions, items in notifications: 171 for subscriber, subscriptions, items in notifications:
164 message = self._pubsub_service._createNotification('items', pep_jid, 172 message = self._pubsub_service._createNotification('items', pep_jid,
166 subscriptions) 174 subscriptions)
167 for item in items: 175 for item in items:
168 retract = domish.Element((None, "retract")) 176 retract = domish.Element((None, "retract"))
169 retract['id'] = item['id'] 177 retract['id'] = item['id']
170 message.event.items.addChild(retract) 178 message.event.items.addChild(retract)
171 self.sendMessage(jid.JID(pep_jid.host), message) 179 self.sendMessage(message)
172 180
173 181
174 # def notifyDelete(self, service, nodeIdentifier, subscribers, 182 # def notifyDelete(self, service, nodeIdentifier, subscribers,
175 # redirectURI=None): 183 # redirectURI=None):
176 # # TODO 184 # # TODO