comparison sat/tools/common/uri.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 003b8b4b56a7
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT: a jabber client 4 # SAT: a jabber client
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 """ XMPP uri parsing tools """ 20 """ XMPP uri parsing tools """
21 21
22 import urlparse 22 import urllib.parse
23 import urllib 23 import urllib.request, urllib.parse, urllib.error
24 24
25 # FIXME: basic implementation, need to follow RFC 5122 25 # FIXME: basic implementation, need to follow RFC 5122
26 26
27 27
28 def parseXMPPUri(uri): 28 def parseXMPPUri(uri):
38 path: XMPP path (jid of the service or entity) 38 path: XMPP path (jid of the service or entity)
39 node: node used 39 node: node used
40 id: id of the element (item for pubsub) 40 id: id of the element (item for pubsub)
41 @raise ValueError: the scheme is not xmpp 41 @raise ValueError: the scheme is not xmpp
42 """ 42 """
43 uri_split = urlparse.urlsplit(uri.encode("utf-8")) 43 uri_split = urllib.parse.urlsplit(uri)
44 if uri_split.scheme != "xmpp": 44 if uri_split.scheme != "xmpp":
45 raise ValueError(u"this is not a XMPP URI") 45 raise ValueError("this is not a XMPP URI")
46 46
47 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend 47 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend
48 # which may use different JID classes 48 # which may use different JID classes
49 data = {u"path": urllib.unquote(uri_split.path).decode("utf-8")} 49 data = {"path": urllib.parse.unquote(uri_split.path)}
50 50
51 query_end = uri_split.query.find(";") 51 query_end = uri_split.query.find(";")
52 query_type = uri_split.query[:query_end] 52 query_type = uri_split.query[:query_end]
53 if query_end == -1 or "=" in query_type: 53 if query_end == -1 or "=" in query_type:
54 raise ValueError("no query type, invalid XMPP URI") 54 raise ValueError("no query type, invalid XMPP URI")
55 55
56 pairs = urlparse.parse_qs(uri_split.geturl()) 56 pairs = urllib.parse.parse_qs(uri_split.geturl())
57 for k, v in pairs.items(): 57 for k, v in list(pairs.items()):
58 if len(v) != 1: 58 if len(v) != 1:
59 raise NotImplementedError(u"multiple values not managed") 59 raise NotImplementedError("multiple values not managed")
60 if k in ("path", "type", "sub_type"): 60 if k in ("path", "type", "sub_type"):
61 raise NotImplementedError(u"reserved key used in URI, this is not supported") 61 raise NotImplementedError("reserved key used in URI, this is not supported")
62 data[k.decode("utf-8")] = urllib.unquote(v[0]).decode("utf-8") 62 data[k] = urllib.parse.unquote(v[0])
63 63
64 if query_type: 64 if query_type:
65 data[u"type"] = query_type.decode("utf-8") 65 data["type"] = query_type
66 elif u"node" in data: 66 elif "node" in data:
67 data[u"type"] = u"pubsub" 67 data["type"] = "pubsub"
68 else: 68 else:
69 data[u"type"] = "" 69 data["type"] = ""
70 70
71 if u"node" in data: 71 if "node" in data:
72 if data[u"node"].startswith(u"urn:xmpp:microblog:"): 72 if data["node"].startswith("urn:xmpp:microblog:"):
73 data[u"sub_type"] = "microblog" 73 data["sub_type"] = "microblog"
74 74
75 return data 75 return data
76 76
77 77
78 def addPairs(uri, pairs): 78 def addPairs(uri, pairs):
79 for k, v in pairs.iteritems(): 79 for k, v in pairs.items():
80 uri.append( 80 uri.append(
81 u";" 81 ";"
82 + urllib.quote_plus(k.encode("utf-8")) 82 + urllib.parse.quote_plus(k.encode("utf-8"))
83 + u"=" 83 + "="
84 + urllib.quote_plus(v.encode("utf-8")) 84 + urllib.parse.quote_plus(v.encode("utf-8"))
85 ) 85 )
86 86
87 87
88 def buildXMPPUri(type_, **kwargs): 88 def buildXMPPUri(type_, **kwargs):
89 uri = [u"xmpp:"] 89 uri = ["xmpp:"]
90 subtype = kwargs.pop("subtype", None) 90 subtype = kwargs.pop("subtype", None)
91 path = kwargs.pop("path") 91 path = kwargs.pop("path")
92 uri.append(urllib.quote_plus(path.encode("utf-8")).replace(u"%40", "@")) 92 uri.append(urllib.parse.quote_plus(path.encode("utf-8")).replace("%40", "@"))
93 93
94 if type_ == u"pubsub": 94 if type_ == "pubsub":
95 if subtype == "microblog" and not kwargs.get("node"): 95 if subtype == "microblog" and not kwargs.get("node"):
96 kwargs[u"node"] = "urn:xmpp:microblog:0" 96 kwargs["node"] = "urn:xmpp:microblog:0"
97 if kwargs: 97 if kwargs:
98 uri.append(u"?") 98 uri.append("?")
99 addPairs(uri, kwargs) 99 addPairs(uri, kwargs)
100 else: 100 else:
101 raise NotImplementedError(u"{type_} URI are not handled yet".format(type_=type_)) 101 raise NotImplementedError("{type_} URI are not handled yet".format(type_=type_))
102 102
103 return u"".join(uri) 103 return "".join(uri)