Factorise the /version callback and make it handle errors

Fixes #3376.
This commit is contained in:
Emmanuel Gil Peyrot 2018-03-14 17:18:46 +01:00
parent ae172f84dd
commit 326de0f161
6 changed files with 29 additions and 76 deletions

View file

@ -16,7 +16,6 @@ from slixmpp.xmlstream.handler import Callback
from slixmpp.xmlstream.matcher import StanzaPath from slixmpp.xmlstream.matcher import StanzaPath
from poezio import common from poezio import common
from poezio import fixes
from poezio import pep from poezio import pep
from poezio import tabs from poezio import tabs
from poezio.bookmarks import Bookmark from poezio.bookmarks import Bookmark
@ -297,29 +296,16 @@ class CommandCore:
""" """
/version <jid> /version <jid>
""" """
def callback(res):
"Callback for /version"
if not res:
return self.core.information('Could not get the software'
' version from %s' % jid,
'Warning')
version = '%s is running %s version %s on %s' % (
jid, res.get('name') or 'an unknown software',
res.get('version') or 'unknown',
res.get('os') or 'an unknown platform')
self.core.information(version, 'Info')
if args is None: if args is None:
return self.help('version') return self.help('version')
jid = safeJID(args[0]) jid = safeJID(args[0])
if jid.resource or jid not in roster or not roster[jid].resources: if jid.resource or jid not in roster or not roster[jid].resources:
fixes.get_version(self.core.xmpp, jid, callback=callback) self.core.xmpp.plugin['xep_0092'].get_version(
jid, callback=self.core.handler.on_version_result)
elif jid in roster: elif jid in roster:
for resource in roster[jid].resources: for resource in roster[jid].resources:
fixes.get_version( self.core.xmpp.plugin['xep_0092'].get_version(
self.core.xmpp, resource.jid, callback=callback) resource.jid, callback=self.core.handler.on_version_result)
def _empty_join(self): def _empty_join(self):
tab = self.core.current_tab() tab = self.core.current_tab()

View file

@ -913,6 +913,25 @@ class HandlerCore:
_composing_tab_state(tab, state) _composing_tab_state(tab, state)
self.core.refresh_tab_win() self.core.refresh_tab_win()
def on_version_result(self, iq):
"""
Handle the result of a /version command.
"""
jid = iq['from']
if iq['type'] == 'error':
error_condition = iq['error']['condition']
error_text = iq['error']['text']
reply = '%s: %s' % (error_condition, error_text) if error_text else error_condition
return self.core.information('Could not get the software '
'version from %s: %s' % (jid, reply),
'Warning')
res = iq['software_version']
version = '%s is running %s version %s on %s' % (
jid, res.get('name', 'an unknown software'),
res.get('version', 'unknown'),
res.get('os', 'an unknown platform'))
self.core.information(version, 'Info')
### subscription-related handlers ### ### subscription-related handlers ###
def on_roster_update(self, iq): def on_roster_update(self, iq):

View file

@ -25,23 +25,6 @@ def has_identity(xmpp, jid, identity, on_true=None, on_false=None):
xmpp.plugin['xep_0030'].get_info(jid=jid, callback=_cb) xmpp.plugin['xep_0030'].get_info(jid=jid, callback=_cb)
def get_version(xmpp, jid, callback=None, **kwargs):
def handle_result(res):
if res and res['type'] != 'error':
ret = res['software_version'].values
else:
ret = False
if callback:
callback(ret)
return ret
iq = xmpp.make_iq_get(ito=jid)
iq['query'] = 'jabber:iq:version'
result = iq.send(callback=handle_result if callback else None)
if not callback:
return handle_result(result)
def get_room_form(xmpp, room, callback): def get_room_form(xmpp, room, callback):
def _cb(result): def _cb(result):
if result["type"] == "error": if result["type"] == "error":

View file

@ -19,7 +19,6 @@ import curses
from poezio.tabs.basetabs import OneToOneTab, Tab from poezio.tabs.basetabs import OneToOneTab, Tab
from poezio import common from poezio import common
from poezio import fixes
from poezio import windows from poezio import windows
from poezio import xhtml from poezio import xhtml
from poezio.common import safeJID from poezio.common import safeJID
@ -240,18 +239,6 @@ class ConversationTab(OneToOneTab):
""" """
/version [jid] /version [jid]
""" """
def callback(res):
if not res:
return self.core.information(
'Could not get the software version from %s' % (jid, ),
'Warning')
version = '%s is running %s version %s on %s' % (
jid, res.get('name') or 'an unknown software',
res.get('version') or 'unknown',
res.get('os') or 'an unknown platform')
self.core.information(version, 'Info')
if args: if args:
return self.core.command.version(args[0]) return self.core.command.version(args[0])
jid = safeJID(self.name) jid = safeJID(self.name)
@ -259,7 +246,8 @@ class ConversationTab(OneToOneTab):
if jid in roster: if jid in roster:
resource = roster[jid].get_highest_priority_resource() resource = roster[jid].get_highest_priority_resource()
jid = resource.jid if resource else jid jid = resource.jid if resource else jid
fixes.get_version(self.core.xmpp, jid, callback=callback) self.core.xmpp.plugin['xep_0092'].get_version(
jid, callback=self.core.handler.on_version_result)
@command_args_parser.ignored @command_args_parser.ignored
def command_add(self): def command_add(self):

View file

@ -1340,18 +1340,6 @@ class MucTab(ChatTab):
""" """
/version <jid or nick> /version <jid or nick>
""" """
def callback(res):
if not res:
return self.core.information('Could not get the software '
'version from %s' % (jid, ),
'Warning')
version = '%s is running %s version %s on %s' % (
jid, res.get('name') or 'an unknown software',
res.get('version') or 'unknown',
res.get('os') or 'an unknown platform')
self.core.information(version, 'Info')
if args is None: if args is None:
return self.core.command.help('version') return self.core.command.help('version')
nick = args[0] nick = args[0]
@ -1360,7 +1348,8 @@ class MucTab(ChatTab):
jid = safeJID(jid + '/' + nick) jid = safeJID(jid + '/' + nick)
else: else:
jid = safeJID(nick) jid = safeJID(nick)
fixes.get_version(self.core.xmpp, jid, callback=callback) self.core.xmpp.plugin['xep_0092'].get_version(
jid, callback=self.core.handler.on_version_result)
@command_args_parser.quoted(1) @command_args_parser.quoted(1)
def command_nick(self, args): def command_nick(self, args):

View file

@ -17,7 +17,6 @@ import curses
from poezio.tabs import OneToOneTab, MucTab, Tab from poezio.tabs import OneToOneTab, MucTab, Tab
from poezio import fixes
from poezio import windows from poezio import windows
from poezio import xhtml from poezio import xhtml
from poezio.common import safeJID from poezio.common import safeJID
@ -207,22 +206,11 @@ class PrivateTab(OneToOneTab):
""" """
/version /version
""" """
def callback(res):
if not res:
return self.core.information(
'Could not get the software version from %s' % (jid, ),
'Warning')
version = '%s is running %s version %s on %s' % (
jid, res.get('name') or 'an unknown software',
res.get('version') or 'unknown',
res.get('os') or 'an unknown platform')
self.core.information(version, 'Info')
if args: if args:
return self.core.command.version(args[0]) return self.core.command.version(args[0])
jid = safeJID(self.name) jid = safeJID(self.name)
fixes.get_version(self.core.xmpp, jid, callback=callback) self.core.xmpp.plugin['xep_0092'].get_version(
jid, callback=self.core.handler.on_version_result)
@command_args_parser.quoted(0, 1) @command_args_parser.quoted(0, 1)
def command_info(self, arg): def command_info(self, arg):