poezio/plugins/ping.py
mathieui e1956533a6 Fix #2231 (update the plugins to use the new help system)
And fix some imprecisions/mistakes in the help.
2013-03-01 19:25:31 +01:00

66 lines
2.4 KiB
Python

from plugin import BasePlugin
from roster import roster
from common import safeJID
import common
import tabs
class Plugin(BasePlugin):
def init(self):
self.core.xmpp.register_plugin('xep_0199')
self.add_command('ping', self.command_ping,
usage='<jid>',
help='Send a XMPP ping to jid (see XEP-0199).',
short='Send a ping',
completion=self.completion_ping)
self.add_tab_command(tabs.MucTab, 'ping', self.command_muc_ping,
usage='<jid|nick>',
help='Send a XMPP ping to jid or nick (see XEP-0199).',
short='Send a ping.',
completion=self.completion_muc_ping)
for _class in (tabs.PrivateTab, tabs.ConversationTab):
self.add_tab_command(_class, 'ping', self.command_private_ping,
usage='[jid]',
help='Send a XMPP ping to the current interlocutor or the given JID.',
short='Send a ping',
completion=self.completion_ping)
def command_ping(self, arg):
if not arg:
return
jid = safeJID(arg)
try:
delay = self.core.xmpp.plugin['xep_0199'].ping(jid=jid, timeout=5)
except:
delay = None
if delay is not None:
self.core.information('%s responded to ping after %s s' % (jid, round(delay, 4)), 'Info')
else:
self.core.information('%s did not respond to ping' % jid, 'Info')
def completion_muc_ping(self, the_input):
users = [user.nick for user in self.core.current_tab().users]
l = [contact.bare_jid for contact in roster.get_contacts()]
users.extend(l)
return the_input.auto_completion(users, '', quotify=False)
def command_private_ping(self, arg):
if arg:
return self.command_ping(arg)
self.command_ping(self.core.current_tab().get_name())
def command_muc_ping(self, arg):
if not arg.strip():
return
user = self.core.current_tab().get_user_by_name(arg)
if user:
jid = safeJID(self.core.current_tab().get_name())
jid.resource = user.nick
else:
jid = safeJID(arg)
self.command_ping(jid.full)
def completion_ping(self, the_input):
l = [contact.bare_jid for contact in roster.get_contacts()]
return the_input.auto_completion(l, '', quotify=False)