Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0352.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 | be6d91572633 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
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) |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from twisted.words.xish import domish |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.i18n import _, D_ |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.log import getLogger |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 log = getLogger(__name__) |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 PLUGIN_INFO = { |
3028 | 28 C.PI_NAME: "Client State Indication", |
29 C.PI_IMPORT_NAME: "XEP-0352", | |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 C.PI_TYPE: C.PLUG_TYPE_XEP, |
3028 | 31 C.PI_PROTOCOLS: ["XEP-0352"], |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_DEPENDENCIES: [], |
3028 | 33 C.PI_MAIN: "XEP_0352", |
34 C.PI_HANDLER: "no", | |
35 C.PI_DESCRIPTION: D_("Notify server when frontend is not actively used, to limit " | |
36 "traffic and save bandwidth and battery life"), | |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 } |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
3028 | 39 NS_CSI = "urn:xmpp:csi:0" |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 class XEP_0352(object): |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 def __init__(self, host): |
3028 | 45 log.info(_("Client State Indication plugin initialization")) |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 self.host = host |
3028 | 47 host.registerNamespace("csi", NS_CSI) |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def isActive(self, client): |
2882
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
50 try: |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
51 if not client._xep_0352_enabled: |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
52 return True |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
53 return client._xep_0352_active |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
54 except AttributeError: |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
55 # _xep_0352_active can not be set if isActive is called before |
0c54970d8e6e
plugin android: fixed csi_timer reset in setActive + crash on call of isActive before session initialisation
Goffi <goffi@goffi.org>
parents:
2871
diff
changeset
|
56 # profileConnected has been called |
3028 | 57 log.debug("isActive called when XEP-0352 plugin has not yet set the " |
58 "attributes") | |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 return True |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def profileConnected(self, client): |
3028 | 62 if (NS_CSI, 'csi') in client.xmlstream.features: |
63 log.info(_("Client State Indication is available on this server")) | |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 client._xep_0352_enabled = True |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 client._xep_0352_active = True |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 else: |
3028 | 67 log.warning(_("Client State Indication is not available on this server, some" |
68 " bandwidth optimisations can't be used.")) | |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 client._xep_0352_enabled = False |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 def setInactive(self, client): |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 if self.isActive(client): |
3028 | 73 inactive_elt = domish.Element((NS_CSI, 'inactive')) |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 client.send(inactive_elt) |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 client._xep_0352_active = False |
3028 | 76 log.info("inactive state set") |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 def setActive(self, client): |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 if not self.isActive(client): |
3028 | 80 active_elt = domish.Element((NS_CSI, 'active')) |
2871
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 client.send(active_elt) |
9a019db21f3c
plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 client._xep_0352_active = True |
3028 | 83 log.info("active state set") |