diff options
Diffstat (limited to 'plugins/ping.py')
-rw-r--r-- | plugins/ping.py | 57 |
1 files changed, 37 insertions, 20 deletions
diff --git a/plugins/ping.py b/plugins/ping.py index 4868ccf9..cc987bf0 100644 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -21,11 +21,13 @@ 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 -from poezio.common import safeJID from poezio.contact import Contact, Resource from poezio.core.structs import Completion from poezio import tabs @@ -57,7 +59,7 @@ class Plugin(BasePlugin): help='Send an XMPP ping to jid (see XEP-0199).', short='Send a ping.', completion=self.completion_ping) - for _class in (tabs.PrivateTab, tabs.ConversationTab): + for _class in (tabs.PrivateTab, tabs.DynamicConversationTab, tabs.StaticConversationTab): self.api.add_tab_command( _class, 'ping', @@ -69,13 +71,19 @@ 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') - jid = safeJID(arg) + try: + jid = JID(arg) + except InvalidJID: + 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 = '' @@ -98,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] @@ -114,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().name) + jid = arg + if not arg: + jid = self.api.current_tab().jid + asyncio.create_task( + self.command_ping(jid) + ) @command_args_parser.raw def command_muc_ping(self, arg): @@ -124,24 +133,32 @@ class Plugin(BasePlugin): return user = self.api.current_tab().get_user_by_name(arg) if user: - jid = safeJID(self.api.current_tab().name) + jid = self.api.current_tab().jid jid.resource = user.nick else: - jid = safeJID(arg) - self.command_ping(jid.full) + try: + jid = JID(arg) + except InvalidJID: + return self.api.information('Invalid JID: %s' % arg, 'Error') + asyncio.create_task( + 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.create_task( + self.command_ping(jid) + ) def resources(self): l = [] |