comparison sat/tools/common/uri.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/tools/common/uri.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
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/>.
19
20 """ XMPP uri parsing tools """
21
22 import urlparse
23 import urllib
24
25 # FIXME: basic implementation, need to follow RFC 5122
26
27 def parseXMPPUri(uri):
28 """Parse an XMPP uri and return a dict with various information
29
30 @param uri(unicode): uri to parse
31 @return dict(unicode, unicode): data depending of the URI where key can be:
32 type: one of ("pubsub", TODO)
33 type is always present
34 sub_type: can be:
35 - microblog
36 only used for pubsub for now
37 path: XMPP path (jid of the service or entity)
38 node: node used
39 id: id of the element (item for pubsub)
40 @raise ValueError: the scheme is not xmpp
41 """
42 uri_split = urlparse.urlsplit(uri.encode('utf-8'))
43 if uri_split.scheme != 'xmpp':
44 raise ValueError(u'this is not a XMPP URI')
45
46 # XXX: we don't use jid.JID for path as it can be used both in backend and frontend
47 # which may use different JID classes
48 data = {u'path': urllib.unquote(uri_split.path).decode('utf-8')}
49
50 query_end = uri_split.query.find(';')
51 query_type = uri_split.query[:query_end]
52 if query_end == -1 or '=' in query_type:
53 raise ValueError('no query type, invalid XMPP URI')
54
55 pairs = urlparse.parse_qs(uri_split.geturl())
56 for k, v in pairs.items():
57 if len(v) != 1:
58 raise NotImplementedError(u"multiple values not managed")
59 if k in ('path', 'type', 'sub_type'):
60 raise NotImplementedError(u"reserved key used in URI, this is not supported")
61 data[k.decode('utf-8')] = urllib.unquote(v[0]).decode('utf-8')
62
63 if query_type:
64 data[u'type'] = query_type.decode('utf-8')
65 elif u'node' in data:
66 data[u'type'] = u'pubsub'
67 else:
68 data[u'type'] = ''
69
70 if u'node' in data:
71 if data[u'node'].startswith(u'urn:xmpp:microblog:'):
72 data[u'sub_type'] = 'microblog'
73
74 return data
75
76 def addPairs(uri, pairs):
77 for k,v in pairs.iteritems():
78 uri.append(u';' + urllib.quote_plus(k.encode('utf-8')) + u'=' + urllib.quote_plus(v.encode('utf-8')))
79
80 def buildXMPPUri(type_, **kwargs):
81 uri = [u'xmpp:']
82 subtype = kwargs.pop('subtype', None)
83 path = kwargs.pop('path')
84 uri.append(urllib.quote_plus(path.encode('utf-8')).replace(u'%40', '@'))
85
86 if type_ == u'pubsub':
87 if subtype == 'microblog' and not kwargs.get('node'):
88 kwargs[u'node'] = 'urn:xmpp:microblog:0'
89 if kwargs:
90 uri.append(u'?')
91 addPairs(uri, kwargs)
92 else:
93 raise NotImplementedError(u'{type_} URI are not handled yet'.format(type_=type_))
94
95 return u''.join(uri)