comparison src/plugins/plugin_xep_0092.py @ 952:91836a647515

plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
author Goffi <goffi@goffi.org>
date Mon, 31 Mar 2014 12:23:13 +0200
parents 899dc9cd0f35
children 8ca5c990ed92
comparison
equal deleted inserted replaced
951:027a054c6dda 952:91836a647515
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from twisted.internet import reactor, defer
22 from wokkel import compat 23 from wokkel import compat
23 from sat.core import exceptions 24 from sat.core import exceptions
24 from logging import debug, info, warning, error 25 from logging import debug, info, warning, error
25 26
26 NS_VERSION = "jabber:iq:version" 27 NS_VERSION = "jabber:iq:version"
28 TIMEOUT = 10
27 29
28 PLUGIN_INFO = { 30 PLUGIN_INFO = {
29 "name": "Software Version Plugin", 31 "name": "Software Version Plugin",
30 "import_name": "XEP-0092", 32 "import_name": "XEP-0092",
31 "type": "XEP", 33 "type": "XEP",
56 - name: Natural language name of the software 58 - name: Natural language name of the software
57 - version: specific version of the software 59 - version: specific version of the software
58 - os: operating system of the queried entity 60 - os: operating system of the queried entity
59 """ 61 """
60 client = self.host.getClient(profile_key) 62 client = self.host.getClient(profile_key)
61 def getVersion(available): 63 def getVersion(dummy):
62 if not available:
63 raise exceptions.FeatureNotFound
64 iq_elt = compat.IQ(client.xmlstream, 'get') 64 iq_elt = compat.IQ(client.xmlstream, 'get')
65 iq_elt['to'] = jid_.full() 65 iq_elt['to'] = jid_.full()
66 iq_elt.addElement("query", NS_VERSION) 66 iq_elt.addElement("query", NS_VERSION)
67 d = iq_elt.send() 67 d = iq_elt.send()
68 d.addCallback(self._gotVersion) 68 d.addCallback(self._gotVersion)
69 return d 69 return d
70 d = self.host.hasFeature(NS_VERSION, jid_, client.profile) 70 d = self.host.checkFeature(NS_VERSION, jid_, client.profile)
71 d.addCallback(getVersion) 71 d.addCallback(getVersion)
72 reactor.callLater(TIMEOUT, d.cancel) # XXX: timeout needed because some clients don't answer the IQ
72 return d 73 return d
73 74
74 def _gotVersion(self, iq_elt): 75 def _gotVersion(self, iq_elt):
75 try: 76 try:
76 query_elt = iq_elt.elements(NS_VERSION, 'query').next() 77 query_elt = iq_elt.elements(NS_VERSION, 'query').next()
96 if version: 97 if version:
97 whois_msg.append(_("Client version: %s") % version) 98 whois_msg.append(_("Client version: %s") % version)
98 if os: 99 if os:
99 whois_msg.append(_("Operating system: %s") % os) 100 whois_msg.append(_("Operating system: %s") % os)
100 def versionEb(failure): 101 def versionEb(failure):
101 failure.trap(exceptions.FeatureNotFound) 102 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
102 whois_msg.append(_("Software informations not available")) 103 if failure.check(failure,exceptions.FeatureNotFound):
104 whois_msg.append(_("Software version not available"))
105 else:
106 whois_msg.append(_("Client software version request timeout"))
103 107
104 d = self.getVersion(target_jid, profile) 108 d = self.getVersion(target_jid, profile)
105 d.addCallbacks(versionCb, versionEb) 109 d.addCallbacks(versionCb, versionEb)
106 return d 110 return d
107 111