From 70cfacfe1a1b3bb8088bc81323dc2ff467954882 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 9 Jul 2022 16:30:37 +0200 Subject: [PATCH] tools (common/uri): accept URIs without type: URIs without type are used to link JIDs. rel 369 --- sat/tools/common/uri.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sat/tools/common/uri.py b/sat/tools/common/uri.py index 5ff54c1e..e2fd98f9 100644 --- a/sat/tools/common/uri.py +++ b/sat/tools/common/uri.py @@ -19,6 +19,7 @@ """ XMPP uri parsing tools """ +from typing import Optional import sys import urllib.parse import urllib.request, urllib.parse, urllib.error @@ -50,9 +51,13 @@ def parseXMPPUri(uri): data = {"path": urllib.parse.unquote(uri_split.path)} query_end = uri_split.query.find(";") - query_type = uri_split.query[:query_end] - if query_end == -1 or "=" in query_type: - raise ValueError("no query type, invalid XMPP URI") + if query_end == -1: + # we just have a JID + query_type = None + else: + query_type = uri_split.query[:query_end] + if "=" in query_type: + raise ValueError("no query type, invalid XMPP URI") if sys.version_info >= (3, 9): # parse_qs behaviour has been modified in Python 3.9, ";" is not understood as a @@ -92,13 +97,19 @@ def addPairs(uri, pairs): ) -def buildXMPPUri(type_, **kwargs): +def buildXMPPUri(type_: Optional[str] = None, **kwargs: str) -> str: uri = ["xmpp:"] subtype = kwargs.pop("subtype", None) path = kwargs.pop("path") uri.append(urllib.parse.quote_plus(path.encode("utf-8")).replace("%40", "@")) - if type_ == "pubsub": + if type_ is None: + # we have an URI to a JID + if kwargs: + raise NotImplementedError( + "keyword arguments are not supported for URI without type" + ) + elif type_ == "pubsub": if subtype == "microblog" and not kwargs.get("node"): kwargs["node"] = "urn:xmpp:microblog:0" if kwargs: