Mercurial > libervia-backend
annotate sat/memory/memory.py @ 3188:a15773c6c273
memory(cache): extend EOL when a file metadata is retrieved
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 26 Feb 2020 22:03:15 +0100 |
parents | 39d7327583e1 |
children | ae09989e9feb |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
0 | 3 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
4 # SAT: a jabber client |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
0 | 6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
7 # 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
|
8 # 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
|
9 # 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
|
10 # (at your option) any later version. |
0 | 11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
12 # 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
|
13 # 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
|
14 # 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
|
15 # GNU Affero General Public License for more details. |
0 | 16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
592
diff
changeset
|
17 # 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
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 19 |
771 | 20 from sat.core.i18n import _ |
0 | 21 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
22 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
23 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
24 log = getLogger(__name__) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
25 |
0 | 26 import os.path |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
27 import copy |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
28 from collections import namedtuple |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
29 from uuid import uuid4 |
1447 | 30 from twisted.python import failure |
31 from twisted.internet import defer, reactor, error | |
47 | 32 from twisted.words.protocols.jabber import jid |
914
1a3ba959f0ab
core (memory): moved Params in its own module + introduced a new core/constants module, and moved some constants there
Goffi <goffi@goffi.org>
parents:
909
diff
changeset
|
33 from sat.core import exceptions |
1a3ba959f0ab
core (memory): moved Params in its own module + introduced a new core/constants module, and moved some constants there
Goffi <goffi@goffi.org>
parents:
909
diff
changeset
|
34 from sat.core.constants import Const as C |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
428
diff
changeset
|
35 from sat.memory.sqlite import SqliteStorage |
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
428
diff
changeset
|
36 from sat.memory.persistent import PersistentDict |
914
1a3ba959f0ab
core (memory): moved Params in its own module + introduced a new core/constants module, and moved some constants there
Goffi <goffi@goffi.org>
parents:
909
diff
changeset
|
37 from sat.memory.params import Params |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
38 from sat.memory.disco import Discovery |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
39 from sat.memory.crypto import BlockCipher |
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
|
40 from sat.memory.crypto import PasswordHasher |
1064
7ee9d9db67b9
memory, tools (config): move special config retrieval from memory to tools
souliane <souliane@mailoo.org>
parents:
1046
diff
changeset
|
41 from sat.tools import config as tools_config |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
42 from sat.tools.common import data_format |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
43 import shortuuid |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
44 import mimetypes |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
45 import time |
420
acd908528ef7
core: profile creation/deletion through database
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
46 |
679
59c9a7ff903d
wix, misc: use picture of dimension 32x32 for tray icon + better PEP-8 compliance
souliane <souliane@mailoo.org>
parents:
677
diff
changeset
|
47 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
48 PresenceTuple = namedtuple("PresenceTuple", ("show", "priority", "statuses")) |
1447 | 49 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
|
50 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
51 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
52 class Sessions(object): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
53 """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
|
54 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
55 DEFAULT_TIMEOUT = 600 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
56 |
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
|
57 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
|
58 """ |
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
|
59 @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
|
60 @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
|
61 """ |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
62 self._sessions = dict() |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
63 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
|
64 self.resettable_timeout = resettable_timeout |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
65 |
1212
628e320eab1f
memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
66 def newSession(self, session_data=None, session_id=None, profile=None): |
1447 | 67 """Create a new session |
68 | |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
69 @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
|
70 @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
|
71 @param profile: if set, the session is owned by the profile, |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
72 and profileGet 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
|
73 @return: session_id, session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
74 """ |
1212
628e320eab1f
memory: Sessions.newSession can be called with a forced session ID
souliane <souliane@mailoo.org>
parents:
1199
diff
changeset
|
75 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
|
76 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
|
77 elif session_id in self._sessions: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
78 raise exceptions.ConflictError( |
3028 | 79 "Session id {} is already used".format(session_id) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
80 ) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
81 timer = reactor.callLater(self.timeout, self._purgeSession, session_id) |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
82 if session_data is None: |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
83 session_data = {} |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
84 self._sessions[session_id] = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
85 (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
|
86 ) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
87 return session_id, session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
88 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
89 def _purgeSession(self, session_id): |
1447 | 90 try: |
91 timer, session_data, profile = self._sessions[session_id] | |
92 except ValueError: | |
93 timer, session_data = self._sessions[session_id] | |
94 profile = None | |
95 try: | |
96 timer.cancel() | |
97 except error.AlreadyCalled: | |
98 # if the session is time-outed, the timer has been called | |
99 pass | |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
100 del self._sessions[session_id] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
101 log.debug( |
3028 | 102 "Session {} purged{}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
103 session_id, |
3028 | 104 " (profile {})".format(profile) if profile is not None else "", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
105 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
106 ) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
107 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
108 def __len__(self): |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
109 return len(self._sessions) |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
110 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
111 def __contains__(self, session_id): |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
112 return session_id in self._sessions |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
113 |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
114 def profileGet(self, session_id, profile): |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
115 try: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
116 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
|
117 except ValueError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
118 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
119 "You need to use __getitem__ when profile is not set" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
120 ) |
1447 | 121 except KeyError: |
122 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
|
123 if profile_set != profile: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
124 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
|
125 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
|
126 timer.reset(self.timeout) |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
127 return session_data |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
128 |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
129 def __getitem__(self, session_id): |
757
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
130 try: |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
131 timer, session_data = self._sessions[session_id] |
bbe55c7bee43
core (memory): added optional profile checking in Sessions:
Goffi <goffi@goffi.org>
parents:
756
diff
changeset
|
132 except ValueError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
133 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
134 "You need to use profileGet instead of __getitem__ when profile is set" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
135 ) |
1447 | 136 except KeyError: |
137 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
|
138 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
|
139 timer.reset(self.timeout) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
140 return session_data |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
141 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
142 def __setitem__(self, key, value): |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
143 raise NotImplementedError("You need do use newSession to create a session") |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
144 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
145 def __delitem__(self, session_id): |
1474
c2a498dce4b4
core (memory): fixed a double timer.cancel in Sessions
Goffi <goffi@goffi.org>
parents:
1460
diff
changeset
|
146 """ delete the session data """ |
1447 | 147 self._purgeSession(session_id) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
148 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
149 def keys(self): |
3028 | 150 return list(self._sessions.keys()) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
151 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
152 def iterkeys(self): |
3028 | 153 return iter(self._sessions.keys()) |
756
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
154 |
efa0e0f57950
core (memory): new Sessions management class (similar to dict)
Goffi <goffi@goffi.org>
parents:
751
diff
changeset
|
155 |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
156 class ProfileSessions(Sessions): |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
157 """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
|
158 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
|
159 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
160 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
161 def _profileGetAllIds(self, profile): |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
162 """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
|
163 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
164 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
165 @return: a list containing the sessions ids |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
166 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
167 ret = [] |
3028 | 168 for session_id in self._sessions.keys(): |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
169 try: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
170 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
|
171 except ValueError: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
172 continue |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
173 if profile == profile_set: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
174 ret.append(session_id) |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
175 return ret |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
176 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
177 def profileGetUnique(self, profile): |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
178 """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
|
179 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
180 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
181 @return: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
182 - mutable data (default: dict) of the unique session |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
183 - 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
|
184 - 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
|
185 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
186 ids = self._profileGetAllIds(profile) |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
187 if len(ids) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
188 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
189 "profileGetUnique has been used but more than one session has been found!" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
190 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
191 return ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
192 self.profileGet(ids[0], profile) if len(ids) == 1 else None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
193 ) # XXX: timeout might be reset |
1214
ed3b01ed70d7
memory: profileSessions.profileGetUnique was not resetting the timer
souliane <souliane@mailoo.org>
parents:
1213
diff
changeset
|
194 |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
195 def profileDelUnique(self, profile): |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
196 """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
|
197 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
198 @param profile: %(doc_profile)s |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
199 @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
|
200 """ |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
201 ids = self._profileGetAllIds(profile) |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
202 if len(ids) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
203 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
204 "profileDelUnique has been used but more than one session has been found!" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
205 ) |
1029
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
206 if len(ids) == 1: |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
207 del self._sessions[ids[0]] |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
208 |
f6182f6418ea
memory: add class ProfileSessions based on Sessions
souliane <souliane@mailoo.org>
parents:
1015
diff
changeset
|
209 |
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
|
210 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
|
211 |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
212 # 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
|
213 # 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
|
214 # 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
|
215 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
|
216 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
|
217 |
c6cf44e6330b
memory: temporary dirty hack to fix the personnal key issue after the auth session expired
souliane <souliane@mailoo.org>
parents:
1240
diff
changeset
|
218 def _purgeSession(self, session_id): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
219 log.debug( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
220 "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
|
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 |
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 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
224 class Memory(object): |
1199 | 225 """This class manage all the persistent information""" |
0 | 226 |
22
bb72c29f3432
added action cb mechanism for buttons. Tested with a temporary new user registration button.
Goffi <goffi@goffi.org>
parents:
21
diff
changeset
|
227 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
228 log.info(_("Memory manager init")) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
229 self.initialized = defer.Deferred() |
41
d24629c631fc
SàT: new constant management, a local dir (~/.sat) is now used
Goffi <goffi@goffi.org>
parents:
38
diff
changeset
|
230 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
231 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
|
232 # /!\ an entity is not necessarily in roster |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
233 # main key is bare jid, value is a dict |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
234 # where main key is resource, or None for bare jid |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
235 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
|
236 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
|
237 self.auth_sessions = PasswordSessions() # remember the authenticated profiles |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
238 self.disco = Discovery(host) |
3053
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
239 # XXX: tmp update code, will be removed in the future |
8b36e5c3f28f
misc: don't import memory in launch script:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
240 tools_config.fixLocalDir(False) |
3148
60a9e47ef988
core: log filenames of read config files
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
241 self.config = tools_config.parseMainConf(log_filenames=True) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
242 database_file = os.path.expanduser( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
243 os.path.join(self.getConfig("", "local_dir"), C.SAVEFILE_DATABASE) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
244 ) |
1375
3a20312d4012
core: if we are in dev version and it's possible, repository data are now checked and added to SàT version
Goffi <goffi@goffi.org>
parents:
1315
diff
changeset
|
245 self.storage = SqliteStorage(database_file, host.version) |
432
31e8c48b5f5d
core: - memory refactoring (moved memory.py and sqlite.py from tools to memory)
Goffi <goffi@goffi.org>
parents:
428
diff
changeset
|
246 PersistentDict.storage = self.storage |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
247 self.params = Params(host, self.storage) |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
248 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
|
249 self.params.load_default_params() |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
250 d = self.storage.initialized.addCallback(lambda ignore: 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
|
251 self.memory_data = PersistentDict("memory") |
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
|
252 d.addCallback(lambda ignore: self.memory_data.load()) |
1684
373ce871b0f3
core (disco): disco hashes are now stored in database to avoid doing the same disco request on next load
Goffi <goffi@goffi.org>
parents:
1682
diff
changeset
|
253 d.addCallback(lambda ignore: self.disco.load()) |
428
a4a9efadabfc
core: fixed memory initialisation sequence
Goffi <goffi@goffi.org>
parents:
425
diff
changeset
|
254 d.chainDeferred(self.initialized) |
0 | 255 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
256 ## Configuration ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
257 |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
258 def getConfig(self, section, name, default=None): |
364 | 259 """Get the main configuration option |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
260 |
364 | 261 @param section: section of the config file (None or '' for DEFAULT) |
262 @param name: name of the option | |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
263 @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
|
264 @return: str, list or dict |
365
efbfccfed623
core: local_dir moved to config file
Goffi <goffi@goffi.org>
parents:
364
diff
changeset
|
265 """ |
1234
9c17bd37e6e5
core: better management of default value in getConfig
Goffi <goffi@goffi.org>
parents:
1224
diff
changeset
|
266 return tools_config.getConfig(self.config, section, name, default) |
364 | 267 |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
268 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
|
269 """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
|
270 |
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
271 @param filename (str): input file |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
272 @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
|
273 """ |
fee00f2e11c2
memory, jp: added jp commands to load/save parameters template
souliane <souliane@mailoo.org>
parents:
1007
diff
changeset
|
274 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
|
275 return False |
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
276 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
|
277 if os.path.exists(filename): |
0 | 278 try: |
677
9a50aa7feefb
core (memory): cleaned the part for load/save to xml (not used)
souliane <souliane@mailoo.org>
parents:
669
diff
changeset
|
279 self.params.load_xml(filename) |
3028 | 280 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
|
281 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
|
282 except Exception as e: |
3028 | 283 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
|
284 return False |
0 | 285 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
286 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
|
287 """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
|
288 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
289 @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
|
290 @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
|
291 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
292 if not filename: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
293 return False |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
294 # 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
|
295 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
|
296 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
297 self.params.save_xml(filename) |
3028 | 298 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
|
299 return True |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
300 except Exception as e: |
3028 | 301 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
|
302 return False |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
303 |
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
|
304 def load(self): |
428
a4a9efadabfc
core: fixed memory initialisation sequence
Goffi <goffi@goffi.org>
parents:
425
diff
changeset
|
305 """Load parameters and all memory things from db""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
306 # parameters data |
428
a4a9efadabfc
core: fixed memory initialisation sequence
Goffi <goffi@goffi.org>
parents:
425
diff
changeset
|
307 return self.params.loadGenParams() |
412 | 308 |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
309 def loadIndividualParams(self, profile): |
412 | 310 """Load individual parameters for a profile |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
311 @param profile: %(doc_profile)s""" |
412 | 312 return self.params.loadIndParams(profile) |
313 | |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
314 ## Profiles/Sessions management ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
315 |
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
|
316 def startSession(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
|
317 """"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
|
318 |
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
|
319 @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
|
320 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
|
321 @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
|
322 @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
|
323 @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
|
324 """ |
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
|
325 profile = self.getProfileName(profile) |
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
|
326 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
327 def createSession(__): |
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
|
328 """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
|
329 self._entities_cache[profile] = {} |
3028 | 330 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
|
331 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
|
332 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
333 def backendInitialised(__): |
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
334 def doStartSession(__=None): |
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
|
335 if self.isSessionStarted(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
|
336 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
|
337 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
|
338 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
|
339 # if there is a value at this point in self._entities_cache, |
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
|
340 # it is the loadIndividualParams Deferred, the session is starting |
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
|
341 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
|
342 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
|
343 # else we do request the params |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
344 session_d = self._entities_cache[profile] = self.loadIndividualParams( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
345 profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
346 ) |
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
|
347 session_d.addCallback(createSession) |
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 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
|
349 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
|
350 |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
351 auth_d = defer.ensureDeferred(self.profileAuthenticate(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
|
352 auth_d.addCallback(doStartSession) |
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
|
353 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
|
354 |
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
|
355 if self.host.initialised.called: |
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
|
356 return defer.succeed(None).addCallback(backendInitialised) |
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
|
357 else: |
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
|
358 return self.host.initialised.addCallback(backendInitialised) |
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 |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
360 def stopSession(self, profile): |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
361 """Delete a profile session |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
362 |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
363 @param profile: %(doc_profile)s |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
364 """ |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
365 if self.host.isConnected(profile): |
3028 | 366 log.debug("Disconnecting profile because of session stop") |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
367 self.host.disconnect(profile) |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
368 self.auth_sessions.profileDelUnique(profile) |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
369 try: |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
370 self._entities_cache[profile] |
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
371 except KeyError: |
3028 | 372 log.warning("Profile was not in cache") |
1693
35426d58471c
core (memory): stopSession implementation
Goffi <goffi@goffi.org>
parents:
1689
diff
changeset
|
373 |
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
|
374 def _isSessionStarted(self, profile_key): |
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
|
375 return self.isSessionStarted(self.getProfileName(profile_key)) |
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 |
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 def isSessionStarted(self, 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
|
378 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
|
379 # 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
|
380 # 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
|
381 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
|
382 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
|
383 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
|
384 |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
385 async def profileAuthenticate(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
|
386 """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
|
387 |
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
|
388 @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
|
389 @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
|
390 @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
|
391 """ |
3128
73b5228715e8
core (memory): avoid session locking if profileAuthenticate is called twice quickly
Goffi <goffi@goffi.org>
parents:
3123
diff
changeset
|
392 if not password and self.auth_sessions.profileGetUnique(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
|
393 # 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
|
394 # 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
|
395 # 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
|
396 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
|
397 |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
398 sat_cipher = await self.asyncGetParamA( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
399 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
|
400 ) |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
401 valid = PasswordHasher.verify(password, sat_cipher) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
402 if not valid: |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
403 log.warning(_("Authentication failure of profile {profile}").format( |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
404 profile=profile)) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
405 raise exceptions.PasswordError("The provided profile password doesn't match.") |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
406 return await self.newAuthSession(password, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
407 |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
408 async def newAuthSession(self, key, profile): |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
409 """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
|
410 |
3128
73b5228715e8
core (memory): avoid session locking if profileAuthenticate is called twice quickly
Goffi <goffi@goffi.org>
parents:
3123
diff
changeset
|
411 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
|
412 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
|
413 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
414 @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
|
415 @param profile: %(doc_profile)s |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
416 """ |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
417 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
|
418 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
|
419 # Create the session for this profile and store the personal key |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
420 session_data = self.auth_sessions.profileGetUnique(profile) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
421 if not session_data: |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
422 self.auth_sessions.newSession( |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
423 {C.MEMORY_CRYPTO_KEY: personal_key}, profile=profile |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
424 ) |
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
425 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
|
426 |
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
|
427 def purgeProfileSession(self, profile): |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
428 """Delete cache of data of profile |
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
429 @param profile: %(doc_profile)s""" |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
946
diff
changeset
|
430 log.info(_("[%s] Profile session purge" % profile)) |
416
32dc8b18c2ae
core: param loading/purging on profile connection/disconnection
Goffi <goffi@goffi.org>
parents:
413
diff
changeset
|
431 self.params.purgeProfile(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
|
432 try: |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
433 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
|
434 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
435 log.error( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
436 _( |
3028 | 437 "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
|
438 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
439 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
440 ) |
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
|
441 |
2146
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
442 def getProfilesList(self, clients=True, components=False): |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
443 """retrieve profiles list |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
444 |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
445 @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
|
446 @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
|
447 @return (list[unicode]): selected profiles |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
448 """ |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
449 if not clients and not components: |
3028 | 450 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
|
451 return [] |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
452 profiles = self.storage.getProfilesList() |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
453 if clients and components: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
454 return sorted(profiles) |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
455 isComponent = self.storage.profileIsComponent |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
456 if clients: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
457 p_filter = lambda p: not isComponent(p) |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
458 else: |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
459 p_filter = lambda p: isComponent(p) |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
460 |
1bb9bf1b4150
core, frontends: getProfilesList renamed to profilesGetList + behaviour change:
Goffi <goffi@goffi.org>
parents:
2144
diff
changeset
|
461 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
|
462 |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
463 def getProfileName(self, profile_key, return_profile_keys=False): |
62
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
464 """Return name of profile from keyword |
1263 | 465 |
1519
fbe86b5d156f
core (memory, params): minor docstrings updates
Goffi <goffi@goffi.org>
parents:
1474
diff
changeset
|
466 @param profile_key: can be the profile name or a keyword (like @DEFAULT@) |
1263 | 467 @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
|
468 @return: requested profile name |
1460
c7fd121a6180
core: getProfileName no raise ProfileUnknownError + minor doc fixes
Goffi <goffi@goffi.org>
parents:
1447
diff
changeset
|
469 @raise exceptions.ProfileUnknownError if profile doesn't exists |
1263 | 470 """ |
728
e07afabc4a25
plugin XEP-0050: Ad-Hoc commands first draft (answering part)
Goffi <goffi@goffi.org>
parents:
722
diff
changeset
|
471 return self.params.getProfileName(profile_key, return_profile_keys) |
62
93cb45a7420f
SàT multi-profile: connection using profiles
Goffi <goffi@goffi.org>
parents:
61
diff
changeset
|
472 |
1595
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
473 def profileSetDefault(self, profile): |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
474 """Set default profile |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
475 |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
476 @param profile: %(doc_profile)s |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
477 """ |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
478 # we want to be sure that the profile exists |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
479 profile = self.getProfileName(profile) |
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
480 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
481 self.memory_data["Profile_default"] = profile |
1595
a3d0cfa5b7a6
core, bridge: added a profileSetDefault method
Goffi <goffi@goffi.org>
parents:
1591
diff
changeset
|
482 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
483 def createProfile(self, name, password, component=None): |
420
acd908528ef7
core: profile creation/deletion through database
Goffi <goffi@goffi.org>
parents:
418
diff
changeset
|
484 """Create a new profile |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
485 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
486 @param name(unicode): profile name |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
487 @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
|
488 Can be empty to disable password |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
489 @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
|
490 @return: Deferred |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
491 @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
|
492 """ |
1263 | 493 if not name: |
3028 | 494 raise ValueError("Empty profile name") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
495 if name[0] == "@": |
3028 | 496 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
|
497 if "\n" in name: |
3028 | 498 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
|
499 |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
500 if name in self._entities_cache: |
3028 | 501 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
|
502 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
503 if component: |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
504 if not component in self.host.plugins: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
505 raise exceptions.NotFound( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
506 _( |
3028 | 507 "Can't find component {component} entry point".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
508 component=component |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
509 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
510 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
511 ) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
512 # 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
|
513 # 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
|
514 # 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
|
515 # component = component))) |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
516 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
517 d = self.params.createProfile(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
|
518 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
519 def initPersonalKey(__): |
1652
fd7f41d8cbdf
memory: fixes asyncCreateProfile to not overwrite the personal key
souliane <souliane@mailoo.org>
parents:
1595
diff
changeset
|
520 # be sure to call this after checking that the profile doesn't exist yet |
3040 | 521 |
522 # generated once for all and saved in a PersistentDict | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
523 personal_key = BlockCipher.getRandomKey( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
524 base64=True |
3040 | 525 ).decode('utf-8') |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
526 self.auth_sessions.newSession( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
527 {C.MEMORY_CRYPTO_KEY: personal_key}, profile=name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
528 ) # will be encrypted by setParam |
1652
fd7f41d8cbdf
memory: fixes asyncCreateProfile to not overwrite the personal key
souliane <souliane@mailoo.org>
parents:
1595
diff
changeset
|
529 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
530 def startFakeSession(__): |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
531 # avoid ProfileNotConnected exception in setParam |
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
532 self._entities_cache[name] = None |
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
533 self.params.loadIndParams(name) |
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
534 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
535 def stopFakeSession(__): |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
536 del self._entities_cache[name] |
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
537 self.params.purgeProfile(name) |
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
538 |
1652
fd7f41d8cbdf
memory: fixes asyncCreateProfile to not overwrite the personal key
souliane <souliane@mailoo.org>
parents:
1595
diff
changeset
|
539 d.addCallback(initPersonalKey) |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
540 d.addCallback(startFakeSession) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
541 d.addCallback( |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
542 lambda __: self.setParam( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
543 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
|
544 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
545 ) |
1695
5a93f13c1e76
core (memory): asyncCreateProfile fix: a fake session is created to set general password param
Goffi <goffi@goffi.org>
parents:
1694
diff
changeset
|
546 d.addCallback(stopFakeSession) |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
547 d.addCallback(lambda __: self.auth_sessions.profileDelUnique(name)) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
548 return d |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
549 |
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
|
550 def asyncDeleteProfile(self, name, force=False): |
68 | 551 """Delete an existing profile |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
552 |
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
|
553 @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
|
554 @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
|
555 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
|
556 @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
|
557 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
558 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
559 def cleanMemory(__): |
1704
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
560 self.auth_sessions.profileDelUnique(name) |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
561 try: |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
562 del self._entities_cache[name] |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
563 except KeyError: |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
564 pass |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
565 |
1704
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
566 d = self.params.asyncDeleteProfile(name, force) |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
567 d.addCallback(cleanMemory) |
292f9c2712f2
core (memory): fixed bad memory cleaning on asyncDeleteProfile
Goffi <goffi@goffi.org>
parents:
1695
diff
changeset
|
568 return d |
60
9764e027ecc0
SàT: multi-profile parameters, first draft
Goffi <goffi@goffi.org>
parents:
57
diff
changeset
|
569 |
2144
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
570 def isComponent(self, profile_name): |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
571 """Tell if a profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
572 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
573 @param profile_name(unicode): name of the profile |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
574 @return (bool): True if profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
575 @raise exceptions.NotFound: profile doesn't exist |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
576 """ |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
577 return self.storage.profileIsComponent(profile_name) |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
578 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
579 def getEntryPoint(self, profile_name): |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
580 """Get a component entry point |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
581 |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
582 @param profile_name(unicode): name of the profile |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
583 @return (bool): True if profile is a component |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
584 @raise exceptions.NotFound: profile doesn't exist |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
585 """ |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
586 return self.storage.getEntryPoint(profile_name) |
1d3f73e065e1
core, jp: component handling + client handling refactoring:
Goffi <goffi@goffi.org>
parents:
2052
diff
changeset
|
587 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
588 ## History ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
589 |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
590 def addToHistory(self, client, data): |
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
591 return self.storage.addToHistory(data, client.profile) |
0 | 592 |
3170
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
593 def _historyGetSerialise(self, history_data): |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
594 return [ |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
595 (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
|
596 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
|
597 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
|
598 ] |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
599 |
2698 | 600 def _historyGet(self, from_jid_s, to_jid_s, limit=C.HISTORY_LIMIT_NONE, between=True, |
601 filters=None, profile=C.PROF_KEY_NONE): | |
3170
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
602 d = self.historyGet(jid.JID(from_jid_s), jid.JID(to_jid_s), limit, between, |
2698 | 603 filters, profile) |
3170
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
604 d.addCallback(self._historyGetSerialise) |
39d7327583e1
core: use serialised dict for `extra` in messageNew and historyGet
Goffi <goffi@goffi.org>
parents:
3163
diff
changeset
|
605 return d |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
606 |
2698 | 607 def historyGet(self, from_jid, to_jid, limit=C.HISTORY_LIMIT_NONE, between=True, |
608 filters=None, profile=C.PROF_KEY_NONE): | |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
609 """Retrieve messages in history |
1955
633b5c21aefd
backend, frontend: messages refactoring (huge commit, not finished):
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
610 |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
611 @param from_jid (JID): source JID (full, or bare for catchall) |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
612 @param to_jid (JID): dest JID (full, or bare for catchall) |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
613 @param limit (int): maximum number of messages to get: |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
614 - 0 for no message (returns the empty list) |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
615 - C.HISTORY_LIMIT_NONE or None for unlimited |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
616 - C.HISTORY_LIMIT_DEFAULT to use the HISTORY_LIMIT parameter value |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
617 @param between (bool): confound source and dest (ignore the direction) |
2698 | 618 @param filters (dict[unicode, unicode]): pattern to filter the history results |
619 (see bridge API for details) | |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
620 @param profile (str): %(doc_profile)s |
1963
a2bc5089c2eb
backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents:
1955
diff
changeset
|
621 @return (D(list)): list of message data as in [messageNew] |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
622 """ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
914
diff
changeset
|
623 assert profile != C.PROF_KEY_NONE |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
624 if limit == C.HISTORY_LIMIT_DEFAULT: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
625 limit = int(self.getParamA(C.HISTORY_LIMIT, "General", profile_key=profile)) |
1222
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
626 elif limit == C.HISTORY_LIMIT_NONE: |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
627 limit = None |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
628 if limit == 0: |
e6e0ea4dc835
memory: add Parameter "Chat history limit"
souliane <souliane@mailoo.org>
parents:
1221
diff
changeset
|
629 return defer.succeed([]) |
2013
b536dd121da1
backend (memory), frontends: improved history filtering:
Goffi <goffi@goffi.org>
parents:
1970
diff
changeset
|
630 return self.storage.historyGet(from_jid, to_jid, limit, between, filters, profile) |
0 | 631 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
632 ## Statuses ## |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
633 |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
634 def _getPresenceStatuses(self, profile_key): |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
635 ret = self.getPresenceStatuses(profile_key) |
3028 | 636 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
|
637 |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
638 def getPresenceStatuses(self, profile_key): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
639 """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
|
640 |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
641 @param profile_key: %(doc_profile_key)s |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
642 @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
|
643 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
644 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
645 profile_cache = self._getProfileCache(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
|
646 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
|
647 |
3028 | 648 for entity_jid, entity_data in profile_cache.items(): |
649 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
|
650 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
|
651 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
652 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
653 presence_data = self.getEntityDatum(full_jid, "presence", profile_key) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
654 except KeyError: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
655 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
656 entities_presence.setdefault(entity_jid, {})[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
657 resource or "" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
658 ] = presence_data |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
659 |
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
|
660 return entities_presence |
0 | 661 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
662 def setPresenceStatus(self, entity_jid, show, priority, statuses, profile_key): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
663 """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
|
664 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
665 @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
|
666 @param show: show status |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
667 @param priority: priority |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
668 @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
|
669 @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
|
670 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
671 presence_data = PresenceTuple(show, priority, statuses) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
672 self.updateEntityData( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
673 entity_jid, "presence", presence_data, profile_key=profile_key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
674 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
675 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
|
676 # 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
|
677 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
678 self.delEntityDatum(entity_jid.userhostJID(), "presence", profile_key) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
679 except (KeyError, exceptions.UnknownEntityError): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
680 pass |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
681 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
682 ## Resources ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
683 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
684 def _getAllResource(self, jid_s, profile_key): |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
685 client = self.host.getClient(profile_key) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
686 jid_ = jid.JID(jid_s) |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
687 return self.getAllResources(client, jid_) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
688 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
689 def getAllResources(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
|
690 """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
|
691 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
692 @param entity_jid: bare 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
|
693 return (list[unicode]): list of resources |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
694 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
695 @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
|
696 @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
|
697 """ |
2597
9446f1ea9eac
core: discoFindByFeatures now return only available resources
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
698 # 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
|
699 if entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
700 raise ValueError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
701 "getAllResources must be used with a bare jid (got {})".format(entity_jid) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
702 ) |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
703 profile_cache = self._getProfileCache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
704 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
705 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
|
706 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
707 raise exceptions.UnknownEntityError( |
3028 | 708 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
709 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
710 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
|
711 resources.discard(None) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
712 return resources |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
713 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
714 def getAvailableResources(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
|
715 """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
|
716 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
717 This method differs from getAllResources by returning only available resources |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
718 @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
|
719 return (list[unicode]): list of available resources |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
720 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
721 @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
|
722 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
723 available = [] |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
724 for resource in self.getAllResources(client, entity_jid): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
725 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
|
726 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
727 try: |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
728 presence_data = self.getEntityDatum(full_jid, "presence", client.profile) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
729 except KeyError: |
3028 | 730 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
|
731 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
732 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
|
733 available.append(resource) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
734 return available |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
735 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
736 def _getMainResource(self, jid_s, profile_key): |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
737 client = self.host.getClient(profile_key) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
738 jid_ = jid.JID(jid_s) |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
739 return self.getMainResource(client, jid_) or "" |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
740 |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
741 def getMainResource(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
|
742 """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
|
743 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
744 @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
|
745 @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
|
746 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
747 if entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
748 raise ValueError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
749 "getMainResource must be used with a bare jid (got {})".format(entity_jid) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
750 ) |
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
|
751 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
752 if self.host.plugins["XEP-0045"].isJoinedRoom(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
|
753 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
|
754 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
|
755 pass |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
756 try: |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
757 resources = self.getAllResources(client, entity_jid) |
1689
a40124e03baf
core (memory): catch exceptions.UnknownEntityError in getMainResource
Goffi <goffi@goffi.org>
parents:
1684
diff
changeset
|
758 except exceptions.UnknownEntityError: |
3028 | 759 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
|
760 return None |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
761 priority_resources = [] |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
762 for resource in resources: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
763 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
|
764 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
765 try: |
1970
200cd707a46d
plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):
Goffi <goffi@goffi.org>
parents:
1963
diff
changeset
|
766 presence_data = self.getEntityDatum(full_jid, "presence", client.profile) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
767 except KeyError: |
3028 | 768 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
|
769 continue |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
770 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
|
771 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
772 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
|
773 except ValueError: |
3028 | 774 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
|
775 return None |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
776 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
777 ## Entities data ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
778 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
779 def _getProfileCache(self, client): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
780 """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
|
781 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
782 @param client: SatXMPPClient |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
783 @return (dict): profile cache |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
784 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
785 return self._entities_cache[client.profile] |
1251
51a85e8f599a
memory: add method isContactConnected
souliane <souliane@mailoo.org>
parents:
1247
diff
changeset
|
786 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
787 def setSignalOnUpdate(self, key, signal=True): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
788 """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
|
789 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
790 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
|
791 @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
|
792 @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
|
793 """ |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
794 if signal: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
795 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
|
796 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
797 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
|
798 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
799 def getAllEntitiesIter(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
|
800 """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
|
801 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
802 @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
|
803 @return (list[unicode]): list of jids |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
804 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
805 profile_cache = self._getProfileCache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
806 # we construct a list of all known full jids (bare jid of entities x resources) |
3028 | 807 for bare_jid, entity_data in profile_cache.items(): |
808 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
|
809 if resource is None: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
810 continue |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
811 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
|
812 full_jid.resource = resource |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
813 yield full_jid |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
814 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
815 def updateEntityData( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
816 self, entity_jid, key, value, silent=False, profile_key=C.PROF_KEY_NONE |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
817 ): |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
818 """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
|
819 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
820 If key was registered with setSignalOnUpdate, 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
|
821 @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
|
822 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
|
823 @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
|
824 @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
|
825 @param silent(bool): if True, doesn't send signal to frontend, even if there is a |
94708a7d3ecf
core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant:
Goffi <goffi@goffi.org>
parents:
2928
diff
changeset
|
826 signal flag (see setSignalOnUpdate) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
827 @param profile_key: %(doc_profile_key)s |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
828 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
829 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
830 profile_cache = self._getProfileCache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
831 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
|
832 entities = self.getAllEntitiesIter(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
|
833 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
834 entities = (entity_jid,) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
835 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
836 for jid_ in entities: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
837 entity_data = profile_cache.setdefault(jid_.userhostJID(), {}).setdefault( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
838 jid_.resource, {} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
839 ) |
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 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
|
842 if key in self._key_signals and not silent: |
3028 | 843 if not isinstance(value, str): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
844 log.error( |
3028 | 845 "Setting a non string value ({}) for a key ({}) which has a signal flag".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
846 value, key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
847 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
848 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
849 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
850 self.host.bridge.entityDataUpdated( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
851 jid_.full(), key, value, self.getProfileName(profile_key) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
852 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
853 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
854 def delEntityDatum(self, entity_jid, key, profile_key): |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
855 """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
|
856 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
857 @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
|
858 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
|
859 @param key: key to delete (eg: C.ENTITY_TYPE) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
860 @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
|
861 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
862 @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
|
863 @raise KeyError: key is not in cache |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
864 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
865 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
866 profile_cache = self._getProfileCache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
867 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
|
868 entities = self.getAllEntitiesIter(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
|
869 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
870 entities = (entity_jid,) |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
871 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
872 for jid_ in entities: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
873 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
874 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
|
875 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
876 raise exceptions.UnknownEntityError( |
3028 | 877 "Entity {} not in cache".format(jid_) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
878 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
879 try: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
880 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
|
881 except KeyError as e: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
882 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
|
883 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
|
884 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
885 raise e |
399 | 886 |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
887 def _getEntitiesData(self, entities_jids, keys_list, profile_key): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
888 ret = self.getEntitiesData( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
889 [jid.JID(jid_) for jid_ in entities_jids], keys_list, profile_key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
890 ) |
3028 | 891 return {jid_.full(): data for jid_, data in ret.items()} |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
892 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
893 def getEntitiesData(self, entities_jids, keys_list=None, profile_key=C.PROF_KEY_NONE): |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
894 """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
|
895 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
896 @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
|
897 @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
|
898 @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
|
899 @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
|
900 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
|
901 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
|
902 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
|
903 in resulting dict |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
904 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
905 @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
|
906 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
907 |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
908 def fillEntityData(entity_cache_data): |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
909 entity_data = {} |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
910 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
|
911 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
|
912 else: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
913 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
|
914 try: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
915 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
|
916 except KeyError: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
917 continue |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
918 return entity_data |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
919 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
920 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
921 profile_cache = self._getProfileCache(client) |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
922 ret_data = {} |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
923 if entities_jids: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
924 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
|
925 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
926 entity_cache_data = profile_cache[entity.userhostJID()][ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
927 entity.resource |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
928 ] |
1314
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
929 except KeyError: |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
930 continue |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
931 ret_data[entity.full()] = fillEntityData(entity_cache_data, keys_list) |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
932 else: |
3028 | 933 for bare_jid, data in profile_cache.items(): |
934 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
|
935 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
|
936 full_jid.resource = resource |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
937 ret_data[full_jid] = fillEntityData(entity_cache_data) |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
938 |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
939 return ret_data |
bb9c32249778
core: added getEntitiesData which get cache data for several entities at once
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
940 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
941 def getEntityData(self, entity_jid, keys_list=None, profile_key=C.PROF_KEY_NONE): |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
942 """Get a list of cached values for entity |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
943 |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
944 @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
|
945 @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
|
946 @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
|
947 @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
|
948 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
|
949 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
|
950 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
951 @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
|
952 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
953 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
954 profile_cache = self._getProfileCache(client) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
955 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
956 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
|
957 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
958 raise exceptions.UnknownEntityError( |
3028 | 959 "Entity {} not in cache (was requesting {})".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
960 entity_jid, keys_list |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
961 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
962 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
963 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
|
964 return entity_data |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
965 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
966 return {key: entity_data[key] for key in keys_list if key in entity_data} |
47 | 967 |
944
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
968 def getEntityDatum(self, entity_jid, key, profile_key): |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
969 """Get a datum from entity |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
970 |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
971 @param entity_jid: JID of the entity |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
972 @param keys: key to get |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
973 @param profile_key: %(doc_profile_key)s |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
974 @return: requested value |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
975 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
976 @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
|
977 @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
|
978 """ |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
979 return self.getEntityData(entity_jid, (key,), profile_key)[key] |
e1842ebcb2f3
core, plugin XEP-0115: discovery refactoring:
Goffi <goffi@goffi.org>
parents:
943
diff
changeset
|
980 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
981 def delEntityCache( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
982 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
|
983 ): |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
984 """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
|
985 |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
986 @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
|
987 @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
|
988 @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
|
989 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
990 @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
|
991 """ |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
992 client = self.host.getClient(profile_key) |
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
993 profile_cache = self._getProfileCache(client) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
994 |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
995 if delete_all_resources: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
996 if entity_jid.resource: |
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
diff
changeset
|
997 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
|
998 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
999 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
|
1000 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1001 raise exceptions.UnknownEntityError( |
3028 | 1002 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1003 ) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1004 else: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1005 try: |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1006 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
|
1007 except KeyError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1008 raise exceptions.UnknownEntityError( |
3028 | 1009 "Entity {} not in cache".format(entity_jid) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1010 ) |
943
71926ec2114d
core (memory): entities cache improvments:
Goffi <goffi@goffi.org>
parents:
935
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 ## Encryption ## |
507
f98bef71a918
frontends, core, plugin XEP-0045: leave implementation + better nick change
Goffi <goffi@goffi.org>
parents:
504
diff
changeset
|
1013 |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1014 def encryptValue(self, value, profile): |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1015 """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
|
1016 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
|
1017 already authenticated. |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1018 |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1019 @param value (str): the value to encrypt |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1020 @param profile (str): %(doc_profile)s |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1021 @return: the deferred encrypted value |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1022 """ |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1023 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1024 personal_key = self.auth_sessions.profileGetUnique(profile)[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1025 C.MEMORY_CRYPTO_KEY |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1026 ] |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1027 except TypeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1028 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1029 _("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
|
1030 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1031 ) |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1032 return BlockCipher.encrypt(personal_key, value) |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1033 |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1034 def decryptValue(self, value, profile): |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1035 """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
|
1036 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
|
1037 already authenticated. |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1038 |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1039 @param value (str): the value to decrypt |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1040 @param profile (str): %(doc_profile)s |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1041 @return: the deferred decrypted value |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1042 """ |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1043 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1044 personal_key = self.auth_sessions.profileGetUnique(profile)[ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1045 C.MEMORY_CRYPTO_KEY |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1046 ] |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1047 except TypeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1048 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1049 _("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
|
1050 % profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1051 ) |
1090
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1052 return BlockCipher.decrypt(personal_key, value) |
594fbdda4a87
memory: add helper methods encryptValue and decryptValue
souliane <souliane@mailoo.org>
parents:
1064
diff
changeset
|
1053 |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1054 def encryptPersonalData(self, data_key, data_value, crypto_key, profile): |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1055 """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
|
1056 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1057 @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
|
1058 @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
|
1059 @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
|
1060 @param profile: %(profile_doc)s |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1061 @return: a deferred None value |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1062 """ |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1063 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1064 def gotIndMemory(data): |
3160
330a5f1d9eea
core (memory/crypto): replaced `PyCrypto` by `cryptography`:
Goffi <goffi@goffi.org>
parents:
3148
diff
changeset
|
1065 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
|
1066 return data.force(data_key) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1067 |
2765
378188abe941
misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents:
2698
diff
changeset
|
1068 def done(__): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1069 log.debug( |
3028 | 1070 _("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
|
1071 % {"ns": C.MEMORY_CRYPTO_NAMESPACE, "key": data_key} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1072 ) |
1030
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1073 |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1074 d = PersistentDict(C.MEMORY_CRYPTO_NAMESPACE, profile).load() |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1075 return d.addCallback(gotIndMemory).addCallback(done) |
15f43b54d697
core, memory, bridge: added profile password + password encryption:
souliane <souliane@mailoo.org>
parents:
1029
diff
changeset
|
1076 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1077 ## Subscription requests ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1078 |
722
04aabc3f2684
core (memory): fixed setDefault behaviour + minor refactoring
Goffi <goffi@goffi.org>
parents:
679
diff
changeset
|
1079 def addWaitingSub(self, type_, entity_jid, profile_key): |
47 | 1080 """Called when a subcription request is received""" |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1081 profile = self.getProfileName(profile_key) |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1082 assert profile |
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1083 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
|
1084 self.subscriptions[profile] = {} |
722
04aabc3f2684
core (memory): fixed setDefault behaviour + minor refactoring
Goffi <goffi@goffi.org>
parents:
679
diff
changeset
|
1085 self.subscriptions[profile][entity_jid] = type_ |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1086 |
486
0d9908ac775e
core: entity cache misc data management + error moved to core.exceptions in memory
Goffi <goffi@goffi.org>
parents:
484
diff
changeset
|
1087 def delWaitingSub(self, entity_jid, profile_key): |
47 | 1088 """Called when a subcription request is finished""" |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1089 profile = self.getProfileName(profile_key) |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1090 assert profile |
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1091 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
|
1092 del self.subscriptions[profile][entity_jid] |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1093 |
346
ca3a041fed30
core: fixed several subscription scheme issues + removed most of profile_key default value in core.sat_main and core.xmmp (source of bugs) + contact update
Goffi <goffi@goffi.org>
parents:
327
diff
changeset
|
1094 def getWaitingSub(self, profile_key): |
47 | 1095 """Called to get a list of currently waiting subscription requests""" |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1096 profile = self.getProfileName(profile_key) |
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1097 if not profile: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1098 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
|
1099 return {} |
592
e5a875a3311b
Fix pep8 support in src/memory.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
1100 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
|
1101 return {} |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1102 |
65
d35c5edab53f
SàT: multi-profile: memory & dbus bridge's methods profile management
Goffi <goffi@goffi.org>
parents:
64
diff
changeset
|
1103 return self.subscriptions[profile] |
0 | 1104 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1105 ## Parameters ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1106 |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
914
diff
changeset
|
1107 def getStringParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
512
diff
changeset
|
1108 return self.params.getStringParamA(name, category, attr, profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1109 |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
914
diff
changeset
|
1110 def getParamA(self, name, category, attr="value", profile_key=C.PROF_KEY_NONE): |
1534
a5e0393a06cd
plugin ip, params: plugin IP discovery, first draft:
Goffi <goffi@goffi.org>
parents:
1519
diff
changeset
|
1111 return self.params.getParamA(name, category, attr, profile_key=profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1112 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1113 def asyncGetParamA( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1114 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1115 name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1116 category, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1117 attr="value", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1118 security_limit=C.NO_SECURITY_LIMIT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1119 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1120 ): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1121 return self.params.asyncGetParamA( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1122 name, category, attr, security_limit, profile_key |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1123 ) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1124 |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1125 def _getParamsValuesFromCategory( |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1126 self, category, security_limit, app, extra_s, profile_key |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1127 ): |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1128 return self.params._getParamsValuesFromCategory( |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1129 category, security_limit, app, extra_s, profile_key |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1130 ) |
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
|
1131 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1132 def asyncGetStringParamA( |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1133 self, name, category, attr="value", security_limit=C.NO_SECURITY_LIMIT, |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1134 profile_key=C.PROF_KEY_NONE): |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1135 |
3123
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1136 profile = self.getProfileName(profile_key) |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1137 return defer.ensureDeferred(self.params.asyncGetStringParamA( |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1138 name, category, attr, security_limit, profile |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1139 )) |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1140 |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1141 def _getParamsUI(self, security_limit, app, extra_s, profile_key): |
130f9cb6e0ab
core (memory/params): added `extra` argument to filter out params notably in `getParamsUI`:
Goffi <goffi@goffi.org>
parents:
3053
diff
changeset
|
1142 return self.params._getParamsUI(security_limit, app, extra_s, profile_key) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1143 |
0 | 1144 def getParamsCategories(self): |
17
74a39f40eb6d
refactoring: using xml params (not finished yet)
Goffi <goffi@goffi.org>
parents:
14
diff
changeset
|
1145 return self.params.getParamsCategories() |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1146 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1147 def setParam( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1148 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1149 name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1150 value, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1151 category, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1152 security_limit=C.NO_SECURITY_LIMIT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1153 profile_key=C.PROF_KEY_NONE, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1154 ): |
641
49587e170f53
core: added the security_limit to setParam
souliane <souliane@mailoo.org>
parents:
639
diff
changeset
|
1155 return self.params.setParam(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
|
1156 |
662
4f747d7fde8c
core: importParams renamed to updateParams: it now updates the parameter instead of appending children if it find an existing one.
Goffi <goffi@goffi.org>
parents:
656
diff
changeset
|
1157 def updateParams(self, xml): |
4f747d7fde8c
core: importParams renamed to updateParams: it now updates the parameter instead of appending children if it find an existing one.
Goffi <goffi@goffi.org>
parents:
656
diff
changeset
|
1158 return self.params.updateParams(xml) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
574
diff
changeset
|
1159 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1160 def paramsRegisterApp(self, xml, security_limit=C.NO_SECURITY_LIMIT, app=""): |
777
5642939d254e
core, bridge: new method paramsRegisterApp to register frontend's specific parameters
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
1161 return self.params.paramsRegisterApp(xml, security_limit, app) |
5642939d254e
core, bridge: new method paramsRegisterApp to register frontend's specific parameters
souliane <souliane@mailoo.org>
parents:
771
diff
changeset
|
1162 |
20
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
1163 def setDefault(self, name, category, callback, errback=None): |
fc8c202cda87
refactoring: using xml params part IV (default values)
Goffi <goffi@goffi.org>
parents:
19
diff
changeset
|
1164 return self.params.setDefault(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
|
1165 |
3163
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1166 ## Private Data ## |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1167 |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1168 def _privateDataSet(self, namespace, key, data_s, profile_key): |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1169 client = self.host.getClient(profile_key) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1170 # we accept any type |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1171 data = data_format.deserialise(data_s, type_check=None) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1172 return self.storage.setPrivateValue( |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1173 namespace, key, data, binary=True, profile=client.profile) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1174 |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1175 def _privateDataGet(self, namespace, key, profile_key): |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1176 client = self.host.getClient(profile_key) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1177 d = self.storage.getPrivates( |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1178 namespace, [key], binary=True, profile=client.profile) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1179 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
|
1180 return d |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1181 |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1182 def _privateDataDelete(self, namespace, key, profile_key): |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1183 client = self.host.getClient(profile_key) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1184 return self.storage.delPrivateValue( |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1185 namespace, key, binary=True, profile=client.profile) |
d10b2368684e
bridge: added methods to let frontends store/retrieve/delete private data
Goffi <goffi@goffi.org>
parents:
3160
diff
changeset
|
1186 |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1187 ## Files ## |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1188 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1189 def checkFilePermission(self, file_data, peer_jid, perms_to_check): |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1190 """check that an entity has the right permission on a file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1191 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1192 @param file_data(dict): data of one file, as returned by getFiles |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1193 @param peer_jid(jid.JID): entity trying to access the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1194 @param perms_to_check(tuple[unicode]): permissions to check |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1195 tuple of C.ACCESS_PERM_* |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1196 @param check_parents(bool): if True, also check all parents until root node |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1197 @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
|
1198 in perms_to_check for file_data |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1199 @raise exceptions.InternalError: perms_to_check is invalid |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1200 """ |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1201 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
|
1202 return |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1203 peer_jid = peer_jid.userhostJID() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1204 if peer_jid == file_data["owner"]: |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1205 # the owner has all rights |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1206 return |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1207 if not C.ACCESS_PERMS.issuperset(perms_to_check): |
3028 | 1208 raise exceptions.InternalError(_("invalid permission")) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1209 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1210 for perm in perms_to_check: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1211 # 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
|
1212 # 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
|
1213 try: |
3028 | 1214 perm_data = file_data["access"][perm] |
1215 perm_type = perm_data["type"] | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1216 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
|
1217 raise exceptions.PermissionError() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1218 if perm_type == C.ACCESS_TYPE_PUBLIC: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1219 continue |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1220 elif perm_type == C.ACCESS_TYPE_WHITELIST: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1221 try: |
3028 | 1222 jids = perm_data["jids"] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1223 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
|
1224 raise exceptions.PermissionError() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1225 if peer_jid.full() in jids: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1226 continue |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1227 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
|
1228 raise exceptions.PermissionError() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1229 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1230 raise exceptions.InternalError( |
3028 | 1231 _("unknown access type: {type}").format(type=perm_type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1232 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1233 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1234 @defer.inlineCallbacks |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1235 def checkPermissionToRoot(self, client, file_data, peer_jid, perms_to_check): |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1236 """do checkFilePermission on file_data and all its parents until root""" |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1237 current = file_data |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1238 while True: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1239 self.checkFilePermission(current, peer_jid, perms_to_check) |
3028 | 1240 parent = current["parent"] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1241 if not parent: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1242 break |
3040 | 1243 files_data = yield self.getFiles( |
1244 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
|
1245 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1246 try: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1247 current = files_data[0] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1248 except IndexError: |
3028 | 1249 raise exceptions.DataError("Missing parent") |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1250 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1251 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1252 def _getParentDir( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1253 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
|
1254 ): |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1255 """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
|
1256 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1257 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
|
1258 @return (tuple[unicode, list[unicode])): parent, remaining path elements: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1259 - 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
|
1260 - 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
|
1261 (i.e. which don't exist) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1262 """ |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1263 # 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
|
1264 if parent is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1265 raise exceptions.ConflictError( |
3028 | 1266 _("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
|
1267 ) |
3028 | 1268 path_elts = [_f for _f in path.split("/") if _f] |
1269 if {"..", "."}.intersection(path_elts): | |
1270 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
|
1271 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1272 # 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
|
1273 # non existing directories will be created |
3028 | 1274 parent = "" |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1275 for idx, path_elt in enumerate(path_elts): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1276 directories = yield self.storage.getFiles( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1277 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1278 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1279 type_=C.FILE_TYPE_DIRECTORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1280 name=path_elt, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1281 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1282 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1283 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1284 if not directories: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1285 defer.returnValue((parent, path_elts[idx:])) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1286 # 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
|
1287 elif len(directories) > 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1288 raise exceptions.InternalError( |
3028 | 1289 _("Several directories found, this should not happen") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1290 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1291 else: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1292 directory = directories[0] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1293 self.checkFilePermission(directory, peer_jid, perms_to_check) |
3028 | 1294 parent = directory["id"] |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1295 defer.returnValue((parent, [])) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1296 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1297 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1298 def getFiles( |
2924 | 1299 self, client, peer_jid, file_id=None, version=None, parent=None, path=None, |
1300 type_=None, file_hash=None, hash_algo=None, name=None, namespace=None, | |
1301 mime_type=None, owner=None, access=None, projection=None, unique=False, | |
1302 perms_to_check=(C.ACCESS_PERM_READ,)): | |
1303 """Retrieve files with with given filters | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1304 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1305 @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
|
1306 needed to check permission. |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1307 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
|
1308 @param file_id(unicode, None): id of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1309 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1310 @param version(unicode, None): version of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1311 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1312 empty string to look for current version |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1313 @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
|
1314 None to ignore |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1315 empty string to look for root files/directories |
2924 | 1316 @param path(unicode, None): path to the directory containing the files |
1317 @param type_(unicode, None): type of file filter, can be one of C.FILE_TYPE_* | |
1318 @param file_hash(unicode, None): hash of the file to retrieve | |
1319 @param hash_algo(unicode, None): algorithm use for file_hash | |
1320 @param name(unicode, None): name of the file to retrieve | |
1321 @param namespace(unicode, None): namespace of the files to retrieve | |
1322 @param mime_type(unicode, None): filter on this mime type | |
1323 @param owner(jid.JID, None): if not None, only get files from this owner | |
1324 @param access(dict, None): get file with given access (see [setFile]) | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1325 @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
|
1326 None to retrieve all |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1327 @param unique(bool): if True will remove duplicates |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1328 @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
|
1329 must be a tuple of C.ACCESS_PERM_* or None |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1330 if None, permission will no be checked (peer_jid must be None too in this case) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1331 other params are the same as for [setFile] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1332 @return (list[dict]): files corresponding to filters |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1333 @raise exceptions.NotFound: parent directory not found (when path is specified) |
2924 | 1334 @raise exceptions.PermissionError: peer_jid can't use perms_to_check for one of |
1335 the file | |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1336 on the path |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1337 """ |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1338 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
|
1339 raise exceptions.InternalError( |
3028 | 1340 "if you want to disable permission check, both peer_jid and " |
1341 "perms_to_check must be None" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1342 ) |
2518
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1343 if owner is not None: |
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1344 owner = owner.userhostJID() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1345 if path is not None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1346 # permission are checked by _getParentDir |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1347 parent, remaining_path_elts = yield self._getParentDir( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1348 client, path, parent, namespace, owner, peer_jid, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1349 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1350 if remaining_path_elts: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1351 # if we have remaining path elements, |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1352 # the parent directory is not found |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1353 raise failure.Failure(exceptions.NotFound()) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1354 if parent and peer_jid: |
2924 | 1355 # 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
|
1356 # we need to check all the parents |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1357 parent_data = yield self.storage.getFiles(client, file_id=parent) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1358 try: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1359 parent_data = parent_data[0] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1360 except IndexError: |
3028 | 1361 raise exceptions.DataError("mising parent") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1362 yield self.checkPermissionToRoot( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1363 client, parent_data, peer_jid, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1364 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1365 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1366 files = yield self.storage.getFiles( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1367 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1368 file_id=file_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1369 version=version, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1370 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1371 type_=type_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1372 file_hash=file_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1373 hash_algo=hash_algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1374 name=name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1375 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1376 mime_type=mime_type, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1377 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1378 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1379 projection=projection, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1380 unique=unique, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1381 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1382 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1383 if peer_jid: |
2924 | 1384 # 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
|
1385 to_remove = [] |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1386 for file_data in files: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1387 try: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1388 self.checkFilePermission(file_data, peer_jid, perms_to_check) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1389 except exceptions.PermissionError: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1390 to_remove.append(file_data) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1391 for file_data in to_remove: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1392 files.remove(file_data) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1393 defer.returnValue(files) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1394 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1395 @defer.inlineCallbacks |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1396 def setFile( |
3028 | 1397 self, client, name, file_id=None, version="", parent=None, path=None, |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1398 type_=C.FILE_TYPE_FILE, file_hash=None, hash_algo=None, size=None, |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1399 namespace=None, mime_type=None, created=None, modified=None, owner=None, |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1400 access=None, extra=None, peer_jid=None, perms_to_check=(C.ACCESS_PERM_WRITE,) |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1401 ): |
2924 | 1402 """Set a file metadata |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1403 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1404 @param name(unicode): basename of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1405 @param file_id(unicode): unique id of the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1406 @param version(unicode): version of this file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1407 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
|
1408 @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
|
1409 @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
|
1410 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
|
1411 if needed, using current access. |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1412 @param file_hash(unicode): unique hash of the payload |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1413 @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
|
1414 @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
|
1415 @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
|
1416 files |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1417 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
|
1418 @param mime_type(unicode): MIME type of the file, or None if not known/guessed |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1419 @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
|
1420 @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
|
1421 created date |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1422 @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
|
1423 component) |
2518
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1424 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
|
1425 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
|
1426 @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
|
1427 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
|
1428 access the file |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1429 key can be on on C.ACCESS_PERM_*, |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1430 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
|
1431 According to type, extra keys can be used: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1432 - 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
|
1433 - 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
|
1434 in the 'jids' key |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1435 will be encoded to json in database |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1436 @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
|
1437 will be encoded to json in database |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1438 @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
|
1439 must be a tuple of C.ACCESS_PERM_* or None |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1440 if None, permission will no be checked (peer_jid must be None too in this |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1441 case) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1442 @param profile(unicode): profile owning the file |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1443 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1444 if "/" in name: |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1445 raise ValueError('name must not contain a slash ("/")') |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1446 if file_id is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1447 file_id = shortuuid.uuid() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1448 if ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1449 file_hash is not None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1450 and hash_algo is None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1451 or hash_algo is not None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1452 and file_hash is None |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1453 ): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1454 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
|
1455 if mime_type is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1456 mime_type, file_encoding = mimetypes.guess_type(name) |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1457 if created is None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1458 created = time.time() |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1459 if namespace is not None: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1460 namespace = namespace.strip() or None |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1461 if type_ == C.FILE_TYPE_DIRECTORY: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1462 if any(version, file_hash, size, mime_type): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1463 raise ValueError( |
3028 | 1464 "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
|
1465 ) |
2518
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1466 if owner is not None: |
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1467 owner = owner.userhostJID() |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1468 |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1469 if path is not None: |
2518
e4de2f16a284
core (memory): use bare jid for owner in setFiles:
Goffi <goffi@goffi.org>
parents:
2508
diff
changeset
|
1470 # _getParentDir will check permissions if peer_jid is set, so we use owner |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1471 parent, remaining_path_elts = yield self._getParentDir( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1472 client, path, parent, namespace, owner, owner, perms_to_check |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1473 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1474 # 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
|
1475 for new_dir in remaining_path_elts: |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1476 new_dir_id = shortuuid.uuid() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1477 yield self.storage.setFile( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1478 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1479 name=new_dir, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1480 file_id=new_dir_id, |
3028 | 1481 version="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1482 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1483 type_=C.FILE_TYPE_DIRECTORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1484 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1485 created=time.time(), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1486 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1487 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1488 extra={}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1489 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1490 parent = new_dir_id |
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1491 elif parent is None: |
3028 | 1492 parent = "" |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1493 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1494 yield self.storage.setFile( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1495 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1496 file_id=file_id, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1497 version=version, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1498 parent=parent, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1499 type_=type_, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1500 file_hash=file_hash, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1501 hash_algo=hash_algo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1502 name=name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1503 size=size, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1504 namespace=namespace, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1505 mime_type=mime_type, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1506 created=created, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1507 modified=modified, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1508 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1509 access=access, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1510 extra=extra, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1511 ) |
2501
3b67fe672206
core (memory): file metadata handling methods:
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1512 |
2526
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1513 def fileUpdate(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
|
1514 """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
|
1515 |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1516 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
|
1517 @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
|
1518 @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
|
1519 @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
|
1520 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
|
1521 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
|
1522 """ |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1523 return self.storage.fileUpdate(file_id, column, update_cb) |
35d591086974
core (memory, sqlite): added fileUpdate method to update "extra" and "access" avoiding race condition
Goffi <goffi@goffi.org>
parents:
2518
diff
changeset
|
1524 |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1525 @defer.inlineCallbacks |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1526 def _deleteFile(self, client, peer_jid, recursive, files_path, file_data): |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1527 """Internal method to delete files/directories recursively |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1528 |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1529 @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
|
1530 to delete) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1531 @param recursive(boolean): True if recursive deletion is needed |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1532 @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
|
1533 @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
|
1534 """ |
3028 | 1535 if file_data['owner'] != peer_jid: |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1536 raise exceptions.PermissionError( |
3028 | 1537 "file {file_name} can't be deleted, {peer_jid} is not the owner" |
1538 .format(file_name=file_data['name'], peer_jid=peer_jid.full())) | |
1539 if file_data['type'] == C.FILE_TYPE_DIRECTORY: | |
1540 sub_files = yield self.getFiles(client, peer_jid, parent=file_data['id']) | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1541 if sub_files and not recursive: |
3028 | 1542 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
|
1543 # we first delete the sub-files |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1544 for sub_file_data in sub_files: |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1545 yield self._deleteFile(client, peer_jid, recursive, sub_file_data) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1546 # then the directory itself |
3028 | 1547 yield self.storage.fileDelete(file_data['id']) |
1548 elif file_data['type'] == C.FILE_TYPE_FILE: | |
1549 log.info(_("deleting file {name} with hash {file_hash}").format( | |
1550 name=file_data['name'], file_hash=file_data['file_hash'])) | |
1551 yield self.storage.fileDelete(file_data['id']) | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1552 references = yield self.getFiles( |
3028 | 1553 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
|
1554 if references: |
3028 | 1555 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
|
1556 else: |
3028 | 1557 file_path = os.path.join(files_path, file_data['file_hash']) |
1558 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
|
1559 file_path=file_path)) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1560 os.unlink(file_path) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1561 else: |
3028 | 1562 raise exceptions.InternalError('Unexpected file type: {file_type}' |
1563 .format(file_type=file_data['type'])) | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1564 |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1565 @defer.inlineCallbacks |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1566 def fileDelete(self, client, peer_jid, file_id, recursive=False): |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1567 """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
|
1568 |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1569 @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
|
1570 @param peer_jid(jid.JID): entity requesting the deletion, |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1571 must be owner of all files to delete |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1572 @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
|
1573 """ |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1574 # 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
|
1575 # should be checked too |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1576 files_data = yield self.getFiles(client, peer_jid, file_id) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1577 if not files_data: |
3028 | 1578 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
|
1579 file_id=file_id)) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1580 file_data = files_data[0] |
3028 | 1581 if file_data["type"] != C.FILE_TYPE_DIRECTORY and recursive: |
1582 raise ValueError("recursive can only be set for directories") | |
2928
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1583 files_path = self.host.getLocalPath(None, C.FILES_DIR, profile=False) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1584 yield self._deleteFile(client, peer_jid, recursive, files_path, file_data) |
c0f6fd75af5f
core (memory, memory/sqlite): implemented fileDelete
Goffi <goffi@goffi.org>
parents:
2926
diff
changeset
|
1585 |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1586 ## Misc ## |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1587 |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
1588 def isEntityAvailable(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
|
1589 """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
|
1590 |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1591 @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
|
1592 @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
|
1593 """ |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1594 if not entity_jid.resource: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1595 return bool( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1596 self.getAvailableResources(client, entity_jid) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2597
diff
changeset
|
1597 ) # 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
|
1598 try: |
2533
8d82a62fa098
core (disco), plugin XEP-0115: client use + capabilities hash improvment:
Goffi <goffi@goffi.org>
parents:
2526
diff
changeset
|
1599 presence_data = self.getEntityDatum(entity_jid, "presence", client.profile) |
1290
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1600 except KeyError: |
3028 | 1601 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
|
1602 return False |
faa1129559b8
core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents:
1263
diff
changeset
|
1603 return presence_data.show != C.PRESENCE_UNAVAILABLE |