comparison sat/plugins/plugin_xep_0092.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_xep_0092.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SàT plugin for Software Version (XEP-0092)
5 # Copyright (C) 2009-2018 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 sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from twisted.internet import reactor, defer
23 from twisted.words.protocols.jabber import jid
24 from wokkel import compat
25 from sat.core import exceptions
26 from sat.core.log import getLogger
27 log = getLogger(__name__)
28
29 NS_VERSION = "jabber:iq:version"
30 TIMEOUT = 10
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "Software Version Plugin",
34 C.PI_IMPORT_NAME: "XEP-0092",
35 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: ["XEP-0092"],
37 C.PI_DEPENDENCIES: [],
38 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
39 C.PI_MAIN: "XEP_0092",
40 C.PI_HANDLER: "no", # version is already handler in core.xmpp module
41 C.PI_DESCRIPTION: _("""Implementation of Software Version""")
42 }
43
44
45 class XEP_0092(object):
46
47 def __init__(self, host):
48 log.info(_("Plugin XEP_0092 initialization"))
49 self.host = host
50 host.bridge.addMethod("getSoftwareVersion", ".plugin", in_sign='ss', out_sign='(sss)', method=self._getVersion, async=True)
51 try:
52 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50)
53 except KeyError:
54 log.info(_("Text commands not available"))
55
56 def _getVersion(self, entity_jid_s, profile_key):
57 def prepareForBridge(data):
58 name, version, os = data
59 return (name or '', version or '', os or '')
60 d = self.getVersion(jid.JID(entity_jid_s), profile_key)
61 d.addCallback(prepareForBridge)
62 return d
63
64 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE):
65 """ Ask version of the client that jid_ is running
66 @param jid_: jid from who we want to know client's version
67 @param profile_key: %(doc_profile_key)s
68 @return (tuple): a defered which fire a tuple with the following data (None if not available):
69 - name: Natural language name of the software
70 - version: specific version of the software
71 - os: operating system of the queried entity
72 """
73 client = self.host.getClient(profile_key)
74 def getVersion(dummy):
75 iq_elt = compat.IQ(client.xmlstream, 'get')
76 iq_elt['to'] = jid_.full()
77 iq_elt.addElement("query", NS_VERSION)
78 d = iq_elt.send()
79 d.addCallback(self._gotVersion)
80 return d
81 d = self.host.checkFeature(client, NS_VERSION, jid_)
82 d.addCallback(getVersion)
83 reactor.callLater(TIMEOUT, d.cancel) # XXX: timeout needed because some clients don't answer the IQ
84 return d
85
86 def _gotVersion(self, iq_elt):
87 try:
88 query_elt = iq_elt.elements(NS_VERSION, 'query').next()
89 except StopIteration:
90 raise exceptions.DataError
91 ret = []
92 for name in ('name', 'version', 'os'):
93 try:
94 data_elt = query_elt.elements(NS_VERSION, name).next()
95 ret.append(unicode(data_elt))
96 except StopIteration:
97 ret.append(None)
98
99 return tuple(ret)
100
101
102 def _whois(self, client, whois_msg, mess_data, target_jid):
103 """ Add software/OS information to whois """
104 def versionCb(version_data):
105 name, version, os = version_data
106 if name:
107 whois_msg.append(_("Client name: %s") % name)
108 if version:
109 whois_msg.append(_("Client version: %s") % version)
110 if os:
111 whois_msg.append(_("Operating system: %s") % os)
112 def versionEb(failure):
113 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
114 if failure.check(failure,exceptions.FeatureNotFound):
115 whois_msg.append(_("Software version not available"))
116 else:
117 whois_msg.append(_("Client software version request timeout"))
118
119 d = self.getVersion(target_jid, client.profile)
120 d.addCallbacks(versionCb, versionEb)
121 return d
122