comparison sat/plugins/plugin_xep_0092.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 26edcf3a30eb
children 378188abe941
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
22 from twisted.internet import reactor, defer 22 from twisted.internet import reactor, defer
23 from twisted.words.protocols.jabber import jid 23 from twisted.words.protocols.jabber import jid
24 from wokkel import compat 24 from wokkel import compat
25 from sat.core import exceptions 25 from sat.core import exceptions
26 from sat.core.log import getLogger 26 from sat.core.log import getLogger
27
27 log = getLogger(__name__) 28 log = getLogger(__name__)
28 29
29 NS_VERSION = "jabber:iq:version" 30 NS_VERSION = "jabber:iq:version"
30 TIMEOUT = 10 31 TIMEOUT = 10
31 32
35 C.PI_TYPE: "XEP", 36 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: ["XEP-0092"], 37 C.PI_PROTOCOLS: ["XEP-0092"],
37 C.PI_DEPENDENCIES: [], 38 C.PI_DEPENDENCIES: [],
38 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS], 39 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
39 C.PI_MAIN: "XEP_0092", 40 C.PI_MAIN: "XEP_0092",
40 C.PI_HANDLER: "no", # version is already handler in core.xmpp module 41 C.PI_HANDLER: "no", # version is already handler in core.xmpp module
41 C.PI_DESCRIPTION: _("""Implementation of Software Version""") 42 C.PI_DESCRIPTION: _("""Implementation of Software Version"""),
42 } 43 }
43 44
44 45
45 class XEP_0092(object): 46 class XEP_0092(object):
46
47 def __init__(self, host): 47 def __init__(self, host):
48 log.info(_("Plugin XEP_0092 initialization")) 48 log.info(_("Plugin XEP_0092 initialization"))
49 self.host = host 49 self.host = host
50 host.bridge.addMethod("getSoftwareVersion", ".plugin", in_sign='ss', out_sign='(sss)', method=self._getVersion, async=True) 50 host.bridge.addMethod(
51 "getSoftwareVersion",
52 ".plugin",
53 in_sign="ss",
54 out_sign="(sss)",
55 method=self._getVersion,
56 async=True,
57 )
51 try: 58 try:
52 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50) 59 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50)
53 except KeyError: 60 except KeyError:
54 log.info(_("Text commands not available")) 61 log.info(_("Text commands not available"))
55 62
56 def _getVersion(self, entity_jid_s, profile_key): 63 def _getVersion(self, entity_jid_s, profile_key):
57 def prepareForBridge(data): 64 def prepareForBridge(data):
58 name, version, os = data 65 name, version, os = data
59 return (name or '', version or '', os or '') 66 return (name or "", version or "", os or "")
67
60 d = self.getVersion(jid.JID(entity_jid_s), profile_key) 68 d = self.getVersion(jid.JID(entity_jid_s), profile_key)
61 d.addCallback(prepareForBridge) 69 d.addCallback(prepareForBridge)
62 return d 70 return d
63 71
64 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE): 72 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE):
69 - name: Natural language name of the software 77 - name: Natural language name of the software
70 - version: specific version of the software 78 - version: specific version of the software
71 - os: operating system of the queried entity 79 - os: operating system of the queried entity
72 """ 80 """
73 client = self.host.getClient(profile_key) 81 client = self.host.getClient(profile_key)
82
74 def getVersion(dummy): 83 def getVersion(dummy):
75 iq_elt = compat.IQ(client.xmlstream, 'get') 84 iq_elt = compat.IQ(client.xmlstream, "get")
76 iq_elt['to'] = jid_.full() 85 iq_elt["to"] = jid_.full()
77 iq_elt.addElement("query", NS_VERSION) 86 iq_elt.addElement("query", NS_VERSION)
78 d = iq_elt.send() 87 d = iq_elt.send()
79 d.addCallback(self._gotVersion) 88 d.addCallback(self._gotVersion)
80 return d 89 return d
90
81 d = self.host.checkFeature(client, NS_VERSION, jid_) 91 d = self.host.checkFeature(client, NS_VERSION, jid_)
82 d.addCallback(getVersion) 92 d.addCallback(getVersion)
83 reactor.callLater(TIMEOUT, d.cancel) # XXX: timeout needed because some clients don't answer the IQ 93 reactor.callLater(
94 TIMEOUT, d.cancel
95 ) # XXX: timeout needed because some clients don't answer the IQ
84 return d 96 return d
85 97
86 def _gotVersion(self, iq_elt): 98 def _gotVersion(self, iq_elt):
87 try: 99 try:
88 query_elt = iq_elt.elements(NS_VERSION, 'query').next() 100 query_elt = iq_elt.elements(NS_VERSION, "query").next()
89 except StopIteration: 101 except StopIteration:
90 raise exceptions.DataError 102 raise exceptions.DataError
91 ret = [] 103 ret = []
92 for name in ('name', 'version', 'os'): 104 for name in ("name", "version", "os"):
93 try: 105 try:
94 data_elt = query_elt.elements(NS_VERSION, name).next() 106 data_elt = query_elt.elements(NS_VERSION, name).next()
95 ret.append(unicode(data_elt)) 107 ret.append(unicode(data_elt))
96 except StopIteration: 108 except StopIteration:
97 ret.append(None) 109 ret.append(None)
98 110
99 return tuple(ret) 111 return tuple(ret)
100 112
101
102 def _whois(self, client, whois_msg, mess_data, target_jid): 113 def _whois(self, client, whois_msg, mess_data, target_jid):
103 """ Add software/OS information to whois """ 114 """ Add software/OS information to whois """
115
104 def versionCb(version_data): 116 def versionCb(version_data):
105 name, version, os = version_data 117 name, version, os = version_data
106 if name: 118 if name:
107 whois_msg.append(_("Client name: %s") % name) 119 whois_msg.append(_("Client name: %s") % name)
108 if version: 120 if version:
109 whois_msg.append(_("Client version: %s") % version) 121 whois_msg.append(_("Client version: %s") % version)
110 if os: 122 if os:
111 whois_msg.append(_("Operating system: %s") % os) 123 whois_msg.append(_("Operating system: %s") % os)
124
112 def versionEb(failure): 125 def versionEb(failure):
113 failure.trap(exceptions.FeatureNotFound, defer.CancelledError) 126 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
114 if failure.check(failure,exceptions.FeatureNotFound): 127 if failure.check(failure, exceptions.FeatureNotFound):
115 whois_msg.append(_("Software version not available")) 128 whois_msg.append(_("Software version not available"))
116 else: 129 else:
117 whois_msg.append(_("Client software version request timeout")) 130 whois_msg.append(_("Client software version request timeout"))
118 131
119 d = self.getVersion(target_jid, client.profile) 132 d = self.getVersion(target_jid, client.profile)
120 d.addCallbacks(versionCb, versionEb) 133 d.addCallbacks(versionCb, versionEb)
121 return d 134 return d
122