annotate sat/plugins/plugin_xep_0352.py @ 2873:e1207b8ad97c

plugin text syntaxes: disable raw HTML parsing in mardown by default
author Goffi <goffi@goffi.org>
date Mon, 25 Mar 2019 08:14:00 +0100
parents 9a019db21f3c
children 0c54970d8e6e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2871
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
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
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
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 = {
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
28 C.PI_NAME: u"Client State Indication",
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
29 C.PI_IMPORT_NAME: u"XEP-0352",
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
30 C.PI_TYPE: C.PLUG_TYPE_XEP,
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_PROTOCOLS: [u"XEP-0352"],
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_DEPENDENCIES: [],
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_MAIN: u"XEP_0352",
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_HANDLER: u"no",
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_DESCRIPTION: D_(u"Notify server when frontend is not actively used, to limit "
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
36 u"traffic and save bandwidth and battery life"),
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
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
39 NS_CSI = u"urn:xmpp:csi:0"
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):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
45 log.info(_(u"Client State Indication plugin initialization"))
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.host = host
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
47 host.registerNamespace(u"csi", NS_CSI)
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):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
50 if not client._xep_0352_enabled:
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
52 return client._xep_0352_active
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
53
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def profileConnected(self, client):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if (NS_CSI, u'csi') in client.xmlstream.features:
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
56 log.info(_(u"Client State Indication is available on this server"))
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
57 client._xep_0352_enabled = True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
58 client._xep_0352_active = True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
59 else:
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
60 log.warning(_(u"Client State Indication is not available on this server, some"
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
61 u" bandwidth optimisations can't be used."))
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
62 client._xep_0352_enabled = False
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
63
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def setInactive(self, client):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
65 if self.isActive(client):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
66 inactive_elt = domish.Element((NS_CSI, u'inactive'))
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
67 client.send(inactive_elt)
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
68 client._xep_0352_active = False
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
69 log.info(u"inactive state set")
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 setActive(self, client):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
72 if not self.isActive(client):
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
73 active_elt = domish.Element((NS_CSI, u'active'))
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
74 client.send(active_elt)
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
75 client._xep_0352_active = True
9a019db21f3c plugin XEP-0352: implementation of Client State Indication
Goffi <goffi@goffi.org>
parents:
diff changeset
76 log.info(u"active state set")