Mercurial > libervia-backend
annotate src/tools/common/uri.py @ 2227:79d279d1ee88
plugin XEP-0277: comments node access model changes:
comments node access model is now copied from parent item by default. If whitelist access is used, parent item affiliations are copied too.
publish model is now open by default instead of subscribers
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 17 Apr 2017 20:33:32 +0200 |
parents | bdc64c487e21 |
children | 761fa220a717 |
rev | line source |
---|---|
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT: a jabber client |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ XMPP uri parsing tools """ |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import urlparse |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import urllib |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 # FIXME: basic implementation, need to follow RFC 5122 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 def parseXMPPUri(uri): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 """Parse an XMPP uri and return a dict with various information |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 @param uri(unicode): uri to parse |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 @return dict(unicode, unicode): data depending of the URI where key can be: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 type: one of ("pubsub", TODO) |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
33 type is always present |
2216
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 sub_type: can be: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 - microblog |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 only used for pubsub for now |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 path: XMPP path (jid of the service or entity) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 node: node used |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 id: id of the element (item for pubsub) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 @raise ValueError: the scheme is not xmpp |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 """ |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 uri_split = urlparse.urlsplit(uri.encode('utf-8')) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 if uri_split.scheme != 'xmpp': |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 raise ValueError(u'this is not a XMPP URI') |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 data = {u'path': urllib.unquote(uri_split.path).decode('utf-8')} |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 query_end = uri_split.query.find(';') |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 query_type = uri_split.query[:query_end] |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 if query_end == -1 or '=' in query_type: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 raise ValueError('no query type, invalid XMPP URI') |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 pairs = urlparse.parse_qs(uri_split.geturl()) |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 for k, v in pairs.items(): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 if len(v) != 1: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 raise NotImplementedError(u"multiple values not managed") |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 if k in ('path', 'type', 'sub_type'): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 raise NotImplementedError(u"reserved key used in URI, this is not supported") |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 data[k.decode('utf-8')] = urllib.unquote(v[0]).decode('utf-8') |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 if query_type: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 data[u'type'] = query_type.decode('utf-8') |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 elif u'node' in data: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 data[u'type'] = u'pubsub' |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 else: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 data[u'type'] = '' |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if u'node' in data: |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 if data[u'node'].startswith(u'urn:xmpp:microblog:'): |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 data[u'sub_type'] = 'microblog' |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
7e06eafef409
tools(common/uri): XMPP uri parsing module, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 return data |
2222
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
73 |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
74 def addPairs(uri, pairs): |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
75 for k,v in pairs.iteritems(): |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
76 uri.append(u';' + urllib.quote_plus(k) + u'=' + urllib.quote_plus(v)) |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
77 |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
78 def buildXMPPUri(type, **kwargs): |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
79 uri = [u'xmpp:'] |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
80 subtype = kwargs.pop('subtype', None) |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
81 path = kwargs.pop('path') |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
82 uri.append(urllib.quote_plus(path).replace(u'%40', '@')) |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
83 |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
84 if type == u'pubsub': |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
85 if subtype == 'microblog' and not 'node' in kwargs: |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
86 kwargs[u'node'] = 'urn:xmpp:microblog:0' |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
87 uri.append(u'?') |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
88 addPairs(uri, kwargs) |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
89 else: |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
90 raise NotImplementedError(u'{type} URI are not handled yet'.format(type=type)) |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
91 |
bdc64c487e21
tools/common (uri): added a method to build URI
Goffi <goffi@goffi.org>
parents:
2216
diff
changeset
|
92 return u''.join(uri) |