comparison libervia/backend/plugins/plugin_xep_0352.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0352.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for Explicit Message Encryption
5 # Copyright (C) 2009-2021 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 from twisted.words.xish import domish
21 from libervia.backend.core.i18n import _, D_
22 from libervia.backend.core.constants import Const as C
23 from libervia.backend.core.log import getLogger
24
25 log = getLogger(__name__)
26
27 PLUGIN_INFO = {
28 C.PI_NAME: "Client State Indication",
29 C.PI_IMPORT_NAME: "XEP-0352",
30 C.PI_TYPE: C.PLUG_TYPE_XEP,
31 C.PI_PROTOCOLS: ["XEP-0352"],
32 C.PI_DEPENDENCIES: [],
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"),
37 }
38
39 NS_CSI = "urn:xmpp:csi:0"
40
41
42 class XEP_0352(object):
43
44 def __init__(self, host):
45 log.info(_("Client State Indication plugin initialization"))
46 self.host = host
47 host.register_namespace("csi", NS_CSI)
48
49 def is_active(self, client):
50 try:
51 if not client._xep_0352_enabled:
52 return True
53 return client._xep_0352_active
54 except AttributeError:
55 # _xep_0352_active can not be set if is_active is called before
56 # profile_connected has been called
57 log.debug("is_active called when XEP-0352 plugin has not yet set the "
58 "attributes")
59 return True
60
61 def profile_connected(self, client):
62 if (NS_CSI, 'csi') in client.xmlstream.features:
63 log.info(_("Client State Indication is available on this server"))
64 client._xep_0352_enabled = True
65 client._xep_0352_active = True
66 else:
67 log.warning(_("Client State Indication is not available on this server, some"
68 " bandwidth optimisations can't be used."))
69 client._xep_0352_enabled = False
70
71 def set_inactive(self, client):
72 if self.is_active(client):
73 inactive_elt = domish.Element((NS_CSI, 'inactive'))
74 client.send(inactive_elt)
75 client._xep_0352_active = False
76 log.info("inactive state set")
77
78 def set_active(self, client):
79 if not self.is_active(client):
80 active_elt = domish.Element((NS_CSI, 'active'))
81 client.send(active_elt)
82 client._xep_0352_active = True
83 log.info("active state set")