2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
This plugin allows you to ping an entity.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
You only have to load the plugin.
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
/load ping
|
|
|
|
|
|
|
|
Command
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/ping
|
|
|
|
**Usage (globally):** ``/ping <jid>``
|
|
|
|
|
|
|
|
**Usage (in a MUC tab):** ``/ping <jid or nick>``
|
|
|
|
|
|
|
|
**Usage (in a conversation tab):** ``/ping [jid]``
|
|
|
|
|
|
|
|
Globally, you can do ``/ping jid@example.com`` to get a ping.
|
|
|
|
|
|
|
|
In a MUC, you can either do it to a JID or a nick (``/ping nick`` or ``/ping
|
|
|
|
jid@example.com``).
|
|
|
|
|
|
|
|
In a private or a direct conversation, you can do ``/ping`` to ping
|
|
|
|
the current interlocutor.
|
|
|
|
"""
|
|
|
|
|
2011-11-15 00:15:08 +00:00
|
|
|
from plugin import BasePlugin
|
|
|
|
from roster import roster
|
2013-02-01 16:14:13 +00:00
|
|
|
from common import safeJID
|
2011-11-15 00:15:08 +00:00
|
|
|
import common
|
|
|
|
import tabs
|
|
|
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
|
|
|
self.core.xmpp.register_plugin('xep_0199')
|
2013-06-01 15:00:56 +00:00
|
|
|
self.core.xmpp.plugin['xep_0115'].update_caps()
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_command('ping', self.command_ping,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='<jid>',
|
|
|
|
help='Send a XMPP ping to jid (see XEP-0199).',
|
|
|
|
short='Send a ping',
|
|
|
|
completion=self.completion_ping)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_tab_command(tabs.MucTab, 'ping', self.command_muc_ping,
|
2013-03-01 18:25:31 +00:00
|
|
|
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):
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_tab_command(_class, 'ping', self.command_private_ping,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='[jid]',
|
|
|
|
help='Send a XMPP ping to the current interlocutor or the given JID.',
|
|
|
|
short='Send a ping',
|
|
|
|
completion=self.completion_ping)
|
2011-11-15 00:15:08 +00:00
|
|
|
|
|
|
|
def command_ping(self, arg):
|
|
|
|
if not arg:
|
|
|
|
return
|
2013-02-01 16:14:13 +00:00
|
|
|
jid = safeJID(arg)
|
2011-11-15 00:15:08 +00:00
|
|
|
try:
|
2013-03-01 01:05:18 +00:00
|
|
|
delay = self.core.xmpp.plugin['xep_0199'].ping(jid=jid, timeout=5)
|
2011-11-15 00:15:08 +00:00
|
|
|
except:
|
|
|
|
delay = None
|
|
|
|
if delay is not None:
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information('%s responded to ping after %s s' % (jid, round(delay, 4)), 'Info')
|
2011-11-15 00:15:08 +00:00
|
|
|
else:
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information('%s did not respond to ping' % jid, 'Info')
|
2011-11-15 00:15:08 +00:00
|
|
|
|
|
|
|
def completion_muc_ping(self, the_input):
|
2013-03-08 21:53:35 +00:00
|
|
|
users = [user.nick for user in self.api.current_tab().users]
|
2011-11-15 00:15:08 +00:00
|
|
|
l = [contact.bare_jid for contact in roster.get_contacts()]
|
|
|
|
users.extend(l)
|
2013-03-01 01:05:18 +00:00
|
|
|
return the_input.auto_completion(users, '', quotify=False)
|
2011-11-15 00:15:08 +00:00
|
|
|
|
|
|
|
def command_private_ping(self, arg):
|
2013-03-01 01:05:18 +00:00
|
|
|
if arg:
|
2013-03-01 11:25:21 +00:00
|
|
|
return self.command_ping(arg)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.command_ping(self.api.current_tab().get_name())
|
2011-11-15 00:15:08 +00:00
|
|
|
|
|
|
|
def command_muc_ping(self, arg):
|
2013-03-01 01:05:18 +00:00
|
|
|
if not arg.strip():
|
2011-11-15 00:15:08 +00:00
|
|
|
return
|
2013-03-08 21:53:35 +00:00
|
|
|
user = self.api.current_tab().get_user_by_name(arg)
|
2011-11-15 00:15:08 +00:00
|
|
|
if user:
|
2013-03-08 21:53:35 +00:00
|
|
|
jid = safeJID(self.api.current_tab().get_name())
|
2011-11-15 00:15:08 +00:00
|
|
|
jid.resource = user.nick
|
|
|
|
else:
|
2013-03-01 01:05:18 +00:00
|
|
|
jid = safeJID(arg)
|
2011-11-15 00:15:08 +00:00
|
|
|
self.command_ping(jid.full)
|
|
|
|
|
|
|
|
def completion_ping(self, the_input):
|
|
|
|
l = [contact.bare_jid for contact in roster.get_contacts()]
|
2013-03-01 01:05:18 +00:00
|
|
|
return the_input.auto_completion(l, '', quotify=False)
|
2011-11-15 00:15:08 +00:00
|
|
|
|