Reformatted code base using black.

This commit is contained in:
Goffi 2024-06-19 18:44:57 +02:00
commit 53fdaa9d09
129 changed files with 12604 additions and 8966 deletions

View file

@ -37,8 +37,10 @@ try:
from lxml import etree
except ImportError:
etree = None
log.warning("Missing module lxml, please download/install it from http://lxml.de/ ."
"Auto D-Bus discovery will be disabled")
log.warning(
"Missing module lxml, please download/install it from http://lxml.de/ ."
"Auto D-Bus discovery will be disabled"
)
try:
import txdbus
@ -59,23 +61,26 @@ MPRIS_PREFIX = "org.mpris.MediaPlayer2"
CMD_GO_BACK = "GoBack"
CMD_GO_FWD = "GoFW"
SEEK_OFFSET = 5 * 1000 * 1000
MPRIS_COMMANDS = ["org.mpris.MediaPlayer2.Player." + cmd for cmd in (
"Previous", CMD_GO_BACK, "PlayPause", CMD_GO_FWD, "Next")]
MPRIS_COMMANDS = [
"org.mpris.MediaPlayer2.Player." + cmd
for cmd in ("Previous", CMD_GO_BACK, "PlayPause", CMD_GO_FWD, "Next")
]
MPRIS_PATH = "/org/mpris/MediaPlayer2"
MPRIS_PROPERTIES = OrderedDict((
("org.mpris.MediaPlayer2", (
"Identity",
)),
("org.mpris.MediaPlayer2.Player", (
"Metadata",
"PlaybackStatus",
"Volume",
)),
))
MPRIS_PROPERTIES = OrderedDict(
(
("org.mpris.MediaPlayer2", ("Identity",)),
(
"org.mpris.MediaPlayer2.Player",
(
"Metadata",
"PlaybackStatus",
"Volume",
),
),
)
)
MPRIS_METADATA_KEY = "Metadata"
MPRIS_METADATA_MAP = OrderedDict((
("xesam:title", "Title"),
))
MPRIS_METADATA_MAP = OrderedDict((("xesam:title", "Title"),))
INTROSPECT_METHOD = "Introspect"
IGNORED_IFACES_START = (
@ -126,18 +131,20 @@ class AdHocDBus:
async def profile_connected(self, client):
if txdbus is not None:
if self.session_con is None:
self.session_con = await dbus_client.connect(reactor, 'session')
self.session_con = await dbus_client.connect(reactor, "session")
self.fd_object = await self.session_con.getRemoteObject(FD_NAME, FD_PATH)
self._c.add_ad_hoc_command(
client, self.local_media_cb, D_("Media Players"),
client,
self.local_media_cb,
D_("Media Players"),
node=NS_MEDIA_PLAYER,
timeout=60*60*6 # 6 hours timeout, to avoid breaking remote
# in the middle of a movie
timeout=60 * 60 * 6, # 6 hours timeout, to avoid breaking remote
# in the middle of a movie
)
async def _dbus_async_call(self, proxy, method, *args, **kwargs):
""" Call a DBus method asynchronously and return a deferred
"""Call a DBus method asynchronously and return a deferred
@param proxy: DBus object proxy, as returner by get_object
@param method: name of the method to call
@ -152,17 +159,19 @@ class AdHocDBus:
async def _dbus_get_property(self, proxy, interface, name):
return await self._dbus_async_call(
proxy, "Get", interface, name, interface="org.freedesktop.DBus.Properties")
proxy, "Get", interface, name, interface="org.freedesktop.DBus.Properties"
)
async def _dbus_list_names(self):
return await self.fd_object.callRemote("ListNames")
async def _dbus_introspect(self, proxy):
return await self._dbus_async_call(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE)
return await self._dbus_async_call(
proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE
)
def _accept_method(self, method):
""" Return True if we accept the method for a command
"""Return True if we accept the method for a command
@param method: etree.Element
@return: True if the method is acceptable
@ -181,9 +190,7 @@ class AdHocDBus:
for node in el.iterchildren("node", "interface"):
if node.tag == "node":
new_path = os.path.join(proxy.object_path, node.get("name"))
new_proxy = await self.session_con.getRemoteObject(
bus_name, new_path
)
new_proxy = await self.session_con.getRemoteObject(bus_name, new_path)
await self._introspect(methods, bus_name, new_proxy)
elif node.tag == "interface":
name = node.get("name")
@ -197,16 +204,40 @@ class AdHocDBus:
log.debug("method accepted: [%s]" % method_name)
methods.add((proxy.object_path, name, method_name))
def _ad_hoc_dbus_add_auto(self, prog_name, allowed_jids, allowed_groups, allowed_magics,
forbidden_jids, forbidden_groups, flags, profile_key):
def _ad_hoc_dbus_add_auto(
self,
prog_name,
allowed_jids,
allowed_groups,
allowed_magics,
forbidden_jids,
forbidden_groups,
flags,
profile_key,
):
client = self.host.get_client(profile_key)
return self.ad_hoc_dbus_add_auto(
client, prog_name, allowed_jids, allowed_groups, allowed_magics,
forbidden_jids, forbidden_groups, flags)
client,
prog_name,
allowed_jids,
allowed_groups,
allowed_magics,
forbidden_jids,
forbidden_groups,
flags,
)
async def ad_hoc_dbus_add_auto(self, client, prog_name, allowed_jids=None, allowed_groups=None,
allowed_magics=None, forbidden_jids=None, forbidden_groups=None,
flags=None):
async def ad_hoc_dbus_add_auto(
self,
client,
prog_name,
allowed_jids=None,
allowed_groups=None,
allowed_magics=None,
forbidden_jids=None,
forbidden_groups=None,
flags=None,
):
bus_names = await self._dbus_list_names()
bus_names = [bus_name for bus_name in bus_names if "." + prog_name in bus_name]
if not bus_names:
@ -241,9 +272,19 @@ class AdHocDBus:
return (str(bus_name), methods)
def _add_command(self, client, adhoc_name, bus_name, methods, allowed_jids=None,
allowed_groups=None, allowed_magics=None, forbidden_jids=None,
forbidden_groups=None, flags=None):
def _add_command(
self,
client,
adhoc_name,
bus_name,
methods,
allowed_jids=None,
allowed_groups=None,
allowed_magics=None,
forbidden_jids=None,
forbidden_groups=None,
flags=None,
):
if flags is None:
flags = set()
@ -331,9 +372,16 @@ class AdHocDBus:
- device name
- device label
"""
found_data = await defer.ensureDeferred(self.host.find_by_features(
client, [self.host.ns_map['commands']], service=False, roster=False,
own_jid=True, local_device=True))
found_data = await defer.ensureDeferred(
self.host.find_by_features(
client,
[self.host.ns_map["commands"]],
service=False,
roster=False,
own_jid=True,
local_device=True,
)
)
remotes = []
@ -344,39 +392,47 @@ class AdHocDBus:
for cmd in cmd_list:
if cmd.nodeIdentifier == NS_MEDIA_PLAYER:
try:
result_elt = await self._c.do(client, device_jid,
NS_MEDIA_PLAYER, timeout=5)
result_elt = await self._c.do(
client, device_jid, NS_MEDIA_PLAYER, timeout=5
)
command_elt = self._c.get_command_elt(result_elt)
form = data_form.findForm(command_elt, NS_MEDIA_PLAYER)
if form is None:
continue
mp_options = form.fields['media_player'].options
session_id = command_elt.getAttribute('sessionid')
mp_options = form.fields["media_player"].options
session_id = command_elt.getAttribute("sessionid")
if mp_options and session_id:
# we just want to discover player, so we cancel the
# session
self._c.do(client, device_jid, NS_MEDIA_PLAYER,
action=self._c.ACTION.CANCEL,
session_id=session_id)
self._c.do(
client,
device_jid,
NS_MEDIA_PLAYER,
action=self._c.ACTION.CANCEL,
session_id=session_id,
)
for opt in mp_options:
remotes.append((device_jid_s,
opt.value,
opt.label or opt.value))
remotes.append(
(device_jid_s, opt.value, opt.label or opt.value)
)
except Exception as e:
log.warning(_(
"Can't retrieve remote controllers on {device_jid}: "
"{reason}".format(device_jid=device_jid, reason=e)))
log.warning(
_(
"Can't retrieve remote controllers on {device_jid}: "
"{reason}".format(device_jid=device_jid, reason=e)
)
)
break
return remotes
async def do_mpris_command(self, proxy, command):
iface, command = command.rsplit(".", 1)
if command == CMD_GO_BACK:
command = 'Seek'
command = "Seek"
args = [-SEEK_OFFSET]
elif command == CMD_GO_FWD:
command = 'Seek'
command = "Seek"
args = [SEEK_OFFSET]
else:
args = []
@ -387,9 +443,7 @@ class AdHocDBus:
for mpris_key, name in MPRIS_METADATA_MAP.items():
if mpris_key in metadata:
value = str(metadata[mpris_key])
form.addField(data_form.Field(fieldType="fixed",
var=name,
value=value))
form.addField(data_form.Field(fieldType="fixed", var=name, value=value))
async def local_media_cb(self, client, command_elt, session_data, action, node):
assert txdbus is not None
@ -408,10 +462,11 @@ class AdHocDBus:
return (None, self._c.STATUS.COMPLETED, None, note)
options = []
status = self._c.STATUS.EXECUTING
form = data_form.Form("form", title=D_("Media Player Selection"),
formNamespace=NS_MEDIA_PLAYER)
form = data_form.Form(
"form", title=D_("Media Player Selection"), formNamespace=NS_MEDIA_PLAYER
)
for bus in bus_names:
player_name = bus[len(MPRIS_PREFIX)+1:]
player_name = bus[len(MPRIS_PREFIX) + 1 :]
if not player_name:
log.warning(_("Ignoring MPRIS bus without suffix"))
continue
@ -430,9 +485,12 @@ class AdHocDBus:
raise ValueError(_("missing media_player value"))
if not bus_name.startswith(MPRIS_PREFIX):
log.warning(_("Media player ad-hoc command trying to use non MPRIS bus. "
"Hack attempt? Refused bus: {bus_name}").format(
bus_name=bus_name))
log.warning(
_(
"Media player ad-hoc command trying to use non MPRIS bus. "
"Hack attempt? Refused bus: {bus_name}"
).format(bus_name=bus_name)
)
note = (self._c.NOTE.ERROR, D_("Invalid player name."))
return (None, self._c.STATUS.COMPLETED, None, note)
@ -453,29 +511,36 @@ class AdHocDBus:
# we construct the remote control form
form = data_form.Form("form", title=D_("Media Player Selection"))
form.addField(data_form.Field(fieldType="hidden",
var="media_player",
value=bus_name))
form.addField(
data_form.Field(fieldType="hidden", var="media_player", value=bus_name)
)
for iface, properties_names in MPRIS_PROPERTIES.items():
for name in properties_names:
try:
value = await self._dbus_get_property(proxy, iface, name)
except Exception as e:
log.warning(_("Can't retrieve attribute {name}: {reason}")
.format(name=name, reason=e))
log.warning(
_("Can't retrieve attribute {name}: {reason}").format(
name=name, reason=e
)
)
continue
if name == MPRIS_METADATA_KEY:
self.add_mpris_metadata(form, value)
else:
form.addField(data_form.Field(fieldType="fixed",
var=name,
value=str(value)))
form.addField(
data_form.Field(fieldType="fixed", var=name, value=str(value))
)
commands = [data_form.Option(c, c.rsplit(".", 1)[1]) for c in MPRIS_COMMANDS]
form.addField(data_form.Field(fieldType="list-single",
var="command",
options=commands,
required=True))
form.addField(
data_form.Field(
fieldType="list-single",
var="command",
options=commands,
required=True,
)
)
payload = form.toElement()
status = self._c.STATUS.EXECUTING

View file

@ -36,8 +36,7 @@ PLUGIN_INFO = {
C.PI_DEPENDENCIES: ["APP_MANAGER"],
C.PI_MAIN: "AppManagerDocker",
C.PI_HANDLER: "no",
C.PI_DESCRIPTION: _(
"""Applications Manager for Docker"""),
C.PI_DESCRIPTION: _("""Applications Manager for Docker"""),
}
@ -48,18 +47,19 @@ class AppManagerDocker(AppManagerBackend):
def __init__(self, host):
log.info(_("Docker App Manager initialization"))
try:
self.docker_compose_path = which('docker-compose')[0]
self.docker_compose_path = which("docker-compose")[0]
except IndexError:
raise exceptions.NotFound(
'"docker-compose" executable not found, Docker can\'t be used with '
'application manager')
"application manager"
)
super().__init__(host)
async def start(self, app_data: dict) -> None:
await self._am.start_common(app_data)
working_dir = app_data['_instance_dir_path']
working_dir = app_data["_instance_dir_path"]
try:
override = app_data['override']
override = app_data["override"]
except KeyError:
pass
else:
@ -75,7 +75,7 @@ class AppManagerDocker(AppManagerBackend):
)
async def stop(self, app_data: dict) -> None:
working_dir = app_data['_instance_dir_path']
working_dir = app_data["_instance_dir_path"]
await async_process.run(
self.docker_compose_path,
"down",
@ -83,17 +83,18 @@ class AppManagerDocker(AppManagerBackend):
)
async def compute_expose(self, app_data: dict) -> None:
working_dir = app_data['_instance_dir_path']
expose = app_data['expose']
ports = expose.get('ports', {})
working_dir = app_data["_instance_dir_path"]
expose = app_data["expose"]
ports = expose.get("ports", {})
for name, port_data in list(ports.items()):
try:
service = port_data['service']
private = port_data['private']
service = port_data["service"]
private = port_data["private"]
int(private)
except (KeyError, ValueError):
log.warning(
f"invalid value found for {name!r} port in {app_data['_file_path']}")
f"invalid value found for {name!r} port in {app_data['_file_path']}"
)
continue
exposed_port = await async_process.run(
self.docker_compose_path,
@ -104,11 +105,12 @@ class AppManagerDocker(AppManagerBackend):
)
exposed_port = exposed_port.decode().strip()
try:
addr, port = exposed_port.split(':')
addr, port = exposed_port.split(":")
int(port)
except ValueError:
log.warning(
f"invalid exposed port for {name}, ignoring: {exposed_port!r}")
f"invalid exposed port for {name}, ignoring: {exposed_port!r}"
)
del ports[name]
else:
ports[name] = exposed_port

View file

@ -377,9 +377,9 @@ class DokuwikiImport(object):
try:
media_repo = options["media_repo"]
if opt_upload_images:
options[
self._blog_import.OPT_UPLOAD_IMAGES
] = False # force using --no-images-upload
options[self._blog_import.OPT_UPLOAD_IMAGES] = (
False # force using --no-images-upload
)
info_msg = _(
"DokuWiki media files will be *downloaded* to {temp_dir} - to finish the import you have to upload them *manually* to {media_repo}"
)

File diff suppressed because it is too large Load diff

View file

@ -29,6 +29,7 @@ from libervia.backend.core.log import getLogger
log = getLogger(__name__)
NS_XMPP_JID_NODE_2_AP = "https://libervia.org/ap_gateway/xmpp_jid_node_2_ap_actor"
class APAdHocService:
"""Ad-Hoc commands for AP Gateway"""
@ -52,7 +53,7 @@ class APAdHocService:
command_elt: domish.Element,
session_data: dict,
action: str,
node: str
node: str,
):
try:
x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x"))
@ -63,18 +64,15 @@ class APAdHocService:
# root request
status = self._c.STATUS.EXECUTING
form = data_form.Form(
"form", title="XMPP JID/node to AP actor conversion",
formNamespace=NS_XMPP_JID_NODE_2_AP
"form",
title="XMPP JID/node to AP actor conversion",
formNamespace=NS_XMPP_JID_NODE_2_AP,
)
field = data_form.Field(
"text-single", "jid", required=True
)
field = data_form.Field("text-single", "jid", required=True)
form.addField(field)
field = data_form.Field(
"text-single", "node", required=False
)
field = data_form.Field("text-single", "node", required=False)
form.addField(field)
payload = form.toElement()

View file

@ -55,32 +55,71 @@ SIGN_HEADERS = {
("digest", "(request-target)"),
],
b"GET": ["host"],
b"POST": ["digest"]
b"POST": ["digest"],
}
PAGE_SIZE = 10
HS2019 = "hs2019"
# delay after which a signed request is not accepted anymore
SIGN_EXP = 12*60*60 # 12 hours (same value as for Mastodon)
SIGN_EXP = 12 * 60 * 60 # 12 hours (same value as for Mastodon)
LRU_MAX_SIZE = 200
ACTIVITY_TYPES = (
"Accept", "Add", "Announce", "Arrive", "Block", "Create", "Delete", "Dislike", "Flag",
"Follow", "Ignore", "Invite", "Join", "Leave", "Like", "Listen", "Move", "Offer",
"Question", "Reject", "Read", "Remove", "TentativeReject", "TentativeAccept",
"Travel", "Undo", "Update", "View",
"Accept",
"Add",
"Announce",
"Arrive",
"Block",
"Create",
"Delete",
"Dislike",
"Flag",
"Follow",
"Ignore",
"Invite",
"Join",
"Leave",
"Like",
"Listen",
"Move",
"Offer",
"Question",
"Reject",
"Read",
"Remove",
"TentativeReject",
"TentativeAccept",
"Travel",
"Undo",
"Update",
"View",
# non-standard activities
"EmojiReact"
"EmojiReact",
)
ACTIVITY_TYPES_LOWER = [a.lower() for a in ACTIVITY_TYPES]
ACTIVITY_OBJECT_MANDATORY = (
"Create", "Update", "Delete", "Follow", "Add", "Remove", "Like", "Block", "Undo"
"Create",
"Update",
"Delete",
"Follow",
"Add",
"Remove",
"Like",
"Block",
"Undo",
)
ACTIVITY_TARGET_MANDATORY = ("Add", "Remove")
# activities which can be used with Shared Inbox (i.e. with no account specified)
# must be lowercase
ACTIVIY_NO_ACCOUNT_ALLOWED = (
"create", "update", "delete", "announce", "undo", "like", "emojireact", "join",
"leave"
"create",
"update",
"delete",
"announce",
"undo",
"like",
"emojireact",
"join",
"leave",
)
# maximum number of parents to retrieve when comments_max_depth option is set
COMMENTS_MAX_PARENTS = 100

View file

@ -45,11 +45,12 @@ AP_EVENTS_CONTEXT = {
"addressCountry": "sc:addressCountry",
"addressLocality": "sc:addressLocality",
"addressRegion": "sc:addressRegion",
"anonymousParticipationEnabled": {"@id": "mz:anonymousParticipationEnabled",
"@type": "sc:Boolean"},
"anonymousParticipationEnabled": {
"@id": "mz:anonymousParticipationEnabled",
"@type": "sc:Boolean",
},
"category": "sc:category",
"commentsEnabled": {"@id": "pt:commentsEnabled",
"@type": "sc:Boolean"},
"commentsEnabled": {"@id": "pt:commentsEnabled", "@type": "sc:Boolean"},
"discoverable": "toot:discoverable",
"discussions": {"@id": "mz:discussions", "@type": "@id"},
"events": {"@id": "mz:events", "@type": "@id"},
@ -57,8 +58,7 @@ AP_EVENTS_CONTEXT = {
"inLanguage": "sc:inLanguage",
"isOnline": {"@id": "mz:isOnline", "@type": "sc:Boolean"},
"joinMode": {"@id": "mz:joinMode", "@type": "mz:joinModeType"},
"joinModeType": {"@id": "mz:joinModeType",
"@type": "rdfs:Class"},
"joinModeType": {"@id": "mz:joinModeType", "@type": "rdfs:Class"},
"location": {"@id": "sc:location", "@type": "sc:Place"},
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"maximumAttendeeCapacity": "sc:maximumAttendeeCapacity",
@ -66,19 +66,21 @@ AP_EVENTS_CONTEXT = {
"members": {"@id": "mz:members", "@type": "@id"},
"mz": "https://joinmobilizon.org/ns#",
"openness": {"@id": "mz:openness", "@type": "@id"},
"participantCount": {"@id": "mz:participantCount",
"@type": "sc:Integer"},
"participationMessage": {"@id": "mz:participationMessage",
"@type": "sc:Text"},
"participantCount": {"@id": "mz:participantCount", "@type": "sc:Integer"},
"participationMessage": {"@id": "mz:participationMessage", "@type": "sc:Text"},
"postalCode": "sc:postalCode",
"posts": {"@id": "mz:posts", "@type": "@id"},
"propertyID": "sc:propertyID",
"pt": "https://joinpeertube.org/ns#",
"remainingAttendeeCapacity": "sc:remainingAttendeeCapacity",
"repliesModerationOption": {"@id": "mz:repliesModerationOption",
"@type": "mz:repliesModerationOptionType"},
"repliesModerationOptionType": {"@id": "mz:repliesModerationOptionType",
"@type": "rdfs:Class"},
"repliesModerationOption": {
"@id": "mz:repliesModerationOption",
"@type": "mz:repliesModerationOptionType",
},
"repliesModerationOptionType": {
"@id": "mz:repliesModerationOptionType",
"@type": "rdfs:Class",
},
"resources": {"@id": "mz:resources", "@type": "@id"},
"sc": "http://schema.org#",
"streetAddress": "sc:streetAddress",
@ -86,7 +88,7 @@ AP_EVENTS_CONTEXT = {
"todos": {"@id": "mz:todos", "@type": "@id"},
"toot": "http://joinmastodon.org/ns#",
"uuid": "sc:identifier",
"value": "sc:value"
"value": "sc:value",
}
@ -99,7 +101,7 @@ class APEvents:
self._events = self.host.plugins["XEP-0471"]
async def event_data_2_ap_item(
self, event_data: dict, author_jid: jid.JID, is_new: bool=True
self, event_data: dict, author_jid: jid.JID, is_new: bool = True
) -> dict:
"""Convert event data to AP activity
@ -114,8 +116,7 @@ class APEvents:
if not event_data.get("id"):
event_data["id"] = shortuuid.uuid()
ap_account = await self.apg.get_ap_account_from_jid_and_node(
author_jid,
self._events.namespace
author_jid, self._events.namespace
)
url_actor = self.apg.build_apurl(TYPE_ACTOR, ap_account)
url_item = self.apg.build_apurl(TYPE_ITEM, ap_account, event_data["id"])
@ -141,12 +142,14 @@ class APEvents:
pass
else:
media_type = mimetypes.guess_type(head_picture_url, False)[0] or "image/jpeg"
attachment.append({
"name": "Banner",
"type": "Document",
"mediaType": media_type,
"url": head_picture_url,
})
attachment.append(
{
"name": "Banner",
"type": "Document",
"mediaType": media_type,
"url": head_picture_url,
}
)
descriptions = event_data.get("descriptions")
if descriptions:
@ -162,10 +165,12 @@ class APEvents:
if categories:
tag = ap_object["tag"] = []
for category in categories:
tag.append({
"name": f"#{category['term']}",
"type": "Hashtag",
})
tag.append(
{
"name": f"#{category['term']}",
"type": "Hashtag",
}
)
locations = event_data.get("locations")
if locations:
@ -203,12 +208,14 @@ class APEvents:
website = extra.get("website")
if website:
attachment.append({
"href": website,
"mediaType": "text/html",
"name": "Website",
"type": "Link"
})
attachment.append(
{
"href": website,
"mediaType": "text/html",
"name": "Website",
"type": "Link",
}
)
accessibility = extra.get("accessibility")
if accessibility:
@ -224,11 +231,13 @@ class APEvents:
log.error(f"unexpected wheelchair value: {wheelchair}")
ap_wc_value = None
if ap_wc_value is not None:
attachment.append({
"propertyID": "mz:accessibility:wheelchairAccessible",
"type": "PropertyValue",
"value": ap_wc_value
})
attachment.append(
{
"propertyID": "mz:accessibility:wheelchairAccessible",
"type": "PropertyValue",
"value": ap_wc_value,
}
)
activity = self.apg.create_activity(
"Create" if is_new else "Update", url_actor, ap_object, activity_id=url_item
@ -236,11 +245,7 @@ class APEvents:
activity["@context"].append(AP_EVENTS_CONTEXT)
return activity
async def ap_item_2_event_data(
self,
requestor_actor_id: str,
ap_item: dict
) -> dict:
async def ap_item_2_event_data(self, requestor_actor_id: str, ap_item: dict) -> dict:
"""Convert AP activity or object to event data
@param requestor_actor_id: ID of the actor doing the request.
@ -251,7 +256,9 @@ class APEvents:
"""
is_activity = self.apg.is_activity(ap_item)
if is_activity:
ap_object = await self.apg.ap_get_object(requestor_actor_id, ap_item, "object")
ap_object = await self.apg.ap_get_object(
requestor_actor_id, ap_item, "object"
)
if not ap_object:
log.warning(f'No "object" found in AP item {ap_item!r}')
raise exceptions.DataError
@ -311,10 +318,7 @@ class APEvents:
# description
content = ap_object.get("content")
if content:
event_data["descriptions"] = [{
"type": "xhtml",
"description": content
}]
event_data["descriptions"] = [{"type": "xhtml", "description": content}]
# categories
tags = ap_object.get("tag")
@ -329,7 +333,7 @@ class APEvents:
continue
categories.append({"term": term})
#location
# location
ap_location = ap_object.get("location")
if ap_location:
location = {}
@ -361,24 +365,24 @@ class APEvents:
# a simple "yes"/"no" form.
rsvp_data = {"fields": []}
event_data["rsvp"] = [rsvp_data]
rsvp_data["fields"].append({
"type": "list-single",
"name": "attending",
"label": "Attending",
"options": [
{"label": "yes", "value": "yes"},
{"label": "no", "value": "no"}
],
"required": True
})
rsvp_data["fields"].append(
{
"type": "list-single",
"name": "attending",
"label": "Attending",
"options": [
{"label": "yes", "value": "yes"},
{"label": "no", "value": "no"},
],
"required": True,
}
)
# comments
if ap_object.get("commentsEnabled"):
__, comments_node = await self.apg.get_comments_nodes(
requestor_actor_id,
object_id,
None
requestor_actor_id, object_id, None
)
event_data["comments"] = {
"service": author_jid,
@ -399,15 +403,10 @@ class APEvents:
return event_data
async def ap_item_2_event_data_and_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> tuple[dict, domish.Element]:
"""Convert AP item to parsed event data and corresponding item element"""
event_data = await self.ap_item_2_event_data(
requestor_actor_id,
ap_item
)
event_data = await self.ap_item_2_event_data(requestor_actor_id, ap_item)
event_elt = self._events.event_data_2_event_elt(event_data)
item_elt = domish.Element((None, "item"))
item_elt["id"] = event_data["id"]
@ -415,13 +414,10 @@ class APEvents:
return event_data, item_elt
async def ap_item_2_event_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert AP item to XMPP item element"""
__, item_elt = await self.ap_item_2_event_data_and_elt(
requestor_actor_id,
ap_item
requestor_actor_id, ap_item
)
return item_elt

View file

@ -41,11 +41,29 @@ from libervia.backend.tools.common import date_utils, uri
from libervia.backend.memory.sqla_mapping import SubscriptionState
from .constants import (
NS_AP, MEDIA_TYPE_AP, MEDIA_TYPE_AP_ALT, CONTENT_TYPE_WEBFINGER, CONTENT_TYPE_AP,
TYPE_ACTOR, TYPE_INBOX, TYPE_SHARED_INBOX, TYPE_OUTBOX, TYPE_EVENT, AP_REQUEST_TYPES,
PAGE_SIZE, ACTIVITY_TYPES_LOWER, ACTIVIY_NO_ACCOUNT_ALLOWED, SIGN_HEADERS, HS2019,
SIGN_EXP, TYPE_FOLLOWERS, TYPE_FOLLOWING, TYPE_ITEM, TYPE_LIKE, TYPE_REACTION,
ST_AP_CACHE
NS_AP,
MEDIA_TYPE_AP,
MEDIA_TYPE_AP_ALT,
CONTENT_TYPE_WEBFINGER,
CONTENT_TYPE_AP,
TYPE_ACTOR,
TYPE_INBOX,
TYPE_SHARED_INBOX,
TYPE_OUTBOX,
TYPE_EVENT,
AP_REQUEST_TYPES,
PAGE_SIZE,
ACTIVITY_TYPES_LOWER,
ACTIVIY_NO_ACCOUNT_ALLOWED,
SIGN_HEADERS,
HS2019,
SIGN_EXP,
TYPE_FOLLOWERS,
TYPE_FOLLOWING,
TYPE_ITEM,
TYPE_LIKE,
TYPE_REACTION,
ST_AP_CACHE,
)
from .regex import RE_SIG_PARAM
@ -53,13 +71,13 @@ from .regex import RE_SIG_PARAM
log = getLogger(__name__)
VERSION = unicodedata.normalize(
'NFKD',
f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}"
"NFKD", f"{C.APP_NAME} ActivityPub Gateway {C.APP_VERSION}"
)
class HTTPAPGServer(web_resource.Resource):
"""HTTP Server handling ActivityPub S2S protocol"""
isLeaf = True
def __init__(self, ap_gateway):
@ -68,30 +86,23 @@ class HTTPAPGServer(web_resource.Resource):
super().__init__()
def response_code(
self,
request: "HTTPRequest",
http_code: int,
msg: Optional[str] = None
self, request: "HTTPRequest", http_code: int, msg: Optional[str] = None
) -> None:
"""Log and set HTTP return code and associated message"""
if msg is not None:
log.warning(msg)
request.setResponseCode(http_code, None if msg is None else msg.encode())
def _on_request_error(self, failure_: failure.Failure, request: "HTTPRequest") -> None:
def _on_request_error(
self, failure_: failure.Failure, request: "HTTPRequest"
) -> None:
exc = failure_.value
if isinstance(exc, exceptions.NotFound):
self.response_code(
request,
http.NOT_FOUND,
str(exc)
)
self.response_code(request, http.NOT_FOUND, str(exc))
else:
log.exception(f"Internal error: {failure_.value}")
self.response_code(
request,
http.INTERNAL_SERVER_ERROR,
f"internal error: {failure_.value}"
request, http.INTERNAL_SERVER_ERROR, f"internal error: {failure_.value}"
)
request.finish()
raise failure_
@ -105,7 +116,7 @@ class HTTPAPGServer(web_resource.Resource):
account = resource[5:].strip()
if not resource.startswith("acct:") or not account:
return web_resource.ErrorPage(
http.BAD_REQUEST, "Bad Request" , "Invalid webfinger resource"
http.BAD_REQUEST, "Bad Request", "Invalid webfinger resource"
).render(request)
actor_url = self.apg.build_apurl(TYPE_ACTOR, account)
@ -114,12 +125,8 @@ class HTTPAPGServer(web_resource.Resource):
"aliases": [actor_url],
"subject": resource,
"links": [
{
"rel": "self",
"type": "application/activity+json",
"href": actor_url
}
]
{"rel": "self", "type": "application/activity+json", "href": actor_url}
],
}
request.setHeader("content-type", CONTENT_TYPE_WEBFINGER)
request.write(json.dumps(resp).encode())
@ -134,7 +141,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -181,9 +188,11 @@ class HTTPAPGServer(web_resource.Resource):
elif type_ == TYPE_LIKE:
await self.handle_attachment_item(client, obj, {"noticed": False})
elif type_ == TYPE_REACTION:
await self.handle_attachment_item(client, obj, {
"reactions": {"operation": "update", "remove": [obj["content"]]}
})
await self.handle_attachment_item(
client,
obj,
{"reactions": {"operation": "update", "remove": [obj["content"]]}},
)
else:
log.warning(f"Unmanaged undo type: {type_!r}")
@ -196,7 +205,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -207,7 +216,7 @@ class HTTPAPGServer(web_resource.Resource):
account_jid,
node,
# subscriptions from AP are always public
options=self.apg._pps.set_public_opt()
options=self.apg._pps.set_public_opt(),
)
except pubsub.SubscriptionPending:
log.info(f"subscription to node {node!r} of {account_jid} is pending")
@ -218,9 +227,7 @@ class HTTPAPGServer(web_resource.Resource):
raise exceptions.InternalError('"subscribed" state was expected')
inbox = await self.apg.get_ap_inbox_from_id(signing_actor, use_shared=False)
actor_id = self.apg.build_apurl(TYPE_ACTOR, ap_account)
accept_data = self.apg.create_activity(
"Accept", actor_id, object_=data
)
accept_data = self.apg.create_activity("Accept", actor_id, object_=data)
await self.apg.sign_and_post(inbox, actor_id, accept_data)
await self.apg._c.synchronise(client, account_jid, node, resync=False)
@ -233,7 +240,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
if node is None:
node = self.apg._m.namespace
@ -253,7 +260,9 @@ class HTTPAPGServer(web_resource.Resource):
continue
try:
sub = next(
s for s in follow_node.subscriptions if s.subscriber==account_jid
s
for s in follow_node.subscriptions
if s.subscriber == account_jid
)
except StopIteration:
log.warning(
@ -283,7 +292,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
if node is None:
node = self.apg._m.namespace
@ -325,13 +334,9 @@ class HTTPAPGServer(web_resource.Resource):
if repeated:
# we don't check sender when item is repeated, as it should be different
# from post author in this case
sender_jid = await self.apg.get_jid_from_id(
requestor_actor_id,
sender
)
sender_jid = await self.apg.get_jid_from_id(requestor_actor_id, sender)
repeater_jid = await self.apg.get_jid_from_id(
requestor_actor_id,
signing_actor
requestor_actor_id, signing_actor
)
repeated_item_id = obj["id"]
if self.apg.is_local_url(repeated_item_id):
@ -347,13 +352,14 @@ class HTTPAPGServer(web_resource.Resource):
if not url_account or not url_item_id:
raise ValueError
except (RuntimeError, ValueError):
raise exceptions.DataError(
"local URI is invalid: {repeated_id}"
)
raise exceptions.DataError("local URI is invalid: {repeated_id}")
else:
url_jid, url_node = await self.apg.get_jid_and_node(url_account)
if ((url_jid != sender_jid
or url_node and url_node != self.apg._m.namespace)):
if (
url_jid != sender_jid
or url_node
and url_node != self.apg._m.namespace
):
raise exceptions.DataError(
"announced ID doesn't match sender ({sender}): "
f"[repeated_item_id]"
@ -368,17 +374,15 @@ class HTTPAPGServer(web_resource.Resource):
"pubsub",
path=sender_jid.full(),
node=self.apg._m.namespace,
item=repeated_item_id
)
item=repeated_item_id,
),
}
# we must use activity's id and targets, not the original item ones
for field in ("id", "to", "bto", "cc", "bcc"):
obj[field] = data.get(field)
else:
if sender != signing_actor:
log.warning(
"Ignoring object not attributed to signing actor: {obj}"
)
log.warning("Ignoring object not attributed to signing actor: {obj}")
continue
await self.apg.new_ap_item(client, account_jid, node, obj)
@ -392,7 +396,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
await self.handle_new_ap_items(
requestor_actor_id, request, data, account_jid, node, signing_actor
@ -407,7 +411,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
# Update is the same as create: the item ID stays the same, thus the item will be
# overwritten
@ -424,7 +428,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
):
# we create a new item
await self.handle_new_ap_items(
@ -434,14 +438,11 @@ class HTTPAPGServer(web_resource.Resource):
account_jid,
node,
signing_actor,
repeated=True
repeated=True,
)
async def handle_attachment_item(
self,
client: SatXMPPEntity,
data: dict,
attachment_data: dict
self, client: SatXMPPEntity, data: dict, attachment_data: dict
) -> None:
target_ids = data.get("object")
if not target_ids:
@ -485,11 +486,7 @@ class HTTPAPGServer(web_resource.Resource):
author_jid, item_node, item_id
)
cached_node = await self.apg.host.memory.storage.get_pubsub_node(
client,
author_jid,
attachment_node,
with_subscriptions=True,
create=True
client, author_jid, attachment_node, with_subscriptions=True, create=True
)
found_items, __ = await self.apg.host.memory.storage.get_items(
cached_node, item_ids=[client.jid.userhost()]
@ -501,19 +498,13 @@ class HTTPAPGServer(web_resource.Resource):
old_item_elt = found_item.data
item_elt = await self.apg._pa.apply_set_handler(
client,
{"extra": attachment_data},
old_item_elt,
None
client, {"extra": attachment_data}, old_item_elt, None
)
# we reparse the element, as there can be other attachments
attachments_data = self.apg._pa.items_2_attachment_data(client, [item_elt])
# and we update the cache
await self.apg.host.memory.storage.cache_pubsub_items(
client,
cached_node,
[item_elt],
attachments_data or [{}]
client, cached_node, [item_elt], attachments_data or [{}]
)
if self.apg.is_virtual_jid(author_jid):
@ -525,7 +516,7 @@ class HTTPAPGServer(web_resource.Resource):
self.apg.pubsub_service.notifyPublish(
author_jid,
attachment_node,
[(subscription.subscriber, None, [item_elt])]
[(subscription.subscriber, None, [item_elt])],
)
else:
# the attachment is on an XMPP item, we publish it to the attachment node
@ -542,7 +533,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"noticed": True})
@ -556,12 +547,12 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {
"reactions": {"operation": "update", "add": [data["content"]]}
})
await self.handle_attachment_item(
client, data, {"reactions": {"operation": "update", "add": [data["content"]]}}
)
async def handle_join_activity(
self,
@ -572,7 +563,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"rsvp": {"attending": "yes"}})
@ -586,7 +577,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: str
signing_actor: str,
) -> None:
client = await self.apg.get_virtual_client(requestor_actor_id, signing_actor)
await self.handle_attachment_item(client, data, {"rsvp": {"attending": "no"}})
@ -599,7 +590,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
inbox = self.apg.build_apurl(TYPE_INBOX, ap_account)
shared_inbox = self.apg.build_apurl(TYPE_SHARED_INBOX)
@ -623,9 +614,8 @@ class HTTPAPGServer(web_resource.Resource):
actor_data = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1"
"https://w3id.org/security/v1",
],
# XXX: Mastodon doesn't like percent-encode arobas, so we have to unescape it
# if it is escaped
"id": ap_url.replace("%40", "@"),
@ -639,7 +629,7 @@ class HTTPAPGServer(web_resource.Resource):
"publicKey": {
"id": f"{ap_url}#main-key",
"owner": ap_url,
"publicKeyPem": self.apg.public_key_pem
"publicKeyPem": self.apg.public_key_pem,
},
"endpoints": {
"sharedInbox": shared_inbox,
@ -664,7 +654,7 @@ class HTTPAPGServer(web_resource.Resource):
actor_data["icon"] = {
"type": "Image",
"url": avatar_url,
"mediaType": media_type
"mediaType": media_type,
}
return actor_data
@ -672,13 +662,12 @@ class HTTPAPGServer(web_resource.Resource):
def get_canonical_url(self, request: "HTTPRequest") -> str:
return parse.urljoin(
f"https://{self.apg.public_url}",
request.path.decode().rstrip("/")
# we unescape "@" for the same reason as in [ap_actor_request]
request.path.decode().rstrip("/"),
# we unescape "@" for the same reason as in [ap_actor_request]
).replace("%40", "@")
def query_data_2_rsm_request(
self,
query_data: Dict[str, List[str]]
self, query_data: Dict[str, List[str]]
) -> rsm.RSMRequest:
"""Get RSM kwargs to use with RSMRequest from query data"""
page = query_data.get("page")
@ -690,7 +679,7 @@ class HTTPAPGServer(web_resource.Resource):
else:
for query_key in ("index", "before", "after"):
try:
kwargs={query_key: query_data[query_key][0], "max_": PAGE_SIZE}
kwargs = {query_key: query_data[query_key][0], "max_": PAGE_SIZE}
except (KeyError, IndexError, ValueError):
pass
else:
@ -705,7 +694,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
query_data: Dict[str, List[str]]
query_data: Dict[str, List[str]],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -719,7 +708,7 @@ class HTTPAPGServer(web_resource.Resource):
service=account_jid,
node=node,
rsm_request=self.query_data_2_rsm_request(query_data),
extra = {C.KEY_USE_CACHE: False}
extra={C.KEY_USE_CACHE: False},
)
except error.StanzaError as e:
log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}")
@ -730,8 +719,7 @@ class HTTPAPGServer(web_resource.Resource):
if node and node.startswith(self.apg._events.namespace):
ordered_items = [
await self.apg.ap_events.event_data_2_ap_item(
self.apg._events.event_elt_2_event_data(item),
account_jid
self.apg._events.event_elt_2_event_data(item), account_jid
)
for item in reversed(items)
]
@ -740,11 +728,8 @@ class HTTPAPGServer(web_resource.Resource):
await self.apg.mb_data_2_ap_item(
self.apg.client,
await self.apg._m.item_2_mb_data(
self.apg.client,
item,
account_jid,
node
)
self.apg.client, item, account_jid, node
),
)
for item in reversed(items)
]
@ -753,7 +738,7 @@ class HTTPAPGServer(web_resource.Resource):
"id": url,
"type": "OrderedCollectionPage",
"partOf": base_url,
"orderedItems": ordered_items
"orderedItems": ordered_items,
}
if "rsm" not in metadata:
@ -764,13 +749,13 @@ class HTTPAPGServer(web_resource.Resource):
# of what we get with RSM (at least with Libervia Pubsub)
if not metadata["complete"]:
try:
last= metadata["rsm"]["last"]
last = metadata["rsm"]["last"]
except KeyError:
last = None
ret_data["prev"] = f"{base_url}?{parse.urlencode({'after': last})}"
if metadata["rsm"]["index"] != 0:
try:
first= metadata["rsm"]["first"]
first = metadata["rsm"]["first"]
except KeyError:
first = None
ret_data["next"] = f"{base_url}?{parse.urlencode({'before': first})}"
@ -785,7 +770,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: str,
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -809,7 +794,7 @@ class HTTPAPGServer(web_resource.Resource):
node=node,
max_items=0,
rsm_request=rsm.RSMRequest(max_=0),
extra = {C.KEY_USE_CACHE: False}
extra={C.KEY_USE_CACHE: False},
)
except error.StanzaError as e:
log.warning(f"Can't get data from pubsub node {node} at {account_jid}: {e}")
@ -844,7 +829,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> None:
assert data is not None
if signing_actor is None:
@ -855,14 +840,14 @@ class HTTPAPGServer(web_resource.Resource):
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"request is not an activity, ignoring"
f"request is not an activity, ignoring",
)
if account_jid is None and activity_type not in ACTIVIY_NO_ACCOUNT_ALLOWED:
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"{activity_type.title()!r} activity must target an account"
f"{activity_type.title()!r} activity must target an account",
)
try:
@ -871,12 +856,18 @@ class HTTPAPGServer(web_resource.Resource):
return self.response_code(
request,
http.UNSUPPORTED_MEDIA_TYPE,
f"{activity_type.title()} activity is not yet supported"
f"{activity_type.title()} activity is not yet supported",
)
else:
await method(
requestor_actor_id, request, data, account_jid, node, ap_account, ap_url,
signing_actor
requestor_actor_id,
request,
data,
account_jid,
node,
ap_account,
ap_url,
signing_actor,
)
async def ap_followers_request(
@ -887,7 +878,7 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict:
if node is None:
node = self.apg._m.namespace
@ -902,20 +893,22 @@ class HTTPAPGServer(web_resource.Resource):
ap_account = self.apg._e.unescape(subscriber.user)
else:
# regular XMPP user
ap_account = await self.apg.get_ap_account_from_jid_and_node(subscriber, node)
ap_account = await self.apg.get_ap_account_from_jid_and_node(
subscriber, node
)
followers.append(ap_account)
url = self.get_canonical_url(request)
return {
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"totalItems": len(subscribers),
"first": {
"type": "OrderedCollectionPage",
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"orderedItems": followers
}
"totalItems": len(subscribers),
"first": {
"type": "OrderedCollectionPage",
"id": url,
"orderedItems": followers,
},
}
async def ap_following_request(
@ -926,12 +919,10 @@ class HTTPAPGServer(web_resource.Resource):
node: Optional[str],
ap_account: Optional[str],
ap_url: str,
signing_actor: Optional[str]
signing_actor: Optional[str],
) -> dict[str, Any]:
client = self.apg.client
subscriptions = await self.apg._pps.subscriptions(
client, account_jid, node
)
subscriptions = await self.apg._pps.subscriptions(client, account_jid, node)
following = []
for sub_dict in subscriptions:
service = jid.JID(sub_dict["service"])
@ -947,15 +938,15 @@ class HTTPAPGServer(web_resource.Resource):
url = self.get_canonical_url(request)
return {
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"totalItems": len(subscriptions),
"first": {
"type": "OrderedCollectionPage",
"@context": ["https://www.w3.org/ns/activitystreams"],
"type": "OrderedCollection",
"id": url,
"orderedItems": following
}
"totalItems": len(subscriptions),
"first": {
"type": "OrderedCollectionPage",
"id": url,
"orderedItems": following,
},
}
def _get_to_log(
@ -965,24 +956,23 @@ class HTTPAPGServer(web_resource.Resource):
) -> List[str]:
"""Get base data to logs in verbose mode"""
from pprint import pformat
to_log = [
"",
f"<<< got {request.method.decode()} request - {request.uri.decode()}"
f"<<< got {request.method.decode()} request - {request.uri.decode()}",
]
if data is not None:
to_log.append(pformat(data))
if self.apg.verbose>=3:
if self.apg.verbose >= 3:
headers = "\n".join(
f" {k.decode()}: {v.decode()}"
for k,v in request.getAllHeaders().items()
for k, v in request.getAllHeaders().items()
)
to_log.append(f" headers:\n{headers}")
return to_log
def get_requestor_actor_id(
self,
data: dict|None = None,
uri_extra_args: list[str]|None = None
self, data: dict | None = None, uri_extra_args: list[str] | None = None
) -> str:
"""Find the actor ID of the requestor.
@ -1034,33 +1024,30 @@ class HTTPAPGServer(web_resource.Resource):
# Still nothing, we'll have to use a generic actor.
log.warning(
"Can't find destinee in \"to\" field, using generic requestor for signature."
)
return self.apg.build_apurl(
TYPE_ACTOR, f"libervia@{self.apg.public_url}"
'Can\'t find destinee in "to" field, using generic requestor for signature.'
)
return self.apg.build_apurl(TYPE_ACTOR, f"libervia@{self.apg.public_url}")
async def ap_request(
self,
request: "HTTPRequest",
data: dict|None = None,
signing_actor: str|None = None,
requestor_actor_id: str|None = None,
data: dict | None = None,
signing_actor: str | None = None,
requestor_actor_id: str | None = None,
) -> None:
if self.apg.verbose:
to_log = self._get_to_log(request, data)
path = request.path.decode()
ap_url = parse.urljoin(
f"https://{self.apg.public_url}",
path
)
ap_url = parse.urljoin(f"https://{self.apg.public_url}", path)
request_type, extra_args = self.apg.parse_apurl(ap_url)
header_accept = request.getHeader("accept") or ""
if ((MEDIA_TYPE_AP not in header_accept
and MEDIA_TYPE_AP_ALT not in header_accept
and request_type in self.apg.html_redirect)):
if (
MEDIA_TYPE_AP not in header_accept
and MEDIA_TYPE_AP_ALT not in header_accept
and request_type in self.apg.html_redirect
):
# this is not a AP request, and we have a redirections for it
kw = {}
if extra_args:
@ -1081,14 +1068,14 @@ class HTTPAPGServer(web_resource.Resource):
if not filters:
break
# if we have filter, they must all match
elif all(v in kw[k] for k,v in filters.items()):
elif all(v in kw[k] for k, v in filters.items()):
break
else:
# no redirection is matching
redirection = None
if redirection is not None:
kw = {k: parse.quote(str(v), safe="") for k,v in kw.items()}
kw = {k: parse.quote(str(v), safe="") for k, v in kw.items()}
target_url = redirection["url"].format(**kw)
content = web_util.redirectTo(target_url.encode(), request)
request.write(content)
@ -1096,9 +1083,7 @@ class HTTPAPGServer(web_resource.Resource):
return
if requestor_actor_id is None:
requestor_actor_id = self.get_requestor_actor_id(
data, extra_args
)
requestor_actor_id = self.get_requestor_actor_id(data, extra_args)
if len(extra_args) == 0:
if request_type != "shared_inbox":
raise exceptions.DataError(f"Invalid request type: {request_type!r}")
@ -1121,21 +1106,29 @@ class HTTPAPGServer(web_resource.Resource):
ap_account = extra_args[0]
account_jid, node = await self.apg.get_jid_and_node(ap_account)
if request_type not in AP_REQUEST_TYPES.get(
request.method.decode().upper(), []
request.method.decode().upper(), []
):
raise exceptions.DataError(f"Invalid request type: {request_type!r}")
method = getattr(self, f"ap_{request_type}_request")
ret_data = await method(
requestor_actor_id, request, data, account_jid, node, ap_account, ap_url, signing_actor
requestor_actor_id,
request,
data,
account_jid,
node,
ap_account,
ap_url,
signing_actor,
)
if ret_data is not None:
request.setHeader("content-type", CONTENT_TYPE_AP)
request.write(json.dumps(ret_data).encode())
if self.apg.verbose:
to_log.append(f"--- RET (code: {request.code})---")
if self.apg.verbose>=2:
if self.apg.verbose >= 2:
if ret_data is not None:
from pprint import pformat
to_log.append(f"{pformat(ret_data)}")
to_log.append("---")
log.info("\n".join(to_log))
@ -1149,15 +1142,13 @@ class HTTPAPGServer(web_resource.Resource):
self.response_code(
request,
http.BAD_REQUEST,
f"invalid body, was expecting a JSON object"
f"invalid body, was expecting a JSON object",
)
request.finish()
return
except (json.JSONDecodeError, ValueError) as e:
self.response_code(
request,
http.BAD_REQUEST,
f"invalid json in inbox request: {e}"
request, http.BAD_REQUEST, f"invalid json in inbox request: {e}"
)
request.finish()
return
@ -1185,18 +1176,12 @@ class HTTPAPGServer(web_resource.Resource):
to_log.append(f" body: {request.content.read()!r}")
request.content.seek(0)
log.info("\n".join(to_log))
self.response_code(
request,
http.FORBIDDEN,
f"invalid signature: {e}"
)
self.response_code(request, http.FORBIDDEN, f"invalid signature: {e}")
request.finish()
return
except Exception as e:
self.response_code(
request,
http.INTERNAL_SERVER_ERROR,
f"Can't check signature: {e}"
request, http.INTERNAL_SERVER_ERROR, f"Can't check signature: {e}"
)
request.finish()
return
@ -1219,10 +1204,7 @@ class HTTPAPGServer(web_resource.Resource):
self._on_request_error(failure.Failure(e), request)
async def check_signing_actor(
self,
requestor_actor_id: str,
data: dict,
signing_actor: str
self, requestor_actor_id: str, data: dict, signing_actor: str
) -> None:
"""That that signing actor correspond to actor declared in data
@ -1241,9 +1223,7 @@ class HTTPAPGServer(web_resource.Resource):
)
async def check_signature(
self,
requestor_actor_id: str,
request: "HTTPRequest"
self, requestor_actor_id: str, request: "HTTPRequest"
) -> str:
"""Check and validate HTTP signature
@ -1264,10 +1244,11 @@ class HTTPAPGServer(web_resource.Resource):
except KeyError:
raise exceptions.EncryptionError('"keyId" is missing from signature')
algorithm = sign_data.get("algorithm", HS2019)
signed_headers = sign_data.get(
"headers",
"(created)" if algorithm==HS2019 else "date"
).lower().split()
signed_headers = (
sign_data.get("headers", "(created)" if algorithm == HS2019 else "date")
.lower()
.split()
)
try:
headers_to_check = SIGN_HEADERS[None] + SIGN_HEADERS[request.method]
except KeyError:
@ -1284,9 +1265,7 @@ class HTTPAPGServer(web_resource.Resource):
f"at least one of following header must be signed: {header}"
)
elif header not in signed_headers:
raise exceptions.EncryptionError(
f"the {header!r} header must be signed"
)
raise exceptions.EncryptionError(f"the {header!r} header must be signed")
body = request.content.read()
request.content.seek(0)
@ -1329,7 +1308,8 @@ class HTTPAPGServer(web_resource.Resource):
if forwarded is not None:
try:
host = [
f[5:] for f in forwarded.split(";")
f[5:]
for f in forwarded.split(";")
if f.startswith("host=")
][0] or None
except IndexError:
@ -1342,7 +1322,8 @@ class HTTPAPGServer(web_resource.Resource):
value = host
elif to_sign == "digest":
hashes = {
algo.lower(): hash_ for algo, hash_ in (
algo.lower(): hash_
for algo, hash_ in (
digest.split("=", 1) for digest in value.split(",")
)
}
@ -1367,7 +1348,6 @@ class HTTPAPGServer(web_resource.Resource):
else:
created = date_utils.date_parse(headers["date"])
try:
expires = float(headers["expires"])
except KeyError:
@ -1386,23 +1366,17 @@ class HTTPAPGServer(web_resource.Resource):
try:
return await self.apg.check_signature(
requestor_actor_id,
sign_data["signature"],
key_id,
headers
requestor_actor_id, sign_data["signature"], key_id, headers
)
except exceptions.EncryptionError:
method, url = headers["(request-target)"].rsplit(' ', 1)
method, url = headers["(request-target)"].rsplit(" ", 1)
headers["(request-target)"] = f"{method} {parse.unquote(url)}"
log.debug(
"Using workaround for (request-target) encoding bug in signature, "
"see https://github.com/mastodon/mastodon/issues/18871"
)
return await self.apg.check_signature(
requestor_actor_id,
sign_data["signature"],
key_id,
headers
requestor_actor_id, sign_data["signature"], key_id, headers
)
def render(self, request):

View file

@ -37,11 +37,7 @@ from libervia.backend.tools.utils import ensure_deferred
from libervia.backend.tools.web import download_file
from libervia.backend.memory.sqla_mapping import PubsubSub, SubscriptionState
from .constants import (
TYPE_ACTOR,
ST_AVATAR,
MAX_AVATAR_SIZE
)
from .constants import TYPE_ACTOR, ST_AVATAR, MAX_AVATAR_SIZE
log = getLogger(__name__)
@ -52,13 +48,14 @@ NODE_CONFIG = [
{"var": "pubsub#max_items", "value": "max"},
{"var": "pubsub#access_model", "type": "list-single", "value": "open"},
{"var": "pubsub#publish_model", "type": "list-single", "value": "open"},
]
NODE_CONFIG_VALUES = {c["var"]: c["value"] for c in NODE_CONFIG}
NODE_OPTIONS = {c["var"]: {} for c in NODE_CONFIG}
for c in NODE_CONFIG:
NODE_OPTIONS[c["var"]].update({k:v for k,v in c.items() if k not in ("var", "value")})
NODE_OPTIONS[c["var"]].update(
{k: v for k, v in c.items() if k not in ("var", "value")}
)
class APPubsubService(rsm.PubSubService):
@ -88,34 +85,30 @@ class APPubsubService(rsm.PubSubService):
in requestor
"""
if not recipient.user:
raise error.StanzaError(
"item-not-found",
text="No user part specified"
)
raise error.StanzaError("item-not-found", text="No user part specified")
requestor_actor_id = self.apg.build_apurl(TYPE_ACTOR, requestor.userhost())
recipient_account = self.apg._e.unescape(recipient.user)
recipient_actor_id = await self.apg.get_ap_actor_id_from_account(recipient_account)
recipient_actor_id = await self.apg.get_ap_actor_id_from_account(
recipient_account
)
inbox = await self.apg.get_ap_inbox_from_id(recipient_actor_id, use_shared=False)
return requestor_actor_id, recipient_actor_id, inbox
@ensure_deferred
async def publish(self, requestor, service, nodeIdentifier, items):
if self.apg.local_only and not self.apg.is_local(requestor):
raise error.StanzaError(
"forbidden",
"Only local users can publish on this gateway."
"forbidden", "Only local users can publish on this gateway."
)
if not service.user:
raise error.StanzaError(
"bad-request",
"You must specify an ActivityPub actor account in JID user part."
"You must specify an ActivityPub actor account in JID user part.",
)
ap_account = self.apg._e.unescape(service.user)
if ap_account.count("@") != 1:
raise error.StanzaError(
"bad-request",
f"{ap_account!r} is not a valid ActivityPub actor account."
"bad-request", f"{ap_account!r} is not a valid ActivityPub actor account."
)
client = self.apg.client.get_virtual_client(requestor)
@ -130,21 +123,17 @@ class APPubsubService(rsm.PubSubService):
cached_node = await self.host.memory.storage.get_pubsub_node(
client, service, nodeIdentifier, with_subscriptions=True, create=True
)
await self.host.memory.storage.cache_pubsub_items(
client,
cached_node,
items
)
await self.host.memory.storage.cache_pubsub_items(client, cached_node, items)
for subscription in cached_node.subscriptions:
if subscription.state != SubscriptionState.SUBSCRIBED:
continue
self.notifyPublish(
service,
nodeIdentifier,
[(subscription.subscriber, None, items)]
service, nodeIdentifier, [(subscription.subscriber, None, items)]
)
async def ap_following_2_elt(self, requestor_actor_id: str, ap_item: dict) -> domish.Element:
async def ap_following_2_elt(
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert actor ID from following collection to XMPP item
@param requestor_actor_id: ID of the actor doing the request.
@ -159,9 +148,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def ap_follower_2_elt(
self,
requestor_actor_id: str,
ap_item: dict
self, requestor_actor_id: str, ap_item: dict
) -> domish.Element:
"""Convert actor ID from followers collection to XMPP item
@ -175,9 +162,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def generate_v_card(
self,
requestor_actor_id: str,
ap_account: str
self, requestor_actor_id: str, ap_account: str
) -> domish.Element:
"""Generate vCard4 (XEP-0292) item element from ap_account's metadata
@ -186,8 +171,7 @@ class APPubsubService(rsm.PubSubService):
@return: <item> with the <vcard> element
"""
actor_data = await self.apg.get_ap_actor_data_from_account(
requestor_actor_id,
ap_account
requestor_actor_id, ap_account
)
identity_data = {}
@ -212,10 +196,7 @@ class APPubsubService(rsm.PubSubService):
return item_elt
async def get_avatar_data(
self,
client: SatXMPPEntity,
requestor_actor_id: str,
ap_account: str
self, client: SatXMPPEntity, requestor_actor_id: str, ap_account: str
) -> dict[str, Any]:
"""Retrieve actor's avatar if any, cache it and file actor_data
@ -259,27 +240,21 @@ class APPubsubService(rsm.PubSubService):
avatar_data = {
"path": dest_path,
"filename": filename,
'media_type': image.guess_type(dest_path),
"media_type": image.guess_type(dest_path),
}
await self.apg._i.cache_avatar(
self.apg.IMPORT_NAME,
avatar_data
)
await self.apg._i.cache_avatar(self.apg.IMPORT_NAME, avatar_data)
else:
avatar_data = {
"cache_uid": cache["uid"],
"path": cache["path"],
"media_type": cache["mime_type"]
}
"cache_uid": cache["uid"],
"path": cache["path"],
"media_type": cache["mime_type"],
}
return avatar_data
async def generate_avatar_metadata(
self,
client: SatXMPPEntity,
requestor_actor_id: str,
ap_account: str
self, client: SatXMPPEntity, requestor_actor_id: str, ap_account: str
) -> domish.Element:
"""Generate the metadata element for user avatar
@ -308,14 +283,11 @@ class APPubsubService(rsm.PubSubService):
"""
if not itemIdentifiers:
avatar_data = await self.get_avatar_data(
client,
requestor_actor_id,
ap_account
client, requestor_actor_id, ap_account
)
if "base64" not in avatar_data:
await threads.deferToThread(
self._blocking_b_6_4_encode_avatar,
avatar_data
self._blocking_b_6_4_encode_avatar, avatar_data
)
else:
if len(itemIdentifiers) > 1:
@ -327,10 +299,7 @@ class APPubsubService(rsm.PubSubService):
cache_data = self.apg.host.common_cache.get_metadata(item_id)
if cache_data is None:
raise error.StanzaError("item-not-found")
avatar_data = {
"cache_uid": item_id,
"path": cache_data["path"]
}
avatar_data = {"cache_uid": item_id, "path": cache_data["path"]}
await threads.deferToThread(self._blocking_b_6_4_encode_avatar, avatar_data)
return self.apg._a.build_item_data_elt(avatar_data)
@ -343,7 +312,7 @@ class APPubsubService(rsm.PubSubService):
node: str,
maxItems: Optional[int],
itemIdentifiers: Optional[List[str]],
rsm_req: Optional[rsm.RSMRequest]
rsm_req: Optional[rsm.RSMRequest],
) -> Tuple[List[domish.Element], Optional[rsm.RSMResponse]]:
if not service.user:
return [], None
@ -353,8 +322,7 @@ class APPubsubService(rsm.PubSubService):
return [], None
requestor_actor_id = self.apg.build_apurl(
TYPE_ACTOR,
await self.apg.get_ap_account_from_jid_and_node(service, node)
TYPE_ACTOR, await self.apg.get_ap_account_from_jid_and_node(service, node)
)
# cached_node may be pre-filled with some nodes (e.g. attachments nodes),
@ -379,17 +347,12 @@ class APPubsubService(rsm.PubSubService):
return [item_elt], None
elif node == self.apg._a.namespace_metadata:
item_elt = await self.generate_avatar_metadata(
self.apg.client,
requestor_actor_id,
ap_account
self.apg.client, requestor_actor_id, ap_account
)
return [item_elt], None
elif node == self.apg._a.namespace_data:
item_elt = await self.generate_avatar_data(
self.apg.client,
requestor_actor_id,
ap_account,
itemIdentifiers
self.apg.client, requestor_actor_id, ap_account, itemIdentifiers
)
return [item_elt], None
elif self.apg._pa.is_attachment_node(node):
@ -410,7 +373,7 @@ class APPubsubService(rsm.PubSubService):
raise error.StanzaError(
"feature-not-implemented",
text=f"AP Gateway {C.APP_VERSION} only supports "
f"{self.apg._m.namespace} node for now"
f"{self.apg._m.namespace} node for now",
)
collection_name = "outbox"
use_cache = True
@ -444,18 +407,26 @@ class APPubsubService(rsm.PubSubService):
if rsm_req is None:
if maxItems is None:
maxItems = 20
kwargs.update({
"max_items": maxItems,
"chronological_pagination": False,
})
kwargs.update(
{
"max_items": maxItems,
"chronological_pagination": False,
}
)
else:
if len(
[v for v in (rsm_req.after, rsm_req.before, rsm_req.index)
if v is not None]
) > 1:
if (
len(
[
v
for v in (rsm_req.after, rsm_req.before, rsm_req.index)
if v is not None
]
)
> 1
):
raise error.StanzaError(
"bad-request",
text="You can't use after, before and index at the same time"
text="You can't use after, before and index at the same time",
)
kwargs.update({"max_items": rsm_req.max})
if rsm_req.after is not None:
@ -476,25 +447,21 @@ class APPubsubService(rsm.PubSubService):
try:
parent_data = await self.apg.ap_get(parent_item, requestor_actor_id)
collection = await self.apg.ap_get_object(
requestor_actor_id,
parent_data.get("object", {}),
"replies"
requestor_actor_id, parent_data.get("object", {}), "replies"
)
except Exception as e:
raise error.StanzaError(
"item-not-found",
text=str(e)
)
raise error.StanzaError("item-not-found", text=str(e))
else:
actor_data = await self.apg.get_ap_actor_data_from_account(
requestor_actor_id,
ap_account
requestor_actor_id, ap_account
)
collection = await self.apg.ap_get_object(
requestor_actor_id, actor_data, collection_name
)
collection = await self.apg.ap_get_object(requestor_actor_id, actor_data, collection_name)
if not collection:
raise error.StanzaError(
"item-not-found",
text=f"No collection found for node {node!r} (account: {ap_account})"
text=f"No collection found for node {node!r} (account: {ap_account})",
)
kwargs["parser"] = parser
@ -528,17 +495,15 @@ class APPubsubService(rsm.PubSubService):
else:
try:
subscription = next(
s for s in node.subscriptions
s
for s in node.subscriptions
if s.subscriber == requestor.userhostJID()
)
except StopIteration:
subscription = None
if subscription is None:
subscription = PubsubSub(
subscriber=requestor.userhostJID(),
state=sub_state
)
subscription = PubsubSub(subscriber=requestor.userhostJID(), state=sub_state)
node.subscriptions.append(subscription)
await self.host.memory.storage.add(node)
else:
@ -586,11 +551,7 @@ class APPubsubService(rsm.PubSubService):
data = self.apg.create_activity(
"Undo",
req_actor_id,
self.apg.create_activity(
"Follow",
req_actor_id,
recip_actor_id
)
self.apg.create_activity("Follow", req_actor_id, recip_actor_id),
)
resp = await self.apg.sign_and_post(inbox, req_actor_id, data)
@ -602,10 +563,7 @@ class APPubsubService(rsm.PubSubService):
return NODE_OPTIONS
def getConfiguration(
self,
requestor: jid.JID,
service: jid.JID,
nodeIdentifier: str
self, requestor: jid.JID, service: jid.JID, nodeIdentifier: str
) -> defer.Deferred:
return defer.succeed(NODE_CONFIG_VALUES)
@ -615,12 +573,9 @@ class APPubsubService(rsm.PubSubService):
service: jid.JID,
nodeIdentifier: str,
pep: bool = False,
recipient: Optional[jid.JID] = None
recipient: Optional[jid.JID] = None,
) -> Optional[dict]:
if not nodeIdentifier:
return None
info = {
"type": "leaf",
"meta-data": NODE_CONFIG
}
info = {"type": "leaf", "meta-data": NODE_CONFIG}
return info

View file

@ -42,17 +42,17 @@ import re
# obs-text = %x80-FF
# ---
ows = '[ \t]*'
bws = f'{ows}'
obs_text = '[\\x80-\\xff]'
qdtext = f'([\t !#-\\[\\]-~]|{obs_text})'
quoted_pair = f'\\\\([\t !-~]|{obs_text})'
ows = "[ \t]*"
bws = f"{ows}"
obs_text = "[\\x80-\\xff]"
qdtext = f"([\t !#-\\[\\]-~]|{obs_text})"
quoted_pair = f"\\\\([\t !-~]|{obs_text})"
quoted_string = f'"({qdtext}|{quoted_pair})*"'
tchar = "([!#$%&`*+\\-.^_]|\\\\'|[|~0-9a-zA-Z])"
token = f'({tchar})+'
token = f"({tchar})+"
RE_SIG_PARAM = re.compile(
f'(?P<key>{token}{bws})={bws}'
f'((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))'
f"(?P<key>{token}{bws})={bws}"
f"((?P<uq_value>{token})|(?P<quoted_value>{quoted_string}))"
)

View file

@ -82,10 +82,7 @@ OVER_QUOTA_TXT = D_(
"{used_space}, you can't upload {file_size} more."
)
HTTP_VERSION = unicodedata.normalize(
'NFKD',
f"{C.APP_NAME} file sharing {C.APP_VERSION}"
)
HTTP_VERSION = unicodedata.normalize("NFKD", f"{C.APP_NAME} file sharing {C.APP_VERSION}")
class HTTPFileServer(resource.Resource):
@ -94,32 +91,32 @@ class HTTPFileServer(resource.Resource):
def errorPage(self, request, code):
request.setResponseCode(code)
if code == http.BAD_REQUEST:
brief = 'Bad Request'
brief = "Bad Request"
details = "Your request is invalid"
elif code == http.FORBIDDEN:
brief = 'Forbidden'
brief = "Forbidden"
details = "You're not allowed to use this resource"
elif code == http.NOT_FOUND:
brief = 'Not Found'
brief = "Not Found"
details = "No resource found at this URL"
else:
brief = 'Error'
brief = "Error"
details = "This resource can't be used"
log.error(f"Unexpected return code used: {code}")
log.warning(
f'Error returned while trying to access url {request.uri.decode()}: '
f"Error returned while trying to access url {request.uri.decode()}: "
f'"{brief}" ({code}): {details}'
)
return resource.ErrorPage(code, brief, details).render(request)
def get_disposition_type(self, media_type, media_subtype):
if media_type in ('image', 'video'):
return 'inline'
elif media_type == 'application' and media_subtype == 'pdf':
return 'inline'
if media_type in ("image", "video"):
return "inline"
elif media_type == "application" and media_subtype == "pdf":
return "inline"
else:
return 'attachment'
return "attachment"
def render(self, request):
request.setHeader("server", HTTP_VERSION)
@ -127,13 +124,14 @@ class HTTPFileServer(resource.Resource):
request.setHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, PUT")
request.setHeader(
"Access-Control-Allow-Headers",
"Content-Type, Range, Xmpp-File-Path, Xmpp-File-No-Http")
"Content-Type, Range, Xmpp-File-Path, Xmpp-File-No-Http",
)
request.setHeader("Access-Control-Allow-Credentials", "true")
request.setHeader("Accept-Ranges", "bytes")
request.setHeader(
"Access-Control-Expose-Headers",
"Date, Content-Length, Content-Range")
"Access-Control-Expose-Headers", "Date, Content-Length, Content-Range"
)
return super().render(request)
def render_OPTIONS(self, request):
@ -157,7 +155,8 @@ class HTTPFileServer(resource.Resource):
request.finish()
return
found_files = await request.file_sharing.host.memory.get_files(
client=None, peer_jid=None, perms_to_check=None, public_id=upload_id)
client=None, peer_jid=None, perms_to_check=None, public_id=upload_id
)
if not found_files:
request.write(self.errorPage(request, http.NOT_FOUND))
request.finish()
@ -166,22 +165,22 @@ class HTTPFileServer(resource.Resource):
log.error(f"more that one files found for public id {upload_id!r}")
found_file = found_files[0]
file_path = request.file_sharing.files_path/found_file['file_hash']
file_path = request.file_sharing.files_path / found_file["file_hash"]
file_res = static.File(file_path)
file_res.type = f'{found_file["media_type"]}/{found_file["media_subtype"]}'
file_res.encoding = file_res.contentEncodings.get(Path(found_file['name']).suffix)
file_res.encoding = file_res.contentEncodings.get(Path(found_file["name"]).suffix)
disp_type = self.get_disposition_type(
found_file['media_type'], found_file['media_subtype'])
found_file["media_type"], found_file["media_subtype"]
)
# the URL is percent encoded, and not all browsers/tools unquote the file name,
# thus we add a content disposition header
request.setHeader(
'Content-Disposition',
f"{disp_type}; filename*=UTF-8''{quote(found_file['name'])}"
"Content-Disposition",
f"{disp_type}; filename*=UTF-8''{quote(found_file['name'])}",
)
# cf. https://xmpp.org/extensions/xep-0363.html#server
request.setHeader(
'Content-Security-Policy',
"default-src 'none'; frame-ancestors 'none';"
"Content-Security-Policy", "default-src 'none'; frame-ancestors 'none';"
)
ret = file_res.render(request)
if ret != server.NOT_DONE_YET:
@ -215,7 +214,7 @@ class HTTPFileServer(resource.Resource):
if path:
path = unquote(path)
else:
path = "/uploads"
path = "/uploads"
if request.getHeader("Xmpp-File-No-Http") is not None:
public_id = None
else:
@ -225,11 +224,14 @@ class HTTPFileServer(resource.Resource):
"name": unquote(upload_request.filename),
"mime_type": upload_request.content_type,
"size": upload_request.size,
"path": path
"path": path,
}
await request.file_sharing.register_received_file(
client, upload_request.from_, file_data, tmp_file_path,
client,
upload_request.from_,
file_data,
tmp_file_path,
public_id=public_id,
)
@ -256,11 +258,13 @@ class FileSharingRequest(server.Request):
# we normalise the path
path = urlparse(path.decode()).path
try:
__, upload_id, filename = path.split('/')
__, upload_id, filename = path.split("/")
except ValueError:
raise exceptions.DataError("no enought path elements")
if len(upload_id) < 10:
raise exceptions.DataError(f"invalid upload ID received for a PUT: {upload_id!r}")
raise exceptions.DataError(
f"invalid upload ID received for a PUT: {upload_id!r}"
)
self._upload_data = (upload_id, filename)
return self._upload_data
@ -276,11 +280,11 @@ class FileSharingRequest(server.Request):
def refuse_request(self):
if self.content is not None:
self.content.close()
self.content = open(os.devnull, 'w+b')
self.content = open(os.devnull, "w+b")
self.channel._respondToBadRequestAndDisconnect()
def gotLength(self, length):
if self.channel._command.decode().upper() == 'PUT':
if self.channel._command.decode().upper() == "PUT":
# for PUT we check early if upload_id is fine, to avoid buffering a file we'll refuse
# we buffer the file in component's TMP_BUFFER_DIR, so we just have to rename it at the end
try:
@ -289,9 +293,13 @@ class FileSharingRequest(server.Request):
log.warning(f"Invalid PUT request, we stop here: {e}")
return self.refuse_request()
try:
client, upload_request, timer = self.file_sharing.expected_uploads.pop(upload_id)
client, upload_request, timer = self.file_sharing.expected_uploads.pop(
upload_id
)
except KeyError:
log.warning(f"unknown (expired?) upload ID received for a PUT: {upload_id!r}")
log.warning(
f"unknown (expired?) upload ID received for a PUT: {upload_id!r}"
)
return self.refuse_request()
if not timer.active:
@ -309,10 +317,9 @@ class FileSharingRequest(server.Request):
self.upload_request_data = (client, upload_request)
file_tmp_path = files_utils.get_unique_name(
self.file_tmp_dir/upload_id)
file_tmp_path = files_utils.get_unique_name(self.file_tmp_dir / upload_id)
self.content = open(file_tmp_path, 'w+b')
self.content = open(file_tmp_path, "w+b")
else:
return super().gotLength(length)
@ -331,8 +338,8 @@ class FileSharingSite(server.Site):
super().__init__(HTTPFileServer())
def getContentFile(self, length):
file_tmp_path = self.file_tmp_dir/shortuuid.uuid()
return open(file_tmp_path, 'w+b')
file_tmp_path = self.file_tmp_dir / shortuuid.uuid()
return open(file_tmp_path, "w+b")
class FileSharing:
@ -356,37 +363,53 @@ class FileSharing:
self._t = self.host.plugins["XEP-0264"]
self._hu = self.host.plugins["XEP-0363"]
self._hu.register_handler(self._on_http_upload)
self.host.trigger.add_with_check("FILE_getDestDir", self, self._get_dest_dir_trigger)
self.host.trigger.add_with_check(
"XEP-0234_fileSendingRequest", self, self._file_sending_request_trigger, priority=1000
"FILE_getDestDir", self, self._get_dest_dir_trigger
)
self.host.trigger.add_with_check(
"XEP-0234_fileSendingRequest",
self,
self._file_sending_request_trigger,
priority=1000,
)
self.host.trigger.add_with_check(
"XEP-0234_buildFileElement", self, self._add_file_metadata_elts
)
self.host.trigger.add_with_check(
"XEP-0234_parseFileElement", self, self._get_file_metadata_elts
)
self.host.trigger.add_with_check(
"XEP-0329_compGetFilesFromNode", self, self._add_file_metadata
)
self.host.trigger.add_with_check("XEP-0234_buildFileElement", self, self._add_file_metadata_elts)
self.host.trigger.add_with_check("XEP-0234_parseFileElement", self, self._get_file_metadata_elts)
self.host.trigger.add_with_check("XEP-0329_compGetFilesFromNode", self, self._add_file_metadata)
self.host.trigger.add_with_check(
"XEP-0329_compGetFilesFromNode_build_directory",
self,
self._add_directory_metadata_elts)
self._add_directory_metadata_elts,
)
self.host.trigger.add_with_check(
"XEP-0329_parseResult_directory",
self,
self._get_directory_metadata_elts)
"XEP-0329_parseResult_directory", self, self._get_directory_metadata_elts
)
self.files_path = self.host.get_local_path(None, C.FILES_DIR)
self.http_port = int(self.host.memory.config_get(
'component file-sharing', 'http_upload_port', 8888))
self.http_port = int(
self.host.memory.config_get(
"component file-sharing", "http_upload_port", 8888
)
)
connection_type = self.host.memory.config_get(
'component file-sharing', 'http_upload_connection_type', 'https')
if connection_type not in ('http', 'https'):
"component file-sharing", "http_upload_connection_type", "https"
)
if connection_type not in ("http", "https"):
raise exceptions.ConfigError(
'bad http_upload_connection_type, you must use one of "http" or "https"'
)
self.server = FileSharingSite(self)
self.expected_uploads = {}
if connection_type == 'http':
if connection_type == "http":
reactor.listenTCP(self.http_port, self.server)
else:
options = tls.get_options_from_config(
self.host.memory.config, "component file-sharing")
self.host.memory.config, "component file-sharing"
)
tls.tls_options_check(options)
context_factory = tls.get_tls_context_factory(options)
reactor.listenSSL(self.http_port, self.server, context_factory)
@ -400,7 +423,8 @@ class FileSharing:
self.init()
public_base_url = self.host.memory.config_get(
'component file-sharing', 'http_upload_public_facing_url')
"component file-sharing", "http_upload_public_facing_url"
)
if public_base_url is None:
client._file_sharing_base_url = f"https://{client.host}:{self.http_port}"
else:
@ -441,7 +465,8 @@ class FileSharing:
thumbnails.append({"id": thumb_id, "size": thumb_size})
async def register_received_file(
self, client, peer_jid, file_data, file_path, public_id=None, extra=None):
self, client, peer_jid, file_data, file_path, public_id=None, extra=None
):
"""Post file reception tasks
once file is received, this method create hash/thumbnails if necessary
@ -463,9 +488,9 @@ class FileSharing:
file_hash = file_data["hash_hasher"].hexdigest()
else:
hasher = self._h.get_hasher(HASH_ALGO)
with file_path.open('rb') as f:
with file_path.open("rb") as f:
file_hash = await self._h.calculate_hash(f, hasher)
final_path = self.files_path/file_hash
final_path = self.files_path / file_hash
if final_path.is_file():
log.debug(
@ -490,8 +515,11 @@ class FileSharing:
try:
await video.get_thumbnail(final_path, thumb_path)
except Exception as e:
log.warning(_("Can't get thumbnail for {final_path}: {e}").format(
final_path=final_path, e=e))
log.warning(
_("Can't get thumbnail for {final_path}: {e}").format(
final_path=final_path, e=e
)
)
else:
await self.generate_thumbnails(extra, thumb_path)
@ -534,14 +562,15 @@ class FileSharing:
text=OVER_QUOTA_TXT.format(
quota=utils.get_human_size(quota),
used_space=utils.get_human_size(used_space),
file_size=utils.get_human_size(file_data['size'])
)
file_size=utils.get_human_size(file_data["size"]),
),
)
file_tmp_dir = self.host.get_local_path(
None, C.FILES_TMP_DIR, peer_jid.userhost(), component=True
)
file_tmp_path = file_data['file_path'] = files_utils.get_unique_name(
file_tmp_dir/filename)
file_tmp_path = file_data["file_path"] = files_utils.get_unique_name(
file_tmp_dir / filename
)
transfer_data["finished_d"].addCallback(
lambda __: defer.ensureDeferred(
@ -559,8 +588,8 @@ class FileSharing:
):
"""This method retrieve a file on request, and send if after checking permissions"""
peer_jid = session["peer_jid"]
if session['local_jid'].user:
owner = client.get_owner_from_jid(session['local_jid'])
if session["local_jid"].user:
owner = client.get_owner_from_jid(session["local_jid"])
else:
owner = peer_jid
try:
@ -592,9 +621,10 @@ class FileSharing:
# we only use the first found file
found_file = found_files[0]
if found_file['type'] != C.FILE_TYPE_FILE:
raise TypeError("a file was expected, type is {type_}".format(
type_=found_file['type']))
if found_file["type"] != C.FILE_TYPE_FILE:
raise TypeError(
"a file was expected, type is {type_}".format(type_=found_file["type"])
)
file_hash = found_file["file_hash"]
file_path = self.files_path / file_hash
file_data["hash_hasher"] = hasher = self._h.get_hasher(found_file["hash_algo"])
@ -624,9 +654,11 @@ class FileSharing:
else:
return (
False,
defer.ensureDeferred(self._retrieve_files(
client, session, content_data, content_name, file_data, file_elt
)),
defer.ensureDeferred(
self._retrieve_files(
client, session, content_data, content_name, file_data, file_elt
)
),
)
## HTTP Upload ##
@ -639,7 +671,7 @@ class FileSharing:
async def _on_http_upload(self, client, request):
# filename should be already cleaned, but it's better to double check
assert '/' not in request.filename
assert "/" not in request.filename
# client._file_sharing_allowed_hosts is set in plugin XEP-0329
if request.from_.host not in client._file_sharing_allowed_hosts:
raise error.StanzaError("forbidden")
@ -654,13 +686,15 @@ class FileSharing:
text=OVER_QUOTA_TXT.format(
quota=utils.get_human_size(quota),
used_space=utils.get_human_size(used_space),
file_size=utils.get_human_size(request.size)
file_size=utils.get_human_size(request.size),
),
appCondition=self._hu.get_file_too_large_elt(
max(quota - used_space, 0)
),
appCondition = self._hu.get_file_too_large_elt(max(quota - used_space, 0))
)
upload_id = shortuuid.ShortUUID().random(length=30)
assert '/' not in upload_id
assert "/" not in upload_id
timer = reactor.callLater(30, self._purge_slot, upload_id)
self.expected_uploads[upload_id] = (client, request, timer)
url = urljoin(client._file_sharing_base_url, f"{upload_id}/{request.filename}")
@ -675,7 +709,7 @@ class FileSharing:
def _add_file_metadata_elts(self, client, file_elt, extra_args):
# affiliation
affiliation = extra_args.get('affiliation')
affiliation = extra_args.get("affiliation")
if affiliation is not None:
file_elt.addElement((NS_FS_AFFILIATION, "affiliation"), content=affiliation)
@ -715,7 +749,8 @@ class FileSharing:
return True
def _add_file_metadata(
self, client, iq_elt, iq_result_elt, owner, node_path, files_data):
self, client, iq_elt, iq_result_elt, owner, node_path, files_data
):
for file_data in files_data:
file_data["comments_url"] = uri.build_xmpp_uri(
"pubsub",
@ -725,22 +760,21 @@ class FileSharing:
return True
def _add_directory_metadata_elts(
self, client, file_data, directory_elt, owner, node_path):
affiliation = file_data.get('affiliation')
self, client, file_data, directory_elt, owner, node_path
):
affiliation = file_data.get("affiliation")
if affiliation is not None:
directory_elt.addElement(
(NS_FS_AFFILIATION, "affiliation"),
content=affiliation
(NS_FS_AFFILIATION, "affiliation"), content=affiliation
)
def _get_directory_metadata_elts(
self, client, elt, file_data):
def _get_directory_metadata_elts(self, client, elt, file_data):
try:
affiliation_elt = next(elt.elements(NS_FS_AFFILIATION, "affiliation"))
except StopIteration:
pass
else:
file_data['affiliation'] = str(affiliation_elt)
file_data["affiliation"] = str(affiliation_elt)
class Comments_handler(pubsub.PubSubService):
@ -841,7 +875,9 @@ class Comments_handler(pubsub.PubSubService):
peer_jid = None
else:
peer_jid = requestor.userhost()
update_cb = partial(self.comments_update, new_comments=comments, peer_jid=peer_jid)
update_cb = partial(
self.comments_update, new_comments=comments, peer_jid=peer_jid
)
try:
await self.host.memory.file_update(file_id, "extra", update_cb)
except exceptions.PermissionError:

View file

@ -85,22 +85,30 @@ class FileSharingManagement(object):
def profile_connected(self, client):
self._c.add_ad_hoc_command(
client, self._on_change_file, "Change Permissions of File(s)",
client,
self._on_change_file,
"Change Permissions of File(s)",
node=NS_FILE_MANAGEMENT_PERM,
allowed_magics=C.ENTITY_ALL,
)
self._c.add_ad_hoc_command(
client, self._on_delete_file, "Delete File(s)",
client,
self._on_delete_file,
"Delete File(s)",
node=NS_FILE_MANAGEMENT_DELETE,
allowed_magics=C.ENTITY_ALL,
)
self._c.add_ad_hoc_command(
client, self._on_gen_thumbnails, "Generate Thumbnails",
client,
self._on_gen_thumbnails,
"Generate Thumbnails",
node=NS_FILE_MANAGEMENT_THUMB,
allowed_magics=C.ENTITY_ALL,
)
self._c.add_ad_hoc_command(
client, self._on_quota, "Get Quota",
client,
self._on_quota,
"Get Quota",
node=NS_FILE_MANAGEMENT_QUOTA,
allowed_magics=C.ENTITY_ALL,
)
@ -108,12 +116,14 @@ class FileSharingManagement(object):
def _delete(self, service_jid_s, path, namespace, profile):
client = self.host.get_client(profile)
service_jid = jid.JID(service_jid_s) if service_jid_s else None
return defer.ensureDeferred(self._c.sequence(
client,
[{"path": path, "namespace": namespace}, {"confirm": True}],
NS_FILE_MANAGEMENT_DELETE,
service_jid,
))
return defer.ensureDeferred(
self._c.sequence(
client,
[{"path": path, "namespace": namespace}, {"confirm": True}],
NS_FILE_MANAGEMENT_DELETE,
service_jid,
)
)
def _err(self, reason):
"""Helper method to get argument to return for error
@ -133,17 +143,14 @@ class FileSharingManagement(object):
@return (tuple): arguments to use in defer.returnValue
"""
status = self._c.STATUS.EXECUTING
form = data_form.Form("form", title="File Management",
formNamespace=NS_FILE_MANAGEMENT)
field = data_form.Field(
"text-single", "path", required=True
form = data_form.Form(
"form", title="File Management", formNamespace=NS_FILE_MANAGEMENT
)
field = data_form.Field("text-single", "path", required=True)
form.addField(field)
field = data_form.Field(
"text-single", "namespace", required=False
)
field = data_form.Field("text-single", "namespace", required=False)
form.addField(field)
payload = form.toElement()
@ -159,17 +166,17 @@ class FileSharingManagement(object):
"""
fields = command_form.fields
try:
path = fields['path'].value.strip()
namespace = fields['namespace'].value or None
path = fields["path"].value.strip()
namespace = fields["namespace"].value or None
except KeyError:
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if not path:
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
requestor = session_data['requestor']
requestor = session_data["requestor"]
requestor_bare = requestor.userhostJID()
path = path.rstrip('/')
path = path.rstrip("/")
parent_path, basename = os.path.split(path)
# TODO: if parent_path and basename are empty, we ask for root directory
@ -177,35 +184,37 @@ class FileSharingManagement(object):
try:
found_files = await self.host.memory.get_files(
client, requestor_bare, path=parent_path, name=basename,
namespace=namespace)
client,
requestor_bare,
path=parent_path,
name=basename,
namespace=namespace,
)
found_file = found_files[0]
except (exceptions.NotFound, IndexError):
raise WorkflowError(self._err(_("file not found")))
except exceptions.PermissionError:
raise WorkflowError(self._err(_("forbidden")))
if found_file['owner'] != requestor_bare:
if found_file["owner"] != requestor_bare:
# only owner can manage files
log.warning(_("Only owner can manage files"))
raise WorkflowError(self._err(_("forbidden")))
session_data['found_file'] = found_file
session_data['namespace'] = namespace
session_data["found_file"] = found_file
session_data["namespace"] = namespace
return found_file
def _update_read_permission(self, access, allowed_jids):
if not allowed_jids:
if C.ACCESS_PERM_READ in access:
del access[C.ACCESS_PERM_READ]
elif allowed_jids == 'PUBLIC':
access[C.ACCESS_PERM_READ] = {
"type": C.ACCESS_TYPE_PUBLIC
}
elif allowed_jids == "PUBLIC":
access[C.ACCESS_PERM_READ] = {"type": C.ACCESS_TYPE_PUBLIC}
else:
access[C.ACCESS_PERM_READ] = {
"type": C.ACCESS_TYPE_WHITELIST,
"jids": [j.full() for j in allowed_jids]
"jids": [j.full() for j in allowed_jids],
}
async def _update_dir(self, client, requestor, namespace, file_data, allowed_jids):
@ -214,19 +223,25 @@ class FileSharingManagement(object):
@param file_data(dict): metadata of the file
@param allowed_jids(list[jid.JID]): list of entities allowed to read the file
"""
assert file_data['type'] == C.FILE_TYPE_DIRECTORY
assert file_data["type"] == C.FILE_TYPE_DIRECTORY
files_data = await self.host.memory.get_files(
client, requestor, parent=file_data['id'], namespace=namespace)
client, requestor, parent=file_data["id"], namespace=namespace
)
for file_data in files_data:
if not file_data['access'].get(C.ACCESS_PERM_READ, {}):
log.debug("setting {perm} read permission for {name}".format(
perm=allowed_jids, name=file_data['name']))
if not file_data["access"].get(C.ACCESS_PERM_READ, {}):
log.debug(
"setting {perm} read permission for {name}".format(
perm=allowed_jids, name=file_data["name"]
)
)
await self.host.memory.file_update(
file_data['id'], 'access',
partial(self._update_read_permission, allowed_jids=allowed_jids))
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
await self._update_dir(client, requestor, namespace, file_data, 'PUBLIC')
file_data["id"],
"access",
partial(self._update_read_permission, allowed_jids=allowed_jids),
)
if file_data["type"] == C.FILE_TYPE_DIRECTORY:
await self._update_dir(client, requestor, namespace, file_data, "PUBLIC")
async def _on_change_file(self, client, command_elt, session_data, action, node):
try:
@ -235,8 +250,8 @@ class FileSharingManagement(object):
except StopIteration:
command_form = None
found_file = session_data.get('found_file')
requestor = session_data['requestor']
found_file = session_data.get("found_file")
requestor = session_data["requestor"]
requestor_bare = requestor.userhostJID()
if command_form is None or len(command_form.fields) == 0:
@ -251,31 +266,39 @@ class FileSharingManagement(object):
return e.err_args
# management request
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
if found_file["type"] == C.FILE_TYPE_DIRECTORY:
instructions = D_("Please select permissions for this directory")
else:
instructions = D_("Please select permissions for this file")
form = data_form.Form("form", title="File Management",
instructions=[instructions],
formNamespace=NS_FILE_MANAGEMENT)
form = data_form.Form(
"form",
title="File Management",
instructions=[instructions],
formNamespace=NS_FILE_MANAGEMENT,
)
field = data_form.Field(
"text-multi", "read_allowed", required=False,
desc='list of jids allowed to read this file (beside yourself), or '
'"PUBLIC" to let a public access'
"text-multi",
"read_allowed",
required=False,
desc="list of jids allowed to read this file (beside yourself), or "
'"PUBLIC" to let a public access',
)
read_access = found_file["access"].get(C.ACCESS_PERM_READ, {})
access_type = read_access.get('type', C.ACCESS_TYPE_WHITELIST)
access_type = read_access.get("type", C.ACCESS_TYPE_WHITELIST)
if access_type == C.ACCESS_TYPE_PUBLIC:
field.values = ['PUBLIC']
field.values = ["PUBLIC"]
else:
field.values = read_access.get('jids', [])
field.values = read_access.get("jids", [])
form.addField(field)
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
if found_file["type"] == C.FILE_TYPE_DIRECTORY:
field = data_form.Field(
"boolean", "recursive", value=False, required=False,
"boolean",
"recursive",
value=False,
required=False,
desc="Files under it will be made public to follow this dir "
"permission (only if they don't have already a permission set)."
"permission (only if they don't have already a permission set).",
)
form.addField(field)
@ -286,42 +309,49 @@ class FileSharingManagement(object):
else:
# final phase, we'll do permission change here
try:
read_allowed = command_form.fields['read_allowed']
read_allowed = command_form.fields["read_allowed"]
except KeyError:
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if read_allowed.value == 'PUBLIC':
allowed_jids = 'PUBLIC'
elif read_allowed.value.strip() == '':
if read_allowed.value == "PUBLIC":
allowed_jids = "PUBLIC"
elif read_allowed.value.strip() == "":
allowed_jids = None
else:
try:
allowed_jids = [jid.JID(v.strip()) for v in read_allowed.values
if v.strip()]
allowed_jids = [
jid.JID(v.strip()) for v in read_allowed.values if v.strip()
]
except RuntimeError as e:
log.warning(_("Can't use read_allowed values: {reason}").format(
reason=e))
log.warning(
_("Can't use read_allowed values: {reason}").format(reason=e)
)
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if found_file['type'] == C.FILE_TYPE_FILE:
if found_file["type"] == C.FILE_TYPE_FILE:
await self.host.memory.file_update(
found_file['id'], 'access',
partial(self._update_read_permission, allowed_jids=allowed_jids))
found_file["id"],
"access",
partial(self._update_read_permission, allowed_jids=allowed_jids),
)
else:
try:
recursive = command_form.fields['recursive']
recursive = command_form.fields["recursive"]
except KeyError:
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
await self.host.memory.file_update(
found_file['id'], 'access',
partial(self._update_read_permission, allowed_jids=allowed_jids))
found_file["id"],
"access",
partial(self._update_read_permission, allowed_jids=allowed_jids),
)
if recursive:
# we set all file under the directory as public (if they haven't
# already a permission set), so allowed entities of root directory
# can read them.
namespace = session_data['namespace']
namespace = session_data["namespace"]
await self._update_dir(
client, requestor_bare, namespace, found_file, 'PUBLIC')
client, requestor_bare, namespace, found_file, "PUBLIC"
)
# job done, we can end the session
status = self._c.STATUS.COMPLETED
@ -336,8 +366,8 @@ class FileSharingManagement(object):
except StopIteration:
command_form = None
found_file = session_data.get('found_file')
requestor = session_data['requestor']
found_file = session_data.get("found_file")
requestor = session_data["requestor"]
requestor_bare = requestor.userhostJID()
if command_form is None or len(command_form.fields) == 0:
@ -350,18 +380,27 @@ class FileSharingManagement(object):
found_file = await self._get_file_data(client, session_data, command_form)
except WorkflowError as e:
return e.err_args
if found_file['type'] == C.FILE_TYPE_DIRECTORY:
msg = D_("Are you sure to delete directory {name} and all files and "
"directories under it?").format(name=found_file['name'])
if found_file["type"] == C.FILE_TYPE_DIRECTORY:
msg = D_(
"Are you sure to delete directory {name} and all files and "
"directories under it?"
).format(name=found_file["name"])
else:
msg = D_("Are you sure to delete file {name}?"
.format(name=found_file['name']))
form = data_form.Form("form", title="File Management",
instructions = [msg],
formNamespace=NS_FILE_MANAGEMENT)
msg = D_(
"Are you sure to delete file {name}?".format(name=found_file["name"])
)
form = data_form.Form(
"form",
title="File Management",
instructions=[msg],
formNamespace=NS_FILE_MANAGEMENT,
)
field = data_form.Field(
"boolean", "confirm", value=False, required=True,
desc="check this box to confirm"
"boolean",
"confirm",
value=False,
required=True,
desc="check this box to confirm",
)
form.addField(field)
status = self._c.STATUS.EXECUTING
@ -371,15 +410,16 @@ class FileSharingManagement(object):
else:
# final phase, we'll do deletion here
try:
confirmed = C.bool(command_form.fields['confirm'].value)
confirmed = C.bool(command_form.fields["confirm"].value)
except KeyError:
self._c.ad_hoc_error(self._c.ERROR.BAD_PAYLOAD)
if not confirmed:
note = None
else:
recursive = found_file['type'] == C.FILE_TYPE_DIRECTORY
recursive = found_file["type"] == C.FILE_TYPE_DIRECTORY
await self.host.memory.file_delete(
client, requestor_bare, found_file['id'], recursive)
client, requestor_bare, found_file["id"], recursive
)
note = (self._c.NOTE.INFO, _("file deleted"))
status = self._c.STATUS.COMPLETED
payload = None
@ -393,16 +433,17 @@ class FileSharingManagement(object):
@param file_data(dict): metadata of the file
"""
if file_data['type'] == C.FILE_TYPE_DIRECTORY:
if file_data["type"] == C.FILE_TYPE_DIRECTORY:
sub_files_data = await self.host.memory.get_files(
client, requestor, parent=file_data['id'], namespace=namespace)
client, requestor, parent=file_data["id"], namespace=namespace
)
for sub_file_data in sub_files_data:
await self._gen_thumbs(client, requestor, namespace, sub_file_data)
elif file_data['type'] == C.FILE_TYPE_FILE:
media_type = file_data['media_type']
file_path = os.path.join(self.files_path, file_data['file_hash'])
if media_type == 'image':
elif file_data["type"] == C.FILE_TYPE_FILE:
media_type = file_data["media_type"]
file_path = os.path.join(self.files_path, file_data["file_hash"])
if media_type == "image":
thumbnails = []
for max_thumb_size in self._t.SIZES:
@ -414,20 +455,26 @@ class FileSharingManagement(object):
60 * 60 * 24 * 31 * 6,
)
except Exception as e:
log.warning(_("Can't create thumbnail: {reason}")
.format(reason=e))
log.warning(
_("Can't create thumbnail: {reason}").format(reason=e)
)
break
thumbnails.append({"id": thumb_id, "size": thumb_size})
await self.host.memory.file_update(
file_data['id'], 'extra',
partial(self._update_thumbs, thumbnails=thumbnails))
file_data["id"],
"extra",
partial(self._update_thumbs, thumbnails=thumbnails),
)
log.info("thumbnails for [{file_name}] generated"
.format(file_name=file_data['name']))
log.info(
"thumbnails for [{file_name}] generated".format(
file_name=file_data["name"]
)
)
else:
log.warning("unmanaged file type: {type_}".format(type_=file_data['type']))
log.warning("unmanaged file type: {type_}".format(type_=file_data["type"]))
async def _on_gen_thumbnails(self, client, command_elt, session_data, action, node):
try:
@ -436,8 +483,8 @@ class FileSharingManagement(object):
except StopIteration:
command_form = None
found_file = session_data.get('found_file')
requestor = session_data['requestor']
found_file = session_data.get("found_file")
requestor = session_data["requestor"]
if command_form is None or len(command_form.fields) == 0:
# root request
@ -451,7 +498,7 @@ class FileSharingManagement(object):
return e.err_args
log.info("Generating thumbnails as requested")
await self._gen_thumbs(client, requestor, found_file['namespace'], found_file)
await self._gen_thumbs(client, requestor, found_file["namespace"], found_file)
# job done, we can end the session
status = self._c.STATUS.COMPLETED
@ -460,7 +507,7 @@ class FileSharingManagement(object):
return (payload, status, None, note)
async def _on_quota(self, client, command_elt, session_data, action, node):
requestor = session_data['requestor']
requestor = session_data["requestor"]
quota = self.host.plugins["file_sharing"].get_quota(client, requestor)
try:
size_used = await self.host.memory.file_get_used_space(client, requestor)
@ -473,11 +520,10 @@ class FileSharingManagement(object):
note = (
self._c.NOTE.INFO,
_("You are currently using {size_used} on {size_quota}").format(
size_used = utils.get_human_size(size_used),
size_quota = (
_("unlimited quota") if quota is None
else utils.get_human_size(quota)
)
)
size_used=utils.get_human_size(size_used),
size_quota=(
_("unlimited quota") if quota is None else utils.get_human_size(quota)
),
),
)
return (payload, status, None, note)

View file

@ -40,7 +40,6 @@ PLUGIN_INFO = {
}
class Manhole(object):
def __init__(self, host):
@ -50,10 +49,17 @@ class Manhole(object):
self.start_manhole(port)
def start_manhole(self, port):
log.warning(_("/!\\ Manhole debug server activated, be sure to not use it in "
"production, this is dangerous /!\\"))
log.info(_("You can connect to manhole server using telnet on port {port}")
.format(port=port))
log.warning(
_(
"/!\\ Manhole debug server activated, be sure to not use it in "
"production, this is dangerous /!\\"
)
)
log.info(
_("You can connect to manhole server using telnet on port {port}").format(
port=port
)
)
f = protocol.ServerFactory()
namespace = {
"host": self.host,
@ -61,9 +67,10 @@ class Manhole(object):
"jid": jid,
"d": defer.ensureDeferred,
}
f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol,
insults.ServerProtocol,
ColoredManhole,
namespace=namespace,
)
f.protocol = lambda: TelnetTransport(
TelnetBootstrapProtocol,
insults.ServerProtocol,
ColoredManhole,
namespace=namespace,
)
reactor.listenTCP(port, f)

View file

@ -41,7 +41,7 @@ PLUGIN_INFO = {
class ExportCommandProtocol(protocol.ProcessProtocol):
""" Try to register an account with prosody """
"""Try to register an account with prosody"""
def __init__(self, parent, client, target, options):
self.parent = parent
@ -73,10 +73,10 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
self.transport.write(message.encode("utf-8"))
def bool_option(self, key):
""" Get boolean value from options
"""Get boolean value from options
@param key: name of the option
@return: True if key exists and set to "true" (case insensitive),
False in all other cases """
False in all other cases"""
value = self.options.get(key, "")
return value.lower() == "true"
@ -92,7 +92,9 @@ class CommandExport(object):
log.info(_("Plugin command export initialization"))
self.host = host
self.spawned = {} # key = entity
host.trigger.add("message_received", self.message_received_trigger, priority=10000)
host.trigger.add(
"message_received", self.message_received_trigger, priority=10000
)
host.bridge.add_method(
"command_export",
".plugin",
@ -102,19 +104,19 @@ class CommandExport(object):
)
def removeProcess(self, entity, process):
""" Called when the process is finished
"""Called when the process is finished
@param entity: jid.JID attached to the process
@param process: process to remove"""
try:
processes_set = self.spawned[(entity, process.client.profile)]
processes_set.discard(process)
if not processes_set:
del (self.spawned[(entity, process.client.profile)])
del self.spawned[(entity, process.client.profile)]
except ValueError:
pass
def message_received_trigger(self, client, message_elt, post_treat):
""" Check if source is linked and repeat message, else do nothing """
"""Check if source is linked and repeat message, else do nothing"""
from_jid = jid.JID(message_elt["from"])
spawned_key = (from_jid.userhostJID(), client.profile)
@ -140,7 +142,7 @@ class CommandExport(object):
return True
def _export_command(self, command, args, targets, options, profile_key):
""" Export a commands to authorised targets
"""Export a commands to authorised targets
@param command: full path of the command to execute
@param args: list of arguments, with command name as first one
@param targets: list of allowed entities

View file

@ -45,9 +45,7 @@ PLUGIN_INFO = {
}
NS_INVITATION = "https://salut-a-toi/protocol/invitation:0"
INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(
ns_invit=NS_INVITATION
)
INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(ns_invit=NS_INVITATION)
NS_INVITATION_LIST = NS_INVITATION + "#list"
@ -91,8 +89,10 @@ class Invitation(object):
"""
if namespace in self._ns_cb:
raise exceptions.ConflictError(
"invitation namespace {namespace} is already register with {callback}"
.format(namespace=namespace, callback=self._ns_cb[namespace]))
"invitation namespace {namespace} is already register with {callback}".format(
namespace=namespace, callback=self._ns_cb[namespace]
)
)
self._ns_cb[namespace] = callback
def _generate_base_invitation(self, client, invitee_jid, name, extra):
@ -118,14 +118,16 @@ class Invitation(object):
invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
if name is not None:
invitation_elt["name"] = name
thumb_url = extra.get('thumb_url')
thumb_url = extra.get("thumb_url")
if thumb_url:
if not thumb_url.startswith('http'):
if not thumb_url.startswith("http"):
log.warning(
"only http URLs are allowed for thumbnails, got {url}, ignoring"
.format(url=thumb_url))
"only http URLs are allowed for thumbnails, got {url}, ignoring".format(
url=thumb_url
)
)
else:
invitation_elt['thumb_url'] = thumb_url
invitation_elt["thumb_url"] = thumb_url
return mess_data, invitation_elt
def send_pubsub_invitation(
@ -136,7 +138,7 @@ class Invitation(object):
node: str,
item_id: Optional[str],
name: Optional[str],
extra: Optional[dict]
extra: Optional[dict],
) -> None:
"""Send an pubsub invitation in a <message> stanza
@ -151,7 +153,8 @@ class Invitation(object):
if extra is None:
extra = {}
mess_data, invitation_elt = self._generate_base_invitation(
client, invitee_jid, name, extra)
client, invitee_jid, name, extra
)
pubsub_elt = invitation_elt.addElement("pubsub")
pubsub_elt["service"] = service.full()
pubsub_elt["node"] = node
@ -173,8 +176,15 @@ class Invitation(object):
client.send(mess_data["xml"])
async def send_file_sharing_invitation(
self, client, invitee_jid, service, repos_type=None, namespace=None, path=None,
name=None, extra=None
self,
client,
invitee_jid,
service,
repos_type=None,
namespace=None,
path=None,
name=None,
extra=None,
):
"""Send a file sharing invitation in a <message> stanza
@ -196,7 +206,7 @@ class Invitation(object):
# FIXME: not the best place to adapt permission, but it's necessary to check them
# for UX
try:
await self.host.plugins['XEP-0329'].affiliationsSet(
await self.host.plugins["XEP-0329"].affiliationsSet(
client, service, namespace, path, {invitee_jid: "member"}
)
except Exception as e:
@ -214,18 +224,20 @@ class Invitation(object):
)
else:
try:
extra['thumb_url'] = own_interest['thumb_url']
extra["thumb_url"] = own_interest["thumb_url"]
except KeyError:
pass
mess_data, invitation_elt = self._generate_base_invitation(
client, invitee_jid, name, extra)
client, invitee_jid, name, extra
)
file_sharing_elt = invitation_elt.addElement("file_sharing")
file_sharing_elt["service"] = service.full()
if repos_type is not None:
if repos_type not in ("files", "photos"):
msg = "unknown repository type: {repos_type}".format(
repos_type=repos_type)
repos_type=repos_type
)
log.warning(msg)
raise exceptions.DateError(msg)
file_sharing_elt["type"] = repos_type
@ -250,8 +262,9 @@ class Invitation(object):
client, service, node, item_ids=[item_id]
)
except Exception as e:
log.warning(_("Can't get item linked with invitation: {reason}").format(
reason=e))
log.warning(
_("Can't get item linked with invitation: {reason}").format(reason=e)
)
try:
item_elt = items[0]
except IndexError:
@ -261,8 +274,9 @@ class Invitation(object):
try:
namespace = item_elt.firstChildElement().uri
except Exception as e:
log.warning(_("Can't retrieve namespace of invitation: {reason}").format(
reason = e))
log.warning(
_("Can't retrieve namespace of invitation: {reason}").format(reason=e)
)
raise exceptions.DataError
args = [service, node, item_id, item_elt]
@ -271,7 +285,7 @@ class Invitation(object):
node_data_elt = next(pubsub_elt.elements(NS_INVITATION, "node_data"))
except StopIteration:
raise exceptions.DataError("Bad invitation, ignoring")
namespace = node_data_elt['namespace']
namespace = node_data_elt["namespace"]
args = [service, node, None, node_data_elt]
return namespace, args
@ -296,7 +310,7 @@ class Invitation(object):
name = invitation_elt.getAttribute("name")
extra = {}
if invitation_elt.hasAttribute("thumb_url"):
extra['thumb_url'] = invitation_elt['thumb_url']
extra["thumb_url"] = invitation_elt["thumb_url"]
for elt in invitation_elt.elements():
if elt.uri != NS_INVITATION:
@ -307,22 +321,26 @@ class Invitation(object):
elif elt.name == "file_sharing":
method = self._parse_file_sharing_elt
else:
log.warning("not implemented invitation element: {xml}".format(
xml = elt.toXml()))
log.warning(
"not implemented invitation element: {xml}".format(xml=elt.toXml())
)
continue
try:
namespace, args = await method(client, elt)
except exceptions.DataError:
log.warning("Can't parse invitation element: {xml}".format(
xml = elt.toXml()))
log.warning(
"Can't parse invitation element: {xml}".format(xml=elt.toXml())
)
continue
try:
cb = self._ns_cb[namespace]
except KeyError:
log.warning(_(
'No handler for namespace "{namespace}", invitation ignored')
.format(namespace=namespace))
log.warning(
_(
'No handler for namespace "{namespace}", invitation ignored'
).format(namespace=namespace)
)
else:
await utils.as_deferred(cb, client, namespace, name, extra, *args)

View file

@ -53,21 +53,35 @@ class FileSharingInvitation:
in_sign="ssssssss",
out_sign="",
method=self._send_file_sharing_invitation,
async_=True
async_=True,
)
def _send_file_sharing_invitation(
self, invitee_jid_s, service_s, repos_type=None, namespace=None, path=None,
name=None, extra_s='', profile_key=C.PROF_KEY_NONE):
self,
invitee_jid_s,
service_s,
repos_type=None,
namespace=None,
path=None,
name=None,
extra_s="",
profile_key=C.PROF_KEY_NONE,
):
client = self.host.get_client(profile_key)
invitee_jid = jid.JID(invitee_jid_s)
service = jid.JID(service_s)
extra = data_format.deserialise(extra_s)
return defer.ensureDeferred(
self.host.plugins["INVITATION"].send_file_sharing_invitation(
client, invitee_jid, service, repos_type=repos_type or None,
namespace=namespace or None, path=path or None, name=name or None,
extra=extra)
client,
invitee_jid,
service,
repos_type=repos_type or None,
namespace=namespace or None,
path=path or None,
name=name or None,
extra=extra,
)
)
def on_invitation(
@ -79,25 +93,31 @@ class FileSharingInvitation:
service: jid.JID,
repos_type: str,
sharing_ns: str,
path: str
path: str,
):
if repos_type == "files":
type_human = _("file sharing")
elif repos_type == "photos":
type_human = _("photo album")
else:
log.warning("Unknown repository type: {repos_type}".format(
repos_type=repos_type))
log.warning(
"Unknown repository type: {repos_type}".format(repos_type=repos_type)
)
repos_type = "file"
type_human = _("file sharing")
log.info(_(
'{profile} has received an invitation for a files repository ({type_human}) '
'with namespace {sharing_ns!r} at path [{path}]').format(
profile=client.profile, type_human=type_human, sharing_ns=sharing_ns,
path=path)
log.info(
_(
"{profile} has received an invitation for a files repository ({type_human}) "
"with namespace {sharing_ns!r} at path [{path}]"
).format(
profile=client.profile,
type_human=type_human,
sharing_ns=sharing_ns,
path=path,
)
)
return defer.ensureDeferred(
self.host.plugins['LIST_INTEREST'].register_file_sharing(
self.host.plugins["LIST_INTEREST"].register_file_sharing(
client, service, repos_type, sharing_ns, path, name, extra
)
)

View file

@ -57,20 +57,23 @@ class PubsubInvitation:
in_sign="sssssss",
out_sign="",
method=self._send_pubsub_invitation,
async_=True
async_=True,
)
def register(
self,
namespace: str,
handler
) -> None:
def register(self, namespace: str, handler) -> None:
self._ns_handler[namespace] = handler
self.host.plugins["INVITATION"].register_namespace(namespace, self.on_invitation)
def _send_pubsub_invitation(
self, invitee_jid_s, service_s, node, item_id=None,
name=None, extra_s='', profile_key=C.PROF_KEY_NONE):
self,
invitee_jid_s,
service_s,
node,
item_id=None,
name=None,
extra_s="",
profile_key=C.PROF_KEY_NONE,
):
client = self.host.get_client(profile_key)
invitee_jid = jid.JID(invitee_jid_s)
service = jid.JID(service_s)
@ -83,7 +86,7 @@ class PubsubInvitation:
node,
item_id or None,
name=name or None,
extra=extra
extra=extra,
)
)
@ -94,7 +97,7 @@ class PubsubInvitation:
service: jid.JID,
node: str,
item_id: Optional[str] = None,
name: str = '',
name: str = "",
extra: Optional[dict] = None,
) -> None:
if extra is None:
@ -112,24 +115,26 @@ class PubsubInvitation:
else:
await utils.as_deferred(
preflight,
client, invitee_jid, service, node, item_id, name, extra
client,
invitee_jid,
service,
node,
item_id,
name,
extra,
)
if item_id is None:
item_id = extra.pop("default_item_id", None)
# we authorize our invitee to see the nodes of interest
await self._p.set_node_affiliations(client, service, node, {invitee_jid: "member"})
await self._p.set_node_affiliations(
client, service, node, {invitee_jid: "member"}
)
log.debug(f"affiliation set on {service}'s {node!r} node")
# now we send the invitation
self.host.plugins["INVITATION"].send_pubsub_invitation(
client,
invitee_jid,
service,
node,
item_id,
name=name or None,
extra=extra
client, invitee_jid, service, node, item_id, name=name or None, extra=extra
)
async def on_invitation(
@ -141,7 +146,7 @@ class PubsubInvitation:
service: jid.JID,
node: str,
item_id: Optional[str],
item_elt: domish.Element
item_elt: domish.Element,
) -> None:
if extra is None:
extra = {}
@ -155,7 +160,14 @@ class PubsubInvitation:
else:
await utils.as_deferred(
preflight,
client, namespace, name, extra, service, node, item_id, item_elt
client,
namespace,
name,
extra,
service,
node,
item_id,
item_elt,
)
if item_id is None:
item_id = extra.pop("default_item_id", None)
@ -164,6 +176,6 @@ class PubsubInvitation:
if not name:
name = extra.pop("name", "")
return await self.host.plugins['LIST_INTEREST'].register_pubsub(
client, namespace, service, node, item_id, creator,
name, element, extra)
return await self.host.plugins["LIST_INTEREST"].register_pubsub(
client, namespace, service, node, item_id, creator, name, element, extra
)

View file

@ -217,17 +217,19 @@ class JingleStream(object):
factory.port_listening = port_listening
break
# we don't want to wait for IQ result of initiate
defer.ensureDeferred(self._j.initiate(
client,
to_jid,
[
{
"app_ns": NS_STREAM,
"senders": self._j.ROLE_INITIATOR,
"app_kwargs": {"stream_object": factory},
}
],
))
defer.ensureDeferred(
self._j.initiate(
client,
to_jid,
[
{
"app_ns": NS_STREAM,
"senders": self._j.ROLE_INITIATOR,
"app_kwargs": {"stream_object": factory},
}
],
)
)
return str(port)
def jingle_session_init(self, client, session, content_name, stream_object):
@ -239,7 +241,9 @@ class JingleStream(object):
return desc_elt
@defer.inlineCallbacks
def jingle_request_confirmation(self, client, action, session, content_name, desc_elt):
def jingle_request_confirmation(
self, client, action, session, content_name, desc_elt
):
"""This method request confirmation for a jingle session"""
content_data = session["contents"][content_name]
if content_data["senders"] not in (
@ -287,7 +291,9 @@ class JingleStream(object):
content_data["stream_object"] = application_data["stream_object"]
finished_d = content_data["finished_d"] = defer.Deferred()
args = [client, session, content_name, content_data]
finished_d.addCallbacks(self._finished_cb, self._finished_eb, args, None, args)
finished_d.addCallbacks(
self._finished_cb, self._finished_eb, args, None, args
)
else:
log.warning("FIXME: unmanaged action {}".format(action))
return desc_elt

View file

@ -79,7 +79,7 @@ class LangDetect(object):
return mess_data
def message_received_trigger(self, client, message_elt, post_treat):
""" Check if source is linked and repeat message, else do nothing """
"""Check if source is linked and repeat message, else do nothing"""
lang_detect = self.host.memory.param_get_a(
NAME, CATEGORY, profile_key=client.profile

View file

@ -99,8 +99,18 @@ class ListInterest(object):
if e.condition == "conflict":
log.debug(_("requested node already exists"))
async def register_pubsub(self, client, namespace, service, node, item_id=None,
creator=False, name=None, element=None, extra=None):
async def register_pubsub(
self,
client,
namespace,
service,
node,
item_id=None,
creator=False,
name=None,
element=None,
extra=None,
):
"""Register an interesting element in personal list
@param namespace(unicode): namespace of the interest
@ -123,10 +133,10 @@ class ListInterest(object):
interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
interest_elt["namespace"] = namespace
if name is not None:
interest_elt['name'] = name
thumb_url = extra.get('thumb_url')
interest_elt["name"] = name
thumb_url = extra.get("thumb_url")
if thumb_url:
interest_elt['thumb_url'] = thumb_url
interest_elt["thumb_url"] = thumb_url
pubsub_elt = interest_elt.addElement("pubsub")
pubsub_elt["service"] = service.full()
pubsub_elt["node"] = node
@ -136,12 +146,9 @@ class ListInterest(object):
pubsub_elt["creator"] = C.BOOL_TRUE
if element is not None:
pubsub_elt.addChild(element)
uri_kwargs = {
"path": service.full(),
"node": node
}
uri_kwargs = {"path": service.full(), "node": node}
if item_id:
uri_kwargs['id'] = item_id
uri_kwargs["id"] = item_id
interest_uri = uri.build_xmpp_uri("pubsub", **uri_kwargs)
# we use URI of the interest as item id to avoid duplicates
item_elt = pubsub.Item(interest_uri, payload=interest_elt)
@ -150,16 +157,22 @@ class ListInterest(object):
)
def _register_file_sharing(
self, service, repos_type, namespace, path, name, extra_raw,
profile
self, service, repos_type, namespace, path, name, extra_raw, profile
):
client = self.host.get_client(profile)
extra = data_format.deserialise(extra_raw)
return defer.ensureDeferred(self.register_file_sharing(
client, jid.JID(service), repos_type or None, namespace or None, path or None,
name or None, extra
))
return defer.ensureDeferred(
self.register_file_sharing(
client,
jid.JID(service),
repos_type or None,
namespace or None,
path or None,
name or None,
extra,
)
)
def normalise_file_sharing_service(self, client, service):
# FIXME: Q&D fix as the bare file sharing service JID will lead to user own
@ -167,14 +180,21 @@ class ListInterest(object):
# By specifying the user part, we for the use of the host repository.
# A cleaner way should be implemented
if service.user is None:
service.user = self.host.plugins['XEP-0106'].escape(client.jid.user)
service.user = self.host.plugins["XEP-0106"].escape(client.jid.user)
def get_file_sharing_id(self, service, namespace, path):
return f"{service}_{namespace or ''}_{path or ''}"
async def register_file_sharing(
self, client, service, repos_type=None, namespace=None, path=None, name=None,
extra=None):
self,
client,
service,
repos_type=None,
namespace=None,
path=None,
name=None,
extra=None,
):
"""Register an interesting file repository in personal list
@param service(jid.JID): service of the file repository
@ -192,10 +212,10 @@ class ListInterest(object):
interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
interest_elt["namespace"] = self.host.get_namespace("fis")
if name is not None:
interest_elt['name'] = name
thumb_url = extra.get('thumb_url')
interest_elt["name"] = name
thumb_url = extra.get("thumb_url")
if thumb_url:
interest_elt['thumb_url'] = thumb_url
interest_elt["thumb_url"] = thumb_url
file_sharing_elt = interest_elt.addElement("file_sharing")
file_sharing_elt["service"] = service.full()
@ -213,36 +233,41 @@ class ListInterest(object):
def _list_interests_serialise(self, interests_data):
interests = []
for item_elt in interests_data[0]:
interest_data = {"id": item_elt['id']}
interest_data = {"id": item_elt["id"]}
interest_elt = item_elt.interest
if interest_elt.hasAttribute('namespace'):
interest_data['namespace'] = interest_elt.getAttribute('namespace')
if interest_elt.hasAttribute('name'):
interest_data['name'] = interest_elt.getAttribute('name')
if interest_elt.hasAttribute('thumb_url'):
interest_data['thumb_url'] = interest_elt.getAttribute('thumb_url')
if interest_elt.hasAttribute("namespace"):
interest_data["namespace"] = interest_elt.getAttribute("namespace")
if interest_elt.hasAttribute("name"):
interest_data["name"] = interest_elt.getAttribute("name")
if interest_elt.hasAttribute("thumb_url"):
interest_data["thumb_url"] = interest_elt.getAttribute("thumb_url")
elt = interest_elt.firstChildElement()
if elt.uri != NS_LIST_INTEREST:
log.warning("unexpected child element, ignoring: {xml}".format(
xml = elt.toXml()))
log.warning(
"unexpected child element, ignoring: {xml}".format(xml=elt.toXml())
)
continue
if elt.name == 'pubsub':
interest_data.update({
"type": "pubsub",
"service": elt['service'],
"node": elt['node'],
})
for attr in ('item', 'creator'):
if elt.name == "pubsub":
interest_data.update(
{
"type": "pubsub",
"service": elt["service"],
"node": elt["node"],
}
)
for attr in ("item", "creator"):
if elt.hasAttribute(attr):
interest_data[attr] = elt[attr]
elif elt.name == 'file_sharing':
interest_data.update({
"type": "file_sharing",
"service": elt['service'],
})
if elt.hasAttribute('type'):
interest_data['subtype'] = elt['type']
for attr in ('files_namespace', 'path'):
elif elt.name == "file_sharing":
interest_data.update(
{
"type": "file_sharing",
"service": elt["service"],
}
)
if elt.hasAttribute("type"):
interest_data["subtype"] = elt["type"]
for attr in ("files_namespace", "path"):
if elt.hasAttribute(attr):
interest_data[attr] = elt[attr]
else:
@ -282,8 +307,9 @@ class ListInterest(object):
try:
interest_elt = next(item.elements(NS_LIST_INTEREST, "interest"))
except StopIteration:
log.warning(_("Missing interest element: {xml}").format(
xml=item.toXml()))
log.warning(
_("Missing interest element: {xml}").format(xml=item.toXml())
)
continue
if interest_elt.getAttribute("namespace") == namespace:
filtered_items.append(item)
@ -292,14 +318,15 @@ class ListInterest(object):
return (items, metadata)
def _interest_retract(self, service_s, item_id, profile_key):
d = self._p._retract_item(
service_s, NS_LIST_INTEREST, item_id, True, profile_key)
d = self._p._retract_item(service_s, NS_LIST_INTEREST, item_id, True, profile_key)
d.addCallback(lambda __: None)
return d
async def get(self, client: SatXMPPEntity, item_id: str) -> dict:
"""Retrieve a specific interest in profile's list"""
items_data = await self._p.get_items(client, None, NS_LIST_INTEREST, item_ids=[item_id])
items_data = await self._p.get_items(
client, None, NS_LIST_INTEREST, item_ids=[item_id]
)
try:
return self._list_interests_serialise(items_data)[0]
except IndexError:

Some files were not shown because too many files have changed in this diff Show more