annotate sat_pubsub/privilege.py @ 488:c41f37f1b51c

mam: fix `complete` attribute setting in `onArchiveRequest`
author Goffi <goffi@goffi.org>
date Sun, 02 Oct 2022 16:46:04 +0200
parents 8bbaa089cb10
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
1 #!/usr/bin/env python3
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
3 # Copyright (c) 2015-2021 Jérôme Poisson
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
4
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
5
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
6 # This program is free software: you can redistribute it and/or modify
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
9 # (at your option) any later version.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
10
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
11 # This program is distributed in the hope that it will be useful,
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
14 # GNU Affero General Public License for more details.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
15
312
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
5d7c3787672e fixed copyright put in docstring instead of comments
Goffi <goffi@goffi.org>
parents: 293
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
18
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
19 "This module implements XEP-0356 (Privileged Entity) to manage rosters, messages and "
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
20 "presences"
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
21
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
22 from typing import Dict, List, Optional, Union, Set
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
23 import time
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
24
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
25 from twisted.internet import defer
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
26 from twisted.python import log
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
27 from twisted.words.protocols.jabber import error, jid
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
28 from twisted.words.protocols.jabber import xmlstream
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
29 from twisted.words.xish import domish
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel import xmppim
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
31 from wokkel import pubsub
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
32 from wokkel import disco
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
33 from wokkel.compat import IQ
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
34 from wokkel.iwokkel import IPubSubService
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
35
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
36 from .error import NotAllowedError
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
37
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
38 NS_FORWARDED = 'urn:xmpp:forward:0'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
39 NS_PRIV_ENT = 'urn:xmpp:privilege:2'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
40 PRIV_ENT_ADV_XPATH = '/message/privilege[@xmlns="{}"]'.format(NS_PRIV_ENT)
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
41 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
42 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
43 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
44 PERM_PRESENCE = 'presence'
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
45 PERM_IQ = 'iq'
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
46 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
47 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
48 ALLOWED_PRESENCE = ('none', 'managed_entity', 'roster')
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
49 TO_CHECK = {
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
50 PERM_ROSTER:ALLOWED_ROSTER,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
51 PERM_MESSAGE:ALLOWED_MESSAGE,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
52 PERM_PRESENCE:ALLOWED_PRESENCE
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
53 }
482
8bbaa089cb10 privilege: use `typing.Dict[]` instead of `dict[]` to be compatible with Python 3.7
Goffi <goffi@goffi.org>
parents: 478
diff changeset
54 PERMS_BASE : Dict[str, Optional[Union[str, Dict[str, Union[str, bool]]]]]= {
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
55 PERM_ROSTER: None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
56 PERM_MESSAGE: None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
57 PERM_PRESENCE: None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
58 PERM_IQ: None,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
59 }
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
60
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
61
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
62 # Number of seconds before a roster cache is not considered valid anymore.
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
63 # We keep this delay to avoid requesting roster too much in a row if an entity is
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
64 # connecting/disconnecting often in a short time.
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
65 ROSTER_TTL = 3600
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
66
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
67
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
68 Roster = Dict[jid.JID, xmppim.RosterItem]
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
69
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
70
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
71 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
72 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
73
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
74 class PrivilegesHandler(disco.DiscoClientProtocol):
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
75 # FIXME: need to manage updates, XEP-0356 must be updated to get roster pushes
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
76 # TODO: cache
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
77
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
78 def __init__(self, service_jid):
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
79 super(PrivilegesHandler, self).__init__()
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
80 self.backend = None
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
81 self._permissions = PERMS_BASE.copy()
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
82 self._pubsub_service = None
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
83 self.caps_map = {} # key: bare jid, value: dict of resources with caps hash
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
84 # key: (hash,version), value: dict with DiscoInfo instance (infos) and nodes to
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
85 # notify (notify)
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
86 self.hash_map = {}
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
87 # dict which will be filled from database once connection is initialized,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
88 # key: jid, value: dict with "timestamp" and "roster"
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
89 self.roster_cache = None
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
90 # key: jid, value: set of entities who need to receive a notification when we
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
91 # get a presence from them. All entities in value have a presence subscription
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
92 # to the key entity.
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
93 self.presence_map = {}
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
94 # resource currently online
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
95 self.presences = set()
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
96
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 @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
98 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
99 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
100
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
101 async def getRosterCacheFromDB(self):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
102 rows = await self.backend.storage.getRosterCache()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
103 for __, owner_jid, version, timestamp, roster_elt in rows:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
104 roster = self.getRosterFromElement(roster_elt)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
105 self.roster_cache[owner_jid] = {
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
106 "timestamp": timestamp,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
107 "roster": roster,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
108 "version": version
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
109 }
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
110 self.updatePresenceMap(owner_jid, roster, None)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
111
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
112 def connectionInitialized(self):
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
113 for handler in self.parent.handlers:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
114 if IPubSubService.providedBy(handler):
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
115 self._pubsub_service = handler
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
116 break
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
117 self.backend = self.parent.parent.getServiceNamed('backend')
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
118 self.xmlstream.addObserver(PRIV_ENT_ADV_XPATH, self.onAdvertise)
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
119 self.xmlstream.addObserver('/presence', self._onPresence)
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
120 if self.roster_cache is None:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
121 self.roster_cache = {}
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
122 defer.ensureDeferred(self.getRosterCacheFromDB())
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
123
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
124 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
125 """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
126
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
127 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
128 """
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
129 self._permissions = PERMS_BASE.copy()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
130 privilege_elt = next(message.elements(NS_PRIV_ENT, 'privilege'))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
131 for perm_elt in privilege_elt.elements(NS_PRIV_ENT, 'perm'):
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
132 try:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
133 perm_access = perm_elt["access"]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
134 except KeyError:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
135 log.err(f"missing 'access' attribute in perm element: {perm_elt.toXml()}")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
136 continue
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
137 if perm_access in (PERM_ROSTER, PERM_MESSAGE, PERM_PRESENCE):
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
138 try:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
139 perm_type = perm_elt["type"]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
140 except KeyError:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
141 log.err(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
142 "missing 'type' attribute in perm element: "
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
143 f"{perm_elt.toXml()}"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
144 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
145 continue
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
146 else:
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
147 if perm_type not in TO_CHECK[perm_access]:
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
148 log.err(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
149 f'bad type {perm_type!r}: {perm_elt.toXml()}'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
150 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
151 continue
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
152 self._permissions[perm_access] = perm_type or None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
153 elif perm_access == "iq":
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
154 iq_perms = self._permissions["iq"] = {}
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
155 for namespace_elt in perm_elt.elements(NS_PRIV_ENT, "namespace"):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
156 ns = namespace_elt.getAttribute("ns")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
157 perm_type = namespace_elt.getAttribute("type")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
158 if not ns or not perm_type:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
159 log.err(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
160 f"invalid namespace element: {namespace_elt.toXml()}"
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
161 )
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
162 else:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
163 if perm_type not in ("get", "set", "both"):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
164 log.err(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
165 f"invalid namespace type: {namespace_elt.toXml()}"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
166 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
167 else:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
168 ns_perms = iq_perms[ns] = {"type": perm_type}
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
169 ns_perms["get"] = perm_type in ("get", "both")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
170 ns_perms["set"] = perm_type in ("set", "both")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
171 else:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
172 log.err(f"unknown {perm_access!r} access: {perm_elt.toXml()}'")
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
173
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
174 perms = self._permissions
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
175 perms_iq = perms["iq"]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
176 if perms_iq is None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
177 iq_perm_txt = " no iq perm advertised"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
178 elif not isinstance(perms_iq, dict):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
179 raise ValueError('INTERNAL ERROR: "iq" perm should a dict')
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
180 else:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
181 iq_perm_txt = "\n".join(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
182 f" - {ns}: {perms['type']}"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
183 for ns, perms in perms_iq.items()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
184 )
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
185
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
186 log.msg(
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
187 "Privileges updated:\n"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
188 f"roster: {perms[PERM_ROSTER]}\n"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
189 f"message: {perms[PERM_MESSAGE]}\n"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
190 f"presence: {perms[PERM_PRESENCE]}\n"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
191 f"iq:\n{iq_perm_txt}"
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
192 )
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
193
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
194 ## 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
195
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
196 def updatePresenceMap(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
197 self,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
198 owner_jid: jid.JID,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
199 roster: Roster,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
200 old_roster: Optional[Roster]
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
201 ) -> None:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
202 """Update ``self.presence_map`` from roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
203
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
204 @param owner_jid: jid of the owner of the roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
205 @param roster: roster dict as returned by self.getRoster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
206 @param old_roster: previously cached roster if any
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
207 """
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
208 if old_roster is not None:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
209 # we check if presence subscription have not been removed and update
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
210 # presence_map accordingly
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
211 for roster_jid, roster_item in old_roster.items():
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
212 if ((roster_item.subscriptionFrom
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
213 and (roster_jid not in roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
214 or not roster[roster_jid].subscriptionFrom)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
215 )):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
216 try:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
217 self.presence_map[roster_jid].discard(owner_jid)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
218 except KeyError:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
219 pass
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
220 if ((roster_item.subscriptionTo
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
221 and (roster_jid not in roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
222 or not roster[roster_jid].subscriptionTo)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
223 )):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
224 try:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
225 self.presence_map[owner_jid].discard(roster_jid)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
226 except KeyError:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
227 pass
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
228
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
229 for roster_jid, roster_item in roster.items():
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
230 if roster_item.subscriptionFrom:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
231 # we need to know who is subscribed to our user, to send them
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
232 # notifications when they send presence to us
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
233 self.presence_map.setdefault(roster_jid, set()).add(owner_jid)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
234 if ((roster_item.subscriptionTo
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
235 and jid.JID(roster_jid.host) == self.backend.server_jid)):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
236 # we also need to know who on this server we are subscribed to, so
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
237 # we can get their notifications even if they didn't connect so far.
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
238 self.presence_map.setdefault(owner_jid, set()).add(roster_jid)
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
239
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
240 def serialiseRoster(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
241 self,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
242 roster: Roster,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
243 version: Optional[str] = None
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
244 ) -> domish.Element:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
245 """Reconstruct Query element of the roster"""
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
246 roster_elt = domish.Element((ROSTER_NS, "query"))
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
247 if version:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
248 roster_elt["ver"] = version
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
249 for item in roster.values():
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
250 roster_elt.addChild(item.toElement())
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
251 return roster_elt
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
252
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
253 async def updateRosterCache(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
254 self,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
255 owner_jid: jid.JID,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
256 roster: Roster,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
257 version: str
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
258 ) -> None:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
259 """Update local roster cache and database"""
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
260 now = time.time()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
261 self.roster_cache[owner_jid] = {
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
262 'timestamp': now,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
263 'roster': roster,
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
264 'version': version
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
265 }
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
266 roster_elt = self.serialiseRoster(roster, version)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
267 await self.backend.storage.setRosterCache(
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
268 owner_jid, version, now, roster_elt
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
269 )
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
270
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
271 def getRosterFromElement(self, query_elt: domish.Element) -> Roster:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
272 """Parse roster query result payload to get a Roster dict"""
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
273 roster = {}
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
274 for element in query_elt.elements(ROSTER_NS, 'item'):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
275 item = xmppim.RosterItem.fromElement(element)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
276 roster[item.entity] = item
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
277 return roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
278
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
279 async def getRoster(self, to_jid: jid.JID) -> Optional[Roster]:
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
280 """Retrieve contact list.
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
281
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
282 @param to_jid: jid of the entity owning the roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
283 @return: roster data
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
284 """
470
a549c8e17827 privilege: don't try to get roster if the request if for a jid not belonging to our server
Goffi <goffi@goffi.org>
parents: 467
diff changeset
285 if jid.JID(to_jid.host) != self.backend.server_jid:
a549c8e17827 privilege: don't try to get roster if the request if for a jid not belonging to our server
Goffi <goffi@goffi.org>
parents: 467
diff changeset
286 # no need to try to get the roster if it's not a user of our own server
a549c8e17827 privilege: don't try to get roster if the request if for a jid not belonging to our server
Goffi <goffi@goffi.org>
parents: 467
diff changeset
287 return 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
288 if self._permissions[PERM_ROSTER] not in ('get', 'both'):
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
289 raise NotAllowedError('roster get is not allowed')
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
290
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
291 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
292 iq.addElement((ROSTER_NS, 'query'))
242
a6170637690d remote roster partial support
Goffi <goffi@goffi.org>
parents:
diff changeset
293 iq["to"] = to_jid.userhost()
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
294 iq_result = await iq.send()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
295 roster = self.getRosterFromElement(iq_result.query)
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
296
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
297 version = iq_result.query.getAttribute('ver')
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
298 cached_roster = self.roster_cache.get("to_jid")
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
299 if not cached_roster:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
300 self.updatePresenceMap(to_jid, roster, None)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
301 await self.updateRosterCache(to_jid, roster, version)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
302 else:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
303 # we already have a roster in cache, we have to check it if the new one is
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
304 # modified, and update presence_map and database
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
305 if version:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
306 if cached_roster["version"] != version:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
307 self.updatePresenceMap(to_jid, roster, cached_roster["roster"])
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
308 await self.updateRosterCache(to_jid, roster, version)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
309 else:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
310 cached_roster["timestamp"] = time.time()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
311 else:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
312 # no version available, we have to compare the whole XML
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
313 if ((self.serialiseRoster(cached_roster["roster"]).toXml() !=
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
314 self.serialiseRoster(roster))):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
315 self.updatePresenceMap(to_jid, roster, cached_roster["roster"])
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
316 await self.updateRosterCache(to_jid, roster, version)
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
317 else:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
318 cached_roster["timestamp"] = time.time()
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
319
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
320 return roster
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
321
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
322 async def isSubscribedFrom(self, entity: jid.JID, roster_owner_jid: jid.JID) -> bool:
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
323 """Check if entity has presence subscription from roster_owner_jid
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
324
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
325 @param entity: entity to check subscription to
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
326 @param roster_owner_jid: owner of the roster to check
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
327 @return: True if entity has a subscription from roster_owner_jid
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
328 """
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
329 roster = await self.getRoster(roster_owner_jid)
348
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
330 try:
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
331 return roster[entity.userhostJID()].subscriptionFrom
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
332 except KeyError:
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
333 return False
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
334
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
335 ## message ##
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
336
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
337 def sendMessage(self, priv_message, to_jid=None):
348
d1f63ae1eaf4 privilege: added isSubscribedFrom method to check if an entity has presence subscription from an other entity.
Goffi <goffi@goffi.org>
parents: 343
diff changeset
338 """Send privileged message (in the name of the server)
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
339
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
340 @param priv_message(domish.Element): privileged message
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
341 @param to_jid(jid.JID, None): main message destinee
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
342 None to use our own server
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
343 """
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
344 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
345 log.msg("WARNING: permission not allowed to send privileged messages")
471
ed9e12701e0f backend: return empty roster when `NotAllowedError` is raised in `getOwnerRoster`:
Goffi <goffi@goffi.org>
parents: 470
diff changeset
346 raise NotAllowedError('privileged messages are not allowed')
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
347
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
348 main_message = domish.Element((None, "message"))
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
349 if to_jid is None:
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
350 to_jid = self.backend.server_jid
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
351 main_message['to'] = to_jid.full()
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
352 privilege_elt = main_message.addElement((NS_PRIV_ENT, 'privilege'))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
353 forwarded_elt = privilege_elt.addElement((NS_FORWARDED, 'forwarded'))
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
354 priv_message['xmlns'] = 'jabber:client'
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
355 forwarded_elt.addChild(priv_message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
356 self.send(main_message)
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
357
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
358 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
359 """Do notifications using privileges"""
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
360 for subscriber, subscriptions, items in notifications:
455
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
361 message = self._pubsub_service._createNotification(
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
362 'items',
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
363 pep_jid,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
364 nodeIdentifier,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
365 subscriber,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
366 subscriptions
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
367 )
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
368 for item in items:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
369 item.uri = pubsub.NS_PUBSUB_EVENT
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
370 message.event.items.addChild(item)
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
371 self.sendMessage(message)
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
372
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
373 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
374 for subscriber, subscriptions, items in notifications:
455
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
375 message = self._pubsub_service._createNotification(
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
376 'items',
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
377 pep_jid,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
378 nodeIdentifier,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
379 subscriber,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
380 subscriptions
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
381 )
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
382 for item in items:
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
383 retract = domish.Element((None, "retract"))
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
384 retract['id'] = item['id']
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
385 message.event.items.addChild(retract)
321
c7fe09894952 privilege: better handling of main message 'to' attribute (i.e. privileged entity's server)
Goffi <goffi@goffi.org>
parents: 312
diff changeset
386 self.sendMessage(message)
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
387
455
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
388 def notifyDelete(self, pep_jid, nodeIdentifier, subscribers, redirectURI=None):
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
389 for subscriber in subscribers:
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
390 message = self._pubsub_service._createNotification(
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
391 'delete',
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
392 pep_jid,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
393 nodeIdentifier,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
394 subscriber
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
395 )
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
396 if redirectURI:
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
397 redirect = message.event.delete.addElement('redirect')
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
398 redirect['uri'] = redirectURI
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
399 self.sendMessage(message)
293
b96a4ac25f8b privilege: added methods to send privileged messages and notifications
Goffi <goffi@goffi.org>
parents: 286
diff changeset
400
455
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
401 def notifyPurge(self, pep_jid, nodeIdentifier, subscribers):
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
402 for subscriber in subscribers:
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
403 message = self._pubsub_service._createNotification(
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
404 'purge',
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
405 pep_jid,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
406 nodeIdentifier,
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
407 subscriber
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
408 )
0b5233981671 backend: fix `delete` notification + add `purge` notification
Goffi <goffi@goffi.org>
parents: 435
diff changeset
409 self.sendMessage(message)
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
410
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
411 ## presence ##
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
412
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
413 def _onPresence(self, presence_elt: domish.Element) -> None:
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
414 defer.ensureDeferred(self.onPresence(presence_elt))
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
415
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
416 async def onPresence(self, presence_elt: domish.Element) -> None:
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
417 from_jid = jid.JID(presence_elt['from'])
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
418 from_jid_bare = from_jid.userhostJID()
460
607616f9ef5b backend: new `server_jid` option:
Goffi <goffi@goffi.org>
parents: 455
diff changeset
419 if ((jid.JID(from_jid.host) == self.backend.server_jid
467
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
420 and (
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
421 from_jid_bare not in self.roster_cache
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
422 or time.time()-self.roster_cache[from_jid_bare]["timestamp"]>ROSTER_TTL
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
423 ))):
d86e0f8a1405 privilege: store roster cache in database:
Goffi <goffi@goffi.org>
parents: 463
diff changeset
424 roster = await self.getRoster(from_jid)
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
425
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
426 presence_type = presence_elt.getAttribute('type')
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
427 if presence_type == "unavailable":
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
428 self.presences.discard(from_jid)
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
429 elif from_jid not in self.presences:
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
430 # new resource available
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
431
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
432 # we keep resources present in cache to avoid sending notifications on each
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
433 # status change
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
434 self.presences.add(from_jid)
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
435
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
436 # we check entity capabilities
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
437 try:
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
438 c_elt = next(
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
439 presence_elt.elements('http://jabber.org/protocol/caps', 'c')
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
440 )
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
441 hash_ = c_elt['hash']
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
442 ver = c_elt['ver']
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
443 except (StopIteration, KeyError):
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
444 # no capabilities, we don't go further
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
445 return
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
446
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
447 # FIXME: hash is not checked (cf. XEP-0115)
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
448 disco_tuple = (hash_, ver)
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
449
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
450 if disco_tuple not in self.hash_map:
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
451 # first time we se this hash, what is behind it?
435
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
452 try:
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
453 infos = await self.requestInfo(from_jid)
435
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
454 except error.StanzaError as e:
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
455 log.msg(
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
456 f"WARNING: can't request disco info for {from_jid!r} (presence: "
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
457 f"{presence_type}): {e}"
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
458 )
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
459 else:
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
460 self.hash_map[disco_tuple] = {
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
461 'notify': {
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
462 f[:-7] for f in infos.features if f.endswith('+notify')
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
463 },
96342e7e9f5d privilege: log error when `requestInfo` is failing
Goffi <goffi@goffi.org>
parents: 414
diff changeset
464 'infos': infos
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
465 }
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
466
400
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
467 # jid_caps must be filled only after hash_map is set, to be sure that
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
468 # the hash data is available in getAutoSubscribers
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
469 jid_caps = self.caps_map.setdefault(from_jid_bare, {})
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
470 if from_jid.resource not in jid_caps:
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
471 jid_caps[from_jid.resource] = disco_tuple
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
472
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
473 # nodes are the nodes subscribed with +notify
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
474 nodes = tuple(self.hash_map[disco_tuple]['notify'])
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
475 if not nodes:
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
476 return
462
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
477 # publishers are entities which have granted presence access to our user
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
478 # + user itself + server
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
479 publishers = (
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
480 tuple(self.presence_map.get(from_jid_bare, ()))
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
481 + (from_jid_bare, self.backend.server_jid)
a017af61a32b privilege: add server JID to `publisher` so `+notify` works with it:
Goffi <goffi@goffi.org>
parents: 460
diff changeset
482 )
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
483
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
484 # FIXME: add "presence" access_model (for node) for getLastItems
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
485 # TODO: manage other access model (whitelist, …)
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
486 last_items = await self.backend.storage.getLastItems(
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
487 publishers,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
488 nodes,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
489 ('open', 'presence'), ('open', 'presence'), True
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
490 )
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
491 # we send message with last item, as required by
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
492 # https://xmpp.org/extensions/xep-0163.html#notify-last
338
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
493 for pep_jid, node, item, item_access_model in last_items:
6d059f07c2d3 privilege: added presence and +notify initial support:
Goffi <goffi@goffi.org>
parents: 321
diff changeset
494 self.notifyPublish(pep_jid, node, [(from_jid, None, [item])])
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
495
478
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
496 ## IQ ##
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
497
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
498 async def sendIQ(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
499 self,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
500 priv_iq: domish.Element,
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
501 to: Optional[jid.JID] = None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
502 ) -> domish.Element:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
503 """Send privileged IQ stanza
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
504
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
505 @param priv_iq: privileged IQ stanza
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
506 @param to: bare jid of user on behalf of who the stanza is sent
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
507 The stanza will be wrapped and sent to the server. Result/Error stanza will sent
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
508 back as return value.
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
509 """
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
510 if to is None:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
511 try:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
512 to = jid.JID(priv_iq["from"])
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
513 except (KeyError, RuntimeError):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
514 raise ValueError(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
515 'no "to" specified, and invalid "to" attribute in priv_iq'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
516 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
517 if not to.user or to.resource or to.host != self.backend.server_jid.userhost():
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
518 raise NotAllowedError(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
519 f'"to" attribute must be set to a bare jid of the server, {to} is invalid'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
520 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
521 iq_type = priv_iq.getAttribute("type")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
522 if iq_type not in ("get", "set"):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
523 raise ValueError(f"invalid IQ type: {priv_iq.toXml()}")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
524 first_child = priv_iq.firstChildElement()
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
525 iq_perms: Optional[dict] = self._permissions[PERM_IQ]
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
526
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
527 if ((not iq_perms or first_child is None or first_child.uri is None
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
528 or not iq_perms.get(first_child.uri, {}).get(iq_type, False))):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
529 raise NotAllowedError(
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
530 "privileged IQ stanza not allowed for this namespace/type combination "
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
531 f"{priv_iq.toXml()}"
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
532 )
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
533
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
534 main_iq = xmlstream.IQ(self.xmlstream, iq_type)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
535 main_iq.timeout = 120
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
536 privileged_iq_elt = main_iq.addElement((NS_PRIV_ENT, "privileged_iq"))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
537 priv_iq['xmlns'] = 'jabber:client'
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
538 privileged_iq_elt.addChild(priv_iq)
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
539 ret_elt = await main_iq.send(to.full())
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
540 # we unwrap the result
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
541 for name, ns in (
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
542 ("privilege", NS_PRIV_ENT),
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
543 ("forwarded", NS_FORWARDED),
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
544 ("iq", "jabber:client")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
545 ):
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
546 try:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
547 ret_elt = next(ret_elt.elements(ns, name))
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
548 except StopIteration:
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
549 raise ValueError(f"Invalid privileged IQ result: {ret_elt.toXml()}")
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
550 return ret_elt
b544109ab4c4 Privileged Entity update + Pubsub Account Management partial implementation + Public Pubsub Subscription
Goffi <goffi@goffi.org>
parents: 471
diff changeset
551
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
552 ## misc ##
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
553
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
554 async def getAutoSubscribers(
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
555 self,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
556 recipient: jid.JID,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
557 nodeIdentifier: str,
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
558 explicit_subscribers: Set[jid.JID]
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
559 ) -> List[jid.JID]:
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
560 """Get automatic subscribers
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
561
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
562 Get subscribers with presence subscription and +notify for this node
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
563 @param recipient: jid of the PEP owner of this node
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
564 @param nodeIdentifier: node
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
565 @param explicit_subscribers: jids of people which have an explicit subscription
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
566 @return: full jid of automatically subscribed entities
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
567 """
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
568 auto_subscribers = []
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
569 roster = await self.getRoster(recipient)
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
570 for roster_jid, roster_item in roster.items():
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
571 if roster_jid in explicit_subscribers:
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
572 continue
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
573 if roster_item.subscriptionFrom:
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
574 try:
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
575 online_resources = self.caps_map[roster_jid]
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
576 except KeyError:
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
577 continue
414
ccb2a22ea0fc Python 3 port:
Goffi <goffi@goffi.org>
parents: 405
diff changeset
578 for res, disco_tuple in online_resources.items():
400
371e72871e19 privilege: fill hash_map before jid_caps to avoid KeyError in getAutoSubscribers
Goffi <goffi@goffi.org>
parents: 369
diff changeset
579 notify = self.hash_map[disco_tuple]['notify']
343
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
580 if nodeIdentifier in notify:
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
581 full_jid = jid.JID(tuple=(roster_jid.user, roster_jid.host, res))
ff8aff4c9b79 backend, psql: implemented notifications for auto subscribers in PEP:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
582 auto_subscribers.append(full_jid)
463
f520ac3164b0 privilege: improvment on last message sending on presence with `+notify`:
Goffi <goffi@goffi.org>
parents: 462
diff changeset
583 return auto_subscribers