Mercurial > libervia-backend
annotate libervia/backend/memory/memory.py @ 4242:8acf46ed7f36
frontends: remote control implementation:
This is the frontends common part of remote control implementation. It handle the creation
of WebRTC session, and management of inputs. For now the reception use freedesktop.org
Desktop portal, and works mostly with Wayland based Desktop Environments.
rel 436
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 11 May 2024 13:52:43 +0200 |
parents | 730f542e4ad0 |
children | 9fc3d28bc3f6 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
3 # Libervia: an XMPP client |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
0 | 5 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
9 # (at your option) any later version. |
0 | 10 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
14 # GNU Affero General Public License for more details. |
0 | 15 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
19 from collections import namedtuple |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
20 import copy |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
21 from dataclasses import dataclass |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
22 from functools import partial |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
23 import mimetypes |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
24 import os.path |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
25 from pathlib import Path |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
26 import time |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
27 from typing import Dict, Optional, Tuple |
3206
ae09989e9feb
core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
28 from uuid import uuid4 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
29 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
30 import shortuuid |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
31 from twisted.internet import defer, error, reactor |
1447 | 32 from twisted.python import failure |
47 | 33 from twisted.words.protocols.jabber import jid |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
34 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
35 from libervia.backend.core import exceptions |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
36 from libervia.backend.core.constants import Const as C |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
37 from libervia.backend.core.core_types import SatXMPPEntity |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
39 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
40 from libervia.backend.memory.crypto import BlockCipher |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
41 from libervia.backend.memory.crypto import PasswordHasher |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
42 from libervia.backend.memory.disco import Discovery |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
43 from libervia.backend.memory.params import Params |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
44 from libervia.backend.memory.persistent import PersistentDict |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
45 from libervia.backend.memory.sqla import ( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
46 Notification, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
47 NotificationPriority, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
48 NotificationStatus, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
49 NotificationType, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
50 Storage, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
51 ) |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
52 from libervia.backend.tools import config as tools_config |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
53 from libervia.backend.tools.common import data_format |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
54 from libervia.backend.tools.common import regex |
3206
ae09989e9feb
core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
55 |
ae09989e9feb
core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
56 |
ae09989e9feb
core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
57 log = getLogger(__name__) |
420
acd908528ef7
core: profile creation/deletion through database
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
58 |
679
59c9a7ff903d
wix, misc: use picture of dimension 32x32 for tray icon + better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
677
diff
changeset
|
59 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
60 PresenceTuple = namedtuple("PresenceTuple", ("show", "priority", "statuses")) |
1447 | 61 MSG_NO_SESSION = "Session id doesn't exist or is finished" |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
62 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
63 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
64 class Sessions(object): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
65 """Sessions are data associated to key used for a temporary moment, with optional profile checking.""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
66 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
67 DEFAULT_TIMEOUT = 600 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
68 |
1215
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
69 def __init__(self, timeout=None, resettable_timeout=True): |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
70 """ |
1215
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
71 @param timeout (int): nb of seconds before session destruction |
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
72 @param resettable_timeout (bool): if True, the timeout is reset on each access |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
73 """ |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
74 self._sessions = dict() |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
75 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT |
1215
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
76 self.resettable_timeout = resettable_timeout |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
77 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
78 def new_session(self, session_data=None, session_id=None, profile=None): |
1447 | 79 """Create a new session |
80 | |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
81 @param session_data: mutable data to use, default to a dict |
1212
628e320eab1f
memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
82 @param session_id (str): force the session_id to the given string |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
83 @param profile: if set, the session is owned by the profile, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
84 and profile_get must be used instead of __getitem__ |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
85 @return: session_id, session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
86 """ |
1212
628e320eab1f
memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
87 if session_id is None: |
628e320eab1f
memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
88 session_id = str(uuid4()) |
1213
b5928601d7aa
memory: handle the case where an existing session ID is re-used for creating a new session
souliane <souliane@mailoo.org>
parents:
1212
diff
changeset
|
89 elif session_id in self._sessions: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
90 raise exceptions.ConflictError( |
3028 | 91 "Session id {} is already used".format(session_id) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
92 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
93 timer = reactor.callLater(self.timeout, self._purge_session, session_id) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
94 if session_data is None: |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
95 session_data = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
96 self._sessions[session_id] = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
97 (timer, session_data) if profile is None else (timer, session_data, profile) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
98 ) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
99 return session_id, session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
100 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
101 def _purge_session(self, session_id): |
1447 | 102 try: |
103 timer, session_data, profile = self._sessions[session_id] | |
104 except ValueError: | |
105 timer, session_data = self._sessions[session_id] | |
106 profile = None | |
107 try: | |
108 timer.cancel() | |
109 except error.AlreadyCalled: | |
110 # if the session is time-outed, the timer has been called | |
111 pass | |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
112 del self._sessions[session_id] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
113 log.debug( |
3028 | 114 "Session {} purged{}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
115 session_id, |
3028 | 116 " (profile {})".format(profile) if profile is not None else "", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
117 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
118 ) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
119 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
120 def __len__(self): |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
121 return len(self._sessions) |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
122 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
123 def __contains__(self, session_id): |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
124 return session_id in self._sessions |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
125 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
126 def profile_get(self, session_id, profile): |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
127 try: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
128 timer, session_data, profile_set = self._sessions[session_id] |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
129 except ValueError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
130 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
131 "You need to use __getitem__ when profile is not set" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
132 ) |
1447 | 133 except KeyError: |
134 raise failure.Failure(KeyError(MSG_NO_SESSION)) | |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
135 if profile_set != profile: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
136 raise exceptions.InternalError("current profile differ from set profile !") |
1215
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
137 if self.resettable_timeout: |
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
138 timer.reset(self.timeout) |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
139 return session_data |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
140 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
141 def __getitem__(self, session_id): |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
142 try: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
143 timer, session_data = self._sessions[session_id] |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
144 except ValueError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
145 raise exceptions.InternalError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
146 "You need to use profile_get instead of __getitem__ when profile is set" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
147 ) |
1447 | 148 except KeyError: |
149 raise failure.Failure(KeyError(MSG_NO_SESSION)) | |
1215
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
150 if self.resettable_timeout: |
d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
souliane <souliane@mailoo.org>
parents:
1214
diff
changeset
|
151 timer.reset(self.timeout) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
152 return session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
153 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
154 def __setitem__(self, key, value): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
155 raise NotImplementedError("You need do use new_session to create a session") |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
156 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
157 def __delitem__(self, session_id): |
1474
c2a498dce4b4
core (memory): fixed a double timer.cancel in Sessions
Goffi <goffi@goffi.org>
parents:
1460
diff
changeset
|
158 """ delete the session data """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
159 self._purge_session(session_id) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
160 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
161 def keys(self): |
3028 | 162 return list(self._sessions.keys()) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
163 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
164 def iterkeys(self): |
3028 | 165 return iter(self._sessions.keys()) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
166 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
167 |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
168 class ProfileSessions(Sessions): |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
169 """ProfileSessions extends the Sessions class, but here the profile can be |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
170 used as the key to retrieve data or delete a session (instead of session id). |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
171 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
172 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
173 def _profile_get_all_ids(self, profile): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
174 """Return a list of the sessions ids that are associated to the given profile. |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
175 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
176 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
177 @return: a list containing the sessions ids |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
178 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
179 ret = [] |
3028 | 180 for session_id in self._sessions.keys(): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
181 try: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
182 timer, session_data, profile_set = self._sessions[session_id] |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
183 except ValueError: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
184 continue |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
185 if profile == profile_set: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
186 ret.append(session_id) |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
187 return ret |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
188 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
189 def profile_get_unique(self, profile): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
190 """Return the data of the unique session that is associated to the given profile. |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
191 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
192 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
193 @return: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
194 - mutable data (default: dict) of the unique session |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
195 - None if no session is associated to the profile |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
196 - raise an error if more than one session are found |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
197 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
198 ids = self._profile_get_all_ids(profile) |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
199 if len(ids) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
200 raise exceptions.InternalError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
201 "profile_get_unique has been used but more than one session has been found!" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
202 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
203 return ( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
204 self.profile_get(ids[0], profile) if len(ids) == 1 else None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
205 ) # XXX: timeout might be reset |
1214
ed3b01ed70d7
memory: profileSessions.profileGetUnique was not resetting the timer
souliane <souliane@mailoo.org>
parents:
1213
diff
changeset
|
206 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
207 def profile_del_unique(self, profile): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
208 """Delete the unique session that is associated to the given profile. |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
209 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
210 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
211 @return: None, but raise an error if more than one session are found |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
212 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
213 ids = self._profile_get_all_ids(profile) |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
214 if len(ids) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
215 raise exceptions.InternalError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
216 "profile_del_unique has been used but more than one session has been found!" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
217 ) |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
218 if len(ids) == 1: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
219 del self._sessions[ids[0]] |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
220 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
221 |
1247
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
222 class PasswordSessions(ProfileSessions): |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
223 |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
224 # FIXME: temporary hack for the user personal key not to be lost. The session |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
225 # must actually be purged and later, when the personal key is needed, the |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
226 # profile password should be asked again in order to decrypt it. |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
227 def __init__(self, timeout=None): |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
228 ProfileSessions.__init__(self, timeout, resettable_timeout=False) |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
229 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
230 def _purge_session(self, session_id): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
231 log.debug( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
232 "FIXME: PasswordSessions should ask for the profile password after the session expired" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
233 ) |
1247
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
234 |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
235 |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3541
diff
changeset
|
236 class Memory: |
1199 | 237 """This class manage all the persistent information""" |
0 | 238 |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
239 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
240 log.info(_("Memory manager init")) |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
241 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
242 self._entities_cache = {} # XXX: keep presence/last resource/other data in cache |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
243 # /!\ an entity is not necessarily in roster |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
244 # main key is bare jid, value is a dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
245 # where main key is resource, or None for bare jid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
246 self._key_signals = set() # key which need a signal to frontends when updated |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
247 self.subscriptions = {} |
1247
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
248 self.auth_sessions = PasswordSessions() # remember the authenticated profiles |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
249 self.disco = Discovery(host) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
250 self.config = tools_config.parse_main_conf(log_filenames=True) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
251 self._cache_path = Path(self.config_get("", "local_dir"), C.CACHE_DIR) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
252 self.admins = self.config_get("", "admins_list", []) |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
253 self.admin_jids = set() |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
254 |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
255 |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
256 async def initialise(self): |
3582
71516731d0aa
core (memory/sqla): database migration using Alembic:
Goffi <goffi@goffi.org>
parents:
3541
diff
changeset
|
257 self.storage = Storage() |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
258 await self.storage.initialise() |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
428
diff
changeset
|
259 PersistentDict.storage = self.storage |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
260 self.params = Params(self.host, self.storage) |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
261 log.info(_("Loading default params template")) |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
262 self.params.load_default_params() |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
263 await self.load() |
443
7099ea9c1b12
core: getPrivate/setPrivate removed from memory, private values now use database storage and persistent dicts \o/
Goffi <goffi@goffi.org>
parents:
432
diff
changeset
|
264 self.memory_data = PersistentDict("memory") |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
265 await self.memory_data.load() |
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
266 await self.disco.load() |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
267 for admin in self.admins: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
268 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
269 admin_jid_s = await self.param_get_a_async( |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
270 "JabberID", "Connection", profile_key=admin |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
271 ) |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
272 except Exception as e: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
273 log.warning(f"Can't retrieve jid of admin {admin!r}: {e}") |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
274 else: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
275 if admin_jid_s is not None: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
276 try: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
277 admin_jid = jid.JID(admin_jid_s).userhostJID() |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
278 except RuntimeError: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
279 log.warning(f"Invalid JID for admin {admin}: {admin_jid_s}") |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
280 else: |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
281 self.admin_jids.add(admin_jid) |
3537
f9a5b810f14d
core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
3523
diff
changeset
|
282 |
0 | 283 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
284 ## Configuration ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
285 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
286 def config_get(self, section, name, default=None): |
364 | 287 """Get the main configuration option |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
288 |
364 | 289 @param section: section of the config file (None or '' for DEFAULT) |
290 @param name: name of the option | |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
291 @param default: value to use if not found |
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
292 @return: str, list or dict |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
364
diff
changeset
|
293 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
294 return tools_config.config_get(self.config, section, name, default) |
364 | 295 |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
296 def load_xml(self, filename): |
1015
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
297 """Load parameters template from xml file |
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
298 |
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
299 @param filename (str): input file |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
300 @return: bool: True in case of success |
1015
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
301 """ |
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
302 if not filename: |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
303 return False |
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
304 filename = os.path.expanduser(filename) |
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
305 if os.path.exists(filename): |
0 | 306 try: |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
307 self.params.load_xml(filename) |
3028 | 308 log.debug(_("Parameters loaded from file: %s") % filename) |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
309 return True |
557
4f856dd4c0d0
core: paramaters are now merged: if a parameter doens't exist in loaded xml but exists in default parameters, it is added
Goffi <goffi@goffi.org>
parents:
556
diff
changeset
|
310 except Exception as e: |
3028 | 311 log.error(_("Can't load parameters from file: %s") % e) |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
312 return False |
0 | 313 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
314 def save_xml(self, filename): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
315 """Save parameters template to xml file |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
316 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
317 @param filename (str): output file |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
318 @return: bool: True in case of success |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
319 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
320 if not filename: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
321 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
322 # TODO: need to encrypt files (at least passwords !) and set permissions |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
323 filename = os.path.expanduser(filename) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
324 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
325 self.params.save_xml(filename) |
3028 | 326 log.debug(_("Parameters saved to file: %s") % filename) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
327 return True |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
328 except Exception as e: |
3028 | 329 log.error(_("Can't save parameters to file: %s") % e) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
330 return False |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
331 |
443
7099ea9c1b12
core: getPrivate/setPrivate removed from memory, private values now use database storage and persistent dicts \o/
Goffi <goffi@goffi.org>
parents:
432
diff
changeset
|
332 def load(self): |
428
a4a9efadabfc
core: fixed memory initialisation sequence
Goffi <goffi@goffi.org>
parents:
425
diff
changeset
|
333 """Load parameters and all memory things from db""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
334 # parameters data |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
335 return self.params.load_gen_params() |
412 | 336 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
337 def load_individual_params(self, profile): |
412 | 338 """Load individual parameters for a profile |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
339 @param profile: %(doc_profile)s""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
340 return self.params.load_ind_params(profile) |
412 | 341 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
342 ## Profiles/Sessions management ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
343 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
344 def start_session(self, password, profile): |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
345 """"Iniatialise session for a profile |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
346 |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
347 @param password(unicode): profile session password |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
348 or empty string is no password is set |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
349 @param profile: %(doc_profile)s |
1694
3c608d660f0b
core (memory): profile is checked in startSession (an error is raised if it doesn't exist)
Goffi <goffi@goffi.org>
parents:
1693
diff
changeset
|
350 @raise exceptions.ProfileUnknownError if profile doesn't exists |
3c608d660f0b
core (memory): profile is checked in startSession (an error is raised if it doesn't exist)
Goffi <goffi@goffi.org>
parents:
1693
diff
changeset
|
351 @raise exceptions.PasswordError: the password does not match |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
352 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
353 profile = self.get_profile_name(profile) |
1694
3c608d660f0b
core (memory): profile is checked in startSession (an error is raised if it doesn't exist)
Goffi <goffi@goffi.org>
parents:
1693
diff
changeset
|
354 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
355 def create_session(__): |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
356 """Called once params are loaded.""" |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
357 self._entities_cache[profile] = {} |
3028 | 358 log.info("[{}] Profile session started".format(profile)) |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
359 return False |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
360 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
361 def backend_initialised(__): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
362 def do_start_session(__=None): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
363 if self.is_session_started(profile): |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
364 log.info("Session already started!") |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
365 return True |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
366 try: |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
367 # if there is a value at this point in self._entities_cache, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
368 # it is the load_individual_params Deferred, the session is starting |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
369 session_d = self._entities_cache[profile] |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
370 except KeyError: |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
371 # else we do request the params |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
372 session_d = self._entities_cache[profile] = self.load_individual_params( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
373 profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
374 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
375 session_d.addCallback(create_session) |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
376 finally: |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
377 return session_d |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
378 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
379 auth_d = defer.ensureDeferred(self.profile_authenticate(password, profile)) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
380 auth_d.addCallback(do_start_session) |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
381 return auth_d |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
382 |
4193
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4159
diff
changeset
|
383 if self.host.init_pre_script.called: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
384 return defer.succeed(None).addCallback(backend_initialised) |
1723
5632c5350296
core: avoid initialisation blocking when a long callback is added to backend.initialised (could prevent sessions starting)
Goffi <goffi@goffi.org>
parents:
1704
diff
changeset
|
385 else: |
4193
730f542e4ad0
core: add new `init_script_path` option:
Goffi <goffi@goffi.org>
parents:
4159
diff
changeset
|
386 return self.host.init_pre_script.addCallback(backend_initialised) |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
387 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
388 def stop_session(self, profile): |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
389 """Delete a profile session |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
390 |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
391 @param profile: %(doc_profile)s |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
392 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
393 if self.host.is_connected(profile): |
3028 | 394 log.debug("Disconnecting profile because of session stop") |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
395 self.host.disconnect(profile) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
396 self.auth_sessions.profile_del_unique(profile) |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
397 try: |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
398 self._entities_cache[profile] |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
399 except KeyError: |
3028 | 400 log.warning("Profile was not in cache") |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
401 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
402 def _is_session_started(self, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
403 return self.is_session_started(self.get_profile_name(profile_key)) |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
404 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
405 def is_session_started(self, profile): |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
406 try: |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
407 # XXX: if the value in self._entities_cache is a Deferred, |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
408 # the session is starting but not started yet |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
409 return not isinstance(self._entities_cache[profile], defer.Deferred) |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
410 except KeyError: |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
411 return False |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
412 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
413 async def profile_authenticate(self, password, profile): |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
414 """Authenticate the profile. |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
415 |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
416 @param password (unicode): the SàT profile password |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
417 @return: None in case of success (an exception is raised otherwise) |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
418 @raise exceptions.PasswordError: the password does not match |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
419 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
420 if not password and self.auth_sessions.profile_get_unique(profile): |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
421 # XXX: this allows any frontend to connect with the empty password as soon as |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
422 # the profile has been authenticated at least once before. It is OK as long as |
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
423 # submitting a form with empty passwords is restricted to local frontends. |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
424 return |
1591
0df9c6247474
core: profile session starting and connection are now separated. Moved profile session starting/authentication to memory module
Goffi <goffi@goffi.org>
parents:
1587
diff
changeset
|
425 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
426 sat_cipher = await self.param_get_a_async( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
427 C.PROFILE_PASS_PATH[1], C.PROFILE_PASS_PATH[0], profile_key=profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
428 ) |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
429 valid = PasswordHasher.verify(password, sat_cipher) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
430 if not valid: |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
431 log.warning(_("Authentication failure of profile {profile}").format( |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
432 profile=profile)) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
433 raise exceptions.PasswordError("The provided profile password doesn't match.") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
434 return await self.new_auth_session(password, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
435 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
436 async def new_auth_session(self, key, profile): |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
437 """Start a new session for the authenticated profile. |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
438 |
3128
73b5228715e8
core (memory): avoid session locking if profileAuthenticate is called twice quickly
Goffi <goffi@goffi.org>
parents:
3123
diff
changeset
|
439 If there is already an existing session, no new one is created |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
440 The personal key is loaded encrypted from a PersistentDict before being decrypted. |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
441 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
442 @param key: the key to decrypt the personal key |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
443 @param profile: %(doc_profile)s |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
444 """ |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
445 data = await PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
446 personal_key = BlockCipher.decrypt(key, data[C.MEMORY_CRYPTO_KEY]) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
447 # Create the session for this profile and store the personal key |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
448 session_data = self.auth_sessions.profile_get_unique(profile) |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
449 if not session_data: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
450 self.auth_sessions.new_session( |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
451 {C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
452 ) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
453 log.debug("auth session created for profile %s" % profile) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
454 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
455 def purge_profile_session(self, profile): |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
456 """Delete cache of data of profile |
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
457 @param profile: %(doc_profile)s""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
458 log.info(_("[%s] Profile session purge" % profile)) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
459 self.params.purge_profile(profile) |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
460 try: |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
461 del self._entities_cache[profile] |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
462 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
463 log.error( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
464 _( |
3028 | 465 "Trying to purge roster status cache for a profile not in memory: [%s]" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
466 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
467 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
468 ) |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
469 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
470 def get_profiles_list(self, clients=True, components=False): |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
471 """retrieve profiles list |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
472 |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
473 @param clients(bool): if True return clients profiles |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
474 @param components(bool): if True return components profiles |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
475 @return (list[unicode]): selected profiles |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
476 """ |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
477 if not clients and not components: |
3028 | 478 log.warning(_("requesting no profiles at all")) |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
479 return [] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
480 profiles = self.storage.get_profiles_list() |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
481 if clients and components: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
482 return sorted(profiles) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
483 is_component = self.storage.profile_is_component |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
484 if clients: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
485 p_filter = lambda p: not is_component(p) |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
486 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
487 p_filter = lambda p: is_component(p) |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
488 |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
489 return sorted(p for p in profiles if p_filter(p)) |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
490 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
491 def get_profile_name(self, profile_key, return_profile_keys=False): |
62
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
492 """Return name of profile from keyword |
1263 | 493 |
1519
fbe86b5d156f
core (memory, params): minor docstrings updates
Goffi <goffi@goffi.org>
parents:
1474
diff
changeset
|
494 @param profile_key: can be the profile name or a keyword (like @DEFAULT@) |
1263 | 495 @param return_profile_keys: if True, return unmanaged profile keys (like "@ALL@"). This keys must be managed by the caller |
1519
fbe86b5d156f
core (memory, params): minor docstrings updates
Goffi <goffi@goffi.org>
parents:
1474
diff
changeset
|
496 @return: requested profile name |
1460
c7fd121a6180
core: getProfileName no raise ProfileUnknownError + minor doc fixes
Goffi <goffi@goffi.org>
parents:
1447
diff
changeset
|
497 @raise exceptions.ProfileUnknownError if profile doesn't exists |
1263 | 498 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
499 return self.params.get_profile_name(profile_key, return_profile_keys) |
62
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
500 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
501 def profile_set_default(self, profile): |
1595
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
502 """Set default profile |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
503 |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
504 @param profile: %(doc_profile)s |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
505 """ |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
506 # we want to be sure that the profile exists |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
507 profile = self.get_profile_name(profile) |
1595
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
508 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
509 self.memory_data["Profile_default"] = profile |
1595
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
510 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
511 def create_profile(self, name, password, component=None): |
420
acd908528ef7
core: profile creation/deletion through database
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
512 """Create a new profile |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
513 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
514 @param name(unicode): profile name |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
515 @param password(unicode): profile password |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
516 Can be empty to disable password |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
517 @param component(None, unicode): set to entry point if this is a component |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
518 @return: Deferred |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
519 @raise exceptions.NotFound: component is not a known plugin import name |
420
acd908528ef7
core: profile creation/deletion through database
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
520 """ |
1263 | 521 if not name: |
3028 | 522 raise ValueError("Empty profile name") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
523 if name[0] == "@": |
3028 | 524 raise ValueError("A profile name can't start with a '@'") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
525 if "\n" in name: |
3028 | 526 raise ValueError("A profile name can't contain line feed ('\\n')") |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1652
diff
changeset
|
527 |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
528 if name in self._entities_cache: |
3028 | 529 raise exceptions.ConflictError("A session for this profile exists") |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
530 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
531 if component: |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
532 if not component in self.host.plugins: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
533 raise exceptions.NotFound( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
534 _( |
3028 | 535 "Can't find component {component} entry point".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
536 component=component |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
537 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
538 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
539 ) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
540 # FIXME: PLUGIN_INFO is not currently accessible after import, but type shoul be tested here |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
541 # if self.host.plugins[component].PLUGIN_INFO[u"type"] != C.PLUG_TYPE_ENTRY_POINT: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
542 # raise ValueError(_(u"Plugin {component} is not an entry point !".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
543 # component = component))) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
544 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
545 d = self.params.create_profile(name, component) |
1682
61391d863709
plugin XEP-0054: fixed vcard-update callback which was updating avatar when hash was empty, resulting in a really slow start
Goffi <goffi@goffi.org>
parents:
1652
diff
changeset
|
546 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
547 def init_personal_key(__): |
1652
fd7f41d8cbdf
memory: fixes asyncCreateProfile to not overwrite the personal key
souliane <souliane@mailoo.org>
parents:
1595
diff
changeset
|
548 # be sure to call this after checking that the profile doesn't exist yet |
3040 | 549 |
550 # generated once for all and saved in a PersistentDict | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
551 personal_key = BlockCipher.get_random_key( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
552 base64=True |
3040 | 553 ).decode('utf-8') |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
554 self.auth_sessions.new_session( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
555 {C.MEMORY_CRYPTO_KEY: personal_key}, profile=name |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
556 ) # will be encrypted by param_set |
1652
fd7f41d8cbdf
memory: fixes asyncCreateProfile to not overwrite the personal key
souliane <souliane@mailoo.org>
parents:
1595
diff
changeset
|
557 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
558 def start_fake_session(__): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
559 # avoid ProfileNotConnected exception in param_set |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
560 self._entities_cache[name] = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
561 self.params.load_ind_params(name) |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
562 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
563 def stop_fake_session(__): |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
564 del self._entities_cache[name] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
565 self.params.purge_profile(name) |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
566 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
567 d.addCallback(init_personal_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
568 d.addCallback(start_fake_session) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
569 d.addCallback( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
570 lambda __: self.param_set( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
571 C.PROFILE_PASS_PATH[1], password, C.PROFILE_PASS_PATH[0], profile_key=name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
572 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
573 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
574 d.addCallback(stop_fake_session) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
575 d.addCallback(lambda __: self.auth_sessions.profile_del_unique(name)) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
576 return d |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
577 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
578 def profile_delete_async(self, name, force=False): |
68 | 579 """Delete an existing profile |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
580 |
894
57c32d8ec847
core (memory): asyncDeleteProfile can force the deletion of a profile, even if it's connected (when called from the backend only)
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
581 @param name: Name of the profile |
57c32d8ec847
core (memory): asyncDeleteProfile can force the deletion of a profile, even if it's connected (when called from the backend only)
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
582 @param force: force the deletion even if the profile is connected. |
57c32d8ec847
core (memory): asyncDeleteProfile can force the deletion of a profile, even if it's connected (when called from the backend only)
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
583 To be used for direct calls only (not through the bridge). |
57c32d8ec847
core (memory): asyncDeleteProfile can force the deletion of a profile, even if it's connected (when called from the backend only)
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
584 @return: a Deferred instance |
57c32d8ec847
core (memory): asyncDeleteProfile can force the deletion of a profile, even if it's connected (when called from the backend only)
souliane <souliane@mailoo.org>
parents:
893
diff
changeset
|
585 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
586 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
587 def clean_memory(__): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
588 self.auth_sessions.profile_del_unique(name) |
1704
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
589 try: |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
590 del self._entities_cache[name] |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
591 except KeyError: |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
592 pass |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
593 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
594 d = self.params.profile_delete_async(name, force) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
595 d.addCallback(clean_memory) |
1704
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
596 return d |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
597 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
598 def is_component(self, profile_name): |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
599 """Tell if a profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
600 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
601 @param profile_name(unicode): name of the profile |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
602 @return (bool): True if profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
603 @raise exceptions.NotFound: profile doesn't exist |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
604 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
605 return self.storage.profile_is_component(profile_name) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
606 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
607 def get_entry_point(self, profile_name): |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
608 """Get a component entry point |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
609 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
610 @param profile_name(unicode): name of the profile |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
611 @return (bool): True if profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
612 @raise exceptions.NotFound: profile doesn't exist |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
613 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
614 return self.storage.get_entry_point(profile_name) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
615 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
616 ## History ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
617 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
618 def add_to_history(self, client, data): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
619 return self.storage.add_to_history(data, client.profile) |
0 | 620 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
621 def _history_get_serialise(self, history_data): |
3170
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
622 return [ |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
623 (uid, timestamp, from_jid, to_jid, message, subject, mess_type, |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
624 data_format.serialise(extra)) for uid, timestamp, from_jid, to_jid, message, |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
625 subject, mess_type, extra in history_data |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
626 ] |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
627 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
628 def _history_get(self, from_jid_s, to_jid_s, limit=C.HISTORY_LIMIT_NONE, between=True, |
2698 | 629 filters=None, profile=C.PROF_KEY_NONE): |
4159
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
630 from_jid = jid.JID(from_jid_s) if from_jid_s else None |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
631 to_jid = jid.JID(to_jid_s) if to_jid_s else None |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
632 d = self.history_get( |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
633 from_jid, to_jid, limit, between, filters, profile |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
634 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
635 d.addCallback(self._history_get_serialise) |
3170
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
636 return d |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
637 |
4159
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
638 def history_get( |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
639 self, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
640 from_jid: jid.JID|None, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
641 to_jid: jid.JID|None, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
642 limit: int = C.HISTORY_LIMIT_NONE, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
643 between: bool = True, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
644 filters: dict[str, str]|None = None, |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
645 profile: str = C.PROF_KEY_NONE |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
646 ) -> defer.Deferred[list]: |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
647 """Retrieve messages in history |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
648 |
4159
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
649 @param from_jid: source JID (full, or bare for catchall) |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
650 @param to_jid: dest JID (full, or bare for catchall) |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
651 @param limit: maximum number of messages to get: |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
652 - 0 for no message (returns the empty list) |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
653 - C.HISTORY_LIMIT_NONE or None for unlimited |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
654 - C.HISTORY_LIMIT_DEFAULT to use the HISTORY_LIMIT parameter value |
4159
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
655 @param between: confound source and dest (ignore the direction) |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
656 @param filters: pattern to filter the history results |
2698 | 657 (see bridge API for details) |
4159
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
658 @param profile: %(doc_profile)s |
54b8cf8c8daf
core (memory/memory): fix the use of empty `from_jid` and `to_jid` in `history_get`
Goffi <goffi@goffi.org>
parents:
4130
diff
changeset
|
659 @return: list of message data as in [message_new] |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
660 """ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
914
diff
changeset
|
661 assert profile != C.PROF_KEY_NONE |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
662 if limit == C.HISTORY_LIMIT_DEFAULT: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
663 limit = int(self.param_get_a(C.HISTORY_LIMIT, "General", profile_key=profile)) |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
664 elif limit == C.HISTORY_LIMIT_NONE: |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
665 limit = None |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
666 if limit == 0: |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
667 return defer.succeed([]) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
668 return self.storage.history_get(from_jid, to_jid, limit, between, filters, profile) |
0 | 669 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
670 ## Statuses ## |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
671 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
672 def _get_presence_statuses(self, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
673 ret = self.presence_statuses_get(profile_key) |
3028 | 674 return {entity.full(): data for entity, data in ret.items()} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
675 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
676 def presence_statuses_get(self, profile_key): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
677 """Get all the presence statuses of a profile |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
678 |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
679 @param profile_key: %(doc_profile_key)s |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
680 @return: presence data: key=entity JID, value=presence data for this entity |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
681 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
682 client = self.host.get_client(profile_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
683 profile_cache = self._get_profile_cache(client) |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
684 entities_presence = {} |
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
685 |
3028 | 686 for entity_jid, entity_data in profile_cache.items(): |
687 for resource, resource_data in entity_data.items(): | |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
688 full_jid = copy.copy(entity_jid) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
689 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
690 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
691 presence_data = self.get_entity_datum(client, full_jid, "presence") |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
692 except KeyError: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
693 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
694 entities_presence.setdefault(entity_jid, {})[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
695 resource or "" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
696 ] = presence_data |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
697 |
484
23cbdf0a0777
core: presence status + last resource refactored and kept in entitiesCache in memory.py, profile cache is purged on disconnection
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
698 return entities_presence |
0 | 699 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
700 def set_presence_status(self, entity_jid, show, priority, statuses, profile_key): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
701 """Change the presence status of an entity |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
702 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
703 @param entity_jid: jid.JID of the entity |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
704 @param show: show status |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
705 @param priority: priority |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
706 @param statuses: dictionary of statuses |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
707 @param profile_key: %(doc_profile_key)s |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
708 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
709 client = self.host.get_client(profile_key) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
710 presence_data = PresenceTuple(show, priority, statuses) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
711 self.update_entity_data( |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
712 client, entity_jid, "presence", presence_data |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
713 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
714 if entity_jid.resource and show != C.PRESENCE_UNAVAILABLE: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
715 # If a resource is available, bare jid should not have presence information |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
716 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
717 self.del_entity_datum(client, entity_jid.userhostJID(), "presence") |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
718 except (KeyError, exceptions.UnknownEntityError): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
719 pass |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
720 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
721 ## Resources ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
722 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
723 def _get_all_resource(self, jid_s, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
724 client = self.host.get_client(profile_key) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
725 jid_ = jid.JID(jid_s) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
726 return self.get_all_resources(client, jid_) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
727 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
728 def get_all_resources(self, client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
729 """Return all resource from jid for which we have had data in this session |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
730 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
731 @param entity_jid: bare jid of the entity |
3206
ae09989e9feb
core, bridge: new `devicesInfosGet` method to get infos on known devices of an entity
Goffi <goffi@goffi.org>
parents:
3170
diff
changeset
|
732 return (set[unicode]): set of resources |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
733 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
734 @raise exceptions.UnknownEntityError: if entity is not in cache |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
735 @raise ValueError: entity_jid has a resource |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
736 """ |
2597
9446f1ea9eac
core: discoFindByFeatures now return only available resources
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
737 # FIXME: is there a need to keep cache data for resources which are not connected anymore? |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
738 if entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
739 raise ValueError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
740 "get_all_resources must be used with a bare jid (got {})".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
741 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
742 profile_cache = self._get_profile_cache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
743 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
744 entity_data = profile_cache[entity_jid.userhostJID()] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
745 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
746 raise exceptions.UnknownEntityError( |
3028 | 747 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
748 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
749 resources = set(entity_data.keys()) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
750 resources.discard(None) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
751 return resources |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
752 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
753 def get_available_resources(self, client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
754 """Return available resource for entity_jid |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
755 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
756 This method differs from get_all_resources by returning only available resources |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
757 @param entity_jid: bare jid of the entit |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
758 return (list[unicode]): list of available resources |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
759 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
760 @raise exceptions.UnknownEntityError: if entity is not in cache |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
761 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
762 available = [] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
763 for resource in self.get_all_resources(client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
764 full_jid = copy.copy(entity_jid) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
765 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
766 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
767 presence_data = self.get_entity_datum(client, full_jid, "presence") |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
768 except KeyError: |
3028 | 769 log.debug("Can't get presence data for {}".format(full_jid)) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
770 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
771 if presence_data.show != C.PRESENCE_UNAVAILABLE: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
772 available.append(resource) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
773 return available |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
774 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
775 def _get_main_resource(self, jid_s, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
776 client = self.host.get_client(profile_key) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
777 jid_ = jid.JID(jid_s) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
778 return self.main_resource_get(client, jid_) or "" |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
779 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
780 def main_resource_get(self, client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
781 """Return the main resource used by an entity |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
782 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
783 @param entity_jid: bare entity jid |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
784 @return (unicode): main resource or None |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
785 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
786 if entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
787 raise ValueError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
788 "main_resource_get must be used with a bare jid (got {})".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
789 ) |
1392
c7082457d03f
memory, plugin XEP-0045: getMainResource returns None when asking a MUC entity + fixes /whois on a MUC (bare) entity
souliane <souliane@mailoo.org>
parents:
1375
diff
changeset
|
790 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
791 if self.host.plugins["XEP-0045"].is_joined_room(client, entity_jid): |
1392
c7082457d03f
memory, plugin XEP-0045: getMainResource returns None when asking a MUC entity + fixes /whois on a MUC (bare) entity
souliane <souliane@mailoo.org>
parents:
1375
diff
changeset
|
792 return None # MUC rooms have no main resource |
c7082457d03f
memory, plugin XEP-0045: getMainResource returns None when asking a MUC entity + fixes /whois on a MUC (bare) entity
souliane <souliane@mailoo.org>
parents:
1375
diff
changeset
|
793 except KeyError: # plugin not found |
c7082457d03f
memory, plugin XEP-0045: getMainResource returns None when asking a MUC entity + fixes /whois on a MUC (bare) entity
souliane <souliane@mailoo.org>
parents:
1375
diff
changeset
|
794 pass |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
795 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
796 resources = self.get_all_resources(client, entity_jid) |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
797 except exceptions.UnknownEntityError: |
3028 | 798 log.warning("Entity is not in cache, we can't find any resource") |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
799 return None |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
800 priority_resources = [] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
801 for resource in resources: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
802 full_jid = copy.copy(entity_jid) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
803 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
804 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
805 presence_data = self.get_entity_datum(client, full_jid, "presence") |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
806 except KeyError: |
3028 | 807 log.debug("No presence information for {}".format(full_jid)) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
808 continue |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
809 priority_resources.append((resource, presence_data.priority)) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
810 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
811 return max(priority_resources, key=lambda res_tuple: res_tuple[1])[0] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
812 except ValueError: |
3028 | 813 log.warning("No resource found at all for {}".format(entity_jid)) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
814 return None |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
815 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
816 ## Entities data ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
817 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
818 def _get_profile_cache(self, client): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
819 """Check profile validity and return its cache |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
820 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
821 @param client: SatXMPPClient |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
822 @return (dict): profile cache |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
823 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
824 return self._entities_cache[client.profile] |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
825 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
826 def set_signal_on_update(self, key, signal=True): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
827 """Set a signal flag on the key |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
828 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
829 When the key will be updated, a signal will be sent to frontends |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
830 @param key: key to signal |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
831 @param signal(boolean): if True, do the signal |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
832 """ |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
833 if signal: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
834 self._key_signals.add(key) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
835 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
836 self._key_signals.discard(key) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
837 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
838 def get_all_entities_iter(self, client, with_bare=False): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
839 """Return an iterator of full jids of all entities in cache |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
840 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
841 @param with_bare: if True, include bare jids |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
842 @return (list[unicode]): list of jids |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
843 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
844 profile_cache = self._get_profile_cache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
845 # we construct a list of all known full jids (bare jid of entities x resources) |
3028 | 846 for bare_jid, entity_data in profile_cache.items(): |
847 for resource in entity_data.keys(): | |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
848 if resource is None: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
849 continue |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
850 full_jid = copy.copy(bare_jid) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
851 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
852 yield full_jid |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
853 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
854 def update_entity_data( |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
855 self, client, entity_jid, key, value, silent=False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
856 ): |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
857 """Set a misc data for an entity |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
858 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
859 If key was registered with set_signal_on_update, a signal will be sent to frontends |
2994
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
860 @param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of |
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
861 all entities, C.ENTITY_ALL for all entities (all resources + bare jids) |
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
862 @param key: key to set (eg: C.ENTITY_TYPE) |
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
863 @param value: value for this key (eg: C.ENTITY_TYPE_MUC) |
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
864 @param silent(bool): if True, doesn't send signal to frontend, even if there is a |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
865 signal flag (see set_signal_on_update) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
866 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
867 profile_cache = self._get_profile_cache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
868 if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
869 entities = self.get_all_entities_iter(client, entity_jid == C.ENTITY_ALL) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
870 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
871 entities = (entity_jid,) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
872 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
873 for jid_ in entities: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
874 entity_data = profile_cache.setdefault(jid_.userhostJID(), {}).setdefault( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
875 jid_.resource, {} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
876 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
877 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
878 entity_data[key] = value |
1315
be3a301540c0
core (memory): updateEntityData now accept a "silent" argument to avoid sending signal to frontends when updating an attribute with "signalOnUpdate" flag.
Goffi <goffi@goffi.org>
parents:
1314
diff
changeset
|
879 if key in self._key_signals and not silent: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
880 self.host.bridge.entity_data_updated( |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
881 jid_.full(), |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
882 key, |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
883 data_format.serialise(value), |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
884 client.profile |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
885 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
886 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
887 def del_entity_datum(self, client, entity_jid, key): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
888 """Delete a data for an entity |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
889 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
890 @param entity_jid: JID of the entity, C.ENTITY_ALL_RESOURCES for all resources of all entities, |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
891 C.ENTITY_ALL for all entities (all resources + bare jids) |
2994
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
892 @param key: key to delete (eg: C.ENTITY_TYPE) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
893 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
894 @raise exceptions.UnknownEntityError: if entity is not in cache |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
895 @raise KeyError: key is not in cache |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
896 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
897 profile_cache = self._get_profile_cache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
898 if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
899 entities = self.get_all_entities_iter(client, entity_jid == C.ENTITY_ALL) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
900 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
901 entities = (entity_jid,) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
902 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
903 for jid_ in entities: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
904 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
905 entity_data = profile_cache[jid_.userhostJID()][jid_.resource] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
906 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
907 raise exceptions.UnknownEntityError( |
3028 | 908 "Entity {} not in cache".format(jid_) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
909 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
910 try: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
911 del entity_data[key] |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
912 except KeyError as e: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
913 if entity_jid in (C.ENTITY_ALL_RESOURCES, C.ENTITY_ALL): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
914 continue # we ignore KeyError when deleting keys from several entities |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
915 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
916 raise e |
399 | 917 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
918 def _get_entities_data(self, entities_jids, keys_list, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
919 client = self.host.get_client(profile_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
920 ret = self.entities_data_get( |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
921 client, [jid.JID(jid_) for jid_ in entities_jids], keys_list |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
922 ) |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
923 return { |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
924 jid_.full(): {k: data_format.serialise(v) for k,v in data.items()} |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
925 for jid_, data in ret.items() |
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
926 } |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
927 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
928 def entities_data_get(self, client, entities_jids, keys_list=None): |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
929 """Get a list of cached values for several entities at once |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
930 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
931 @param entities_jids: jids of the entities, or empty list for all entities in cache |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
932 @param keys_list (iterable,None): list of keys to get, None for everything |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
933 @param profile_key: %(doc_profile_key)s |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
934 @return: dict withs values for each key in keys_list. |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
935 if there is no value of a given key, resulting dict will |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
936 have nothing with that key nether |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
937 if an entity doesn't exist in cache, it will not appear |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
938 in resulting dict |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
939 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
940 @raise exceptions.UnknownEntityError: if entity is not in cache |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
941 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
942 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
943 def fill_entity_data(entity_cache_data): |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
944 entity_data = {} |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
945 if keys_list is None: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
946 entity_data = entity_cache_data |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
947 else: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
948 for key in keys_list: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
949 try: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
950 entity_data[key] = entity_cache_data[key] |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
951 except KeyError: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
952 continue |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
953 return entity_data |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
954 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
955 profile_cache = self._get_profile_cache(client) |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
956 ret_data = {} |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
957 if entities_jids: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
958 for entity in entities_jids: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
959 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
960 entity_cache_data = profile_cache[entity.userhostJID()][ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
961 entity.resource |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
962 ] |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
963 except KeyError: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
964 continue |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
965 ret_data[entity.full()] = fill_entity_data(entity_cache_data, keys_list) |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
966 else: |
3028 | 967 for bare_jid, data in profile_cache.items(): |
968 for resource, entity_cache_data in data.items(): | |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
969 full_jid = copy.copy(bare_jid) |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
970 full_jid.resource = resource |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
971 ret_data[full_jid] = fill_entity_data(entity_cache_data) |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
972 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
973 return ret_data |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
974 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
975 def _get_entity_data(self, entity_jid_s, keys_list=None, profile=C.PROF_KEY_NONE): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
976 return self.entity_data_get( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
977 self.host.get_client(profile), jid.JID(entity_jid_s), keys_list) |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
978 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
979 def entity_data_get(self, client, entity_jid, keys_list=None): |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
980 """Get a list of cached values for entity |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
981 |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
982 @param entity_jid: JID of the entity |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
983 @param keys_list (iterable,None): list of keys to get, None for everything |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
984 @param profile_key: %(doc_profile_key)s |
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
985 @return: dict withs values for each key in keys_list. |
504
65ecbb473cbb
core, quick frontend, plugin xep-0054, bridge: use of memory's entities data for vcard:
Goffi <goffi@goffi.org>
parents:
494
diff
changeset
|
986 if there is no value of a given key, resulting dict will |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
987 have nothing with that key nether |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
988 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
989 @raise exceptions.UnknownEntityError: if entity is not in cache |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
990 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
991 profile_cache = self._get_profile_cache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
992 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
993 entity_data = profile_cache[entity_jid.userhostJID()][entity_jid.resource] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
994 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
995 raise exceptions.UnknownEntityError( |
3028 | 996 "Entity {} not in cache (was requesting {})".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
997 entity_jid, keys_list |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
998 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
999 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1000 if keys_list is None: |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
1001 return entity_data |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1002 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1003 return {key: entity_data[key] for key in keys_list if key in entity_data} |
47 | 1004 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1005 def get_entity_datum(self, client, entity_jid, key): |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1006 """Get a datum from entity |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1007 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1008 @param entity_jid: JID of the entity |
3254
6cf4bd6972c2
core, frontends: avatar refactoring:
Goffi <goffi@goffi.org>
parents:
3206
diff
changeset
|
1009 @param key: key to get |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1010 @return: requested value |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1011 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1012 @raise exceptions.UnknownEntityError: if entity is not in cache |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1013 @raise KeyError: if there is no value for this key and this entity |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1014 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1015 return self.entity_data_get(client, entity_jid, (key,))[key] |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
1016 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1017 def del_entity_cache( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1018 self, entity_jid, delete_all_resources=True, profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1019 ): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1020 """Remove all cached data for entity |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1021 |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1022 @param entity_jid: JID of the entity to delete |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1023 @param delete_all_resources: if True also delete all known resources from cache (a bare jid must be given in this case) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1024 @param profile_key: %(doc_profile_key)s |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1025 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1026 @raise exceptions.UnknownEntityError: if entity is not in cache |
507
f98bef71a918
frontends, core, plugin XEP-0045: leave implementation + better nick change
Goffi <goffi@goffi.org>
parents:
504
diff
changeset
|
1027 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1028 client = self.host.get_client(profile_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1029 profile_cache = self._get_profile_cache(client) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1030 |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1031 if delete_all_resources: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1032 if entity_jid.resource: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1033 raise ValueError(_("Need a bare jid to delete all resources")) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1034 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1035 del profile_cache[entity_jid] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1036 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1037 raise exceptions.UnknownEntityError( |
3028 | 1038 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1039 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1040 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1041 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1042 del profile_cache[entity_jid.userhostJID()][entity_jid.resource] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1043 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1044 raise exceptions.UnknownEntityError( |
3028 | 1045 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1046 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
1047 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1048 ## Encryption ## |
507
f98bef71a918
frontends, core, plugin XEP-0045: leave implementation + better nick change
Goffi <goffi@goffi.org>
parents:
504
diff
changeset
|
1049 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1050 def encrypt_value(self, value, profile): |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1051 """Encrypt a value for the given profile. The personal key must be loaded |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1052 already in the profile session, that should be the case if the profile is |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1053 already authenticated. |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1054 |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1055 @param value (str): the value to encrypt |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1056 @param profile (str): %(doc_profile)s |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1057 @return: the deferred encrypted value |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1058 """ |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1059 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1060 personal_key = self.auth_sessions.profile_get_unique(profile)[ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1061 C.MEMORY_CRYPTO_KEY |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1062 ] |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1063 except TypeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1064 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1065 _("Trying to encrypt a value for %s while the personal key is undefined!") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1066 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1067 ) |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1068 return BlockCipher.encrypt(personal_key, value) |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1069 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1070 def decrypt_value(self, value, profile): |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1071 """Decrypt a value for the given profile. The personal key must be loaded |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1072 already in the profile session, that should be the case if the profile is |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1073 already authenticated. |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1074 |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1075 @param value (str): the value to decrypt |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1076 @param profile (str): %(doc_profile)s |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1077 @return: the deferred decrypted value |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1078 """ |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1079 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1080 personal_key = self.auth_sessions.profile_get_unique(profile)[ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1081 C.MEMORY_CRYPTO_KEY |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1082 ] |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1083 except TypeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1084 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1085 _("Trying to decrypt a value for %s while the personal key is undefined!") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1086 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1087 ) |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1088 return BlockCipher.decrypt(personal_key, value) |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1089 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1090 def encrypt_personal_data(self, data_key, data_value, crypto_key, profile): |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1091 """Re-encrypt a personal data (saved to a PersistentDict). |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1092 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1093 @param data_key: key for the individual PersistentDict instance |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1094 @param data_value: the value to be encrypted |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1095 @param crypto_key: the key to encrypt the value |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1096 @param profile: %(profile_doc)s |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1097 @return: a deferred None value |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1098 """ |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1099 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1100 def got_ind_memory(data): |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
1101 data[data_key] = BlockCipher.encrypt(crypto_key, data_value) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
1102 return data.force(data_key) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1103 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
1104 def done(__): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1105 log.debug( |
3028 | 1106 _("Personal data (%(ns)s, %(key)s) has been successfuly encrypted") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1107 % {"ns": C.MEMORY_CRYPTO_NAMESPACE, "key": data_key} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1108 ) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1109 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1110 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1111 return d.addCallback(got_ind_memory).addCallback(done) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1112 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1113 ## Subscription requests ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1114 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1115 def add_waiting_sub(self, type_, entity_jid, profile_key): |
47 | 1116 """Called when a subcription request is received""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1117 profile = self.get_profile_name(profile_key) |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1118 assert profile |
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1119 if profile not in self.subscriptions: |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1120 self.subscriptions[profile] = {} |
722
04aabc3f2684
core (memory): fixed setDefault behaviour + minor refactoring
Goffi <goffi@goffi.org>
parents:
679
diff
changeset
|
1121 self.subscriptions[profile][entity_jid] = type_ |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1122 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1123 def del_waiting_sub(self, entity_jid, profile_key): |
47 | 1124 """Called when a subcription request is finished""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1125 profile = self.get_profile_name(profile_key) |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1126 assert profile |
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1127 if profile in self.subscriptions and entity_jid in self.subscriptions[profile]: |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
1128 del self.subscriptions[profile][entity_jid] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1129 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1130 def sub_waiting_get(self, profile_key): |
47 | 1131 """Called to get a list of currently waiting subscription requests""" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1132 profile = self.get_profile_name(profile_key) |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1133 if not profile: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1134 log.error(_("Asking waiting subscriptions for a non-existant profile")) |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1135 return {} |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1136 if profile not in self.subscriptions: |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1137 return {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1138 |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1139 return self.subscriptions[profile] |
0 | 1140 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1141 ## Parameters ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1142 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1143 def get_string_param_a(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1144 return self.params.get_string_param_a(name, category, attr, profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1145 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1146 def param_get_a(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1147 return self.params.param_get_a(name, category, attr, profile_key=profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1148 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1149 def param_get_a_async( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1150 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1151 name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1152 category, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1153 attr="value", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1154 security_limit=C.NO_SECURITY_LIMIT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1155 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1156 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1157 return self.params.param_get_a_async( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1158 name, category, attr, security_limit, profile_key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1159 ) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1160 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1161 def _get_params_values_from_category( |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1162 self, category, security_limit, app, extra_s, profile_key |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1163 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1164 return self.params._get_params_values_from_category( |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1165 category, security_limit, app, extra_s, profile_key |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1166 ) |
1587
698d6755d62a
core, bridge (params): added asyncGetParamsValuesFromCategory (yes that's a long name!) method to retrive params names and values for a given category
Goffi <goffi@goffi.org>
parents:
1586
diff
changeset
|
1167 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1168 def async_get_string_param_a( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1169 self, name, category, attribute="value", security_limit=C.NO_SECURITY_LIMIT, |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1170 profile_key=C.PROF_KEY_NONE): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1171 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1172 profile = self.get_profile_name(profile_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1173 return defer.ensureDeferred(self.params.async_get_string_param_a( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1174 name, category, attribute, security_limit, profile |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1175 )) |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1176 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1177 def _get_params_ui(self, security_limit, app, extra_s, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1178 return self.params._get_params_ui(security_limit, app, extra_s, profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1179 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1180 def params_categories_get(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1181 return self.params.params_categories_get() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1182 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1183 def param_set( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1184 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1185 name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1186 value, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1187 category, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1188 security_limit=C.NO_SECURITY_LIMIT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1189 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1190 ): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1191 return self.params.param_set(name, value, category, security_limit, profile_key) |
19
f2a745ca0fbc
refactoring: using xml params part III (parameters import)
Goffi <goffi@goffi.org>
parents:
18
diff
changeset
|
1192 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1193 def update_params(self, xml): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1194 return self.params.update_params(xml) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1195 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1196 def params_register_app(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1197 return self.params.params_register_app(xml, security_limit, app) |
777
5642939d254e
core, bridge: new method paramsRegisterApp to register frontend's specific parameters
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
1198 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1199 def set_default(self, name, category, callback, errback=None): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1200 return self.params.set_default(name, category, callback, errback) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1201 |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1202 ## Private Data ## |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1203 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1204 def _private_data_set(self, namespace, key, data_s, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1205 client = self.host.get_client(profile_key) |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1206 # we accept any type |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1207 data = data_format.deserialise(data_s, type_check=None) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1208 return defer.ensureDeferred(self.storage.set_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1209 namespace, key, data, binary=True, profile=client.profile)) |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1210 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1211 def _private_data_get(self, namespace, key, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1212 client = self.host.get_client(profile_key) |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1213 d = defer.ensureDeferred( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1214 self.storage.get_privates( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1215 namespace, [key], binary=True, profile=client.profile) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1216 ) |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1217 d.addCallback(lambda data_dict: data_format.serialise(data_dict.get(key))) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1218 return d |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1219 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1220 def _private_data_delete(self, namespace, key, profile_key): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1221 client = self.host.get_client(profile_key) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1222 return defer.ensureDeferred(self.storage.del_private_value( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1223 namespace, key, binary=True, profile=client.profile)) |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1224 |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1225 ## Files ## |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1226 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1227 def check_file_permission( |
3313
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1228 self, |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1229 file_data: dict, |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1230 peer_jid: Optional[jid.JID], |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1231 perms_to_check: Optional[Tuple[str]], |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1232 set_affiliation: bool = False |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1233 ) -> None: |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1234 """Check that an entity has the right permission on a file |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1235 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1236 @param file_data: data of one file, as returned by get_files |
3313
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1237 @param peer_jid: entity trying to access the file |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1238 @param perms_to_check: permissions to check |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1239 tuple of C.ACCESS_PERM_* |
3313
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1240 @param check_parents: if True, also check all parents until root node |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1241 @parma set_affiliation: if True, "affiliation" metadata will be set |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1242 @raise exceptions.PermissionError: peer_jid doesn't have all permission |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1243 in perms_to_check for file_data |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1244 @raise exceptions.InternalError: perms_to_check is invalid |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1245 """ |
3313
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1246 # TODO: knowing if user is owner is not enough, we need to check permission |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1247 # to see if user can modify/delete files, and set corresponding affiliation (publisher, member) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1248 if peer_jid is None and perms_to_check is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1249 return |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1250 peer_jid = peer_jid.userhostJID() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1251 if peer_jid == file_data["owner"]: |
3313
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1252 if set_affiliation: |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1253 file_data['affiliation'] = 'owner' |
624c60293deb
memory: new "affiliation" metadata for files:
Goffi <goffi@goffi.org>
parents:
3300
diff
changeset
|
1254 # the owner has all rights, nothing to check |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1255 return |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1256 if not C.ACCESS_PERMS.issuperset(perms_to_check): |
3028 | 1257 raise exceptions.InternalError(_("invalid permission")) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1258 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1259 for perm in perms_to_check: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1260 # we check each perm and raise PermissionError as soon as one condition is not valid |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1261 # we must never return here, we only return after the loop if nothing was blocking the access |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1262 try: |
3028 | 1263 perm_data = file_data["access"][perm] |
1264 perm_type = perm_data["type"] | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1265 except KeyError: |
3317
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1266 # No permission is set. |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1267 # If we are in a root file/directory, we deny access |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1268 # otherwise, we use public permission, as the parent directory will |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1269 # block anyway, this avoid to have to recursively change permissions for |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1270 # all sub directories/files when modifying a permission |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1271 if not file_data.get('parent'): |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1272 raise exceptions.PermissionError() |
83f25da66bec
core (memory): files are now public by default except for those in root directory:
Goffi <goffi@goffi.org>
parents:
3313
diff
changeset
|
1273 else: |
3349
2a7e36b69fd2
core (memory/memory): fixed assignations
Goffi <goffi@goffi.org>
parents:
3346
diff
changeset
|
1274 perm_type = C.ACCESS_TYPE_PUBLIC |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1275 if perm_type == C.ACCESS_TYPE_PUBLIC: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1276 continue |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1277 elif perm_type == C.ACCESS_TYPE_WHITELIST: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1278 try: |
3028 | 1279 jids = perm_data["jids"] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1280 except KeyError: |
2926
4cd7545c4ebb
core (memory): raise PermissionError directly instead of using failureFailure, as it could not be catched correctly in every case.
Goffi <goffi@goffi.org>
parents:
2924
diff
changeset
|
1281 raise exceptions.PermissionError() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1282 if peer_jid.full() in jids: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1283 continue |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1284 else: |
2926
4cd7545c4ebb
core (memory): raise PermissionError directly instead of using failureFailure, as it could not be catched correctly in every case.
Goffi <goffi@goffi.org>
parents:
2924
diff
changeset
|
1285 raise exceptions.PermissionError() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1286 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1287 raise exceptions.InternalError( |
3028 | 1288 _("unknown access type: {type}").format(type=perm_type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1289 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1290 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1291 async def check_permission_to_root(self, client, file_data, peer_jid, perms_to_check): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1292 """do check_file_permission on file_data and all its parents until root""" |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1293 current = file_data |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1294 while True: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1295 self.check_file_permission(current, peer_jid, perms_to_check) |
3028 | 1296 parent = current["parent"] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1297 if not parent: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1298 break |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1299 files_data = await self.get_files( |
3040 | 1300 client, peer_jid=None, file_id=parent, perms_to_check=None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1301 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1302 try: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1303 current = files_data[0] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1304 except IndexError: |
3028 | 1305 raise exceptions.DataError("Missing parent") |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1306 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1307 async def _get_parent_dir( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1308 self, client, path, parent, namespace, owner, peer_jid, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1309 ): |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1310 """Retrieve parent node from a path, or last existing directory |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1311 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1312 each directory of the path will be retrieved, until the last existing one |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1313 @return (tuple[unicode, list[unicode])): parent, remaining path elements: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1314 - parent is the id of the last retrieved directory (or u'' for root) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1315 - remaining path elements are the directories which have not been retrieved |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1316 (i.e. which don't exist) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1317 """ |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1318 # if path is set, we have to retrieve parent directory of the file(s) from it |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1319 if parent is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1320 raise exceptions.ConflictError( |
3028 | 1321 _("You can't use path and parent at the same time") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1322 ) |
3028 | 1323 path_elts = [_f for _f in path.split("/") if _f] |
1324 if {"..", "."}.intersection(path_elts): | |
1325 raise ValueError(_('".." or "." can\'t be used in path')) | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1326 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1327 # we retrieve all directories from path until we get the parent container |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1328 # non existing directories will be created |
3028 | 1329 parent = "" |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1330 for idx, path_elt in enumerate(path_elts): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1331 directories = await self.storage.get_files( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1332 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1333 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1334 type_=C.FILE_TYPE_DIRECTORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1335 name=path_elt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1336 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1337 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1338 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1339 if not directories: |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1340 return (parent, path_elts[idx:]) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1341 # from this point, directories don't exist anymore, we have to create them |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1342 elif len(directories) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1343 raise exceptions.InternalError( |
3028 | 1344 _("Several directories found, this should not happen") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1345 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1346 else: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1347 directory = directories[0] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1348 self.check_file_permission(directory, peer_jid, perms_to_check) |
3028 | 1349 parent = directory["id"] |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1350 return (parent, []) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1351 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1352 def get_file_affiliations(self, file_data: dict) -> Dict[jid.JID, str]: |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1353 """Convert file access to pubsub like affiliations""" |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1354 affiliations = {} |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1355 access_data = file_data['access'] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1356 |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1357 read_data = access_data.get(C.ACCESS_PERM_READ, {}) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1358 if read_data.get('type') == C.ACCESS_TYPE_WHITELIST: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1359 for entity_jid_s in read_data['jids']: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1360 entity_jid = jid.JID(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1361 affiliations[entity_jid] = 'member' |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1362 |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1363 write_data = access_data.get(C.ACCESS_PERM_WRITE, {}) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1364 if write_data.get('type') == C.ACCESS_TYPE_WHITELIST: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1365 for entity_jid_s in write_data['jids']: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1366 entity_jid = jid.JID(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1367 affiliations[entity_jid] = 'publisher' |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1368 |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1369 owner = file_data.get('owner') |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1370 if owner: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1371 affiliations[owner] = 'owner' |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1372 |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1373 return affiliations |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1374 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1375 def _set_file_affiliations_update( |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1376 self, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1377 access: dict, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1378 file_data: dict, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1379 affiliations: Dict[jid.JID, str] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1380 ) -> None: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1381 read_data = access.setdefault(C.ACCESS_PERM_READ, {}) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1382 if read_data.get('type') != C.ACCESS_TYPE_WHITELIST: |
3349
2a7e36b69fd2
core (memory/memory): fixed assignations
Goffi <goffi@goffi.org>
parents:
3346
diff
changeset
|
1383 read_data['type'] = C.ACCESS_TYPE_WHITELIST |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1384 if 'jids' not in read_data: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1385 read_data['jids'] = [] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1386 read_whitelist = read_data['jids'] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1387 write_data = access.setdefault(C.ACCESS_PERM_WRITE, {}) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1388 if write_data.get('type') != C.ACCESS_TYPE_WHITELIST: |
3349
2a7e36b69fd2
core (memory/memory): fixed assignations
Goffi <goffi@goffi.org>
parents:
3346
diff
changeset
|
1389 write_data['type'] = C.ACCESS_TYPE_WHITELIST |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1390 if 'jids' not in write_data: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1391 write_data['jids'] = [] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1392 write_whitelist = write_data['jids'] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1393 for entity_jid, affiliation in affiliations.items(): |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1394 entity_jid_s = entity_jid.full() |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1395 if affiliation == "none": |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1396 try: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1397 read_whitelist.remove(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1398 except ValueError: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1399 log.warning( |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1400 "removing affiliation from an entity without read permission: " |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1401 f"{entity_jid}" |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1402 ) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1403 try: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1404 write_whitelist.remove(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1405 except ValueError: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1406 pass |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1407 elif affiliation == "publisher": |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1408 if entity_jid_s not in read_whitelist: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1409 read_whitelist.append(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1410 if entity_jid_s not in write_whitelist: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1411 write_whitelist.append(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1412 elif affiliation == "member": |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1413 if entity_jid_s not in read_whitelist: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1414 read_whitelist.append(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1415 try: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1416 write_whitelist.remove(entity_jid_s) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1417 except ValueError: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1418 pass |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1419 elif affiliation == "owner": |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1420 raise NotImplementedError('"owner" affiliation can\'t be set') |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1421 else: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1422 raise ValueError(f"unknown affiliation: {affiliation!r}") |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1423 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1424 async def set_file_affiliations( |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1425 self, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1426 client, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1427 file_data: dict, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1428 affiliations: Dict[jid.JID, str] |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1429 ) -> None: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1430 """Apply pubsub like affiliation to file_data |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1431 |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1432 Affiliations are converted to access types, then set in a whitelist. |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1433 Affiliation are mapped as follow: |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1434 - "owner" can't be set (for now) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1435 - "publisher" gives read and write permissions |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1436 - "member" gives read permission only |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1437 - "none" removes both read and write permissions |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1438 """ |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1439 file_id = file_data['id'] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1440 await self.file_update( |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1441 file_id, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1442 'access', |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1443 update_cb=partial( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1444 self._set_file_affiliations_update, |
3318
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1445 file_data=file_data, |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1446 affiliations=affiliations |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1447 ), |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1448 ) |
460606155bec
core (memory): `getFileAffiliations` and `setFileAffiliations` implementations:
Goffi <goffi@goffi.org>
parents:
3317
diff
changeset
|
1449 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1450 def _set_file_access_model_update( |
3319
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1451 self, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1452 access: dict, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1453 file_data: dict, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1454 access_model: str |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1455 ) -> None: |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1456 read_data = access.setdefault(C.ACCESS_PERM_READ, {}) |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1457 if access_model == "open": |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1458 requested_type = C.ACCESS_TYPE_PUBLIC |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1459 elif access_model == "whitelist": |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1460 requested_type = C.ACCESS_TYPE_WHITELIST |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1461 else: |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1462 raise ValueError(f"unknown access model: {access_model}") |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1463 |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1464 read_data['type'] = requested_type |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1465 if requested_type == C.ACCESS_TYPE_WHITELIST and 'jids' not in read_data: |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1466 read_data['jids'] = [] |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1467 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1468 async def set_file_access_model( |
3319
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1469 self, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1470 client, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1471 file_data: dict, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1472 access_model: str, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1473 ) -> None: |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1474 """Apply pubsub like access_model to file_data |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1475 |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1476 Only 2 access models are supported so far: |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1477 - "open": set public access to file/dir |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1478 - "whitelist": set whitelist to file/dir |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1479 """ |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1480 file_id = file_data['id'] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1481 await self.file_update( |
3319
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1482 file_id, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1483 'access', |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1484 update_cb=partial( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1485 self._set_file_access_model_update, |
3319
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1486 file_data=file_data, |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1487 access_model=access_model |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1488 ), |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1489 ) |
3a15e76a694e
core (memory): `setFileAccessModel` implementation:
Goffi <goffi@goffi.org>
parents:
3318
diff
changeset
|
1490 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1491 def get_files_owner( |
3499
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1492 self, |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1493 client, |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1494 owner: Optional[jid.JID], |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1495 peer_jid: Optional[jid.JID], |
3523
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1496 file_id: Optional[str] = None, |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1497 parent: Optional[str] = None |
3499
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1498 ) -> jid.JID: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1499 """Get owner to use for a file operation |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1500 |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1501 if owner is not explicitely set, a suitable one will be used (client.jid for |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1502 clients, peer_jid for components). |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1503 @raise exception.InternalError: we are one a component, and neither owner nor |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1504 peer_jid are set |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1505 """ |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1506 if owner is not None: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1507 return owner.userhostJID() |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1508 if client is None: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1509 # client may be None when looking for file with public_id |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1510 return None |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1511 if file_id or parent: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1512 # owner has already been filtered on parent file |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1513 return None |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1514 if not client.is_component: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1515 return client.jid.userhostJID() |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1516 if peer_jid is None: |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1517 raise exceptions.InternalError( |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1518 "Owner must be set for component if peer_jid is None" |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1519 ) |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1520 return peer_jid.userhostJID() |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1521 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1522 async def get_files( |
2924 | 1523 self, client, peer_jid, file_id=None, version=None, parent=None, path=None, |
1524 type_=None, file_hash=None, hash_algo=None, name=None, namespace=None, | |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1525 mime_type=None, public_id=None, owner=None, access=None, projection=None, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1526 unique=False, perms_to_check=(C.ACCESS_PERM_READ,)): |
2924 | 1527 """Retrieve files with with given filters |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1528 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1529 @param peer_jid(jid.JID, None): jid trying to access the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1530 needed to check permission. |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1531 Use None to ignore permission (perms_to_check must be None too) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1532 @param file_id(unicode, None): id of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1533 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1534 @param version(unicode, None): version of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1535 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1536 empty string to look for current version |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1537 @param parent(unicode, None): id of the directory containing the files |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1538 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1539 empty string to look for root files/directories |
3358
b14e95f7034f
core (memory/memory): use `str` for `path` in `getFiles` and `setFile` to allow use of `Path` instances
Goffi <goffi@goffi.org>
parents:
3349
diff
changeset
|
1540 @param path(Path, unicode, None): path to the directory containing the files |
2924 | 1541 @param type_(unicode, None): type of file filter, can be one of C.FILE_TYPE_* |
1542 @param file_hash(unicode, None): hash of the file to retrieve | |
1543 @param hash_algo(unicode, None): algorithm use for file_hash | |
1544 @param name(unicode, None): name of the file to retrieve | |
1545 @param namespace(unicode, None): namespace of the files to retrieve | |
1546 @param mime_type(unicode, None): filter on this mime type | |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1547 @param public_id(unicode, None): filter on this public id |
2924 | 1548 @param owner(jid.JID, None): if not None, only get files from this owner |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1549 @param access(dict, None): get file with given access (see [set_file]) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1550 @param projection(list[unicode], None): name of columns to retrieve |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1551 None to retrieve all |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1552 @param unique(bool): if True will remove duplicates |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1553 @param perms_to_check(tuple[unicode],None): permission to check |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1554 must be a tuple of C.ACCESS_PERM_* or None |
3499
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1555 if None, permission will no be checked (peer_jid must be None too in this |
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1556 case) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1557 other params are the same as for [set_file] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1558 @return (list[dict]): files corresponding to filters |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1559 @raise exceptions.NotFound: parent directory not found (when path is specified) |
2924 | 1560 @raise exceptions.PermissionError: peer_jid can't use perms_to_check for one of |
1561 the file | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1562 on the path |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1563 """ |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1564 if peer_jid is None and perms_to_check or perms_to_check is None and peer_jid: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1565 raise exceptions.InternalError( |
3028 | 1566 "if you want to disable permission check, both peer_jid and " |
1567 "perms_to_check must be None" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1568 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1569 owner = self.get_files_owner(client, owner, peer_jid, file_id, parent) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1570 if path is not None: |
3358
b14e95f7034f
core (memory/memory): use `str` for `path` in `getFiles` and `setFile` to allow use of `Path` instances
Goffi <goffi@goffi.org>
parents:
3349
diff
changeset
|
1571 path = str(path) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1572 # permission are checked by _get_parent_dir |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1573 parent, remaining_path_elts = await self._get_parent_dir( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1574 client, path, parent, namespace, owner, peer_jid, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1575 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1576 if remaining_path_elts: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1577 # if we have remaining path elements, |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1578 # the parent directory is not found |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1579 raise failure.Failure(exceptions.NotFound()) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1580 if parent and peer_jid: |
2924 | 1581 # if parent is given directly and permission check is requested, |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1582 # we need to check all the parents |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1583 parent_data = await self.storage.get_files(client, file_id=parent) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1584 try: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1585 parent_data = parent_data[0] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1586 except IndexError: |
3028 | 1587 raise exceptions.DataError("mising parent") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1588 await self.check_permission_to_root( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1589 client, parent_data, peer_jid, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1590 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1591 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1592 files = await self.storage.get_files( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1593 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1594 file_id=file_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1595 version=version, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1596 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1597 type_=type_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1598 file_hash=file_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1599 hash_algo=hash_algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1600 name=name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1601 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1602 mime_type=mime_type, |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1603 public_id=public_id, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1604 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1605 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1606 projection=projection, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1607 unique=unique, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1608 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1609 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1610 if peer_jid: |
2924 | 1611 # if permission are checked, we must remove all file that user can't access |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1612 to_remove = [] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1613 for file_data in files: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1614 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1615 self.check_file_permission( |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1616 file_data, peer_jid, perms_to_check, set_affiliation=True |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1617 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1618 except exceptions.PermissionError: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1619 to_remove.append(file_data) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1620 for file_data in to_remove: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1621 files.remove(file_data) |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3537
diff
changeset
|
1622 return files |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1623 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1624 async def set_file( |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1625 self, client, name, file_id=None, version="", parent=None, path=None, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1626 type_=C.FILE_TYPE_FILE, file_hash=None, hash_algo=None, size=None, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1627 namespace=None, mime_type=None, public_id=None, created=None, modified=None, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1628 owner=None, access=None, extra=None, peer_jid=None, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1629 perms_to_check=(C.ACCESS_PERM_WRITE,) |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1630 ): |
2924 | 1631 """Set a file metadata |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1632 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1633 @param name(unicode): basename of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1634 @param file_id(unicode): unique id of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1635 @param version(unicode): version of this file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1636 empty string for current version or when there is no versioning |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1637 @param parent(unicode, None): id of the directory containing the files |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1638 @param path(unicode, None): virtual path of the file in the namespace |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1639 if set, parent must be None. All intermediate directories will be created |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1640 if needed, using current access. |
3361
43e60c40de65
core (memory/memory): fixed directory creation in setFile
Goffi <goffi@goffi.org>
parents:
3358
diff
changeset
|
1641 @param type_(str, None): type of file filter, can be one of C.FILE_TYPE_* |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1642 @param file_hash(unicode): unique hash of the payload |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1643 @param hash_algo(unicode): algorithm used for hashing the file (usually sha-256) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1644 @param size(int): size in bytes |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1645 @param namespace(unicode, None): identifier (human readable is better) to group |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1646 files |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1647 For instance, namespace could be used to group files in a specific photo album |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1648 @param mime_type(unicode): MIME type of the file, or None if not known/guessed |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1649 @param public_id(unicode): id used to share publicly the file via HTTP |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1650 @param created(int): UNIX time of creation |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1651 @param modified(int,None): UNIX time of last modification, or None to use |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1652 created date |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1653 @param owner(jid.JID, None): jid of the owner of the file (mainly useful for |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1654 component) |
2518
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1655 will be used to check permission (only bare jid is used, don't use with MUC). |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1656 Use None to ignore permission (perms_to_check must be None too) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1657 @param access(dict, None): serialisable dictionary with access rules. |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1658 None (or empty dict) to use private access, i.e. allow only profile's jid to |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1659 access the file |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1660 key can be on on C.ACCESS_PERM_*, |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1661 then a sub dictionary with a type key is used (one of C.ACCESS_TYPE_*). |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1662 According to type, extra keys can be used: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1663 - C.ACCESS_TYPE_PUBLIC: the permission is granted for everybody |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1664 - C.ACCESS_TYPE_WHITELIST: the permission is granted for jids (as unicode) |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1665 in the 'jids' key |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1666 will be encoded to json in database |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1667 @param extra(dict, None): serialisable dictionary of any extra data |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1668 will be encoded to json in database |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1669 @param perms_to_check(tuple[unicode],None): permission to check |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1670 must be a tuple of C.ACCESS_PERM_* or None |
3499
a83a04b7394b
memory: new `getFilesOwner` method:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
1671 if None, permission will not be checked (peer_jid must be None too in this |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1672 case) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1673 @param profile(unicode): profile owning the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1674 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1675 if "/" in name: |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1676 raise ValueError('name must not contain a slash ("/")') |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1677 if file_id is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1678 file_id = shortuuid.uuid() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1679 if ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1680 file_hash is not None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1681 and hash_algo is None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1682 or hash_algo is not None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1683 and file_hash is None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1684 ): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1685 raise ValueError("file_hash and hash_algo must be set at the same time") |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1686 if mime_type is None: |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1687 mime_type, __ = mimetypes.guess_type(name) |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1688 else: |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1689 mime_type = mime_type.lower() |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1690 if public_id is not None: |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1691 assert len(public_id)>0 |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1692 if created is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1693 created = time.time() |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1694 if namespace is not None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1695 namespace = namespace.strip() or None |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1696 if type_ == C.FILE_TYPE_DIRECTORY: |
3361
43e60c40de65
core (memory/memory): fixed directory creation in setFile
Goffi <goffi@goffi.org>
parents:
3358
diff
changeset
|
1697 if any((version, file_hash, size, mime_type)): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1698 raise ValueError( |
3028 | 1699 "version, file_hash, size and mime_type can't be set for a directory" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1700 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1701 owner = self.get_files_owner(client, owner, peer_jid, file_id, parent) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1702 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1703 if path is not None: |
3358
b14e95f7034f
core (memory/memory): use `str` for `path` in `getFiles` and `setFile` to allow use of `Path` instances
Goffi <goffi@goffi.org>
parents:
3349
diff
changeset
|
1704 path = str(path) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1705 # _get_parent_dir will check permissions if peer_jid is set, so we use owner |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1706 parent, remaining_path_elts = await self._get_parent_dir( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1707 client, path, parent, namespace, owner, owner, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1708 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1709 # if remaining directories don't exist, we have to create them |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1710 for new_dir in remaining_path_elts: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1711 new_dir_id = shortuuid.uuid() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1712 await self.storage.set_file( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1713 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1714 name=new_dir, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1715 file_id=new_dir_id, |
3028 | 1716 version="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1717 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1718 type_=C.FILE_TYPE_DIRECTORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1719 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1720 created=time.time(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1721 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1722 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1723 extra={}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1724 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1725 parent = new_dir_id |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1726 elif parent is None: |
3028 | 1727 parent = "" |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1728 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1729 await self.storage.set_file( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1730 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1731 file_id=file_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1732 version=version, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1733 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1734 type_=type_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1735 file_hash=file_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1736 hash_algo=hash_algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1737 name=name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1738 size=size, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1739 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1740 mime_type=mime_type, |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1741 public_id=public_id, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1742 created=created, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1743 modified=modified, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1744 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1745 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1746 extra=extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1747 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1748 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1749 async def file_get_used_space( |
3523
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1750 self, |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1751 client, |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1752 peer_jid: jid.JID, |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1753 owner: Optional[jid.JID] = None |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1754 ) -> int: |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1755 """Get space taken by all files owned by an entity |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1756 |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1757 @param peer_jid: entity requesting the size |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1758 @param owner: entity owning the file to check. If None, will be determined by |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1759 get_files_owner |
3523
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1760 @return: size of total space used by files of this owner |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1761 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1762 owner = self.get_files_owner(client, owner, peer_jid) |
3523
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1763 if peer_jid.userhostJID() != owner and client.profile not in self.admins: |
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1764 raise exceptions.PermissionError("You are not allowed to check this size") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1765 return await self.storage.file_get_used_space(client, owner) |
3523
30779935c0aa
core (memory, sqlite): new `fileGetUsedSpace` method
Goffi <goffi@goffi.org>
parents:
3499
diff
changeset
|
1766 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1767 def file_update(self, file_id, column, update_cb): |
2926
4cd7545c4ebb
core (memory): raise PermissionError directly instead of using failureFailure, as it could not be catched correctly in every case.
Goffi <goffi@goffi.org>
parents:
2924
diff
changeset
|
1768 """Update a file column taking care of race condition |
2526
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1769 |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1770 access is NOT checked in this method, it must be checked beforehand |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1771 @param file_id(unicode): id of the file to update |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1772 @param column(unicode): one of "access" or "extra" |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1773 @param update_cb(callable): method to update the value of the colum |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1774 the method will take older value as argument, and must update it in place |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1775 Note that the callable must be thread-safe |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1776 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1777 return self.storage.file_update(file_id, column, update_cb) |
2526
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1778 |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1779 @defer.inlineCallbacks |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1780 def _delete_file( |
3288
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1781 self, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1782 client, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1783 peer_jid: jid.JID, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1784 recursive: bool, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1785 files_path: Path, |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1786 file_data: dict |
780fb8dd07ef
core (memory/sqlite): new database schema (v9):
Goffi <goffi@goffi.org>
parents:
3281
diff
changeset
|
1787 ): |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1788 """Internal method to delete files/directories recursively |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1789 |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1790 @param peer_jid(jid.JID): entity requesting the deletion (must be owner of files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1791 to delete) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1792 @param recursive(boolean): True if recursive deletion is needed |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1793 @param files_path(unicode): path of the directory containing the actual files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1794 @param file_data(dict): data of the file to delete |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1795 """ |
3028 | 1796 if file_data['owner'] != peer_jid: |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1797 raise exceptions.PermissionError( |
3028 | 1798 "file {file_name} can't be deleted, {peer_jid} is not the owner" |
1799 .format(file_name=file_data['name'], peer_jid=peer_jid.full())) | |
1800 if file_data['type'] == C.FILE_TYPE_DIRECTORY: | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1801 sub_files = yield self.get_files(client, peer_jid, parent=file_data['id']) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1802 if sub_files and not recursive: |
3028 | 1803 raise exceptions.DataError(_("Can't delete directory, it is not empty")) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1804 # we first delete the sub-files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1805 for sub_file_data in sub_files: |
3300
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1806 if sub_file_data['type'] == C.FILE_TYPE_DIRECTORY: |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1807 sub_file_path = files_path / sub_file_data['name'] |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1808 else: |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1809 sub_file_path = files_path |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1810 yield self._delete_file( |
3300
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1811 client, peer_jid, recursive, sub_file_path, sub_file_data) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1812 # then the directory itself |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1813 yield self.storage.file_delete(file_data['id']) |
3028 | 1814 elif file_data['type'] == C.FILE_TYPE_FILE: |
1815 log.info(_("deleting file {name} with hash {file_hash}").format( | |
1816 name=file_data['name'], file_hash=file_data['file_hash'])) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1817 yield self.storage.file_delete(file_data['id']) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1818 references = yield self.get_files( |
3028 | 1819 client, peer_jid, file_hash=file_data['file_hash']) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1820 if references: |
3028 | 1821 log.debug("there are still references to the file, we keep it") |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1822 else: |
3028 | 1823 file_path = os.path.join(files_path, file_data['file_hash']) |
1824 log.info(_("no reference left to {file_path}, deleting").format( | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1825 file_path=file_path)) |
3300
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1826 try: |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1827 os.unlink(file_path) |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1828 except FileNotFoundError: |
b56e4c6b13fc
core (memory): fixed recursive file deletion + log an error and continue when deleting a missing file
Goffi <goffi@goffi.org>
parents:
3288
diff
changeset
|
1829 log.error(f"file at {file_path!r} doesn't exist but it was referenced in files database") |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1830 else: |
3028 | 1831 raise exceptions.InternalError('Unexpected file type: {file_type}' |
1832 .format(file_type=file_data['type'])) | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1833 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1834 async def file_delete(self, client, peer_jid, file_id, recursive=False): |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1835 """Delete a single file or a directory and all its sub-files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1836 |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1837 @param file_id(unicode): id of the file to delete |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1838 @param peer_jid(jid.JID): entity requesting the deletion, |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1839 must be owner of all files to delete |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1840 @param recursive(boolean): must be True to delete a directory and all sub-files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1841 """ |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1842 # FIXME: we only allow owner of file to delete files for now, but WRITE access |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1843 # should be checked too |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1844 files_data = await self.get_files(client, peer_jid, file_id) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1845 if not files_data: |
3028 | 1846 raise exceptions.NotFound("Can't find the file with id {file_id}".format( |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1847 file_id=file_id)) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1848 file_data = files_data[0] |
3028 | 1849 if file_data["type"] != C.FILE_TYPE_DIRECTORY and recursive: |
1850 raise ValueError("recursive can only be set for directories") | |
3932
7af29260ecb8
core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents:
3782
diff
changeset
|
1851 files_path = self.host.get_local_path(None, C.FILES_DIR) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1852 await self._delete_file(client, peer_jid, recursive, files_path, file_data) |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1853 |
3371
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1854 ## Cache ## |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1855 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1856 def get_cache_path(self, namespace: str, *args: str) -> Path: |
3371
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1857 """Get path to use to get a common path for a namespace |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1858 |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1859 This can be used by plugins to manage permanent data. It's the responsability |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1860 of plugins to clean this directory from unused data. |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1861 @param namespace: unique namespace to use |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1862 @param args: extra identifier which will be added to the path |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1863 """ |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1864 namespace = namespace.strip().lower() |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1865 return Path( |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1866 self._cache_path, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1867 regex.path_escape(namespace), |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
1868 *(regex.path_escape(a) for a in args) |
3371
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1869 ) |
e8d74ac7c479
core (memory/memory): added `getCachePath` method to get a cache dir from a namespace.
Goffi <goffi@goffi.org>
parents:
3364
diff
changeset
|
1870 |
4130
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1871 ## Notifications ## |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1872 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1873 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1874 def _add_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1875 self, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1876 type_: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1877 body_plain: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1878 body_rich: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1879 title: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1880 is_global: bool, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1881 requires_action: bool, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1882 priority: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1883 expire_at: float, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1884 extra_s: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1885 profile_key: str |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1886 ) -> defer.Deferred: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1887 client = self.host.get_client(profile_key) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1888 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1889 if not client.is_admin: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1890 raise exceptions.PermissionError("Only admins can add a notification") |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1891 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1892 try: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1893 notification_type = NotificationType[type_] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1894 notification_priority = NotificationPriority[priority] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1895 except KeyError as e: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1896 raise exceptions.DataError( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1897 f"invalid notification type or priority data: {e}" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1898 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1899 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1900 return defer.ensureDeferred( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1901 self.add_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1902 client, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1903 notification_type, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1904 body_plain, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1905 body_rich or None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1906 title or None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1907 is_global, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1908 requires_action, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1909 notification_priority, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1910 expire_at or None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1911 data_format.deserialise(extra_s) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1912 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1913 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1914 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1915 async def add_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1916 self, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1917 client: SatXMPPEntity, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1918 type_: NotificationType, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1919 body_plain: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1920 body_rich: Optional[str] = None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1921 title: Optional[str] = None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1922 is_global: bool = False, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1923 requires_action: bool = False, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1924 priority: NotificationPriority = NotificationPriority.MEDIUM, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1925 expire_at: Optional[float] = None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1926 extra: Optional[dict] = None, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1927 ) -> None: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1928 """Create and broadcast a new notification. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1929 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1930 @param client: client associated with the notification. If None, the notification |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1931 will be global (i.e. for all profiles). |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1932 @param type_: type of the notification. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1933 @param body_plain: plain text body. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1934 @param body_rich: rich text (XHTML) body. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1935 @param title: optional title. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1936 @param is_global: True if the notification is for all profiles. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1937 @param requires_action: True if the notification requires user action (e.g. a |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1938 dialog need to be answered). |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1939 @priority: how urgent the notification is |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1940 @param expire_at: expiration timestamp for the notification. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1941 @param extra: additional data. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1942 """ |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1943 notification = await self.storage.add_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1944 None if is_global else client, type_, body_plain, body_rich, title, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1945 requires_action, priority, expire_at, extra |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1946 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1947 self.host.bridge.notification_new( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1948 str(notification.id), |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1949 notification.timestamp, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1950 type_.value, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1951 body_plain, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1952 body_rich or '', |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1953 title or '', |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1954 requires_action, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1955 priority.value, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1956 expire_at or 0, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1957 data_format.serialise(extra) if extra else '', |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1958 C.PROF_KEY_ALL if is_global else client.profile |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1959 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1960 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1961 def _get_notifications(self, filters_s: str, profile_key: str) -> defer.Deferred: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1962 """Fetch notifications for bridge with given filters and profile key. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1963 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1964 @param filters_s: serialized filter conditions. Keys can be: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1965 :type_ (str): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1966 Filter by type of the notification. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1967 :status (str): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1968 Filter by status of the notification. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1969 :requires_action (bool): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1970 Filter by notifications that require user action. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1971 :min_priority (str): |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1972 Filter by minimum priority value. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1973 @param profile_key: key of the profile to fetch notifications for. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1974 @return: Deferred which fires with a list of serialised notifications. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1975 """ |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1976 client = self.host.get_client(profile_key) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1977 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1978 filters = data_format.deserialise(filters_s) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1979 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1980 try: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1981 if 'type' in filters: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1982 filters['type_'] = NotificationType[filters.pop('type')] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1983 if 'status' in filters: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1984 filters['status'] = NotificationStatus[filters['status']] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1985 if 'min_priority' in filters: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1986 filters['min_priority'] = NotificationPriority[filters['min_priority']].value |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1987 except KeyError as e: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1988 raise exceptions.DataError(f"invalid filter data: {e}") |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1989 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1990 d = defer.ensureDeferred(self.storage.get_notifications(client, **filters)) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1991 d.addCallback( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1992 lambda notifications: data_format.serialise( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1993 [notification.serialise() for notification in notifications] |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1994 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1995 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1996 return d |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1997 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1998 def _delete_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1999 self, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2000 id_: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2001 is_global: bool, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2002 profile_key: str |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2003 ) -> defer.Deferred: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2004 client = self.host.get_client(profile_key) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2005 if is_global and not client.is_admin: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2006 raise exceptions.PermissionError( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2007 "Only admins can delete global notifications" |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2008 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2009 return defer.ensureDeferred(self.delete_notification(client, id_, is_global)) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2010 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2011 async def delete_notification( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2012 self, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2013 client: SatXMPPEntity, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2014 id_: str, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2015 is_global: bool=False |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2016 ) -> None: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2017 """Delete a notification |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2018 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2019 the notification must be from the requesting profile. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2020 @param id_: ID of the notification |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2021 is_global: if True, a global notification will be removed. |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2022 """ |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2023 await self.storage.delete_notification(None if is_global else client, id_) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2024 self.host.bridge.notification_deleted( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2025 id_, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2026 C.PROF_KEY_ALL if is_global else client.profile |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2027 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2028 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2029 def _notifications_expired_clean( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2030 self, limit_timestamp: float, profile_key: str |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2031 ) -> defer.Deferred: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2032 if profile_key == C.PROF_KEY_NONE: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2033 client = None |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2034 else: |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2035 client = self.host.get_client(profile_key) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2036 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2037 return defer.ensureDeferred( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2038 self.storage.clean_expired_notifications( |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2039 client, |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2040 None if limit_timestamp == -1.0 else limit_timestamp |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2041 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2042 ) |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2043 |
02f0adc745c6
core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
2044 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2045 ## Misc ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2046 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
2047 def is_entity_available(self, client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2048 """Tell from the presence information if the given entity is available. |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2049 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2050 @param entity_jid (JID): the entity to check (if bare jid is used, all resources are tested) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2051 @return (bool): True if entity is available |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2052 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2053 if not entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
2054 return bool( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
2055 self.get_available_resources(client, entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
2056 ) # is any resource is available, entity is available |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2057 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
2058 presence_data = self.get_entity_datum(client, entity_jid, "presence") |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2059 except KeyError: |
3028 | 2060 log.debug("No presence information for {}".format(entity_jid)) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2061 return False |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
2062 return presence_data.show != C.PRESENCE_UNAVAILABLE |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2063 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
2064 def is_admin(self, profile: str) -> bool: |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2065 """Tell if given profile has administrator privileges""" |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2066 return profile in self.admins |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2067 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3932
diff
changeset
|
2068 def is_admin_jid(self, entity: jid.JID) -> bool: |
3777
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2069 """Tells if an entity jid correspond to an admin one |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2070 |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2071 It is sometime not possible to use the profile alone to check if an entity is an |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2072 admin (e.g. a request managed by a component). In this case we check if the JID |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2073 correspond to an admin profile |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2074 """ |
001ea5f4a2f9
core: method to know if a profile/entity is an admin:
Goffi <goffi@goffi.org>
parents:
3582
diff
changeset
|
2075 return entity.userhostJID() in self.admin_jids |