annotate sat/plugins/plugin_xep_0329.py @ 2845:42380a4f6433

quick frontend (app): new synchronisation mechanism: on some frontend (notably on mobiles) frontend can be paused and lose synchronisation with backend, this can also happen if backend restart a connexion without resuming using stream management. To manage that, QuickApp now has a sync attribute which is True when the backend is in sync with frontend. When synchronisation has been lost and is possible again, all widget will have their "resync" method called, which can be used to do necessary calls to update the status. It's the responsability of each widget to then reset the "sync" attribute, once everything has been resynchronised.
author Goffi <goffi@goffi.org>
date Sat, 09 Mar 2019 16:33:31 +0100
parents 003b8b4b56a7
children 90146552cde5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for File Information Sharing (XEP-0329)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # (at your option) any later version.
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # 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
13 # 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
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # 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
18 # 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
19
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core import exceptions
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.constants import Const as C
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
24
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 log = getLogger(__name__)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import stream
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from sat.tools.common import regex
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from wokkel import disco, iwokkel
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from zope.interface import implements
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from twisted.words.protocols.jabber import xmlstream
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from twisted.words.protocols.jabber import jid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 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
33 from twisted.internet import defer
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 import mimetypes
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 import json
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 import os
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
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 PLUGIN_INFO = {
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_NAME: "File Information Sharing",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_IMPORT_NAME: "XEP-0329",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_TYPE: "XEP",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_MODES: C.PLUG_MODE_BOTH,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 C.PI_PROTOCOLS: ["XEP-0329"],
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300"],
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 C.PI_MAIN: "XEP_0329",
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 C.PI_HANDLER: "yes",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
48 C.PI_DESCRIPTION: _(u"""Implementation of File Information Sharing"""),
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 }
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
51 NS_FIS = "urn:xmpp:fis:0"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 IQ_FIS_REQUEST = C.IQ_GET + '/query[@xmlns="' + NS_FIS + '"]'
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 SINGLE_FILES_DIR = u"files"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
55 TYPE_VIRTUAL = u"virtual"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
56 TYPE_PATH = u"path"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
57 SHARE_TYPES = (TYPE_PATH, TYPE_VIRTUAL)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
58 KEY_TYPE = u"type"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 class ShareNode(object):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """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
63
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 host = None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def __init__(self, name, parent, type_, access, path=None):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 assert type_ in SHARE_TYPES
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if name is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
69 if name == u".." or u"/" in name or u"\\" in name:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
70 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
71 _(u"path change chars found in name [{name}], hack attempt?").format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
72 name=name
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
73 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
74 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
75 if name == u"..":
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
76 name = u"--"
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 name = regex.pathEscape(name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.name = name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 self.children = {}
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.type = type_
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.access = {} if access is None else access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 assert isinstance(self.access, dict)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 self.parent = None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if parent is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 assert name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 parent.addChild(self)
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 assert name is None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if path is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if type_ != TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 raise exceptions.InternalError(_(u"path can only be set on path nodes"))
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 self._path = path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 @property
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 def path(self):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 return self._path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 def __getitem__(self, key):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 return self.children[key]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
101
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def __contains__(self, item):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 return self.children.__contains__(item)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
104
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 def __iter__(self):
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
106 return self.children.__iter__()
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
107
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 def iteritems(self):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 return self.children.iteritems()
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
111 def itervalues(self):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
112 return self.children.itervalues()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
113
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def getOrCreate(self, name, type_=TYPE_VIRTUAL, access=None):
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 """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
116 if access is None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 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
118 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 return self.children[name]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 node = ShareNode(name, self, type_=type_, access=access)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 return node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
123
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 def addChild(self, node):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 if node.parent is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 raise exceptions.ConflictError(_(u"a node can't have several parents"))
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 node.parent = self
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 self.children[node.name] = node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
129
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
130 def removeFromParent(self):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
131 try:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
132 del self.parent.children[self.name]
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 except TypeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
134 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
135 u"trying to remove a node from inexisting parent"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
136 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
137 except KeyError:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
138 raise exceptions.InternalError(u"node not found in parent's children")
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
139 self.parent = None
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
140
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def _checkNodePermission(self, client, node, perms, peer_jid):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 """Check access to this node for peer_jid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 @param node(SharedNode): node to check access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 @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
146 @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
147 @return (bool): True if entity can access
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
149 file_data = {u"access": self.access, u"owner": client.jid.userhostJID()}
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.host.memory.checkFilePermission(file_data, peer_jid, perms)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 except exceptions.PermissionError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 return False
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 return True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
156
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
157 def checkPermissions(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
158 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
159 ):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 """check that peer_jid can access this node and all its parents
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
161
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 @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
163 @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
164 @param check_parents(bool): if True, access of all parents of this node will be checked too
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 @return (bool): True if entity can access this node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 """
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 peer_jid = peer_jid.userhostJID()
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 if peer_jid == client.jid.userhostJID():
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 return True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
170
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 parent = self
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 while parent != None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 if not self._checkNodePermission(client, parent, perms, peer_jid):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 return False
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 parent = parent.parent
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 return True
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 @staticmethod
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 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
181 """find node corresponding to a path
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 @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
184 @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
185 used to check permission
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 @return (dict, unicode): shared data, remaining path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 @raise exceptions.PermissionError: user can't access this file
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 @raise exceptions.DataError: path is invalid
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 @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
190 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
191 path_elts = filter(None, path.split(u"/"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
192
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
193 if u".." in path_elts:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
194 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
195 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
196 u'parent dir ("..") found in path, hack attempt? path is {path} [{profile}]'
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
197 ).format(path=path, profile=client.profile)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
198 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 raise exceptions.PermissionError(u"illegal path elements")
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
200
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 if not path_elts:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
202 raise exceptions.DataError(_(u"path is invalid: {path}").format(path=path))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
203
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 node = client._XEP_0329_root_node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 while path_elts:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 if node.type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 node = node[path_elts.pop(0)]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 raise exceptions.NotFound
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 elif node.type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 break
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
214
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
215 if not node.checkPermissions(client, peer_jid, perms=perms):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 raise exceptions.PermissionError(u"permission denied")
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
217
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
218 return node, u"/".join(path_elts)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
219
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
220 def findByLocalPath(self, path):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
221 """retrieve nodes linking to local path
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
222
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
223 @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
224 @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
225 """
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
226 shared_paths = self.getSharedPaths()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
227 try:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
228 return shared_paths[path]
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
229 except KeyError:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
230 raise exceptions.NotFound
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
231
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
232 def _getSharedPaths(self, node, paths):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
233 if node.type == TYPE_VIRTUAL:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
234 for node in node.itervalues():
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
235 self._getSharedPaths(node, paths)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
236 elif node.type == TYPE_PATH:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
237 paths.setdefault(node.path, []).append(node)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
238 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
239 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
240 u"unknown node type: {type}".format(type=node.type)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
241 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
242
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
243 def getSharedPaths(self):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
244 """retrieve nodes by shared path
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
245
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
246 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
247 @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
248 """
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
249 if self.type == TYPE_PATH:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
250 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
251 "getSharedPaths must be used on a virtual node"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
252 )
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
253 paths = {}
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
254 self._getSharedPaths(self, paths)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
255 return paths
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
256
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
257
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 class XEP_0329(object):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 def __init__(self, host):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 log.info(_("File Information Sharing initialization"))
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 self.host = host
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 ShareNode.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
263 self._h = host.plugins["XEP-0300"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
264 self._jf = host.plugins["XEP-0234"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
265 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
266 "FISList",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
267 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
268 in_sign="ssa{ss}s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
269 out_sign="aa{ss}",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
270 method=self._listFiles,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
271 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
272 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
273 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
274 "FISLocalSharesGet",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
275 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
276 in_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
277 out_sign="as",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
278 method=self._localSharesGet,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
279 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
280 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
281 "FISSharePath",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
282 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
283 in_sign="ssss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
284 out_sign="s",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
285 method=self._sharePath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
286 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
287 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
288 "FISUnsharePath",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
289 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
290 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
291 out_sign="",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
292 method=self._unsharePath,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
293 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
294 host.bridge.addSignal("FISSharedPathNew", ".plugin", signature="sss")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
295 host.bridge.addSignal("FISSharedPathRemoved", ".plugin", signature="ss")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
297 host.registerNamespace("fis", NS_FIS)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
298
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 def getHandler(self, client):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 return XEP_0329_handler(self)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
301
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 def profileConnected(self, client):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 if not client.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
304 client._XEP_0329_root_node = ShareNode(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
305 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
306 None,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
307 TYPE_VIRTUAL,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
308 {C.ACCESS_PERM_READ: {KEY_TYPE: C.ACCESS_TYPE_PUBLIC}},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
309 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
310 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
311
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
312 def _fileSendingRequestTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
313 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
314 ):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 """this trigger check that a requested file is available, and fill suitable data if so
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
316
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 path and name are used to retrieve the file. If path is missing, we try our luck with known names
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 """
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 if client.is_component:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
321
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
323 name = file_data[u"name"]
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 return True, None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
326 assert u"/" not in name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
327
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
328 path = file_data.get(u"path")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 if path is not None:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 # 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
331 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
332 node, rem_path = ShareNode.find(client, path, session[u"peer_jid"])
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 except (exceptions.PermissionError, exceptions.NotFound):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
334 #  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
335 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 except exceptions.DataError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
337 log.warning(_(u"invalid path: {path}").format(path=path))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
339
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 if node.type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 # 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
342 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 path = node[name].path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 elif node.type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 # 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
348 path = os.path.join(node.path, rem_path, name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
350 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
351 u"unknown type: {type}".format(type=node.type)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
352 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
353 if not os.path.exists(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
354 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
355 size = os.path.getsize(path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
356 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
357 # 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
358 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
359 name_data = client._XEP_0329_names_data[name]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
360 except KeyError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
361 return True, None
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
362
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
363 for path, shared_file in name_data.iteritems():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
364 if True: #  FIXME: filters are here
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 break
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
366 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
367 return True, None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
368 parent_node = shared_file[u"parent"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
369 if not parent_node.checkPermissions(client, session[u"peer_jid"]):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
370 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
371 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
372 u"{peer_jid} requested a file (s)he can't access [{profile}]"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
373 ).format(peer_jid=session[u"peer_jid"], profile=client.profile)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
374 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
375 return True, None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
376 size = shared_file[u"size"]
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
377
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
378 file_data[u"size"] = size
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
379 file_elt.addElement(u"size", content=unicode(size))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
380 hash_algo = file_data[u"hash_algo"] = self._h.getDefaultAlgo()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
381 hasher = file_data[u"hash_hasher"] = self._h.getHasher(hash_algo)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
382 file_elt.addChild(self._h.buildHashUsedElt(hash_algo))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
383 content_data["stream_object"] = stream.FileStreamObject(
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
384 self.host,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
385 client,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
386 path,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
387 uid=self._jf.getProgressId(session, content_name),
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
388 size=size,
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
389 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
390 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
391 return False, True
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
392
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
393 # common methods
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
394
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
395 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
396 iq_elt.handled = True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
397 owner = jid.JID(iq_elt["from"]).userhostJID()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
398 node = iq_elt.query.getAttribute("node")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
399 if not node:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
400 d = defer.maybeDeferred(root_nodes_cb, client, iq_elt, owner)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
401 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
402 d = defer.maybeDeferred(files_from_node_cb, client, iq_elt, owner, node)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
403 d.addErrback(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
404 lambda failure_: log.error(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
405 _(u"error while retrieving files: {msg}").format(msg=failure_)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
406 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
407 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
408
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
409 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
410 error_elt = jabber_error.StanzaError(condition).toResponse(iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
411 client.send(error_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
412
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
413 #  client
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
414
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
415 def _addPathData(self, client, query_elt, path, parent_node):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
416 """Fill query_elt with files/directories found in path"""
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
417 name = os.path.basename(path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
418 if os.path.isfile(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
419 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
420 mime_type = mimetypes.guess_type(path, strict=False)[0]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
421 file_elt = self._jf.buildFileElement(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
422 name=name, size=size, mime_type=mime_type, modified=os.path.getmtime(path)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
423 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
424
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 query_elt.addChild(file_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
426 # we don't specify hash as it would be too resource intensive to calculate it for all files
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
427 # 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
428 name_data = client._XEP_0329_names_data.setdefault(name, {})
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
429 if path not in name_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
430 name_data[path] = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
431 "size": size,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
432 "mime_type": mime_type,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
433 "parent": parent_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
434 }
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
435 else:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
436 # we have a directory
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
437 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
438 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
439
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
440 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
441 """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
442 path = os.path.join(node.path, path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
443
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
444 if not os.path.exists(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 # 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
446 return self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 elif os.path.isfile(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
448 self._addPathData(client, query_elt, path, node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
449 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
450 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
451 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
452 name = name.decode("utf-8", "strict")
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 except UnicodeDecodeError as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
454 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
455 _(u"ignoring invalid unicode name ({name}): {msg}").format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
456 name=name.decode("utf-8", "replace"), msg=e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
457 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
458 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
459 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
460 full_path = os.path.join(path, name)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
461 self._addPathData(client, query_elt, full_path, node)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
462
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 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
464 """Fill query_elt for virtual nodes"""
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 for name, child_node in node.iteritems():
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
466 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
467 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
468 node_type = child_node.type
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
469 if node_type == TYPE_VIRTUAL:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
470 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
471 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
472 elif node_type == TYPE_PATH:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 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
474 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
475 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
476 _(u"unexpected type: {type}").format(type=node_type)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
477 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
478
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 def _getRootNodesCb(self, client, iq_elt, owner):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
480 peer_jid = jid.JID(iq_elt["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
481 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
482 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
483 for name, node in client._XEP_0329_root_node.iteritems():
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
484 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
485 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
486 directory_elt = query_elt.addElement("directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
487 directory_elt["name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
488 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
489
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
490 def _getFilesFromNodeCb(self, client, iq_elt, owner, node_path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 """Main method to retrieve files/directories from a node_path"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
492 peer_jid = jid.JID(iq_elt[u"from"])
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
493 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
494 node, path = ShareNode.find(client, node_path, peer_jid)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
495 except (exceptions.PermissionError, exceptions.NotFound):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 return self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 except exceptions.DataError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
498 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
499
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 node_type = node.type
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
501 peer_jid = jid.JID(iq_elt["from"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
502 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
503 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
504 query_elt[u"node"] = node_path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
505
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
506 # we now fill query_elt according to node_type
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
507 if node_type == TYPE_PATH:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
508 #  it's a physical path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 self._pathNodeHandler(client, iq_elt, query_elt, node, path)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 elif node_type == TYPE_VIRTUAL:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
511 assert not path
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
512 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
513 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
514 raise exceptions.InternalError(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
515 _(u"unknown node type: {type}").format(type=node_type)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
516 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
517
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
518 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
519
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
520 def onRequest(self, iq_elt, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
521 return self._requestHandler(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
522 client, iq_elt, self._getRootNodesCb, self._getFilesFromNodeCb
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
523 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
524
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
525 # Component
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
526
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
527 @defer.inlineCallbacks
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
528 def _compGetRootNodesCb(self, client, iq_elt, owner):
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 files_data = yield self.host.memory.getFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
531 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
532 peer_jid=peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
533 parent=u"",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
534 type_=C.FILE_TYPE_DIRECTORY,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
535 owner=owner,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
536 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
537 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
538 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
539 for file_data in files_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
540 name = file_data[u"name"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
541 directory_elt = query_elt.addElement(u"directory")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
542 directory_elt[u"name"] = name
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
543 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
544
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
545 @defer.inlineCallbacks
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 def _compGetFilesFromNodeCb(self, client, iq_elt, owner, node_path):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
547 """retrieve files from local files repository according to permissions
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
548
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
549 result stanza is then built and sent to requestor
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
550 @trigger XEP-0329_compGetFilesFromNode(client, iq_elt, owner, node_path, files_data): can be used to add data/elements
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
551 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
552 peer_jid = jid.JID(iq_elt["from"])
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
553 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
554 files_data = yield self.host.memory.getFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
555 client, peer_jid=peer_jid, path=node_path, owner=owner
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
556 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
557 except exceptions.NotFound:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
558 self._iqError(client, iq_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
559 return
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
560 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
561 query_elt = iq_result_elt.addElement((NS_FIS, "query"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
562 query_elt[u"node"] = node_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
563 if not self.host.trigger.point(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
564 u"XEP-0329_compGetFilesFromNode", client, iq_elt, owner, node_path, files_data
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
565 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
566 return
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
567 for file_data in files_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
568 file_elt = self._jf.buildFileElementFromDict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
569 file_data, modified=file_data.get(u"modified", file_data[u"created"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
570 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
571 query_elt.addChild(file_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
572 client.send(iq_result_elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
573
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
574 def onComponentRequest(self, iq_elt, client):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
575 return self._requestHandler(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
576 client, iq_elt, self._compGetRootNodesCb, self._compGetFilesFromNodeCb
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
577 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
578
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
579 def _parseResult(self, iq_elt):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
580 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
581 files = []
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
582
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
583 for elt in query_elt.elements():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
584 if elt.name == "file":
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
585 # we have a file
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
586 try:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
587 file_data = self._jf.parseFileElement(elt)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
588 except exceptions.DataError:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
589 continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
590 file_data[u"type"] = C.FILE_TYPE_FILE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
591 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
592 # we have a directory
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
593
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
594 file_data = {"name": elt["name"], "type": C.FILE_TYPE_DIRECTORY}
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
596 log.warning(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
597 _(u"unexpected element, ignoring: {elt}").format(elt=elt.toXml())
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
598 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
599 continue
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
600 files.append(file_data)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
601 return files
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
602
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
603 # file methods #
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
604
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
605 def _serializeData(self, files_data):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 for file_data in files_data:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
607 for key, value in file_data.iteritems():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
608 file_data[key] = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
609 json.dumps(value) if key in ("extra",) else unicode(value)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
610 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
611 return files_data
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
612
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
613 def _listFiles(self, target_jid, path, extra, profile):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
614 client = self.host.getClient(profile)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
615 target_jid = client.jid.userhostJID() if not target_jid else jid.JID(target_jid)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
616 d = self.listFiles(client, target_jid, path or None)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
617 d.addCallback(self._serializeData)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
618 return d
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
619
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
620 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
621 """List file shared by an entity
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
622
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
623 @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
624 @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
625 None to get root directories
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
626 @param extra(dict, None): extra data
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
627 @return list(dict): shared files
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
628 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
629 iq_elt = client.IQ("get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
630 iq_elt["to"] = target_jid.full()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
631 query_elt = iq_elt.addElement((NS_FIS, "query"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
632 if path:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
633 query_elt["node"] = path
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
634 d = iq_elt.send()
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 d.addCallback(self._parseResult)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
636 return d
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
637
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
638 def _localSharesGet(self, profile):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
639 client = self.host.getClient(profile)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
640 return self.localSharesGet(client)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
641
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
642 def localSharesGet(self, client):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
643 return client._XEP_0329_root_node.getSharedPaths().keys()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
644
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 def _sharePath(self, name, path, access, profile):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
646 client = self.host.getClient(profile)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
647 access = json.loads(access)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
648 return self.sharePath(client, name or None, path, access)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
649
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
650 def sharePath(self, client, name, path, access):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
651 if client.is_component:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
652 raise exceptions.ClientTypeError
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
653 if not os.path.exists(path):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 raise ValueError(_(u"This path doesn't exist!"))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
655 if not path or not path.strip(u" /"):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
656 raise ValueError(_(u"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
657 if not isinstance(access, dict):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
658 raise ValueError(_(u"access must be a dict"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
659
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
660 node = client._XEP_0329_root_node
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
661 node_type = TYPE_PATH
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
662 if os.path.isfile(path):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
663 #  we have a single file, the workflow is diferrent as we store all single files in the same dir
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
664 node = node.getOrCreate(SINGLE_FILES_DIR)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
665
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
666 if not name:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
667 name = os.path.basename(path.rstrip(u" /"))
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
668 if not name:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
669 raise exceptions.InternalError(_(u"Can't find a proper name"))
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
670
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
671 if name in node or name == SINGLE_FILES_DIR:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
672 idx = 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
673 new_name = name + "_" + unicode(idx)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
674 while new_name in node:
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
675 idx += 1
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
676 new_name = name + "_" + unicode(idx)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
677 name = new_name
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
678 log.info(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
679 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
680 u"A directory with this name is already shared, renamed to {new_name} [{profile}]".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
681 new_name=new_name, profile=client.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
682 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
683 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
684 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
685
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
686 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
687 self.host.bridge.FISSharedPathNew(path, name, client.profile)
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
688 return name
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
689
2589
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
690 def _unsharePath(self, path, profile):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
691 client = self.host.getClient(profile)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
692 return self.unsharePath(client, path)
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
693
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
694 def unsharePath(self, client, path):
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
695 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
696 for node in nodes:
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
697 node.removeFromParent()
282d1314d574 plugin XEP-0329: new methods/signals to handle shares:
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
698 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
699
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
700
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
701 class XEP_0329_handler(xmlstream.XMPPHandler):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
702 implements(iwokkel.IDisco)
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
703
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
704 def __init__(self, plugin_parent):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
705 self.plugin_parent = plugin_parent
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
706 self.host = plugin_parent.host
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
707
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
708 def connectionInitialized(self):
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
709 if self.parent.is_component:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
710 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
711 IQ_FIS_REQUEST, self.plugin_parent.onComponentRequest, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
712 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
713 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
714 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
715 IQ_FIS_REQUEST, self.plugin_parent.onRequest, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
716 )
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
717
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
718 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
719 return [disco.DiscoFeature(NS_FIS)]
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
720
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2589
diff changeset
721 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2503
c0bec8bac2b5 XEP-0329: implementation of File Information Sharing:
Goffi <goffi@goffi.org>
parents:
diff changeset
722 return []