2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
This plugin retrieves the uptime of a server.
|
|
|
|
|
|
|
|
Command
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/uptime
|
|
|
|
**Usage:** ``/uptime <jid>``
|
|
|
|
|
|
|
|
Retrieve the uptime of the server of ``jid``.
|
|
|
|
"""
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
2021-04-12 18:43:42 +00:00
|
|
|
from poezio.common import parse_secs_to_str
|
2014-07-24 00:07:08 +00:00
|
|
|
from slixmpp.xmlstream import ET
|
2021-01-30 12:59:42 +00:00
|
|
|
from slixmpp import JID, InvalidJID
|
|
|
|
from slixmpp.exceptions import IqError, IqTimeout
|
2012-02-24 01:14:54 +00:00
|
|
|
|
2018-08-15 11:13:17 +00:00
|
|
|
|
2012-02-24 01:14:54 +00:00
|
|
|
class Plugin(BasePlugin):
|
|
|
|
def init(self):
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.add_command(
|
|
|
|
'uptime',
|
|
|
|
self.command_uptime,
|
|
|
|
usage='<jid>',
|
|
|
|
help='Ask for the uptime of a server or component (see XEP-0012).',
|
|
|
|
short='Get the uptime')
|
2012-02-24 01:14:54 +00:00
|
|
|
|
2021-01-30 12:59:42 +00:00
|
|
|
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'))
|
|
|
|
try:
|
|
|
|
iq = await iq.send()
|
|
|
|
result = iq.xml.find('{jabber:iq:last}query')
|
|
|
|
if result is not None:
|
2018-08-15 11:13:17 +00:00
|
|
|
self.api.information(
|
|
|
|
'Server %s online since %s' %
|
|
|
|
(iq['from'], parse_secs_to_str(
|
2021-01-30 12:59:42 +00:00
|
|
|
int(result.attrib['seconds']))), 'Info')
|
2012-02-24 01:14:54 +00:00
|
|
|
return
|
2021-01-30 12:59:42 +00:00
|
|
|
except (IqError, IqTimeout):
|
|
|
|
pass
|
|
|
|
self.api.information('Could not retrieve uptime', 'Error')
|
2018-08-15 11:13:17 +00:00
|
|
|
|