annotate sat_pubsub/privilege.py @ 302:b8b25efae0bc

fixed item_data issues after introduction of ItemData namedtuple
author Goffi <goffi@goffi.org>
date Wed, 25 Nov 2015 22:48:59 +0100
parents b96a4ac25f8b
children 5d7c3787672e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
3 #
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
286
2f87fa282dfd updated old docstring (privilege is actually a new (2015) module not derivated from idavoll)
Goffi <goffi@goffi.org>
parents: 285
diff changeset
5 Copyright (c) 2015 Jérôme Poisson
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
6
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
7
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU Affero General Public License as published by
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
12
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU Affero General Public License for more details.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
17
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU Affero General Public License
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
20
286
2f87fa282dfd updated old docstring (privilege is actually a new (2015) module not derivated from idavoll)
Goffi <goffi@goffi.org>
parents: 285
diff changeset
21 ---
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
22
286
2f87fa282dfd updated old docstring (privilege is actually a new (2015) module not derivated from idavoll)
Goffi <goffi@goffi.org>
parents: 285
diff changeset
23 This module implements XEP-0356 (Privileged Entity) to manage rosters, messages and presences
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
24 """
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
25
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from wokkel import xmppim
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from wokkel.compat import IQ
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
28 from wokkel import pubsub
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
29 from wokkel.iwokkel import IPubSubService
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
30 from wokkel.subprotocols import XMPPHandler
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
31 from twisted.python import log
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
32 from twisted.python import failure
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
33 from twisted.words.xish import domish
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
34 from twisted.words.protocols.jabber import jid
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
35
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
36 FORWARDED_NS = 'urn:xmpp:forward:0'
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
37 PRIV_ENT_NS = 'urn:xmpp:privilege:1'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
38 PRIV_ENT_ADV_XPATH = '/message/privilege[@xmlns="{}"]'.format(PRIV_ENT_NS)
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
39 ROSTER_NS = 'jabber:iq:roster'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
40 PERM_ROSTER = 'roster'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
41 PERM_MESSAGE = 'message'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
42 PERM_PRESENCE = 'presence'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
43 ALLOWED_ROSTER = ('none', 'get', 'set', 'both')
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
44 ALLOWED_MESSAGE = ('none', 'outgoing')
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
45 ALLOWED_PRESENCE = ('none', 'managed_entity', 'roster')
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
46 TO_CHECK = {PERM_ROSTER:ALLOWED_ROSTER, PERM_MESSAGE:ALLOWED_MESSAGE, PERM_PRESENCE:ALLOWED_PRESENCE}
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
47
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
48
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
49 class InvalidStanza(Exception):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
50 pass
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
51
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
52 class NotAllowedError(Exception):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
53 pass
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
54
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
55 class PrivilegesHandler(XMPPHandler):
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
56 #FIXME: need to manage updates, and database sync
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
57 #TODO: cache
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
58
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
59 def __init__(self):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
60 super(PrivilegesHandler, self).__init__()
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
61 self._permissions = {PERM_ROSTER: 'none',
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
62 PERM_MESSAGE: 'none',
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
63 PERM_PRESENCE: 'none'}
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
64 self._pubsub_service = None
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
65
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
66 @property
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
67 def permissions(self):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
68 return self._permissions
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
69
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
70 def connectionInitialized(self):
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
71 for handler in self.parent.handlers:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
72 if IPubSubService.providedBy(handler):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
73 self._pubsub_service = handler
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
74 break
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
75 self.xmlstream.addObserver(PRIV_ENT_ADV_XPATH, self.onAdvertise)
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
76
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
77 def onAdvertise(self, message):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
78 """Managage the <message/> advertising privileges
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
79
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
80 self._permissions will be updated according to advertised privileged
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
81 """
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
82 privilege_elt = message.elements(PRIV_ENT_NS, 'privilege').next()
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
83 for perm_elt in privilege_elt.elements(PRIV_ENT_NS):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
84 try:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
85 if perm_elt.name != 'perm':
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
86 raise InvalidStanza(u'unexpected element {}'.format(perm_elt.name))
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
87 perm_access = perm_elt['access']
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
88 perm_type = perm_elt['type']
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
89 try:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
90 if perm_type not in TO_CHECK[perm_access]:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
91 raise InvalidStanza(u'bad type [{}] for permission {}'.format(perm_type, perm_access))
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
92 except KeyError:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
93 raise InvalidStanza(u'bad permission [{}]'.format(perm_access))
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
94 except InvalidStanza as e:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
95 log.msg("Invalid stanza received ({}), setting permission to none".format(e))
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
96 for perm in self._permissions:
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
97 self._permissions[perm] = 'none'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
98 break
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
99
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
100 self._permissions[perm_access] = perm_type or 'none'
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
101
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
102 log.msg('Privileges updated: roster={roster}, message={message}, presence={presence}'.format(**self._permissions))
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
103
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
104 ## roster ##
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
105
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def getRoster(self, to_jid):
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
107 """
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
108 Retrieve contact list.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
109
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @return: Roster as a mapping from L{JID} to L{RosterItem}.
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @rtype: L{twisted.internet.defer.Deferred}
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
113 if self._permissions[PERM_ROSTER] not in ('get', 'both'):
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
114 log.msg("WARNING: permission not allowed to get roster")
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
115 raise failure.Failure(NotAllowedError('roster get is not allowed'))
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
116
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def processRoster(result):
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
118 roster = {}
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
119 for element in result.elements(ROSTER_NS, 'item'):
253
06494c9b25f2 update to fix broken RemoteRoster after Wokkel 0.7.1 changes
Goffi <goffi@goffi.org>
parents: 242
diff changeset
120 item = xmppim.RosterItem.fromElement(element)
06494c9b25f2 update to fix broken RemoteRoster after Wokkel 0.7.1 changes
Goffi <goffi@goffi.org>
parents: 242
diff changeset
121 roster[item.entity] = item
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
122
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return roster
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
124
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
125 iq = IQ(self.xmlstream, 'get')
285
a87c155d0fd5 replaced former roster dirty hack by a XEP-0356 first draft implementation, only roster get is implemented so far
Goffi <goffi@goffi.org>
parents: 283
diff changeset
126 iq.addElement((ROSTER_NS, 'query'))
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
127 iq["to"] = to_jid.userhost()
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
128 d = iq.send()
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
129 d.addCallback(processRoster)
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
130 return d
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
131
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
132 ## message ##
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
133
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
134 def sendMessage(self, to_jid, priv_message):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
135 """ Send privileged message (in the name of the server)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
136
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
137 @param to_jid(jid.JID): main message destinee
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
138 @param priv_message(domish.Element): privileged message
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
139 """
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
140 if self._permissions[PERM_MESSAGE] not in ('outgoing',):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
141 log.msg("WARNING: permission not allowed to send privileged messages")
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
142 raise failure.Failure(NotAllowedError('privileged messages are not allowed'))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
143
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
144 main_message = domish.Element((None, "message"))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
145 main_message['to'] = to_jid.full()
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
146 privilege_elt = main_message.addElement((PRIV_ENT_NS, 'privilege'))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
147 forwarded_elt = privilege_elt.addElement((FORWARDED_NS, 'forwarded'))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
148 priv_message['xmlns'] = 'jabber:client'
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
149 forwarded_elt.addChild(priv_message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
150 self.send(main_message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
151
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
152 def notifyPublish(self, pep_jid, nodeIdentifier, notifications):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
153 """Do notifications using privileges"""
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
154 for subscriber, subscriptions, items in notifications:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
155 message = self._pubsub_service._createNotification('items', pep_jid,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
156 nodeIdentifier, subscriber,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
157 subscriptions)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
158 for item in items:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
159 item.uri = pubsub.NS_PUBSUB_EVENT
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
160 message.event.items.addChild(item)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
161 self.sendMessage(jid.JID(pep_jid.host), message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
162
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
163
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
164 def notifyRetract(self, pep_jid, nodeIdentifier, notifications):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
165 for subscriber, subscriptions, items in notifications:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
166 message = self._pubsub_service._createNotification('items', pep_jid,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
167 nodeIdentifier, subscriber,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
168 subscriptions)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
169 for item in items:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
170 retract = domish.Element((None, "retract"))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
171 retract['id'] = item['id']
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
172 message.event.items.addChild(retract)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
173 self.sendMessage(jid.JID(pep_jid.host), message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
174
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
175
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
176 # def notifyDelete(self, service, nodeIdentifier, subscribers,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
177 # redirectURI=None):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
178 # # TODO
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
179 # for subscriber in subscribers:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
180 # message = self._createNotification('delete', service,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
181 # nodeIdentifier,
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
182 # subscriber)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
183 # if redirectURI:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
184 # redirect = message.event.delete.addElement('redirect')
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
185 # redirect['uri'] = redirectURI
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
186 # self.send(message)