annotate sat/plugins/plugin_xep_0329.py @ 3320:bb92085720c8

plugin XEP-0329: implemented ways to get/set affiliations: PubSub-like affiliations can now be get/set using non standard methods. This has been done to prepare the move to a pubsub based file sharing system.
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:12:44 +0200
parents 5887fb414758
children 8bbd2ed924e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
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)
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3118
diff changeset
4 # Copyright (C) 2009-2020 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
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
22 from pathlib import Path
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
23 from typing import Optional, Dict
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
24 from zope.interface import implementer
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import xmlstream
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import jid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 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
28 from twisted.internet import defer
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
29 from wokkel import disco, iwokkel, data_form
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
30 from sat.core.i18n import _
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
31 from sat.core.xmpp import SatXMPPEntity
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
32 from sat.core import exceptions
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
33 from sat.core.constants import Const as C
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
34 from sat.core.log import getLogger
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
35 from sat.tools import stream
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
36 from sat.tools.common import regex
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
37
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
38
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
39 log = getLogger(__name__)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
40
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 PLUGIN_INFO = {
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_NAME: "File Information Sharing",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_IMPORT_NAME: "XEP-0329",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_TYPE: "XEP",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_MODES: C.PLUG_MODE_BOTH,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_PROTOCOLS: ["XEP-0329"],
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
47 C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300", "XEP-0106"],
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_MAIN: "XEP_0329",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_HANDLER: "yes",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
50 C.PI_DESCRIPTION: _("""Implementation of File Information Sharing"""),
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
53 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
54 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
55 NS_FIS_CONFIGURATION = "org.salut-a-toi.fis-configuration"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
56
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
57 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
58 # 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
59 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
60 IQ_FIS_AFFILIATION_SET = f'{C.IQ_SET}/affiliations[@xmlns="{NS_FIS_AFFILIATION}"]'
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
61 SINGLE_FILES_DIR = "files"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
62 TYPE_VIRTUAL = "virtual"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
63 TYPE_PATH = "path"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
64 SHARE_TYPES = (TYPE_PATH, TYPE_VIRTUAL)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
65 KEY_TYPE = "type"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
68 class RootPathException(Exception):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
69 """Root path is requested"""
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
70
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
71
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 class ShareNode(object):
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 """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
74
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 host = None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def __init__(self, name, parent, type_, access, path=None):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 assert type_ in SHARE_TYPES
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 if name is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
80 if name == ".." or "/" in name or "\\" in name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
81 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
82 _("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
83 name=name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
84 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
85 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
86 if name == "..":
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
87 name = "--"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 name = regex.pathEscape(name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.name = name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.children = {}
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.type = type_
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self.access = {} if access is None else access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 assert isinstance(self.access, dict)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.parent = None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 if parent is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 assert name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 parent.addChild(self)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 assert name is None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 if path is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if type_ != TYPE_PATH:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
103 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
104 self._path = path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
105
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @property
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 def path(self):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return self._path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 def __getitem__(self, key):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return self.children[key]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 def __contains__(self, item):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 return self.children.__contains__(item)
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 __iter__(self):
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
117 return self.children.__iter__()
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
3032
95e2fd14a761 plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
119 def items(self):
95e2fd14a761 plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
120 return self.children.items()
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
121
3032
95e2fd14a761 plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
122 def values(self):
95e2fd14a761 plugin XEP-0329: fixed ShareNode item() and values after Python 3 port
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
123 return self.children.values()
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 def getOrCreate(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
126 """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
127 if access is None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 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
129 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 return self.children[name]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 node = ShareNode(name, self, type_=type_, access=access)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 return node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def addChild(self, node):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 if node.parent is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
137 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
138 node.parent = self
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 self.children[node.name] = node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
141 def removeFromParent(self):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
142 try:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
143 del self.parent.children[self.name]
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
144 except TypeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
145 raise exceptions.InternalError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
146 "trying to remove a node from inexisting parent"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
147 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
148 except KeyError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
149 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
150 self.parent = None
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
151
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 def _checkNodePermission(self, client, node, perms, peer_jid):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """Check access to this node for peer_jid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
154
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 @param node(SharedNode): node to check access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 @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
157 @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
158 @return (bool): True if entity can access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
160 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
161 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 self.host.memory.checkFilePermission(file_data, peer_jid, perms)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 except exceptions.PermissionError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 return False
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 return True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
168 def checkPermissions(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
169 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
170 ):
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
171 """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
172
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 @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
174 @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
175 @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
176 checked too
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 @return (bool): True if entity can access this node
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 peer_jid = peer_jid.userhostJID()
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 if peer_jid == client.jid.userhostJID():
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 return True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
182
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 parent = self
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 while parent != None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if not self._checkNodePermission(client, parent, perms, peer_jid):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 return False
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 parent = parent.parent
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 return True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 @staticmethod
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 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
193 """find node corresponding to a path
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 @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
196 @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
197 used to check permission
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 @return (dict, unicode): shared data, remaining path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 @raise exceptions.PermissionError: user can't access this file
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 @raise exceptions.DataError: path is invalid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 @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
202 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
203 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
204
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
205 if ".." in path_elts:
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
206 log.warning(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
207 'parent dir ("..") found in path, hack attempt? path is {path} '
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
208 '[{profile}]').format(path=path, profile=client.profile))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
209 raise exceptions.PermissionError("illegal path elements")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
210
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 if not path_elts:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
212 raise exceptions.DataError(_("path is invalid: {path}").format(path=path))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 node = client._XEP_0329_root_node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 while path_elts:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if node.type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 node = node[path_elts.pop(0)]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 raise exceptions.NotFound
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 elif node.type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 break
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
225 if not node.checkPermissions(client, peer_jid, perms=perms):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
226 raise exceptions.PermissionError("permission denied")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
228 return node, "/".join(path_elts)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
229
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
230 def findByLocalPath(self, path):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
231 """retrieve nodes linking to local path
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
232
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 @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
234 @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
235 """
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
236 shared_paths = self.getSharedPaths()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
237 try:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 return shared_paths[path]
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
239 except KeyError:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
240 raise exceptions.NotFound
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
241
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242 def _getSharedPaths(self, node, paths):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 if node.type == TYPE_VIRTUAL:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
244 for node in node.values():
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245 self._getSharedPaths(node, paths)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
246 elif node.type == TYPE_PATH:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
247 paths.setdefault(node.path, []).append(node)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
248 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
249 raise exceptions.InternalError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
250 "unknown node type: {type}".format(type=node.type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
251 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
252
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
253 def getSharedPaths(self):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
254 """retrieve nodes by shared path
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
255
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
256 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
257 @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
258 """
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
259 if self.type == TYPE_PATH:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
260 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
261 "getSharedPaths must be used on a virtual node"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
262 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
263 paths = {}
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
264 self._getSharedPaths(self, paths)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
265 return paths
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
266
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 class XEP_0329(object):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 def __init__(self, host):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 log.info(_("File Information Sharing initialization"))
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 self.host = host
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 ShareNode.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
273 self._h = host.plugins["XEP-0300"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
274 self._jf = host.plugins["XEP-0234"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
275 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
276 "FISList",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
277 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
278 in_sign="ssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
279 out_sign="aa{ss}",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
280 method=self._listFiles,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
281 async_=True,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
282 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
283 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
284 "FISLocalSharesGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
285 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
286 in_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
287 out_sign="as",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
288 method=self._localSharesGet,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
289 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
290 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
291 "FISSharePath",
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="ssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
294 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
295 method=self._sharePath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
296 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
297 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
298 "FISUnsharePath",
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="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
301 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
302 method=self._unsharePath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
303 )
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
304 host.bridge.addMethod(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
305 "FISAffiliationsGet",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
306 ".plugin",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
307 in_sign="ssss",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
308 out_sign="a{ss}",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
309 method=self._affiliationsGet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
310 async_=True,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
311 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
312 host.bridge.addMethod(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
313 "FISAffiliationsSet",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
314 ".plugin",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
315 in_sign="sssa{ss}s",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
316 out_sign="",
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
317 method=self._affiliationsSet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
318 async_=True,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
319 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
320 host.bridge.addSignal("FISSharedPathNew", ".plugin", signature="sss")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
321 host.bridge.addSignal("FISSharedPathRemoved", ".plugin", signature="ss")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
323 host.registerNamespace("fis", NS_FIS)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
324
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 def getHandler(self, client):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 return XEP_0329_handler(self)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
327
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 def profileConnected(self, client):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 if not client.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
330 client._XEP_0329_root_node = ShareNode(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
331 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
332 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
333 TYPE_VIRTUAL,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
334 {C.ACCESS_PERM_READ: {KEY_TYPE: C.ACCESS_TYPE_PUBLIC}},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
335 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
336 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
337
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
338 def _fileSendingRequestTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
339 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
340 ):
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
341 """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
342
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
343 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
344 with known names
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 """
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 if client.is_component:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
348
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
350 name = file_data["name"]
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
351 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
352 return True, None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
353 assert "/" not in name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
354
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
355 path = file_data.get("path")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 if path is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 # 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
358 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
359 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
360 except (exceptions.PermissionError, exceptions.NotFound):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
361 #  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
362 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 except exceptions.DataError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
364 log.warning(_("invalid path: {path}").format(path=path))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
366
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 if node.type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
368 # 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
369 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
370 path = node[name].path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
371 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
372 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
373 elif node.type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
374 # 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
375 path = os.path.join(node.path, rem_path, name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
376 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
377 raise exceptions.InternalError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
378 "unknown type: {type}".format(type=node.type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
379 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
380 if not os.path.exists(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
381 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 size = os.path.getsize(path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
383 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 # 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
385 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 name_data = client._XEP_0329_names_data[name]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
389
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
390 for path, shared_file in name_data.items():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
391 if True: #  FIXME: filters are here
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
392 break
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
394 return True, None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
395 parent_node = shared_file["parent"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
396 if not parent_node.checkPermissions(client, session["peer_jid"]):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
397 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
398 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
399 "{peer_jid} requested a file (s)he can't access [{profile}]"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
400 ).format(peer_jid=session["peer_jid"], profile=client.profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
401 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 return True, None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
403 size = shared_file["size"]
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
404
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
405 file_data["size"] = size
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
406 file_elt.addElement("size", content=str(size))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
407 hash_algo = file_data["hash_algo"] = self._h.getDefaultAlgo()
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
408 hasher = file_data["hash_hasher"] = self._h.getHasher(hash_algo)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 file_elt.addChild(self._h.buildHashUsedElt(hash_algo))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
410 content_data["stream_object"] = stream.FileStreamObject(
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 self.host,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
412 client,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
413 path,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
414 uid=self._jf.getProgressId(session, content_name),
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 size=size,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
417 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 return False, True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
419
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
420 # common methods
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
421
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
422 def _requestHandler(self, client, iq_elt, root_nodes_cb, files_from_node_cb):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 iq_elt.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
424 node = iq_elt.query.getAttribute("node")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 if not node:
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
426 d = defer.maybeDeferred(root_nodes_cb, client, iq_elt)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 else:
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
428 d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, node)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
429 d.addErrback(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
430 lambda failure_: log.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
431 _("error while retrieving files: {msg}").format(msg=failure_)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
432 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
433 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
434
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 def _iqError(self, client, iq_elt, condition="item-not-found"):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 error_elt = jabber_error.StanzaError(condition).toResponse(iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 client.send(error_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
438
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
439 #  client
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
440
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
441 def _addPathData(self, client, query_elt, path, parent_node):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
442 """Fill query_elt with files/directories found in path"""
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
443 name = os.path.basename(path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if os.path.isfile(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 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
446 mime_type = mimetypes.guess_type(path, strict=False)[0]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
447 file_elt = self._jf.buildFileElement(
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
448 client=client, name=name, size=size, mime_type=mime_type,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
449 modified=os.path.getmtime(path)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
450 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
451
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
452 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
453 # 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
454 # it for all files.
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
455 # 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
456 name_data = client._XEP_0329_names_data.setdefault(name, {})
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
457 if path not in name_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
458 name_data[path] = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
459 "size": size,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
460 "mime_type": mime_type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
461 "parent": parent_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
462 }
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 # we have a directory
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
465 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
466 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
467
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 def _pathNodeHandler(self, client, iq_elt, query_elt, node, path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 """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
470 path = os.path.join(node.path, path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
471
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 if not os.path.exists(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 # path may have been moved since it has been shared
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
474 return self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
475 elif os.path.isfile(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
476 self._addPathData(client, query_elt, path, node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
478 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
479 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
480 name = name.decode("utf-8", "strict")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
481 except UnicodeDecodeError as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
482 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
483 _("ignoring invalid unicode name ({name}): {msg}").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
484 name=name.decode("utf-8", "replace"), msg=e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
485 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
486 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
487 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 full_path = os.path.join(path, name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 self._addPathData(client, query_elt, full_path, node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
490
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 def _virtualNodeHandler(self, client, peer_jid, iq_elt, query_elt, node):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
492 """Fill query_elt for virtual nodes"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
493 for name, child_node in node.items():
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 if not child_node.checkPermissions(client, peer_jid, check_parents=False):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 node_type = child_node.type
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 if node_type == TYPE_VIRTUAL:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
498 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
499 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 elif node_type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 self._addPathData(client, query_elt, child_node.path, child_node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
503 raise exceptions.InternalError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
504 _("unexpected type: {type}").format(type=node_type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
505 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
506
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
507 def _getRootNodesCb(self, client, iq_elt):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
508 peer_jid = jid.JID(iq_elt["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
509 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
510 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
511 for name, node in client._XEP_0329_root_node.items():
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 if not node.checkPermissions(client, peer_jid, check_parents=False):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
513 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
514 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
515 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
516 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
517
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
518 def _getFilesFromNodeCb(self, client, iq_elt, node_path):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
519 """Main method to retrieve files/directories from a node_path"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
520 peer_jid = jid.JID(iq_elt["from"])
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
521 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
522 node, path = ShareNode.find(client, node_path, peer_jid)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
523 except (exceptions.PermissionError, exceptions.NotFound):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
524 return self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 except exceptions.DataError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
526 return self._iqError(client, iq_elt, condition="not-acceptable")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
527
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 node_type = node.type
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
529 peer_jid = jid.JID(iq_elt["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
530 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
531 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
532 query_elt["node"] = node_path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
533
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
534 # we now fill query_elt according to node_type
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
535 if node_type == TYPE_PATH:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
536 #  it's a physical path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
537 self._pathNodeHandler(client, iq_elt, query_elt, node, path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
538 elif node_type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
539 assert not path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
540 self._virtualNodeHandler(client, peer_jid, iq_elt, query_elt, node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
541 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
542 raise exceptions.InternalError(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
543 _("unknown node type: {type}").format(type=node_type)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
544 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
545
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
547
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
548 def onRequest(self, iq_elt, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
549 return self._requestHandler(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
550 client, iq_elt, self._getRootNodesCb, self._getFilesFromNodeCb
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
551 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
552
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 # Component
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
554
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
555 def _compParseJids(self, client, iq_elt):
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
556 """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
557
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
558 @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
559 @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
560 """
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
561 to_jid = jid.JID(iq_elt['to'])
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
562 if to_jid.user:
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
563 user = self.host.plugins['XEP-0106'].unescape(to_jid.user)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
564 if '@' in user:
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
565 # a full jid is specified
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
566 owner = jid.JID(user)
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
567 else:
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
568 # only user part is specified, we use our own host to build the full jid
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
569 owner = jid.JID(None, (user, client.host, None))
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
570 else:
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
571 owner = jid.JID(iq_elt["from"]).userhostJID()
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
572
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
573 peer_jid = jid.JID(iq_elt["from"])
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
574 return peer_jid, owner
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
575
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
576 @defer.inlineCallbacks
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
577 def _compGetRootNodesCb(self, client, iq_elt):
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
578 peer_jid, owner = self._compParseJids(client, iq_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
579 files_data = yield self.host.memory.getFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
580 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
581 peer_jid=peer_jid,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
582 parent="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
583 type_=C.FILE_TYPE_DIRECTORY,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
584 owner=owner,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
585 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
586 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
587 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
588 for file_data in files_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
589 name = file_data["name"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
590 directory_elt = query_elt.addElement("directory")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
591 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
592 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
593
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
594 @defer.inlineCallbacks
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
595 def _compGetFilesFromNodeCb(self, client, iq_elt, node_path):
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
596 """Retrieve files from local files repository according to permissions
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
597
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
598 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
599 @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
600 files_data):
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
601 can be used to add data/elements
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
602 """
2923
1fd3ecb3351a plugin XEP-0329: use local part of jid for components:
Goffi <goffi@goffi.org>
parents: 2909
diff changeset
603 peer_jid, owner = self._compParseJids(client, iq_elt)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
604 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
605 files_data = yield self.host.memory.getFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
606 client, peer_jid=peer_jid, path=node_path, owner=owner
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
607 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
608 except exceptions.NotFound:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
609 self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
610 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
611 except exceptions.PermissionError:
db0890c9c7db plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents: 2923
diff changeset
612 self._iqError(client, iq_elt, condition='not-allowed')
db0890c9c7db plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents: 2923
diff changeset
613 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
614 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
615 log.error("internal server error: {e}".format(e=e))
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
616 self._iqError(client, iq_elt, condition='internal-server-error')
db0890c9c7db plugin XEP-0329: correctly handle error if something is wrong when getting file from component.
Goffi <goffi@goffi.org>
parents: 2923
diff changeset
617 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
618 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
619 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
620 query_elt["node"] = node_path
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
621 if not self.host.trigger.point(
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
622 "XEP-0329_compGetFilesFromNode",
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
623 client,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
624 iq_elt,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
625 iq_result_elt,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
626 owner,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
627 node_path,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
628 files_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
629 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
630 return
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
631 for file_data in files_data:
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
632 if file_data['type'] == C.FILE_TYPE_DIRECTORY:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
633 directory_elt = query_elt.addElement("directory")
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
634 directory_elt['name'] = file_data['name']
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
635 self.host.trigger.point(
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
636 "XEP-0329_compGetFilesFromNode_build_directory",
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
637 client,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
638 file_data,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
639 directory_elt,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
640 owner,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
641 node_path,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
642 )
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
643 else:
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
644 file_elt = self._jf.buildFileElementFromDict(
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
645 client,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
646 file_data,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
647 modified=file_data.get("modified", file_data["created"])
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
648 )
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
649 query_elt.addChild(file_elt)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
651
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
652 def onComponentRequest(self, iq_elt, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
653 return self._requestHandler(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
654 client, iq_elt, self._compGetRootNodesCb, self._compGetFilesFromNodeCb
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
655 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
656
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
657 def _parseResult(self, iq_elt, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
658 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
659 files = []
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
660
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 for elt in query_elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
662 if elt.name == "file":
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
663 # we have a file
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
664 try:
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
665 file_data = self._jf.parseFileElement(client, elt)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
666 except exceptions.DataError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
667 continue
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
668 file_data["type"] = C.FILE_TYPE_FILE
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
669 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
670 # we have a directory
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
671
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
672 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
673 self.host.trigger.point(
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
674 "XEP-0329_parseResult_directory",
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
675 client,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
676 elt,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
677 file_data,
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
678 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
679 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
680 log.warning(
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
681 _(f"unexpected element, ignoring: {elt.toXml()}")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
682 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
683 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
684 files.append(file_data)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
685 return files
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
686
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
687 # affiliations #
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
688
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
689 async def _parseElement(self, client, iq_elt, element, namespace):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
690 from_jid = jid.JID(iq_elt['from'])
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
691 elt = next(iq_elt.elements(namespace, element))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
692 path = Path("/", elt['path'])
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
693 if len(path.parts) < 2:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
694 raise RootPathException
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
695 namespace = elt.getAttribute('namespace')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
696 files_data = await self.host.memory.getFiles(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
697 client,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
698 peer_jid=from_jid,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
699 path=str(path.parent),
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
700 name=path.name,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
701 namespace=namespace,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
702 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
703 if len(files_data) != 1:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
704 client.sendError(iq_elt, 'item-not-found')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
705 raise exceptions.CancelError
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
706 file_data = files_data[0]
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
707 return from_jid, elt, path, namespace, file_data
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
708
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
709 def _affiliationsGet(self, service_jid_s, namespace, path, profile):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
710 client = self.host.getClient(profile)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
711 service = jid.JID(service_jid_s)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
712 d = defer.ensureDeferred(self.affiliationsGet(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
713 client, service, namespace or None, path))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
714 d.addCallback(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
715 lambda affiliations: {
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
716 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
717 }
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
718 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
719 return d
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
720
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
721 async def affiliationsGet(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
722 self,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
723 client: SatXMPPEntity,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
724 service: jid.JID,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
725 namespace: Optional[str],
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
726 path: str
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
727 ) -> Dict[jid.JID, str]:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
728 if not path:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
729 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
730 iq_elt = client.IQ("get")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
731 iq_elt['to'] = service.full()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
732 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
733 if namespace:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
734 affiliations_elt["namespace"] = namespace
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
735 affiliations_elt["path"] = path
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
736 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
737 try:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
738 affiliations_elt = next(iq_result_elt.elements(NS_FIS_AFFILIATION, "affiliations"))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
739 except StopIteration:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
740 raise exceptions.DataError(f"Invalid result to affiliations request: {iq_result_elt.toXml()}")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
741
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
742 affiliations = {}
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
743 for affiliation_elt in affiliations_elt.elements(NS_FIS_AFFILIATION, 'affiliation'):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
744 try:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
745 affiliations[jid.JID(affiliation_elt['jid'])] = affiliation_elt['affiliation']
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
746 except (KeyError, RuntimeError):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
747 raise exceptions.DataError(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
748 f"invalid affiliation element: {affiliation_elt.toXml()}")
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 return affiliations
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
751
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
752 def _affiliationsSet(self, service_jid_s, namespace, path, affiliations, profile):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
753 client = self.host.getClient(profile)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
754 service = jid.JID(service_jid_s)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
755 affiliations = {jid.JID(e): a for e, a in affiliations.items()}
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
756 return defer.ensureDeferred(self.affiliationsSet(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
757 client, service, namespace or None, path, affiliations))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
758
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
759 async def affiliationsSet(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
760 self,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
761 client: SatXMPPEntity,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
762 service: jid.JID,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
763 namespace: Optional[str],
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
764 path: str,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
765 affiliations: Dict[jid.JID, str],
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 if not path:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
768 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
769 iq_elt = client.IQ("set")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
770 iq_elt['to'] = service.full()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
771 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
772 if namespace:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
773 affiliations_elt["namespace"] = namespace
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
774 affiliations_elt["path"] = path
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
775 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
776 affiliation_elt = affiliations_elt.addElement('affiliation')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
777 affiliation_elt['jid'] = entity_jid.full()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
778 affiliation_elt['affiliation'] = affiliation
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
779 await iq_elt.send()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
780
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
781 def _onComponentAffiliationsGet(self, iq_elt, client):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
782 iq_elt.handled = True
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
783 defer.ensureDeferred(self.onComponentAffiliationsGet(client, iq_elt))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
784
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
785 async def onComponentAffiliationsGet(self, client, iq_elt):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
786 try:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
787 (
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
788 from_jid, affiliations_elt, path, namespace, file_data
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
789 ) = await self._parseElement(client, iq_elt, "affiliations", NS_FIS_AFFILIATION)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
790 except exceptions.CancelError:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
791 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
792 except RootPathException:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
793 # if root path is requested, we only get owner affiliation
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
794 peer_jid, owner = self._compParseJids(client, iq_elt)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
795 is_owner = peer_jid.userhostJID() == owner
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
796 affiliations = {owner: 'owner'}
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
797 else:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
798 from_jid_bare = from_jid.userhostJID()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
799 is_owner = from_jid_bare == file_data.get('owner')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
800 affiliations = self.host.memory.getFileAffiliations(file_data)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
801 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
802 affiliations_elt = iq_result_elt.addElement((NS_FIS_AFFILIATION, 'affiliations'))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
803 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
804 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
805 # only onwer can get all affiliations
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
806 continue
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
807 affiliation_elt = affiliations_elt.addElement('affiliation')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
808 affiliation_elt['jid'] = entity_jid.userhost()
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
809 affiliation_elt['affiliation'] = affiliation
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
810 client.send(iq_result_elt)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
811
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
812 def _onComponentAffiliationsSet(self, iq_elt, client):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
813 iq_elt.handled = True
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
814 defer.ensureDeferred(self.onComponentAffiliationsSet(client, iq_elt))
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
815
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
816 async def onComponentAffiliationsSet(self, client, iq_elt):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
817 try:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
818 (
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
819 from_jid, affiliations_elt, path, namespace, file_data
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
820 ) = await self._parseElement(client, iq_elt, "affiliations", NS_FIS_AFFILIATION)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
821 except exceptions.CancelError:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
822 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
823 except RootPathException:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
824 client.sendError(iq_elt, 'bad-request', "Root path can't be used")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
825 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
826
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
827 if from_jid.userhostJID() != file_data['owner']:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
828 log.warning(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
829 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
830 f"{file_data['owner']}"
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
831 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
832 client.sendError(iq_elt, 'forbidden')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
833 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
834
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
835 try:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
836 affiliations = {
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
837 jid.JID(e['jid']): e['affiliation']
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
838 for e in affiliations_elt.elements(NS_FIS_AFFILIATION, 'affiliation')
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
839 }
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
840 except (KeyError, RuntimeError):
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
841 log.warning(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
842 f"invalid affiliation element: {affiliations_elt.toXml()}"
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
843 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
844 client.sendError(iq_elt, 'bad-request', "invalid affiliation element")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
845 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
846 except Exception as e:
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
847 log.error(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
848 f"unexepected exception while setting affiliation element: {e}\n"
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
849 f"{affiliations_elt.toXml()}"
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
850 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
851 client.sendError(iq_elt, 'internal-server-error', f"{e}")
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
852 return
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
853
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
854 await self.host.memory.setFileAffiliations(client, file_data, affiliations)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
855
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
856 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
857 client.send(iq_result_elt)
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
858
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
859 # file methods #
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
860
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
861 def _serializeData(self, files_data):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
862 for file_data in files_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
863 for key, value in file_data.items():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
864 file_data[key] = (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
865 json.dumps(value) if key in ("extra",) else str(value)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
866 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
867 return files_data
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
868
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
869 def _listFiles(self, target_jid, path, extra, profile):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
870 client = self.host.getClient(profile)
3040
fee60f17ebac jp: jp asyncio port:
Goffi <goffi@goffi.org>
parents: 3032
diff changeset
871 target_jid = client.jid if not target_jid else jid.JID(target_jid)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
872 d = self.listFiles(client, target_jid, path or None)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
873 d.addCallback(self._serializeData)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
874 return d
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
875
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
876 def listFiles(self, client, target_jid, path=None, extra=None):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
877 """List file shared by an entity
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
878
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
879 @param target_jid(jid.JID): jid of the sharing entity
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
880 @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
881 None to get root directories
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
882 @param extra(dict, None): extra data
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
883 @return list(dict): shared files
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
884 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
885 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
886 iq_elt["to"] = target_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
887 query_elt = iq_elt.addElement((NS_FIS, "query"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
888 if path:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
889 query_elt["node"] = path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
890 d = iq_elt.send()
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
891 d.addCallback(self._parseResult, client)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
892 return d
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
893
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
894 def _localSharesGet(self, profile):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
895 client = self.host.getClient(profile)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
896 return self.localSharesGet(client)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
897
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
898 def localSharesGet(self, client):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
899 return list(client._XEP_0329_root_node.getSharedPaths().keys())
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
900
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
901 def _sharePath(self, name, path, access, profile):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
902 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
903 access = json.loads(access)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
904 return self.sharePath(client, name or None, path, access)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
905
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
906 def sharePath(self, client, name, path, access):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
907 if client.is_component:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
908 raise exceptions.ClientTypeError
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
909 if not os.path.exists(path):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
910 raise ValueError(_("This path doesn't exist!"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
911 if not path or not path.strip(" /"):
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
912 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
913 if not isinstance(access, dict):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
914 raise ValueError(_("access must be a dict"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
915
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
916 node = client._XEP_0329_root_node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
917 node_type = TYPE_PATH
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
918 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
919 # 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
920 # files in the same dir
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
921 node = node.getOrCreate(SINGLE_FILES_DIR)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
922
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
923 if not name:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
924 name = os.path.basename(path.rstrip(" /"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
925 if not name:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
926 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
927
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
928 if name in node or name == SINGLE_FILES_DIR:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
929 idx = 1
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
930 new_name = name + "_" + str(idx)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
931 while new_name in node:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
932 idx += 1
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
933 new_name = name + "_" + str(idx)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
934 name = new_name
2909
90146552cde5 core (memory), plugin XEP-0329, plugin invitation: minor style improvments
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
935 log.info(_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
936 "A directory with this name is already shared, renamed to {new_name} "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
937 "[{profile}]".format( new_name=new_name, profile=client.profile)))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
938
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
939 ShareNode(name=name, parent=node, type_=node_type, access=access, path=path)
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
940 self.host.bridge.FISSharedPathNew(path, name, client.profile)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
941 return name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
942
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
943 def _unsharePath(self, path, profile):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
944 client = self.host.getClient(profile)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
945 return self.unsharePath(client, path)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
946
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
947 def unsharePath(self, client, path):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
948 nodes = client._XEP_0329_root_node.findByLocalPath(path)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
949 for node in nodes:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
950 node.removeFromParent()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
951 self.host.bridge.FISSharedPathRemoved(path, client.profile)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
952
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
953
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2937
diff changeset
954 @implementer(iwokkel.IDisco)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
955 class XEP_0329_handler(xmlstream.XMPPHandler):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
956
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
957 def __init__(self, plugin_parent):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
958 self.plugin_parent = plugin_parent
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
959 self.host = plugin_parent.host
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
960
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
961 def connectionInitialized(self):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
962 if self.parent.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
963 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
964 IQ_FIS_REQUEST, self.plugin_parent.onComponentRequest, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
965 )
3320
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
966 self.xmlstream.addObserver(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
967 IQ_FIS_AFFILIATION_GET,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
968 self.plugin_parent._onComponentAffiliationsGet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
969 client=self.parent
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
970 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
971 self.xmlstream.addObserver(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
972 IQ_FIS_AFFILIATION_SET,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
973 self.plugin_parent._onComponentAffiliationsSet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
974 client=self.parent
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
975 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
976 self.xmlstream.addObserver(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
977 IQ_FIS_CONFIGURATION_GET,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
978 self.plugin_parent._onComponentConfigurationGet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
979 client=self.parent
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
980 )
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
981 self.xmlstream.addObserver(
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
982 IQ_FIS_CONFIGURATION_SET,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
983 self.plugin_parent._onComponentConfigurationSet,
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
984 client=self.parent
bb92085720c8 plugin XEP-0329: implemented ways to get/set affiliations:
Goffi <goffi@goffi.org>
parents: 3314
diff changeset
985 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
986 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
987 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
988 IQ_FIS_REQUEST, self.plugin_parent.onRequest, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
989 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
990
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
991 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
992 return [disco.DiscoFeature(NS_FIS)]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
993
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
994 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
995 return []