annotate libervia/backend/plugins/plugin_xep_0352.py @ 4351:6a0a081485b8

plugin autocrypt: Autocrypt protocol implementation: Implementation of autocrypt: `autocrypt` header is checked, and if present and no public key is known for the peer, the key is imported. `autocrypt` header is also added to outgoing message (only if an email gateway is detected). For the moment, the JID is use as identifier, but the real email used by gateway should be used in the future. rel 456
author Goffi <goffi@goffi.org>
date Fri, 28 Feb 2025 09:23:35 +0100
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
1 #!/usr/bin/env python3
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
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
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3137
diff changeset
5 # Copyright (C) 2009-2021 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
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
21 from libervia.backend.core.i18n import _, D_
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 from libervia.backend.core.log import getLogger
2871
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
28 C.PI_NAME: "Client State Indication",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
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
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
33 C.PI_MAIN: "XEP_0352",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
34 C.PI_HANDLER: "no",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
35 C.PI_DESCRIPTION: D_(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
36 "Notify server when frontend is not actively used, to limit "
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
37 "traffic and save bandwidth and battery life"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
38 ),
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
40
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
41 NS_CSI = "urn:xmpp:csi:0"
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
42
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 class XEP_0352(object):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
45
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
47 log.info(_("Client State Indication plugin initialization"))
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
49 host.register_namespace("csi", NS_CSI)
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
50
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
51 def is_active(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
52 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
53 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
54 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
55 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
56 except AttributeError:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
57 # _xep_0352_active can not be set if is_active is called before
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
58 # profile_connected has been called
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
59 log.debug(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
60 "is_active called when XEP-0352 plugin has not yet set the " "attributes"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
61 )
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
62 return True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
63
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
64 def profile_connected(self, client):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
65 if (NS_CSI, "csi") in client.xmlstream.features:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
66 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
67 client._xep_0352_enabled = True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
68 client._xep_0352_active = True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
69 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
70 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
71 _(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
72 "Client State Indication is not available on this server, some"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
73 " bandwidth optimisations can't be used."
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
74 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
75 )
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
76 client._xep_0352_enabled = False
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
77
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
78 def set_inactive(self, client):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
79 if self.is_active(client):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
80 inactive_elt = domish.Element((NS_CSI, "inactive"))
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
81 client.send(inactive_elt)
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
82 client._xep_0352_active = False
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
83 log.info("inactive state set")
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
84
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
85 def set_active(self, client):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3479
diff changeset
86 if not self.is_active(client):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
87 active_elt = domish.Element((NS_CSI, "active"))
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
88 client.send(active_elt)
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
89 client._xep_0352_active = True
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2882
diff changeset
90 log.info("active state set")