Commit 46d90bf8 authored by mathieui's avatar mathieui Committed by Link Mauve

plugins: remove callbacks

parent e2224b93
Pipeline #3625 passed with stages
in 4 minutes and 57 seconds
...@@ -51,9 +51,9 @@ class Plugin(BasePlugin): ...@@ -51,9 +51,9 @@ class Plugin(BasePlugin):
else: else:
self.api.information('No Contact Addresses for %s' % iq['from'], 'Error') self.api.information('No Contact Addresses for %s' % iq['from'], 'Error')
def command_disco(self, jid): async def command_disco(self, jid):
try: try:
self.core.xmpp.plugin['xep_0030'].get_info(jid=jid, cached=False, iq = await self.core.xmpp.plugin['xep_0030'].get_info(jid=jid, cached=False)
callback=self.on_disco) self.on_disco(iq)
except InvalidJID as e: except InvalidJID as e:
self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error') self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error')
...@@ -73,7 +73,7 @@ class Plugin(BasePlugin): ...@@ -73,7 +73,7 @@ class Plugin(BasePlugin):
self.api.information('\n'.join(describe(item) for item in items['items']), 'Items') self.api.information('\n'.join(describe(item) for item in items['items']), 'Items')
@command_args_parser.quoted(1, 3) @command_args_parser.quoted(1, 3)
def command_disco(self, args): async def command_disco(self, args):
if args is None: if args is None:
self.core.command.help('disco') self.core.command.help('disco')
return return
...@@ -88,10 +88,14 @@ class Plugin(BasePlugin): ...@@ -88,10 +88,14 @@ class Plugin(BasePlugin):
jid, node, type_ = args jid, node, type_ = args
try: try:
if type_ == 'info': if type_ == 'info':
self.core.xmpp.plugin['xep_0030'].get_info( iq = await self.core.xmpp.plugin['xep_0030'].get_info(
jid=jid, node=node, cached=False, callback=self.on_info) jid=jid, node=node, cached=False
)
self.on_info(iq)
elif type_ == 'items': elif type_ == 'items':
self.core.xmpp.plugin['xep_0030'].get_items( iq = await self.core.xmpp.plugin['xep_0030'].get_items(
jid=jid, node=node, cached=False, callback=self.on_items) jid=jid, node=node, cached=False
)
self.on_items(iq)
except InvalidJID as e: except InvalidJID as e:
self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error') self.api.information('Invalid JID “%s”: %s' % (jid, e), 'Error')
...@@ -21,8 +21,10 @@ Command ...@@ -21,8 +21,10 @@ Command
In a private or a direct conversation, you can do ``/ping`` to ping In a private or a direct conversation, you can do ``/ping`` to ping
the current interlocutor. the current interlocutor.
""" """
import asyncio
from slixmpp import InvalidJID, JID from slixmpp import InvalidJID, JID
from slixmpp.exceptions import IqTimeout
from poezio.decorators import command_args_parser from poezio.decorators import command_args_parser
from poezio.plugin import BasePlugin from poezio.plugin import BasePlugin
from poezio.roster import roster from poezio.roster import roster
...@@ -69,7 +71,7 @@ class Plugin(BasePlugin): ...@@ -69,7 +71,7 @@ class Plugin(BasePlugin):
completion=self.completion_ping) completion=self.completion_ping)
@command_args_parser.raw @command_args_parser.raw
def command_ping(self, arg): async def command_ping(self, arg):
if not arg: if not arg:
return self.core.command.help('ping') return self.core.command.help('ping')
try: try:
...@@ -78,7 +80,10 @@ class Plugin(BasePlugin): ...@@ -78,7 +80,10 @@ class Plugin(BasePlugin):
return self.api.information('Invalid JID: %s' % arg, 'Error') return self.api.information('Invalid JID: %s' % arg, 'Error')
start = time.time() start = time.time()
def callback(iq): try:
iq = await self.core.xmpp.plugin['xep_0199'].send_ping(
jid=jid, timeout=10
)
delay = time.time() - start delay = time.time() - start
error = False error = False
reply = '' reply = ''
...@@ -101,13 +106,11 @@ class Plugin(BasePlugin): ...@@ -101,13 +106,11 @@ class Plugin(BasePlugin):
message = '%s responded to ping after %ss%s' % ( message = '%s responded to ping after %ss%s' % (
jid, round(delay, 4), reply) jid, round(delay, 4), reply)
self.api.information(message, 'Info') self.api.information(message, 'Info')
except IqTimeout:
def timeout(iq):
self.api.information( self.api.information(
'%s did not respond to ping after 10s: timeout' % jid, 'Info') '%s did not respond to ping after 10s: timeout' % jid,
'Info'
self.core.xmpp.plugin['xep_0199'].send_ping( )
jid=jid, callback=callback, timeout=10, timeout_callback=timeout)
def completion_muc_ping(self, the_input): def completion_muc_ping(self, the_input):
users = [user.nick for user in self.api.current_tab().users] users = [user.nick for user in self.api.current_tab().users]
...@@ -117,9 +120,12 @@ class Plugin(BasePlugin): ...@@ -117,9 +120,12 @@ class Plugin(BasePlugin):
@command_args_parser.raw @command_args_parser.raw
def command_private_ping(self, arg): def command_private_ping(self, arg):
if arg: jid = arg
return self.command_ping(arg) if not arg:
self.command_ping(self.api.current_tab().jid) jid = self.api.current_tab().jid
asyncio.ensure_future(
self.command_ping(jid)
)
@command_args_parser.raw @command_args_parser.raw
def command_muc_ping(self, arg): def command_muc_ping(self, arg):
...@@ -134,20 +140,25 @@ class Plugin(BasePlugin): ...@@ -134,20 +140,25 @@ class Plugin(BasePlugin):
jid = JID(arg) jid = JID(arg)
except InvalidJID: except InvalidJID:
return self.api.information('Invalid JID: %s' % arg, 'Error') return self.api.information('Invalid JID: %s' % arg, 'Error')
asyncio.ensure_future(
self.command_ping(jid.full) self.command_ping(jid.full)
)
@command_args_parser.raw @command_args_parser.raw
def command_roster_ping(self, arg): def command_roster_ping(self, arg):
if arg: if arg:
self.command_ping(arg) jid = arg
else: else:
current = self.api.current_tab().selected_row current = self.api.current_tab().selected_row
if isinstance(current, Resource): if isinstance(current, Resource):
self.command_ping(current.jid) jid = current.jid
elif isinstance(current, Contact): elif isinstance(current, Contact):
res = current.get_highest_priority_resource() res = current.get_highest_priority_resource()
if res is not None: if res is not None:
self.command_ping(res.jid) jid =res.jid
asyncio.ensure_future(
self.command_ping(jid)
)
def resources(self): def resources(self):
l = [] l = []
......
...@@ -14,6 +14,8 @@ Command ...@@ -14,6 +14,8 @@ Command
from poezio.plugin import BasePlugin from poezio.plugin import BasePlugin
from poezio.common import parse_secs_to_str, safeJID from poezio.common import parse_secs_to_str, safeJID
from slixmpp.xmlstream import ET from slixmpp.xmlstream import ET
from slixmpp import JID, InvalidJID
from slixmpp.exceptions import IqError, IqTimeout
class Plugin(BasePlugin): class Plugin(BasePlugin):
...@@ -25,19 +27,23 @@ class Plugin(BasePlugin): ...@@ -25,19 +27,23 @@ class Plugin(BasePlugin):
help='Ask for the uptime of a server or component (see XEP-0012).', help='Ask for the uptime of a server or component (see XEP-0012).',
short='Get the uptime') short='Get the uptime')
def command_uptime(self, arg): async def command_uptime(self, arg):
def callback(iq): try:
for query in iq.xml.getiterator('{jabber:iq:last}query'): jid = JID(arg)
except InvalidJID:
return
iq = self.core.xmpp.make_iq_get(ito=jid.server)
iq.append(ET.Element('{jabber:iq:last}query'))
try:
iq = await iq.send()
result = iq.xml.find('{jabber:iq:last}query')
if result is not None:
self.api.information( self.api.information(
'Server %s online since %s' % 'Server %s online since %s' %
(iq['from'], parse_secs_to_str( (iq['from'], parse_secs_to_str(
int(query.attrib['seconds']))), 'Info') int(result.attrib['seconds']))), 'Info')
return return
except (IqError, IqTimeout):
pass
self.api.information('Could not retrieve uptime', 'Error') self.api.information('Could not retrieve uptime', 'Error')
jid = safeJID(arg)
if not jid.server:
return
iq = self.core.xmpp.make_iq_get(ito=jid.server)
iq.append(ET.Element('{jabber:iq:last}query'))
iq.send(callback=callback)
...@@ -25,15 +25,16 @@ Command ...@@ -25,15 +25,16 @@ Command
vcard from the current interlocutor, and in the contact list to do it vcard from the current interlocutor, and in the contact list to do it
on the currently selected contact. on the currently selected contact.
""" """
import asyncio
from poezio.decorators import command_args_parser from poezio.decorators import command_args_parser
from poezio.plugin import BasePlugin from poezio.plugin import BasePlugin
from poezio.roster import roster from poezio.roster import roster
from poezio.common import safeJID
from poezio.contact import Contact, Resource from poezio.contact import Contact, Resource
from poezio.core.structs import Completion from poezio.core.structs import Completion
from poezio import tabs from poezio import tabs
from slixmpp.jid import JID, InvalidJID from slixmpp.jid import JID, InvalidJID
from slixmpp.exceptions import IqTimeout
class Plugin(BasePlugin): class Plugin(BasePlugin):
...@@ -240,19 +241,18 @@ class Plugin(BasePlugin): ...@@ -240,19 +241,18 @@ class Plugin(BasePlugin):
on_cancel = lambda form: self.core.close_tab() on_cancel = lambda form: self.core.close_tab()
self.core.open_new_form(form, on_cancel, on_validate) self.core.open_new_form(form, on_cancel, on_validate)
def _get_vcard(self, jid): async def _get_vcard(self, jid):
'''Send an iq to ask the vCard.''' '''Send an iq to ask the vCard.'''
try:
def timeout_cb(iq): vcard = await self.core.xmpp.plugin['xep_0054'].get_vcard(
jid=jid,
timeout=30,
)
self._handle_vcard(vcard)
except IqTimeout:
self.api.information('Timeout while retrieving vCard for %s' % jid, self.api.information('Timeout while retrieving vCard for %s' % jid,
'Error') 'Error')
return
self.core.xmpp.plugin['xep_0054'].get_vcard(
jid=jid,
timeout=30,
callback=self._handle_vcard,
timeout_callback=timeout_cb)
@command_args_parser.raw @command_args_parser.raw
def command_vcard(self, arg): def command_vcard(self, arg):
...@@ -266,7 +266,9 @@ class Plugin(BasePlugin): ...@@ -266,7 +266,9 @@ class Plugin(BasePlugin):
self.api.information('Invalid JID: %s' % arg, 'Error') self.api.information('Invalid JID: %s' % arg, 'Error')
return return
asyncio.ensure_future(
self._get_vcard(jid) self._get_vcard(jid)
)
@command_args_parser.raw @command_args_parser.raw
def command_private_vcard(self, arg): def command_private_vcard(self, arg):
...@@ -285,10 +287,12 @@ class Plugin(BasePlugin): ...@@ -285,10 +287,12 @@ class Plugin(BasePlugin):
jid = self.api.current_tab().jid.bare + '/' + user.nick jid = self.api.current_tab().jid.bare + '/' + user.nick
else: else:
try: try:
jid = safeJID(arg) jid = JID(arg)
except InvalidJID: except InvalidJID:
return self.api.information('Invalid JID: %s' % arg, 'Error') return self.api.information('Invalid JID: %s' % arg, 'Error')
asyncio.ensure_future(
self._get_vcard(jid) self._get_vcard(jid)
)
@command_args_parser.raw @command_args_parser.raw
def command_roster_vcard(self, arg): def command_roster_vcard(self, arg):
...@@ -297,9 +301,13 @@ class Plugin(BasePlugin): ...@@ -297,9 +301,13 @@ class Plugin(BasePlugin):
return return
current = self.api.current_tab().selected_row current = self.api.current_tab().selected_row
if isinstance(current, Resource): if isinstance(current, Resource):
asyncio.ensure_future(
self._get_vcard(JID(current.jid).bare) self._get_vcard(JID(current.jid).bare)
)
elif isinstance(current, Contact): elif isinstance(current, Contact):
asyncio.ensure_future(
self._get_vcard(current.bare_jid) self._get_vcard(current.bare_jid)
)
def completion_vcard(self, the_input): def completion_vcard(self, the_input):
contacts = [contact.bare_jid for contact in roster.get_contacts()] contacts = [contact.bare_jid for contact in roster.get_contacts()]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment