comparison sat/plugins/plugin_xep_0092.py @ 4066:e75827204fe0

plugin XEP-0092: use `client` instead of `profile_key` in `version_get`
author Goffi <goffi@goffi.org>
date Tue, 30 May 2023 22:23:55 +0200
parents 524856bd7b19
children
comparison
equal deleted inserted replaced
4065:34c8e7e4fa52 4066:e75827204fe0
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
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 typing import Tuple
21 from sat.core.constants import Const as C 21
22 from twisted.internet import reactor, defer 22 from twisted.internet import defer, reactor
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
25 from sat.core import exceptions 26 from sat.core import exceptions
27 from sat.core.constants import Const as C
28 from sat.core.core_types import SatXMPPEntity
29 from sat.core.i18n import _
26 from sat.core.log import getLogger 30 from sat.core.log import getLogger
27 31
28 log = getLogger(__name__) 32 log = getLogger(__name__)
29 33
30 NS_VERSION = "jabber:iq:version" 34 NS_VERSION = "jabber:iq:version"
63 def _get_version(self, entity_jid_s, profile_key): 67 def _get_version(self, entity_jid_s, profile_key):
64 def prepare_for_bridge(data): 68 def prepare_for_bridge(data):
65 name, version, os = data 69 name, version, os = data
66 return (name or "", version or "", os or "") 70 return (name or "", version or "", os or "")
67 71
68 d = self.version_get(jid.JID(entity_jid_s), profile_key) 72 client = self.host.get_client(profile_key)
73 d = self.version_get(client, jid.JID(entity_jid_s))
69 d.addCallback(prepare_for_bridge) 74 d.addCallback(prepare_for_bridge)
70 return d 75 return d
71 76
72 def version_get(self, jid_, profile_key=C.PROF_KEY_NONE): 77 def version_get(
73 """ Ask version of the client that jid_ is running 78 self,
79 client: SatXMPPEntity,
80 jid_: jid.JID,
81 ) -> Tuple[str, str, str]:
82 """Ask version of the client that jid_ is running
83
74 @param jid_: jid from who we want to know client's version 84 @param jid_: jid from who we want to know client's version
75 @param profile_key: %(doc_profile_key)s 85 @return: a defered which fire a tuple with the following data (None if not available):
76 @return (tuple): a defered which fire a tuple with the following data (None if not available): 86 - name: Natural language name of the software
77 - name: Natural language name of the software 87 - version: specific version of the software
78 - version: specific version of the software 88 - os: operating system of the queried entity
79 - os: operating system of the queried entity
80 """ 89 """
81 client = self.host.get_client(profile_key)
82 90
83 def version_get(__): 91 def do_version_get(__):
84 iq_elt = compat.IQ(client.xmlstream, "get") 92 iq_elt = compat.IQ(client.xmlstream, "get")
85 iq_elt["to"] = jid_.full() 93 iq_elt["to"] = jid_.full()
86 iq_elt.addElement("query", NS_VERSION) 94 iq_elt.addElement("query", NS_VERSION)
87 d = iq_elt.send() 95 d = iq_elt.send()
88 d.addCallback(self._got_version) 96 d.addCallback(self._got_version)
89 return d 97 return d
90 98
91 d = self.host.check_feature(client, NS_VERSION, jid_) 99 d = self.host.check_feature(client, NS_VERSION, jid_)
92 d.addCallback(version_get) 100 d.addCallback(do_version_get)
93 reactor.callLater( 101 reactor.callLater(
94 TIMEOUT, d.cancel 102 TIMEOUT, d.cancel
95 ) # XXX: timeout needed because some clients don't answer the IQ 103 ) # XXX: timeout needed because some clients don't answer the IQ
96 return d 104 return d
97 105
109 ret.append(None) 117 ret.append(None)
110 118
111 return tuple(ret) 119 return tuple(ret)
112 120
113 def _whois(self, client, whois_msg, mess_data, target_jid): 121 def _whois(self, client, whois_msg, mess_data, target_jid):
114 """ Add software/OS information to whois """ 122 """Add software/OS information to whois"""
115 123
116 def version_cb(version_data): 124 def version_cb(version_data):
117 name, version, os = version_data 125 name, version, os = version_data
118 if name: 126 if name:
119 whois_msg.append(_("Client name: %s") % name) 127 whois_msg.append(_("Client name: %s") % name)
127 if failure.check(failure, exceptions.FeatureNotFound): 135 if failure.check(failure, exceptions.FeatureNotFound):
128 whois_msg.append(_("Software version not available")) 136 whois_msg.append(_("Software version not available"))
129 else: 137 else:
130 whois_msg.append(_("Client software version request timeout")) 138 whois_msg.append(_("Client software version request timeout"))
131 139
132 d = self.version_get(target_jid, client.profile) 140 d = self.version_get(client, target_jid)
133 d.addCallbacks(version_cb, version_eb) 141 d.addCallbacks(version_cb, version_eb)
134 return d 142 return d