Mercurial > libervia-pubsub
comparison sat_pubsub/privilege.py @ 293:b96a4ac25f8b
privilege: added methods to send privileged messages and notifications
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 16 Aug 2015 01:15:13 +0200 |
parents | 2f87fa282dfd |
children | 5d7c3787672e |
comparison
equal
deleted
inserted
replaced
292:6918a0dad359 | 293:b96a4ac25f8b |
---|---|
23 This module implements XEP-0356 (Privileged Entity) to manage rosters, messages and presences | 23 This module implements XEP-0356 (Privileged Entity) to manage rosters, messages and presences |
24 """ | 24 """ |
25 | 25 |
26 from wokkel import xmppim | 26 from wokkel import xmppim |
27 from wokkel.compat import IQ | 27 from wokkel.compat import IQ |
28 from wokkel import pubsub | |
29 from wokkel.iwokkel import IPubSubService | |
28 from wokkel.subprotocols import XMPPHandler | 30 from wokkel.subprotocols import XMPPHandler |
29 from twisted.python import log | 31 from twisted.python import log |
30 from twisted.python import failure | 32 from twisted.python import failure |
33 from twisted.words.xish import domish | |
34 from twisted.words.protocols.jabber import jid | |
31 | 35 |
36 FORWARDED_NS = 'urn:xmpp:forward:0' | |
32 PRIV_ENT_NS = 'urn:xmpp:privilege:1' | 37 PRIV_ENT_NS = 'urn:xmpp:privilege:1' |
33 PRIV_ENT_ADV_XPATH = '/message/privilege[@xmlns="{}"]'.format(PRIV_ENT_NS) | 38 PRIV_ENT_ADV_XPATH = '/message/privilege[@xmlns="{}"]'.format(PRIV_ENT_NS) |
34 ROSTER_NS = 'jabber:iq:roster' | 39 ROSTER_NS = 'jabber:iq:roster' |
35 PERM_ROSTER = 'roster' | 40 PERM_ROSTER = 'roster' |
36 PERM_MESSAGE = 'message' | 41 PERM_MESSAGE = 'message' |
54 def __init__(self): | 59 def __init__(self): |
55 super(PrivilegesHandler, self).__init__() | 60 super(PrivilegesHandler, self).__init__() |
56 self._permissions = {PERM_ROSTER: 'none', | 61 self._permissions = {PERM_ROSTER: 'none', |
57 PERM_MESSAGE: 'none', | 62 PERM_MESSAGE: 'none', |
58 PERM_PRESENCE: 'none'} | 63 PERM_PRESENCE: 'none'} |
64 self._pubsub_service = None | |
59 | 65 |
60 @property | 66 @property |
61 def permissions(self): | 67 def permissions(self): |
62 return self._permissions | 68 return self._permissions |
63 | 69 |
64 def connectionInitialized(self): | 70 def connectionInitialized(self): |
71 for handler in self.parent.handlers: | |
72 if IPubSubService.providedBy(handler): | |
73 self._pubsub_service = handler | |
74 break | |
65 self.xmlstream.addObserver(PRIV_ENT_ADV_XPATH, self.onAdvertise) | 75 self.xmlstream.addObserver(PRIV_ENT_ADV_XPATH, self.onAdvertise) |
66 | 76 |
67 def onAdvertise(self, message): | 77 def onAdvertise(self, message): |
68 """Managage the <message/> advertising privileges | 78 """Managage the <message/> advertising privileges |
69 | 79 |
89 | 99 |
90 self._permissions[perm_access] = perm_type or 'none' | 100 self._permissions[perm_access] = perm_type or 'none' |
91 | 101 |
92 log.msg('Privileges updated: roster={roster}, message={message}, presence={presence}'.format(**self._permissions)) | 102 log.msg('Privileges updated: roster={roster}, message={message}, presence={presence}'.format(**self._permissions)) |
93 | 103 |
104 ## roster ## | |
94 | 105 |
95 def getRoster(self, to_jid): | 106 def getRoster(self, to_jid): |
96 """ | 107 """ |
97 Retrieve contact list. | 108 Retrieve contact list. |
98 | 109 |
115 iq.addElement((ROSTER_NS, 'query')) | 126 iq.addElement((ROSTER_NS, 'query')) |
116 iq["to"] = to_jid.userhost() | 127 iq["to"] = to_jid.userhost() |
117 d = iq.send() | 128 d = iq.send() |
118 d.addCallback(processRoster) | 129 d.addCallback(processRoster) |
119 return d | 130 return d |
131 | |
132 ## message ## | |
133 | |
134 def sendMessage(self, to_jid, priv_message): | |
135 """ Send privileged message (in the name of the server) | |
136 | |
137 @param to_jid(jid.JID): main message destinee | |
138 @param priv_message(domish.Element): privileged message | |
139 """ | |
140 if self._permissions[PERM_MESSAGE] not in ('outgoing',): | |
141 log.msg("WARNING: permission not allowed to send privileged messages") | |
142 raise failure.Failure(NotAllowedError('privileged messages are not allowed')) | |
143 | |
144 main_message = domish.Element((None, "message")) | |
145 main_message['to'] = to_jid.full() | |
146 privilege_elt = main_message.addElement((PRIV_ENT_NS, 'privilege')) | |
147 forwarded_elt = privilege_elt.addElement((FORWARDED_NS, 'forwarded')) | |
148 priv_message['xmlns'] = 'jabber:client' | |
149 forwarded_elt.addChild(priv_message) | |
150 self.send(main_message) | |
151 | |
152 def notifyPublish(self, pep_jid, nodeIdentifier, notifications): | |
153 """Do notifications using privileges""" | |
154 for subscriber, subscriptions, items in notifications: | |
155 message = self._pubsub_service._createNotification('items', pep_jid, | |
156 nodeIdentifier, subscriber, | |
157 subscriptions) | |
158 for item in items: | |
159 item.uri = pubsub.NS_PUBSUB_EVENT | |
160 message.event.items.addChild(item) | |
161 self.sendMessage(jid.JID(pep_jid.host), message) | |
162 | |
163 | |
164 def notifyRetract(self, pep_jid, nodeIdentifier, notifications): | |
165 for subscriber, subscriptions, items in notifications: | |
166 message = self._pubsub_service._createNotification('items', pep_jid, | |
167 nodeIdentifier, subscriber, | |
168 subscriptions) | |
169 for item in items: | |
170 retract = domish.Element((None, "retract")) | |
171 retract['id'] = item['id'] | |
172 message.event.items.addChild(retract) | |
173 self.sendMessage(jid.JID(pep_jid.host), message) | |
174 | |
175 | |
176 # def notifyDelete(self, service, nodeIdentifier, subscribers, | |
177 # redirectURI=None): | |
178 # # TODO | |
179 # for subscriber in subscribers: | |
180 # message = self._createNotification('delete', service, | |
181 # nodeIdentifier, | |
182 # subscriber) | |
183 # if redirectURI: | |
184 # redirect = message.event.delete.addElement('redirect') | |
185 # redirect['uri'] = redirectURI | |
186 # self.send(message) |