diff --git a/plugins/contact.py b/plugins/contact.py index 461a1875..a3a0514b 100644 --- a/plugins/contact.py +++ b/plugins/contact.py @@ -51,9 +51,9 @@ class Plugin(BasePlugin): else: self.api.information('No Contact Addresses for %s' % iq['from'], 'Error') - def command_disco(self, jid): + async def command_disco(self, jid): try: - self.core.xmpp.plugin['xep_0030'].get_info(jid=jid, cached=False, - callback=self.on_disco) + iq = await self.core.xmpp.plugin['xep_0030'].get_info(jid=jid, cached=False) + self.on_disco(iq) except InvalidJID as e: self.api.information('Invalid JID ā€œ%sā€: %s' % (jid, e), 'Error') diff --git a/plugins/disco.py b/plugins/disco.py index 368b35d8..52963d39 100644 --- a/plugins/disco.py +++ b/plugins/disco.py @@ -73,7 +73,7 @@ class Plugin(BasePlugin): self.api.information('\n'.join(describe(item) for item in items['items']), 'Items') @command_args_parser.quoted(1, 3) - def command_disco(self, args): + async def command_disco(self, args): if args is None: self.core.command.help('disco') return @@ -88,10 +88,14 @@ class Plugin(BasePlugin): jid, node, type_ = args try: if type_ == 'info': - self.core.xmpp.plugin['xep_0030'].get_info( - jid=jid, node=node, cached=False, callback=self.on_info) + iq = await self.core.xmpp.plugin['xep_0030'].get_info( + jid=jid, node=node, cached=False + ) + self.on_info(iq) elif type_ == 'items': - self.core.xmpp.plugin['xep_0030'].get_items( - jid=jid, node=node, cached=False, callback=self.on_items) + iq = await self.core.xmpp.plugin['xep_0030'].get_items( + jid=jid, node=node, cached=False + ) + self.on_items(iq) except InvalidJID as e: self.api.information('Invalid JID ā€œ%sā€: %s' % (jid, e), 'Error') diff --git a/plugins/ping.py b/plugins/ping.py index 4a3ba8ef..46ce4efc 100644 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -21,8 +21,10 @@ Command In a private or a direct conversation, you can do ``/ping`` to ping the current interlocutor. """ +import asyncio from slixmpp import InvalidJID, JID +from slixmpp.exceptions import IqTimeout from poezio.decorators import command_args_parser from poezio.plugin import BasePlugin from poezio.roster import roster @@ -69,7 +71,7 @@ class Plugin(BasePlugin): completion=self.completion_ping) @command_args_parser.raw - def command_ping(self, arg): + async def command_ping(self, arg): if not arg: return self.core.command.help('ping') try: @@ -78,7 +80,10 @@ class Plugin(BasePlugin): return self.api.information('Invalid JID: %s' % arg, 'Error') 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 error = False reply = '' @@ -101,13 +106,11 @@ class Plugin(BasePlugin): message = '%s responded to ping after %ss%s' % ( jid, round(delay, 4), reply) self.api.information(message, 'Info') - - def timeout(iq): + except IqTimeout: self.api.information( - '%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) + '%s did not respond to ping after 10s: timeout' % jid, + 'Info' + ) def completion_muc_ping(self, the_input): users = [user.nick for user in self.api.current_tab().users] @@ -117,9 +120,12 @@ class Plugin(BasePlugin): @command_args_parser.raw def command_private_ping(self, arg): - if arg: - return self.command_ping(arg) - self.command_ping(self.api.current_tab().jid) + jid = arg + if not arg: + jid = self.api.current_tab().jid + asyncio.ensure_future( + self.command_ping(jid) + ) @command_args_parser.raw def command_muc_ping(self, arg): @@ -134,20 +140,25 @@ class Plugin(BasePlugin): jid = JID(arg) except InvalidJID: return self.api.information('Invalid JID: %s' % arg, 'Error') - self.command_ping(jid.full) + asyncio.ensure_future( + self.command_ping(jid.full) + ) @command_args_parser.raw def command_roster_ping(self, arg): if arg: - self.command_ping(arg) + jid = arg else: current = self.api.current_tab().selected_row if isinstance(current, Resource): - self.command_ping(current.jid) + jid = current.jid elif isinstance(current, Contact): res = current.get_highest_priority_resource() if res is not None: - self.command_ping(res.jid) + jid =res.jid + asyncio.ensure_future( + self.command_ping(jid) + ) def resources(self): l = [] diff --git a/plugins/uptime.py b/plugins/uptime.py index d5a07b7b..eca89cc5 100644 --- a/plugins/uptime.py +++ b/plugins/uptime.py @@ -14,6 +14,8 @@ Command from poezio.plugin import BasePlugin from poezio.common import parse_secs_to_str, safeJID from slixmpp.xmlstream import ET +from slixmpp import JID, InvalidJID +from slixmpp.exceptions import IqError, IqTimeout class Plugin(BasePlugin): @@ -25,19 +27,23 @@ class Plugin(BasePlugin): help='Ask for the uptime of a server or component (see XEP-0012).', short='Get the uptime') - def command_uptime(self, arg): - def callback(iq): - for query in iq.xml.getiterator('{jabber:iq:last}query'): - self.api.information( - 'Server %s online since %s' % - (iq['from'], parse_secs_to_str( - int(query.attrib['seconds']))), 'Info') - return - self.api.information('Could not retrieve uptime', 'Error') - - jid = safeJID(arg) - if not jid.server: + async def command_uptime(self, arg): + try: + jid = JID(arg) + except InvalidJID: return iq = self.core.xmpp.make_iq_get(ito=jid.server) iq.append(ET.Element('{jabber:iq:last}query')) - iq.send(callback=callback) + try: + iq = await iq.send() + result = iq.xml.find('{jabber:iq:last}query') + if result is not None: + self.api.information( + 'Server %s online since %s' % + (iq['from'], parse_secs_to_str( + int(result.attrib['seconds']))), 'Info') + return + except (IqError, IqTimeout): + pass + self.api.information('Could not retrieve uptime', 'Error') + diff --git a/plugins/vcard.py b/plugins/vcard.py index e3a776e3..ef70dc79 100644 --- a/plugins/vcard.py +++ b/plugins/vcard.py @@ -25,15 +25,16 @@ Command vcard from the current interlocutor, and in the contact list to do it on the currently selected contact. """ +import asyncio from poezio.decorators import command_args_parser from poezio.plugin import BasePlugin from poezio.roster import roster -from poezio.common import safeJID from poezio.contact import Contact, Resource from poezio.core.structs import Completion from poezio import tabs from slixmpp.jid import JID, InvalidJID +from slixmpp.exceptions import IqTimeout class Plugin(BasePlugin): @@ -240,19 +241,18 @@ class Plugin(BasePlugin): on_cancel = lambda form: self.core.close_tab() 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.''' - - def timeout_cb(iq): + try: + 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, '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 def command_vcard(self, arg): @@ -266,7 +266,9 @@ class Plugin(BasePlugin): self.api.information('Invalid JID: %s' % arg, 'Error') return - self._get_vcard(jid) + asyncio.ensure_future( + self._get_vcard(jid) + ) @command_args_parser.raw def command_private_vcard(self, arg): @@ -285,10 +287,12 @@ class Plugin(BasePlugin): jid = self.api.current_tab().jid.bare + '/' + user.nick else: try: - jid = safeJID(arg) + jid = JID(arg) except InvalidJID: return self.api.information('Invalid JID: %s' % arg, 'Error') - self._get_vcard(jid) + asyncio.ensure_future( + self._get_vcard(jid) + ) @command_args_parser.raw def command_roster_vcard(self, arg): @@ -297,9 +301,13 @@ class Plugin(BasePlugin): return current = self.api.current_tab().selected_row if isinstance(current, Resource): - self._get_vcard(JID(current.jid).bare) + asyncio.ensure_future( + self._get_vcard(JID(current.jid).bare) + ) elif isinstance(current, Contact): - self._get_vcard(current.bare_jid) + asyncio.ensure_future( + self._get_vcard(current.bare_jid) + ) def completion_vcard(self, the_input): contacts = [contact.bare_jid for contact in roster.get_contacts()]