Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0106.py @ 3254:6cf4bd6972c2
core, frontends: avatar refactoring:
/!\ huge commit
Avatar logic has been reworked around the IDENTITY plugin: plugins able to handle avatar
or other identity related metadata (like nicknames) register to IDENTITY plugin in the
same way as for other features like download/upload. Once registered, IDENTITY plugin will
call them when suitable in order of priority, and handle caching.
Methods to manage those metadata from frontend now use serialised data.
For now `avatar` and `nicknames` are handled:
- `avatar` is now a dict with `path` + metadata like `media_type`, instead of just a string
path
- `nicknames` is now a list of nicknames in order of priority. This list is never empty,
and `nicknames[0]` should be the preferred nickname to use by frontends in most cases.
In addition to contact specified nicknames, user set nickname (the one set in roster) is
used in priority when available.
Among the side changes done with this commit, there are:
- a new `contactGet` bridge method to get roster metadata for a single contact
- SatPresenceProtocol.send returns a Deferred to check when it has actually been sent
- memory's methods to handle entities data now use `client` as first argument
- metadata filter can be specified with `getIdentity`
- `getAvatar` and `setAvatar` are now part of the IDENTITY plugin instead of XEP-0054 (and
there signature has changed)
- `isRoom` and `getBareOrFull` are now part of XEP-0045 plugin
- jp avatar/get command uses `xdg-open` first when available for `--show` flag
- `--no-cache` has been added to jp avatar/get and identity/get
- jp identity/set has been simplified, explicit options (`--nickname` only for now) are
used instead of `--field`. `--field` may come back in the future if necessary for extra
data.
- QuickContactList `SetContact` now handle None as a value, and doesn't use it to delete the
metadata anymore
- improved cache handling for `metadata` and `nicknames` in quick frontend
- new `default` argument in QuickContactList `getCache`
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 14 Apr 2020 21:00:33 +0200 |
parents | 559a625a236b |
children | 66c4fc1f7c30 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Explicit Message Encryption |
3136 | 5 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org) |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.protocols.jabber import xmlstream |
3028 | 24 from zope.interface import implementer |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from wokkel import disco |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 log = getLogger(__name__) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 PLUGIN_INFO = { |
3028 | 30 C.PI_NAME: "JID Escaping", |
31 C.PI_IMPORT_NAME: "XEP-0106", | |
32 C.PI_TYPE: "XEP", | |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_MODES: C.PLUG_MODE_BOTH, |
3028 | 34 C.PI_PROTOCOLS: ["XEP-0106"], |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_DEPENDENCIES: [], |
3028 | 36 C.PI_MAIN: "XEP_0106", |
37 C.PI_HANDLER: "yes", | |
38 C.PI_DESCRIPTION: _("""(Un)escape JID to use disallowed chars in local parts"""), | |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
3028 | 41 NS_JID_ESCAPING = r"jid\20escaping" |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 ESCAPE_MAP = { |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 ' ': r'\20', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 '"': r'\22', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 '&': r'\26', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 "'": r'\27', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 '/': r'\2f', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 ':': r'\3a', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 '<': r'\3c', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 '>': r'\3e', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 '@': r'\40', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 '\\': r'\5c', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 } |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 class XEP_0106(object): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def __init__(self, host): |
3028 | 59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.items()} |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def getHandler(self, client): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 return XEP_0106_handler() |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def escape(self, text): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 """Escape text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 @param text(unicode): text to escape |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 @return (unicode): escaped text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 @raise ValueError: text can't be escaped |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 """ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 if not text or text[0] == ' ' or text[-1] == ' ': |
3028 | 72 raise ValueError("text must not be empty, or start or end with a whitespace") |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 escaped = [] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 for c in text: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 if c in ESCAPE_MAP: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 escaped.append(ESCAPE_MAP[c]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 else: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 escaped.append(c) |
3028 | 79 return ''.join(escaped) |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 def unescape(self, escaped): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 """Unescape text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 @param escaped(unicode): text to unescape |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 @return (unicode): unescaped text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 @raise ValueError: text can't be unescaped |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 """ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 if not escaped or escaped.startswith(r'\27') or escaped.endswith(r'\27'): |
3028 | 89 raise ValueError("escaped value must not be empty, or start or end with a " |
90 "whitespace") | |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 unescaped = [] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 idx = 0 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 while idx < len(escaped): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 char_seq = escaped[idx:idx+3] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 if char_seq in self.reverse_map: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 unescaped.append(self.reverse_map[char_seq]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 idx += 3 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 else: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 unescaped.append(escaped[idx]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 idx += 1 |
3028 | 101 return ''.join(unescaped) |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
3028 | 104 @implementer(disco.IDisco) |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 class XEP_0106_handler(xmlstream.XMPPHandler): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return [disco.DiscoFeature(NS_JID_ESCAPING)] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 return [] |