Mercurial > libervia-backend
annotate libervia/backend/plugins/plugin_xep_0329.py @ 4305:4cd4922de876
tests: reformat tests using black.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:11:56 +0200 |
parents | 0d7bb4df2343 |
children |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # SAT plugin for File Information Sharing (XEP-0329) |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
19 import mimetypes |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
20 import json |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
21 import os |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3516
diff
changeset
|
22 import traceback |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
23 from pathlib import Path |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
24 from typing import Optional, Dict |
3028 | 25 from zope.interface import implementer |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber import xmlstream |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.jabber import jid |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.words.protocols.jabber import error as jabber_error |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from twisted.internet import defer |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
30 from wokkel import disco, iwokkel, data_form |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
31 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
32 from libervia.backend.core.xmpp import SatXMPPEntity |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
33 from libervia.backend.core import exceptions |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.core.constants import Const as C |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.core.log import getLogger |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
36 from libervia.backend.tools import stream |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
37 from libervia.backend.tools import utils |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.tools.common import regex |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
41 log = getLogger(__name__) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
42 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 PLUGIN_INFO = { |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 C.PI_NAME: "File Information Sharing", |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 C.PI_IMPORT_NAME: "XEP-0329", |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 C.PI_TYPE: "XEP", |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 C.PI_MODES: C.PLUG_MODE_BOTH, |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 C.PI_PROTOCOLS: ["XEP-0329"], |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
49 C.PI_DEPENDENCIES: ["XEP-0231", "XEP-0234", "XEP-0300", "XEP-0106"], |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 C.PI_MAIN: "XEP_0329", |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 C.PI_HANDLER: "yes", |
3028 | 52 C.PI_DESCRIPTION: _("""Implementation of File Information Sharing"""), |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 } |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
55 NS_FIS = "urn:xmpp:fis:0" |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
56 NS_FIS_AFFILIATION = "org.salut-a-toi.fis-affiliation" |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
57 NS_FIS_CONFIGURATION = "org.salut-a-toi.fis-configuration" |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
58 NS_FIS_CREATE = "org.salut-a-toi.fis-create" |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
60 IQ_FIS_REQUEST = f'{C.IQ_GET}/query[@xmlns="{NS_FIS}"]' |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
61 # not in the standard, but needed, and it's handy to keep it here |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
62 IQ_FIS_AFFILIATION_GET = f'{C.IQ_GET}/affiliations[@xmlns="{NS_FIS_AFFILIATION}"]' |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
63 IQ_FIS_AFFILIATION_SET = f'{C.IQ_SET}/affiliations[@xmlns="{NS_FIS_AFFILIATION}"]' |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
64 IQ_FIS_CONFIGURATION_GET = f'{C.IQ_GET}/configuration[@xmlns="{NS_FIS_CONFIGURATION}"]' |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
65 IQ_FIS_CONFIGURATION_SET = f'{C.IQ_SET}/configuration[@xmlns="{NS_FIS_CONFIGURATION}"]' |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
66 IQ_FIS_CREATE_DIR = f'{C.IQ_SET}/dir[@xmlns="{NS_FIS_CREATE}"]' |
3028 | 67 SINGLE_FILES_DIR = "files" |
68 TYPE_VIRTUAL = "virtual" | |
69 TYPE_PATH = "path" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
70 SHARE_TYPES = (TYPE_PATH, TYPE_VIRTUAL) |
3028 | 71 KEY_TYPE = "type" |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
74 class RootPathException(Exception): |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
75 """Root path is requested""" |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
76 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
77 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 class ShareNode(object): |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
79 """Node containing directory or files to share, virtual or real""" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
80 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 host = None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 def __init__(self, name, parent, type_, access, path=None): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 assert type_ in SHARE_TYPES |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 if name is not None: |
3028 | 86 if name == ".." or "/" in name or "\\" in name: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
87 log.warning( |
3028 | 88 _("path change chars found in name [{name}], hack attempt?").format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
89 name=name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
90 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
91 ) |
3028 | 92 if name == "..": |
93 name = "--" | |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
95 name = regex.path_escape(name) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 self.name = name |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 self.children = {} |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 self.type = type_ |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 self.access = {} if access is None else access |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 assert isinstance(self.access, dict) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 self.parent = None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 if parent is not None: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 assert name |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 parent.addChild(self) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 else: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 assert name is None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 if path is not None: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 if type_ != TYPE_PATH: |
3028 | 109 raise exceptions.InternalError(_("path can only be set on path nodes")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 self._path = path |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 @property |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def path(self): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return self._path |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def __getitem__(self, key): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 return self.children[key] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 def __contains__(self, item): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 return self.children.__contains__(item) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 def __iter__(self): |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 return self.children.__iter__() |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
3032
95e2fd14a761
plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
125 def items(self): |
95e2fd14a761
plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
126 return self.children.items() |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
3032
95e2fd14a761
plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
128 def values(self): |
95e2fd14a761
plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
129 return self.children.values() |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
131 def get_or_create(self, name, type_=TYPE_VIRTUAL, access=None): |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
132 """Get a node or create a virtual node and return it""" |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 if access is None: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 access = {C.ACCESS_PERM_READ: {KEY_TYPE: C.ACCESS_TYPE_PUBLIC}} |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 try: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 return self.children[name] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 except KeyError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 node = ShareNode(name, self, type_=type_, access=access) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 return node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 def addChild(self, node): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 if node.parent is not None: |
3028 | 143 raise exceptions.ConflictError(_("a node can't have several parents")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 node.parent = self |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 self.children[node.name] = node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
147 def remove_from_parent(self): |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 try: |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 del self.parent.children[self.name] |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
150 except TypeError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
151 raise exceptions.InternalError( |
3028 | 152 "trying to remove a node from inexisting parent" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
153 ) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
154 except KeyError: |
3028 | 155 raise exceptions.InternalError("node not found in parent's children") |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
156 self.parent = None |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
157 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
158 def _check_node_permission(self, client, node, perms, peer_jid): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 """Check access to this node for peer_jid |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 @param node(SharedNode): node to check access |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 @param perms(unicode): permissions to check, iterable of C.ACCESS_PERM_* |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 @param peer_jid(jid.JID): entity which try to access the node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 @return (bool): True if entity can access |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 """ |
3028 | 166 file_data = {"access": self.access, "owner": client.jid.userhostJID()} |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
168 self.host.memory.check_file_permission(file_data, peer_jid, perms) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 except exceptions.PermissionError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 return False |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 else: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 return True |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
174 def check_permissions( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
175 self, client, peer_jid, perms=(C.ACCESS_PERM_READ,), check_parents=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
176 ): |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
177 """Check that peer_jid can access this node and all its parents |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 @param peer_jid(jid.JID): entrity trying to access the node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 @param perms(unicode): permissions to check, iterable of C.ACCESS_PERM_* |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
181 @param check_parents(bool): if True, access of all parents of this node will be |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
182 checked too |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 @return (bool): True if entity can access this node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 """ |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 peer_jid = peer_jid.userhostJID() |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 if peer_jid == client.jid.userhostJID(): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 return True |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 parent = self |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 while parent != None: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
191 if not self._check_node_permission(client, parent, perms, peer_jid): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 return False |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 parent = parent.parent |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 return True |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 @staticmethod |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 def find(client, path, peer_jid, perms=(C.ACCESS_PERM_READ,)): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 """find node corresponding to a path |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 @param path(unicode): path to the requested file or directory |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 @param peer_jid(jid.JID): entity trying to find the node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 used to check permission |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 @return (dict, unicode): shared data, remaining path |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 @raise exceptions.PermissionError: user can't access this file |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 @raise exceptions.DataError: path is invalid |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 @raise NotFound: path lead to a non existing file/directory |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 """ |
3028 | 209 path_elts = [_f for _f in path.split("/") if _f] |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 |
3028 | 211 if ".." in path_elts: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
212 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
213 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
214 'parent dir ("..") found in path, hack attempt? path is {path} ' |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
215 "[{profile}]" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
216 ).format(path=path, profile=client.profile) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
217 ) |
3028 | 218 raise exceptions.PermissionError("illegal path elements") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 node = client._XEP_0329_root_node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 while path_elts: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 if node.type == TYPE_VIRTUAL: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 try: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 node = node[path_elts.pop(0)] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 except KeyError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 raise exceptions.NotFound |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 elif node.type == TYPE_PATH: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 break |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
231 if not node.check_permissions(client, peer_jid, perms=perms): |
3028 | 232 raise exceptions.PermissionError("permission denied") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 |
3028 | 234 return node, "/".join(path_elts) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
236 def find_by_local_path(self, path): |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
237 """retrieve nodes linking to local path |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
238 |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
239 @return (list[ShareNode]): found nodes associated to path |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
240 @raise exceptions.NotFound: no node has been found with this path |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
241 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
242 shared_paths = self.get_shared_paths() |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
243 try: |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 return shared_paths[path] |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 except KeyError: |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
246 raise exceptions.NotFound |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
247 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
248 def _get_shared_paths(self, node, paths): |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 if node.type == TYPE_VIRTUAL: |
3028 | 250 for node in node.values(): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
251 self._get_shared_paths(node, paths) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
252 elif node.type == TYPE_PATH: |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
253 paths.setdefault(node.path, []).append(node) |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
254 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
255 raise exceptions.InternalError( |
3028 | 256 "unknown node type: {type}".format(type=node.type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
257 ) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
259 def get_shared_paths(self): |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
260 """retrieve nodes by shared path |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
261 |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
262 this method will retrieve recursively shared path in children of this node |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
263 @return (dict): map from shared path to list of nodes |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 """ |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 if self.type == TYPE_PATH: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
266 raise exceptions.InternalError( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
267 "get_shared_paths must be used on a virtual node" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
268 ) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
269 paths = {} |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
270 self._get_shared_paths(self, paths) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
271 return paths |
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
272 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 class XEP_0329(object): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 def __init__(self, host): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 log.info(_("File Information Sharing initialization")) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 self.host = host |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 ShareNode.host = host |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
279 self._b = host.plugins["XEP-0231"] |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
280 self._h = host.plugins["XEP-0300"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
281 self._jf = host.plugins["XEP-0234"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
282 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
283 "fis_list", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
284 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
285 in_sign="ssa{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
286 out_sign="aa{ss}", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
287 method=self._list_files, |
3028 | 288 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
289 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
290 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
291 "fis_local_shares_get", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
292 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
293 in_sign="s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
294 out_sign="as", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
295 method=self._local_shares_get, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
296 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
297 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
298 "fis_share_path", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
299 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
300 in_sign="ssss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
301 out_sign="s", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
302 method=self._share_path, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
303 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
304 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
305 "fis_unshare_path", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
306 ".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
307 in_sign="ss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
308 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
309 method=self._unshare_path, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
310 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
311 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
312 "fis_affiliations_get", |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
313 ".plugin", |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
314 in_sign="ssss", |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
315 out_sign="a{ss}", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
316 method=self._affiliations_get, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
317 async_=True, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
318 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
319 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
320 "fis_affiliations_set", |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
321 ".plugin", |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
322 in_sign="sssa{ss}s", |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
323 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
324 method=self._affiliations_set, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
325 async_=True, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
326 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
327 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
328 "fis_configuration_get", |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
329 ".plugin", |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
330 in_sign="ssss", |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
331 out_sign="a{ss}", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
332 method=self._configuration_get, |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
333 async_=True, |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
334 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
335 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
336 "fis_configuration_set", |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
337 ".plugin", |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
338 in_sign="sssa{ss}s", |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
339 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
340 method=self._configuration_set, |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
341 async_=True, |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
342 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
343 host.bridge.add_method( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
344 "fis_create_dir", |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
345 ".plugin", |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
346 in_sign="sssa{ss}s", |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
347 out_sign="", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
348 method=self._create_dir, |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
349 async_=True, |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
350 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
351 host.bridge.add_signal("fis_shared_path_new", ".plugin", signature="sss") |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
352 host.bridge.add_signal("fis_shared_path_removed", ".plugin", signature="ss") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
353 host.trigger.add( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
354 "XEP-0234_fileSendingRequest", self._file_sending_request_trigger |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
355 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
356 host.register_namespace("fis", NS_FIS) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
358 def get_handler(self, client): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 return XEP_0329_handler(self) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
361 def profile_connected(self, client): |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
362 if client.is_component: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
363 client._file_sharing_allowed_hosts = self.host.memory.config_get( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
364 "component file_sharing", "http_upload_allowed_hosts_list" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
365 ) or [client.host] |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
366 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
367 client._XEP_0329_root_node = ShareNode( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
368 None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
369 None, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
370 TYPE_VIRTUAL, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
371 {C.ACCESS_PERM_READ: {KEY_TYPE: C.ACCESS_TYPE_PUBLIC}}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
372 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
373 client._XEP_0329_names_data = {} # name to share map |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
375 def _file_sending_request_trigger( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
376 self, client, session, content_data, content_name, file_data, file_elt |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
377 ): |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
378 """This trigger check that a requested file is available, and fill suitable data |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
380 Path and name are used to retrieve the file. If path is missing, we try our luck |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
381 with known names |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 """ |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 if client.is_component: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 try: |
3028 | 387 name = file_data["name"] |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 except KeyError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
389 return True, None |
3028 | 390 assert "/" not in name |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 |
3028 | 392 path = file_data.get("path") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 if path is not None: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 # we have a path, we can follow it to find node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 try: |
3028 | 396 node, rem_path = ShareNode.find(client, path, session["peer_jid"]) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 except (exceptions.PermissionError, exceptions.NotFound): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
398 # no file, or file not allowed, we continue normal workflow |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 except exceptions.DataError: |
3028 | 401 log.warning(_("invalid path: {path}").format(path=path)) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 if node.type == TYPE_VIRTUAL: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 # we have a virtual node, so name must link to a path node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 try: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 path = node[name].path |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 except KeyError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 elif node.type == TYPE_PATH: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 # we have a path node, so we can retrieve the full path now |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 path = os.path.join(node.path, rem_path, name) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
414 raise exceptions.InternalError( |
3028 | 415 "unknown type: {type}".format(type=node.type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
416 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
417 if not os.path.exists(path): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
418 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
419 size = os.path.getsize(path) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
420 else: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 # we don't have the path, we try to find the file by its name |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
422 try: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 name_data = client._XEP_0329_names_data[name] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 except KeyError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 return True, None |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 |
3028 | 427 for path, shared_file in name_data.items(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
428 if True: # FIXME: filters are here |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
429 break |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 else: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
431 return True, None |
3028 | 432 parent_node = shared_file["parent"] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
433 if not parent_node.check_permissions(client, session["peer_jid"]): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
434 log.warning( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
435 _( |
3028 | 436 "{peer_jid} requested a file (s)he can't access [{profile}]" |
437 ).format(peer_jid=session["peer_jid"], profile=client.profile) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
438 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 return True, None |
3028 | 440 size = shared_file["size"] |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 |
3028 | 442 file_data["size"] = size |
443 file_elt.addElement("size", content=str(size)) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
444 hash_algo = file_data["hash_algo"] = self._h.get_default_algo() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
445 hasher = file_data["hash_hasher"] = self._h.get_hasher(hash_algo) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
446 file_elt.addChild(self._h.build_hash_used_elt(hash_algo)) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
447 content_data["stream_object"] = stream.FileStreamObject( |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
448 self.host, |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 client, |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
450 path, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
451 uid=self._jf.get_progress_id(session, content_name), |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 size=size, |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 data_cb=lambda data: hasher.update(data), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
454 ) |
3516
e47aa1fb7b24
plugin XEP-0329: fix root node retrieval and `_fileSendingRequestTrigger` return value
Goffi <goffi@goffi.org>
parents:
3500
diff
changeset
|
455 return False, defer.succeed(True) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
456 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
457 # common methods |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
458 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
459 def _request_handler(self, client, iq_elt, root_nodes_cb, files_from_node_cb): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
460 iq_elt.handled = True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
461 node = iq_elt.query.getAttribute("node") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 if not node: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
463 d = utils.as_deferred(root_nodes_cb, client, iq_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
465 d = utils.as_deferred(files_from_node_cb, client, iq_elt, node) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
466 d.addErrback( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
467 lambda failure_: log.error( |
3028 | 468 _("error while retrieving files: {msg}").format(msg=failure_) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
469 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
470 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
472 def _iq_error(self, client, iq_elt, condition="item-not-found"): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 error_elt = jabber_error.StanzaError(condition).toResponse(iq_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 client.send(error_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
476 # client |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
478 def _add_path_data(self, client, query_elt, path, parent_node): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
479 """Fill query_elt with files/directories found in path""" |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
480 name = os.path.basename(path) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 if os.path.isfile(path): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
482 size = os.path.getsize(path) |
2512
025afb04c10b
plugin XEP-0234: some cleaning + added triggers to allow plugins to change parsing/generation of <file> element
Goffi <goffi@goffi.org>
parents:
2503
diff
changeset
|
483 mime_type = mimetypes.guess_type(path, strict=False)[0] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
484 file_elt = self._jf.build_file_element( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
485 client=client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
486 name=name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
487 size=size, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
488 mime_type=mime_type, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
489 modified=os.path.getmtime(path), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
490 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 query_elt.addChild(file_elt) |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
493 # we don't specify hash as it would be too resource intensive to calculate |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
494 # it for all files. |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 # we add file to name_data, so users can request it later |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 name_data = client._XEP_0329_names_data.setdefault(name, {}) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 if path not in name_data: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
498 name_data[path] = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
499 "size": size, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
500 "mime_type": mime_type, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
501 "parent": parent_node, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
502 } |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
503 else: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
504 # we have a directory |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
505 directory_elt = query_elt.addElement("directory") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
506 directory_elt["name"] = name |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
508 def _path_node_handler(self, client, iq_elt, query_elt, node, path): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
509 """Fill query_elt for path nodes, i.e. physical directories""" |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
510 path = os.path.join(node.path, path) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
511 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 if not os.path.exists(path): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 # path may have been moved since it has been shared |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
514 return self._iq_error(client, iq_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
515 elif os.path.isfile(path): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
516 self._add_path_data(client, query_elt, path, node) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
517 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
518 for name in sorted(os.listdir(path.encode("utf-8")), key=lambda n: n.lower()): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
519 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
520 name = name.decode("utf-8", "strict") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
521 except UnicodeDecodeError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
522 log.warning( |
3028 | 523 _("ignoring invalid unicode name ({name}): {msg}").format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
524 name=name.decode("utf-8", "replace"), msg=e |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
525 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
526 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
527 continue |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
528 full_path = os.path.join(path, name) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
529 self._add_path_data(client, query_elt, full_path, node) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
531 def _virtual_node_handler(self, client, peer_jid, iq_elt, query_elt, node): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 """Fill query_elt for virtual nodes""" |
3028 | 533 for name, child_node in node.items(): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
534 if not child_node.check_permissions(client, peer_jid, check_parents=False): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
535 continue |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 node_type = child_node.type |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 if node_type == TYPE_VIRTUAL: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
538 directory_elt = query_elt.addElement("directory") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
539 directory_elt["name"] = name |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
540 elif node_type == TYPE_PATH: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
541 self._add_path_data(client, query_elt, child_node.path, child_node) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
542 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
543 raise exceptions.InternalError( |
3028 | 544 _("unexpected type: {type}").format(type=node_type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
545 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
546 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
547 def _get_root_nodes_cb(self, client, iq_elt): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
548 peer_jid = jid.JID(iq_elt["from"]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
549 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
550 query_elt = iq_result_elt.addElement((NS_FIS, "query")) |
3028 | 551 for name, node in client._XEP_0329_root_node.items(): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
552 if not node.check_permissions(client, peer_jid, check_parents=False): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 continue |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
554 directory_elt = query_elt.addElement("directory") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
555 directory_elt["name"] = name |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 client.send(iq_result_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
558 def _get_files_from_node_cb(self, client, iq_elt, node_path): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 """Main method to retrieve files/directories from a node_path""" |
3028 | 560 peer_jid = jid.JID(iq_elt["from"]) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 try: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 node, path = ShareNode.find(client, node_path, peer_jid) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 except (exceptions.PermissionError, exceptions.NotFound): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
564 return self._iq_error(client, iq_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 except exceptions.DataError: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
566 return self._iq_error(client, iq_elt, condition="not-acceptable") |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
567 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 node_type = node.type |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
569 peer_jid = jid.JID(iq_elt["from"]) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
570 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
571 query_elt = iq_result_elt.addElement((NS_FIS, "query")) |
3028 | 572 query_elt["node"] = node_path |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 # we now fill query_elt according to node_type |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 if node_type == TYPE_PATH: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
576 # it's a physical path |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
577 self._path_node_handler(client, iq_elt, query_elt, node, path) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 elif node_type == TYPE_VIRTUAL: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
579 assert not path |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
580 self._virtual_node_handler(client, peer_jid, iq_elt, query_elt, node) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
581 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
582 raise exceptions.InternalError( |
3028 | 583 _("unknown node type: {type}").format(type=node_type) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
584 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
585 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
586 client.send(iq_result_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
587 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
588 def on_request(self, iq_elt, client): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
589 return self._request_handler( |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
590 client, iq_elt, self._get_root_nodes_cb, self._get_files_from_node_cb |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
591 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
592 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
593 # Component |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
594 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
595 def _comp_parse_jids(self, client, iq_elt): |
2923
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
596 """Retrieve peer_jid and owner to use from IQ stanza |
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
597 |
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
598 @param iq_elt(domish.Element): IQ stanza of the FIS request |
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
599 @return (tuple[jid.JID, jid.JID]): peer_jid and owner |
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
600 """ |
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
601 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
602 async def _comp_get_root_nodes_cb(self, client, iq_elt): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
603 peer_jid, owner = client.get_owner_and_peer(iq_elt) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
604 files_data = await self.host.memory.get_files( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
605 client, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
606 peer_jid=peer_jid, |
3028 | 607 parent="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
608 type_=C.FILE_TYPE_DIRECTORY, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
609 owner=owner, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
610 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
611 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
612 query_elt = iq_result_elt.addElement((NS_FIS, "query")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 for file_data in files_data: |
3028 | 614 name = file_data["name"] |
615 directory_elt = query_elt.addElement("directory") | |
616 directory_elt["name"] = name | |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
617 client.send(iq_result_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
618 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
619 async def _comp_get_files_from_node_cb(self, client, iq_elt, node_path): |
2923
1fd3ecb3351a
plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents:
2909
diff
changeset
|
620 """Retrieve files from local files repository according to permissions |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
621 |
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
622 result stanza is then built and sent to requestor |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
623 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
624 files_data): |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
625 can be used to add data/elements |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
626 """ |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
627 peer_jid, owner = client.get_owner_and_peer(iq_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
628 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
629 files_data = await self.host.memory.get_files( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
630 client, peer_jid=peer_jid, path=node_path, owner=owner |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
631 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
632 except exceptions.NotFound: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
633 self._iq_error(client, iq_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
634 return |
2937
db0890c9c7db
plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents:
2923
diff
changeset
|
635 except exceptions.PermissionError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
636 self._iq_error(client, iq_elt, condition="not-allowed") |
2937
db0890c9c7db
plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents:
2923
diff
changeset
|
637 return |
db0890c9c7db
plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents:
2923
diff
changeset
|
638 except Exception as e: |
3541
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3516
diff
changeset
|
639 tb = traceback.format_tb(e.__traceback__) |
888109774673
core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
3516
diff
changeset
|
640 log.error(f"internal server error: {e}\n{''.join(tb)}") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
641 self._iq_error(client, iq_elt, condition="internal-server-error") |
2937
db0890c9c7db
plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents:
2923
diff
changeset
|
642 return |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
643 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
644 query_elt = iq_result_elt.addElement((NS_FIS, "query")) |
3028 | 645 query_elt["node"] = node_path |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
646 if not self.host.trigger.point( |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
647 "XEP-0329_compGetFilesFromNode", |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
648 client, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
649 iq_elt, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
650 iq_result_elt, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
651 owner, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
652 node_path, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
653 files_data, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
654 ): |
2528
65e278997715
component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents:
2527
diff
changeset
|
655 return |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
656 for file_data in files_data: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
657 if file_data["type"] == C.FILE_TYPE_DIRECTORY: |
3040 | 658 directory_elt = query_elt.addElement("directory") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
659 directory_elt["name"] = file_data["name"] |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
660 self.host.trigger.point( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
661 "XEP-0329_compGetFilesFromNode_build_directory", |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
662 client, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
663 file_data, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
664 directory_elt, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
665 owner, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
666 node_path, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
667 ) |
3040 | 668 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
669 file_elt = self._jf.build_file_element_from_dict( |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
670 client, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
671 file_data, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
672 modified=file_data.get("modified", file_data["created"]), |
3040 | 673 ) |
674 query_elt.addChild(file_elt) | |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
675 client.send(iq_result_elt) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
676 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
677 def on_component_request(self, iq_elt, client): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
678 return self._request_handler( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
679 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
680 iq_elt, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
681 self._comp_get_root_nodes_cb, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
682 self._comp_get_files_from_node_cb, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
683 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
684 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
685 async def _parse_result(self, client, peer_jid, iq_elt): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
686 query_elt = next(iq_elt.elements(NS_FIS, "query")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
687 files = [] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
688 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
689 for elt in query_elt.elements(): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
690 if elt.name == "file": |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
691 # we have a file |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
692 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
693 file_data = await self._jf.parse_file_element(client, elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
694 except exceptions.DataError: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
695 continue |
3028 | 696 file_data["type"] = C.FILE_TYPE_FILE |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
697 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
698 thumbs = file_data["extra"][C.KEY_THUMBNAILS] |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
699 except KeyError: |
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
700 log.debug(f"No thumbnail found for {file_data}") |
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
701 else: |
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
702 for thumb in thumbs: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
703 if "url" not in thumb and "id" in thumb: |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
704 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
705 file_path = await self._b.get_file( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
706 client, peer_jid, thumb["id"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
707 ) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
708 except Exception as e: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
709 log.warning( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
710 f"Can't get thumbnail {thumb['id']!r} for {file_data}: {e}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
711 ) |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
712 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
713 thumb["filename"] = file_path.name |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
714 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
715 elif elt.name == "directory" and elt.uri == NS_FIS: |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
716 # we have a directory |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
717 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
718 file_data = {"name": elt["name"], "type": C.FILE_TYPE_DIRECTORY} |
3314
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
719 self.host.trigger.point( |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
720 "XEP-0329_parseResult_directory", |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
721 client, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
722 elt, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
723 file_data, |
5887fb414758
component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
724 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
725 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
726 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
727 _("unexpected element, ignoring: {elt}").format(elt=elt.toXml()) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
728 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
729 continue |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
730 files.append(file_data) |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
731 return files |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
732 |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
733 # affiliations # |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
734 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
735 async def _parse_element(self, client, iq_elt, element, namespace): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
736 peer_jid, owner = client.get_owner_and_peer(iq_elt) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
737 elt = next(iq_elt.elements(namespace, element)) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
738 path = Path("/", elt["path"]) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
739 if len(path.parts) < 2: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
740 raise RootPathException |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
741 namespace = elt.getAttribute("namespace") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
742 files_data = await self.host.memory.get_files( |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
743 client, |
3365
626046010a2d
plugin XEP-0329: fix filtering by owner on components
Goffi <goffi@goffi.org>
parents:
3362
diff
changeset
|
744 peer_jid=peer_jid, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
745 path=str(path.parent), |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
746 name=path.name, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
747 namespace=namespace, |
3365
626046010a2d
plugin XEP-0329: fix filtering by owner on components
Goffi <goffi@goffi.org>
parents:
3362
diff
changeset
|
748 owner=owner, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
749 ) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
750 if len(files_data) != 1: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
751 client.sendError(iq_elt, "item-not-found") |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
752 raise exceptions.CancelError |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
753 file_data = files_data[0] |
3365
626046010a2d
plugin XEP-0329: fix filtering by owner on components
Goffi <goffi@goffi.org>
parents:
3362
diff
changeset
|
754 return peer_jid, elt, path, namespace, file_data |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
755 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
756 def _affiliations_get(self, service_jid_s, namespace, path, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
757 client = self.host.get_client(profile) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
758 service = jid.JID(service_jid_s) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
759 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
760 self.affiliationsGet(client, service, namespace or None, path) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
761 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
762 d.addCallback( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
763 lambda affiliations: { |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
764 str(entity): affiliation for entity, affiliation in affiliations.items() |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
765 } |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
766 ) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
767 return d |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
768 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
769 async def affiliationsGet( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
770 self, client: SatXMPPEntity, service: jid.JID, namespace: Optional[str], path: str |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
771 ) -> Dict[jid.JID, str]: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
772 if not path: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
773 raise ValueError(f"invalid path: {path!r}") |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
774 iq_elt = client.IQ("get") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
775 iq_elt["to"] = service.full() |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
776 affiliations_elt = iq_elt.addElement((NS_FIS_AFFILIATION, "affiliations")) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
777 if namespace: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
778 affiliations_elt["namespace"] = namespace |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
779 affiliations_elt["path"] = path |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
780 iq_result_elt = await iq_elt.send() |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
781 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
782 affiliations_elt = next( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
783 iq_result_elt.elements(NS_FIS_AFFILIATION, "affiliations") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
784 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
785 except StopIteration: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
786 raise exceptions.DataError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
787 f"Invalid result to affiliations request: {iq_result_elt.toXml()}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
788 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
789 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
790 affiliations = {} |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
791 for affiliation_elt in affiliations_elt.elements( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
792 NS_FIS_AFFILIATION, "affiliation" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
793 ): |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
794 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
795 affiliations[jid.JID(affiliation_elt["jid"])] = affiliation_elt[ |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
796 "affiliation" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
797 ] |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
798 except (KeyError, RuntimeError): |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
799 raise exceptions.DataError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
800 f"invalid affiliation element: {affiliation_elt.toXml()}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
801 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
802 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
803 return affiliations |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
804 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
805 def _affiliations_set(self, service_jid_s, namespace, path, affiliations, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
806 client = self.host.get_client(profile) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
807 service = jid.JID(service_jid_s) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
808 affiliations = {jid.JID(e): a for e, a in affiliations.items()} |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
809 return defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
810 self.affiliationsSet(client, service, namespace or None, path, affiliations) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
811 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
812 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
813 async def affiliationsSet( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
814 self, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
815 client: SatXMPPEntity, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
816 service: jid.JID, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
817 namespace: Optional[str], |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
818 path: str, |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
819 affiliations: Dict[jid.JID, str], |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
820 ): |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
821 if not path: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
822 raise ValueError(f"invalid path: {path!r}") |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
823 iq_elt = client.IQ("set") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
824 iq_elt["to"] = service.full() |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
825 affiliations_elt = iq_elt.addElement((NS_FIS_AFFILIATION, "affiliations")) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
826 if namespace: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
827 affiliations_elt["namespace"] = namespace |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
828 affiliations_elt["path"] = path |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
829 for entity_jid, affiliation in affiliations.items(): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
830 affiliation_elt = affiliations_elt.addElement("affiliation") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
831 affiliation_elt["jid"] = entity_jid.full() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
832 affiliation_elt["affiliation"] = affiliation |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
833 await iq_elt.send() |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
834 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
835 def _on_component_affiliations_get(self, iq_elt, client): |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
836 iq_elt.handled = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
837 defer.ensureDeferred(self.on_component_affiliations_get(client, iq_elt)) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
838 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
839 async def on_component_affiliations_get(self, client, iq_elt): |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
840 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
841 (from_jid, affiliations_elt, path, namespace, file_data) = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
842 await self._parse_element( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
843 client, iq_elt, "affiliations", NS_FIS_AFFILIATION |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
844 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
845 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
846 except exceptions.CancelError: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
847 return |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
848 except RootPathException: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
849 # if root path is requested, we only get owner affiliation |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
850 peer_jid, owner = client.get_owner_and_peer(iq_elt) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
851 is_owner = peer_jid.userhostJID() == owner |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
852 affiliations = {owner: "owner"} |
3355
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
853 except exceptions.NotFound: |
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
854 client.sendError(iq_elt, "item-not-found") |
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
855 return |
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
856 except Exception as e: |
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
857 client.sendError(iq_elt, "internal-server-error", str(e)) |
33d9b38b5890
plugin XEP-0329: better error handling in `onComponentAffiliationsGet`
Goffi <goffi@goffi.org>
parents:
3333
diff
changeset
|
858 return |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
859 else: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
860 from_jid_bare = from_jid.userhostJID() |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
861 is_owner = from_jid_bare == file_data.get("owner") |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
862 affiliations = self.host.memory.get_file_affiliations(file_data) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
863 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
864 affiliations_elt = iq_result_elt.addElement((NS_FIS_AFFILIATION, "affiliations")) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
865 for entity_jid, affiliation in affiliations.items(): |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
866 if not is_owner and entity_jid.userhostJID() != from_jid_bare: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
867 # only onwer can get all affiliations |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
868 continue |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
869 affiliation_elt = affiliations_elt.addElement("affiliation") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
870 affiliation_elt["jid"] = entity_jid.userhost() |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
871 affiliation_elt["affiliation"] = affiliation |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
872 client.send(iq_result_elt) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
873 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
874 def _on_component_affiliations_set(self, iq_elt, client): |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
875 iq_elt.handled = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
876 defer.ensureDeferred(self.on_component_affiliations_set(client, iq_elt)) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
877 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
878 async def on_component_affiliations_set(self, client, iq_elt): |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
879 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
880 (from_jid, affiliations_elt, path, namespace, file_data) = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
881 await self._parse_element( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
882 client, iq_elt, "affiliations", NS_FIS_AFFILIATION |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
883 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
884 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
885 except exceptions.CancelError: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
886 return |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
887 except RootPathException: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
888 client.sendError(iq_elt, "bad-request", "Root path can't be used") |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
889 return |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
890 |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
891 if from_jid.userhostJID() != file_data["owner"]: |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
892 log.warning( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
893 f"{from_jid} tried to modify {path} affiliations while the owner is " |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
894 f"{file_data['owner']}" |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
895 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
896 client.sendError(iq_elt, "forbidden") |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
897 return |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
898 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
899 try: |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
900 affiliations = { |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
901 jid.JID(e["jid"]): e["affiliation"] |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
902 for e in affiliations_elt.elements(NS_FIS_AFFILIATION, "affiliation") |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
903 } |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
904 except (KeyError, RuntimeError): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
905 log.warning(f"invalid affiliation element: {affiliations_elt.toXml()}") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
906 client.sendError(iq_elt, "bad-request", "invalid affiliation element") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
907 return |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
908 except Exception as e: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
909 log.error( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
910 f"unexepected exception while setting affiliation element: {e}\n" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
911 f"{affiliations_elt.toXml()}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
912 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
913 client.sendError(iq_elt, "internal-server-error", f"{e}") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
914 return |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
915 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
916 await self.host.memory.set_file_affiliations(client, file_data, affiliations) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
917 |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
918 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
919 client.send(iq_result_elt) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
920 |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
921 # configuration |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
922 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
923 def _configuration_get(self, service_jid_s, namespace, path, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
924 client = self.host.get_client(profile) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
925 service = jid.JID(service_jid_s) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
926 d = defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
927 self.configuration_get(client, service, namespace or None, path) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
928 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
929 d.addCallback( |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
930 lambda configuration: { |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
931 str(entity): affiliation for entity, affiliation in configuration.items() |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
932 } |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
933 ) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
934 return d |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
935 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
936 async def configuration_get( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
937 self, client: SatXMPPEntity, service: jid.JID, namespace: Optional[str], path: str |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
938 ) -> Dict[str, str]: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
939 if not path: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
940 raise ValueError(f"invalid path: {path!r}") |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
941 iq_elt = client.IQ("get") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
942 iq_elt["to"] = service.full() |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
943 configuration_elt = iq_elt.addElement((NS_FIS_CONFIGURATION, "configuration")) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
944 if namespace: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
945 configuration_elt["namespace"] = namespace |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
946 configuration_elt["path"] = path |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
947 iq_result_elt = await iq_elt.send() |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
948 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
949 configuration_elt = next( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
950 iq_result_elt.elements(NS_FIS_CONFIGURATION, "configuration") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
951 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
952 except StopIteration: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
953 raise exceptions.DataError( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
954 f"Invalid result to configuration request: {iq_result_elt.toXml()}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
955 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
956 |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
957 form = data_form.findForm(configuration_elt, NS_FIS_CONFIGURATION) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
958 configuration = {f.var: f.value for f in form.fields.values()} |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
959 |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
960 return configuration |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
961 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
962 def _configuration_set(self, service_jid_s, namespace, path, configuration, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
963 client = self.host.get_client(profile) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
964 service = jid.JID(service_jid_s) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
965 return defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
966 self.configuration_set( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
967 client, service, namespace or None, path, configuration |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
968 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
969 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
970 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
971 async def configuration_set( |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
972 self, |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
973 client: SatXMPPEntity, |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
974 service: jid.JID, |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
975 namespace: Optional[str], |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
976 path: str, |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
977 configuration: Dict[str, str], |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
978 ): |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
979 if not path: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
980 raise ValueError(f"invalid path: {path!r}") |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
981 iq_elt = client.IQ("set") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
982 iq_elt["to"] = service.full() |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
983 configuration_elt = iq_elt.addElement((NS_FIS_CONFIGURATION, "configuration")) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
984 if namespace: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
985 configuration_elt["namespace"] = namespace |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
986 configuration_elt["path"] = path |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
987 form = data_form.Form(formType="submit", formNamespace=NS_FIS_CONFIGURATION) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
988 form.makeFields(configuration) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
989 configuration_elt.addChild(form.toElement()) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
990 await iq_elt.send() |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
991 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
992 def _on_component_configuration_get(self, iq_elt, client): |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
993 iq_elt.handled = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
994 defer.ensureDeferred(self.on_component_configuration_get(client, iq_elt)) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
995 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
996 async def on_component_configuration_get(self, client, iq_elt): |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
997 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
998 (from_jid, configuration_elt, path, namespace, file_data) = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
999 await self._parse_element( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1000 client, iq_elt, "configuration", NS_FIS_CONFIGURATION |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1001 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1002 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1003 except exceptions.CancelError: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1004 return |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1005 except RootPathException: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1006 client.sendError(iq_elt, "bad-request", "Root path can't be used") |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1007 return |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1008 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1009 access_type = file_data["access"][C.ACCESS_PERM_READ]["type"] |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1010 except KeyError: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1011 access_model = "whitelist" |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1012 else: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1013 access_model = "open" if access_type == C.ACCESS_TYPE_PUBLIC else "whitelist" |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1014 |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1015 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1016 configuration_elt = iq_result_elt.addElement( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1017 (NS_FIS_CONFIGURATION, "configuration") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1018 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1019 form = data_form.Form(formType="form", formNamespace=NS_FIS_CONFIGURATION) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1020 form.makeFields({"access_model": access_model}) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1021 configuration_elt.addChild(form.toElement()) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1022 client.send(iq_result_elt) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1023 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1024 async def _set_configuration(self, client, configuration_elt, file_data): |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1025 form = data_form.findForm(configuration_elt, NS_FIS_CONFIGURATION) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1026 for name, value in form.items(): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1027 if name == "access_model": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1028 await self.host.memory.set_file_access_model(client, file_data, value) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1029 else: |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1030 # TODO: send a IQ error? |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1031 log.warning( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1032 f"Trying to set a not implemented configuration option: {name}" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1033 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1034 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1035 def _on_component_configuration_set(self, iq_elt, client): |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1036 iq_elt.handled = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1037 defer.ensureDeferred(self.on_component_configuration_set(client, iq_elt)) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1038 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1039 async def on_component_configuration_set(self, client, iq_elt): |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1040 try: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1041 (from_jid, configuration_elt, path, namespace, file_data) = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1042 await self._parse_element( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1043 client, iq_elt, "configuration", NS_FIS_CONFIGURATION |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1044 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1045 ) |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1046 except exceptions.CancelError: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1047 return |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1048 except RootPathException: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1049 client.sendError(iq_elt, "bad-request", "Root path can't be used") |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1050 return |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1051 |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1052 from_jid_bare = from_jid.userhostJID() |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1053 is_owner = from_jid_bare == file_data.get("owner") |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1054 if not is_owner: |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1055 log.warning( |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1056 f"{from_jid} tried to modify {path} configuration while the owner is " |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1057 f"{file_data['owner']}" |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1058 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1059 client.sendError(iq_elt, "forbidden") |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1060 return |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1061 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1062 await self._set_configuration(client, configuration_elt, file_data) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1063 |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1064 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1065 client.send(iq_result_elt) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1066 |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1067 # directory creation |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1068 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1069 def _create_dir(self, service_jid_s, namespace, path, configuration, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1070 client = self.host.get_client(profile) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1071 service = jid.JID(service_jid_s) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1072 return defer.ensureDeferred( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1073 self.create_dir( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1074 client, service, namespace or None, path, configuration or None |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1075 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1076 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1077 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1078 async def create_dir( |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1079 self, |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1080 client: SatXMPPEntity, |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1081 service: jid.JID, |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1082 namespace: Optional[str], |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1083 path: str, |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1084 configuration: Optional[Dict[str, str]], |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1085 ): |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1086 if not path: |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1087 raise ValueError(f"invalid path: {path!r}") |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1088 iq_elt = client.IQ("set") |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1089 iq_elt["to"] = service.full() |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1090 create_dir_elt = iq_elt.addElement((NS_FIS_CREATE, "dir")) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1091 if namespace: |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1092 create_dir_elt["namespace"] = namespace |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1093 create_dir_elt["path"] = path |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1094 if configuration: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1095 configuration_elt = create_dir_elt.addElement( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1096 (NS_FIS_CONFIGURATION, "configuration") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1097 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1098 form = data_form.Form(formType="submit", formNamespace=NS_FIS_CONFIGURATION) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1099 form.makeFields(configuration) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1100 configuration_elt.addChild(form.toElement()) |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1101 await iq_elt.send() |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1102 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1103 def _on_component_create_dir(self, iq_elt, client): |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1104 iq_elt.handled = True |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1105 defer.ensureDeferred(self.on_component_create_dir(client, iq_elt)) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1106 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1107 async def on_component_create_dir(self, client, iq_elt): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1108 peer_jid, owner = client.get_owner_and_peer(iq_elt) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1109 if peer_jid.host not in client._file_sharing_allowed_hosts: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1110 client.sendError(iq_elt, "forbidden") |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1111 return |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1112 create_dir_elt = next(iq_elt.elements(NS_FIS_CREATE, "dir")) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1113 namespace = create_dir_elt.getAttribute("namespace") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1114 path = Path("/", create_dir_elt["path"]) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1115 if len(path.parts) < 2: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1116 client.sendError(iq_elt, "bad-request", "Root path can't be used") |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1117 return |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1118 # for root directories, we check permission here |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1119 if len(path.parts) == 2 and owner != peer_jid.userhostJID(): |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1120 log.warning( |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1121 f"{peer_jid} is trying to create a dir at {owner}'s repository:\n" |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1122 f"path: {path}\nnamespace: {namespace!r}" |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1123 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1124 client.sendError(iq_elt, "forbidden", "You can't create a directory there") |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1125 return |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1126 # when going further into the path, the permissions will be checked by get_files |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1127 files_data = await self.host.memory.get_files( |
3500
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1128 client, |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1129 peer_jid=peer_jid, |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1130 path=path.parent, |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1131 namespace=namespace, |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1132 owner=owner, |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1133 ) |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1134 if path.name in [d["name"] for d in files_data]: |
3500
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1135 log.warning( |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1136 f"Conflict when trying to create a directory (from: {peer_jid} " |
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1137 f"namespace: {namespace!r} path: {path!r})" |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1138 ) |
3500
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1139 client.sendError( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1140 iq_elt, "conflict", "there is already a file or dir at this path" |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1141 ) |
3500
73b8a8d938be
plugin XEP-0329: check files conflict also for root files
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
1142 return |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1143 |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1144 try: |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1145 configuration_elt = next( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1146 create_dir_elt.elements(NS_FIS_CONFIGURATION, "configuration") |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1147 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1148 except StopIteration: |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1149 configuration_elt = None |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1150 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1151 await self.host.memory.set_file( |
3362
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1152 client, |
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1153 path.name, |
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1154 path=path.parent, |
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1155 type_=C.FILE_TYPE_DIRECTORY, |
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1156 namespace=namespace, |
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1157 owner=owner, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1158 peer_jid=peer_jid, |
3362
02583a401e51
plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
Goffi <goffi@goffi.org>
parents:
3360
diff
changeset
|
1159 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1160 |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1161 if configuration_elt is not None: |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1162 file_data = ( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1163 await self.host.memory.get_files( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1164 client, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1165 peer_jid=peer_jid, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1166 path=path.parent, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1167 name=path.name, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1168 namespace=namespace, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1169 owner=owner, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1170 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1171 )[0] |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1172 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1173 await self._set_configuration(client, configuration_elt, file_data) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1174 |
3321
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1175 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1176 client.send(iq_result_elt) |
8bbd2ed924e8
plugin XEP-0329: added way to change `access_model` using PubSub-like configuration:
Goffi <goffi@goffi.org>
parents:
3320
diff
changeset
|
1177 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1178 # file methods # |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1179 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1180 def _serialize_data(self, files_data): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1181 for file_data in files_data: |
3028 | 1182 for key, value in file_data.items(): |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1183 file_data[key] = json.dumps(value) if key in ("extra",) else str(value) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1184 return files_data |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1185 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1186 def _list_files(self, target_jid, path, extra, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1187 client = self.host.get_client(profile) |
3040 | 1188 target_jid = client.jid if not target_jid else jid.JID(target_jid) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1189 d = defer.ensureDeferred(self.list_files(client, target_jid, path or None)) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1190 d.addCallback(self._serialize_data) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1191 return d |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1192 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1193 async def list_files(self, client, peer_jid, path=None, extra=None): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1194 """List file shared by an entity |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1195 |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
1196 @param peer_jid(jid.JID): jid of the sharing entity |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1197 @param path(unicode, None): path to the directory containing shared files |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1198 None to get root directories |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1199 @param extra(dict, None): extra data |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1200 @return list(dict): shared files |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1201 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1202 iq_elt = client.IQ("get") |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
1203 iq_elt["to"] = peer_jid.full() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1204 query_elt = iq_elt.addElement((NS_FIS, "query")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1205 if path: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1206 query_elt["node"] = path |
3333
ac9342f359e9
plugin XEP-0329: download thumbnails:
Goffi <goffi@goffi.org>
parents:
3321
diff
changeset
|
1207 iq_result_elt = await iq_elt.send() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1208 return await self._parse_result(client, peer_jid, iq_result_elt) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1209 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1210 def _local_shares_get(self, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1211 client = self.host.get_client(profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1212 return self.local_shares_get(client) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1213 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1214 def local_shares_get(self, client): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1215 return list(client._XEP_0329_root_node.get_shared_paths().keys()) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1216 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1217 def _share_path(self, name, path, access, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1218 client = self.host.get_client(profile) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1219 access = json.loads(access) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1220 return self.share_path(client, name or None, path, access) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1221 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1222 def share_path(self, client, name, path, access): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1223 if client.is_component: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1224 raise exceptions.ClientTypeError |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1225 if not os.path.exists(path): |
3028 | 1226 raise ValueError(_("This path doesn't exist!")) |
1227 if not path or not path.strip(" /"): | |
1228 raise ValueError(_("A path need to be specified")) | |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1229 if not isinstance(access, dict): |
3028 | 1230 raise ValueError(_("access must be a dict")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1231 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1232 node = client._XEP_0329_root_node |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1233 node_type = TYPE_PATH |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1234 if os.path.isfile(path): |
2909
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1235 # we have a single file, the workflow is diferrent as we store all single |
90146552cde5
core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
1236 # files in the same dir |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1237 node = node.get_or_create(SINGLE_FILES_DIR) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1238 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1239 if not name: |
3028 | 1240 name = os.path.basename(path.rstrip(" /")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1241 if not name: |
3028 | 1242 raise exceptions.InternalError(_("Can't find a proper name")) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1243 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1244 if name in node or name == SINGLE_FILES_DIR: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1245 idx = 1 |
3028 | 1246 new_name = name + "_" + str(idx) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1247 while new_name in node: |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1248 idx += 1 |
3028 | 1249 new_name = name + "_" + str(idx) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1250 name = new_name |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1251 log.info( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1252 _( |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1253 "A directory with this name is already shared, renamed to {new_name} " |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1254 "[{profile}]".format(new_name=new_name, profile=client.profile) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1255 ) |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1256 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1257 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1258 ShareNode(name=name, parent=node, type_=node_type, access=access, path=path) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1259 self.host.bridge.fis_shared_path_new(path, name, client.profile) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1260 return name |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1261 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1262 def _unshare_path(self, path, profile): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1263 client = self.host.get_client(profile) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1264 return self.unshare_path(client, path) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1265 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1266 def unshare_path(self, client, path): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1267 nodes = client._XEP_0329_root_node.find_by_local_path(path) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1268 for node in nodes: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1269 node.remove_from_parent() |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1270 self.host.bridge.fis_shared_path_removed(path, client.profile) |
2589
282d1314d574
plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1271 |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1272 |
3028 | 1273 @implementer(iwokkel.IDisco) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1274 class XEP_0329_handler(xmlstream.XMPPHandler): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1275 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1276 def __init__(self, plugin_parent): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1277 self.plugin_parent = plugin_parent |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1278 self.host = plugin_parent.host |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1279 |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1280 def connectionInitialized(self): |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1281 if self.parent.is_component: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1282 self.xmlstream.addObserver( |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1283 IQ_FIS_REQUEST, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1284 self.plugin_parent.on_component_request, |
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1285 client=self.parent, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1286 ) |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1287 self.xmlstream.addObserver( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1288 IQ_FIS_AFFILIATION_GET, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1289 self.plugin_parent._on_component_affiliations_get, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1290 client=self.parent, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1291 ) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1292 self.xmlstream.addObserver( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1293 IQ_FIS_AFFILIATION_SET, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1294 self.plugin_parent._on_component_affiliations_set, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1295 client=self.parent, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1296 ) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1297 self.xmlstream.addObserver( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1298 IQ_FIS_CONFIGURATION_GET, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1299 self.plugin_parent._on_component_configuration_get, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1300 client=self.parent, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1301 ) |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1302 self.xmlstream.addObserver( |
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1303 IQ_FIS_CONFIGURATION_SET, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1304 self.plugin_parent._on_component_configuration_set, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1305 client=self.parent, |
3320
bb92085720c8
plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents:
3314
diff
changeset
|
1306 ) |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1307 self.xmlstream.addObserver( |
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1308 IQ_FIS_CREATE_DIR, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1309 self.plugin_parent._on_component_create_dir, |
4270
0d7bb4df2343
Reformatted code base using black.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
1310 client=self.parent, |
3359
000b6722bd35
plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents:
3355
diff
changeset
|
1311 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1312 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1313 self.xmlstream.addObserver( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3573
diff
changeset
|
1314 IQ_FIS_REQUEST, self.plugin_parent.on_request, client=self.parent |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1315 ) |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1316 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1317 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1318 return [disco.DiscoFeature(NS_FIS)] |
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1319 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2589
diff
changeset
|
1320 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
2503
c0bec8bac2b5
XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1321 return [] |