annotate sat/plugins/plugin_xep_0092.py @ 2886:b06cb71079fa

core (xmpp): new networkEnabled() and networkDisabled() methods: those methods can be called by platform specific plugins when network is known to be (un)available. This way, connection attempts can be cancelled when no network is available, saving resources (notably battery on mobile devices), or attempts can be restarted immediately when network is known to be available again.
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2019 19:05:57 +0200
parents 003b8b4b56a7
children ab2696e34d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for Software Version (XEP-0092)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
22 from twisted.internet import reactor, defer
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
23 from twisted.words.protocols.jabber import jid
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
24 from wokkel import compat
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
26 from sat.core.log import getLogger
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
27
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
28 log = getLogger(__name__)
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
29
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
30 NS_VERSION = "jabber:iq:version"
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
31 TIMEOUT = 10
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
32
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
34 C.PI_NAME: "Software Version Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
35 C.PI_IMPORT_NAME: "XEP-0092",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
36 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
37 C.PI_PROTOCOLS: ["XEP-0092"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
38 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
39 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
40 C.PI_MAIN: "XEP_0092",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 C.PI_HANDLER: "no", # version is already handler in core.xmpp module
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42 C.PI_DESCRIPTION: _("""Implementation of Software Version"""),
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
44
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
45
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
46 class XEP_0092(object):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
48 log.info(_("Plugin XEP_0092 initialization"))
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 host.bridge.addMethod(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 "getSoftwareVersion",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 ".plugin",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 in_sign="ss",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 out_sign="(sss)",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 method=self._getVersion,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 async=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 )
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
58 try:
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 926
diff changeset
59 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
60 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
61 log.info(_("Text commands not available"))
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
62
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
63 def _getVersion(self, entity_jid_s, profile_key):
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
64 def prepareForBridge(data):
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
65 name, version, os = data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 return (name or "", version or "", os or "")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
68 d = self.getVersion(jid.JID(entity_jid_s), profile_key)
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
69 d.addCallback(prepareForBridge)
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
70 return d
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
71
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 """ Ask version of the client that jid_ is running
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
74 @param jid_: jid from who we want to know client's version
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
75 @param profile_key: %(doc_profile_key)s
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
76 @return (tuple): a defered which fire a tuple with the following data (None if not available):
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
77 - name: Natural language name of the software
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
78 - version: specific version of the software
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
79 - os: operating system of the queried entity
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
81 client = self.host.getClient(profile_key)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82
2765
378188abe941 misc: replaced all "dummy" by the more conventional and readable "__" ("_" being used for gettext)
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
83 def getVersion(__):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
84 iq_elt = compat.IQ(client.xmlstream, "get")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 iq_elt["to"] = jid_.full()
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
86 iq_elt.addElement("query", NS_VERSION)
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
87 d = iq_elt.send()
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
88 d.addCallback(self._gotVersion)
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
89 return d
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
90
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
91 d = self.host.checkFeature(client, NS_VERSION, jid_)
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
92 d.addCallback(getVersion)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 reactor.callLater(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 TIMEOUT, d.cancel
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
95 ) # XXX: timeout needed because some clients don't answer the IQ
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
96 return d
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
97
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def _gotVersion(self, iq_elt):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
100 query_elt = iq_elt.elements(NS_VERSION, "query").next()
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
101 except StopIteration:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
102 raise exceptions.DataError
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
103 ret = []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
104 for name in ("name", "version", "os"):
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
105 try:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
106 data_elt = query_elt.elements(NS_VERSION, name).next()
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
107 ret.append(unicode(data_elt))
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
108 except StopIteration:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
109 ret.append(None)
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
110
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return tuple(ret)
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
112
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
113 def _whois(self, client, whois_msg, mess_data, target_jid):
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
114 """ Add software/OS information to whois """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
116 def versionCb(version_data):
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
117 name, version, os = version_data
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
118 if name:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
119 whois_msg.append(_("Client name: %s") % name)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
120 if version:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
121 whois_msg.append(_("Client version: %s") % version)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
122 if os:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
123 whois_msg.append(_("Operating system: %s") % os)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
124
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
125 def versionEb(failure):
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
126 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
127 if failure.check(failure, exceptions.FeatureNotFound):
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
128 whois_msg.append(_("Software version not available"))
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
129 else:
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
130 whois_msg.append(_("Client software version request timeout"))
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
131
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
132 d = self.getVersion(target_jid, client.profile)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
133 d.addCallbacks(versionCb, versionEb)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
134 return d