core commands: remove uses of callback=

This commit is contained in:
mathieui 2021-01-30 11:51:02 +01:00 committed by Link Mauve
parent 19b58d86f5
commit 048f3d5fd3

View file

@ -301,27 +301,40 @@ class CommandCore:
jid = self.core.tabs.current_tab.jid jid = self.core.tabs.current_tab.jid
if jid is None or not jid.domain: if jid is None or not jid.domain:
return None return None
asyncio.ensure_future(
self._list_async(jid)
)
async def _list_async(self, jid: JID):
jid = JID(jid.domain) jid = JID(jid.domain)
list_tab = tabs.MucListTab(self.core, jid) list_tab = tabs.MucListTab(self.core, jid)
self.core.add_tab(list_tab, True) self.core.add_tab(list_tab, True)
cb = list_tab.on_muc_list_item_received iq = await self.core.xmpp.plugin['xep_0030'].get_items(jid=jid)
self.core.xmpp.plugin['xep_0030'].get_items(jid=jid, callback=cb) list_tab.on_muc_list_item_received(iq)
@command_args_parser.quoted(1) @command_args_parser.quoted(1)
def version(self, args): async def version(self, args):
""" """
/version <jid> /version <jid>
""" """
if args is None: if args is None:
return self.help('version') return self.help('version')
jid = safeJID(args[0]) try:
jid = JID(args[0])
except InvalidJID:
return self.core.information(
'Invalid JID for /version: %s' % args[0],
'Error'
)
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:
self.core.xmpp.plugin['xep_0092'].get_version( iq = await self.core.xmpp.plugin['xep_0092'].get_version(jid)
jid, callback=self.core.handler.on_version_result) self.core.handler.on_version_result(iq)
elif jid in roster: elif jid in roster:
for resource in roster[jid].resources: for resource in roster[jid].resources:
self.core.xmpp.plugin['xep_0092'].get_version( iq = await self.core.xmpp.plugin['xep_0092'].get_version(
resource.jid, callback=self.core.handler.on_version_result) resource.jid
)
self.core.handler.on_version_result(iq)
def _empty_join(self): def _empty_join(self):
tab = self.core.tabs.current_tab tab = self.core.tabs.current_tab
@ -1049,6 +1062,7 @@ class CommandCore:
jid = JID(args[0]).full jid = JID(args[0]).full
except InvalidJID: except InvalidJID:
self.core.information('Invalid JID %s' % args, 'Error') self.core.information('Invalid JID %s' % args, 'Error')
return
current_tab = self.core.tabs.current_tab current_tab = self.core.tabs.current_tab
if jid is None: if jid is None:
@ -1071,20 +1085,20 @@ class CommandCore:
if isinstance(current_tab, chattabs): if isinstance(current_tab, chattabs):
jid = current_tab.jid.bare jid = current_tab.jid.bare
def callback(iq: Iq) -> None: if jid is None:
if iq['type'] == 'error':
return self.core.information(
'Could not block %s.' % jid, 'Error',
)
if iq['type'] == 'result':
return self.core.information('Blocked %s.' % jid, 'Info')
return None
if jid is not None:
self.core.xmpp.plugin['xep_0191'].block(jid, callback=callback)
else:
self.core.information('No specified JID to block', 'Error') self.core.information('No specified JID to block', 'Error')
else:
asyncio.ensure_future(self._block_async(jid))
async def _block_async(self, jid: JID):
"""Block a JID, asynchronously"""
try:
await self.core.xmpp.plugin['xep_0191'].block(jid)
return self.core.information('Blocked %s.' % jid, 'Info')
except (IqError, IqTimeout):
return self.core.information(
'Could not block %s.' % jid, 'Error',
)
@command_args_parser.quoted(0, 1) @command_args_parser.quoted(0, 1)
def unblock(self, args: List[str]) -> None: def unblock(self, args: List[str]) -> None:
@ -1103,6 +1117,7 @@ class CommandCore:
jid = JID(args[0]).full jid = JID(args[0]).full
except InvalidJID: except InvalidJID:
self.core.information('Invalid JID %s' % args, 'Error') self.core.information('Invalid JID %s' % args, 'Error')
return
current_tab = self.core.tabs.current_tab current_tab = self.core.tabs.current_tab
if jid is None: if jid is None:
@ -1126,17 +1141,21 @@ class CommandCore:
jid = current_tab.jid.bare jid = current_tab.jid.bare
if jid is not None: if jid is not None:
def callback(iq: Iq): asyncio.ensure_future(
if iq['type'] == 'error': self._unblock_async(jid)
return self.core.information('Could not unblock the contact.', )
'Error')
elif iq['type'] == 'result':
return self.core.information('Unblocked %s.' % jid, 'Info')
self.core.xmpp.plugin['xep_0191'].unblock(jid, callback=callback)
else: else:
self.core.information('No specified JID to unblock', 'Error') self.core.information('No specified JID to unblock', 'Error')
async def _unblock_async(self, jid: JID):
"""Unblock a JID, asynchrously"""
try:
await self.core.xmpp.plugin['xep_0191'].unblock(jid)
return self.core.information('Unblocked %s.' % jid, 'Info')
except (IqError, IqTimeout):
return self.core.information('Could not unblock the contact.',
'Error')
### Commands without a completion in this class ### ### Commands without a completion in this class ###
@command_args_parser.ignored @command_args_parser.ignored
@ -1344,15 +1363,23 @@ class CommandCore:
self.core.xml_tab = tab self.core.xml_tab = tab
@command_args_parser.quoted(1) @command_args_parser.quoted(1)
def adhoc(self, args): async def adhoc(self, args):
if not args: if not args:
return self.help('ad-hoc') return self.help('ad-hoc')
jid = safeJID(args[0]) try:
jid = JID(args[0])
except InvalidJID:
return self.core.information(
'Invalid JID for ad-hoc command: %s' % args[0],
'Error',
)
list_tab = tabs.AdhocCommandsListTab(self.core, jid) list_tab = tabs.AdhocCommandsListTab(self.core, jid)
self.core.add_tab(list_tab, True) self.core.add_tab(list_tab, True)
cb = list_tab.on_list_received iq = await self.core.xmpp.plugin['xep_0050'].get_commands(
self.core.xmpp.plugin['xep_0050'].get_commands( jid=jid,
jid=jid, local=False, callback=cb) local=False
)
list_tab.on_list_received(iq)
@command_args_parser.ignored @command_args_parser.ignored
def self_(self): def self_(self):