uptime.py 1.13 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3 4 5 6 7 8 9 10 11 12 13
"""
This plugin retrieves the uptime of a server.

Command
-------

.. glossary::

    /uptime
        **Usage:** ``/uptime <jid>``

        Retrieve the uptime of the server of ``jid``.
"""
mathieui's avatar
mathieui committed
14
from plugin import BasePlugin
15
from common import parse_secs_to_str, safeJID
mathieui's avatar
mathieui committed
16 17 18 19
from sleekxmpp.xmlstream import ET

class Plugin(BasePlugin):
    def init(self):
20
        self.api.add_command('uptime', self.command_uptime,
21 22 23
                usage='<jid>',
                help='Ask for the uptime of a server or component (see XEP-0012).',
                short='Get the uptime')
mathieui's avatar
mathieui committed
24 25 26 27

    def command_uptime(self, arg):
        def callback(iq):
            for query in iq.xml.getiterator('{jabber:iq:last}query'):
28
                self.api.information('Server %s online since %s' % (iq['from'], parse_secs_to_str(int(query.attrib['seconds']))), 'Info')
mathieui's avatar
mathieui committed
29
                return
30 31
            self.api.information('Could not retrieve uptime', 'Error')
        jid = safeJID(arg)
mathieui's avatar
mathieui committed
32 33 34 35 36
        if not jid.server:
            return
        iq = self.core.xmpp.makeIqGet(ito=jid.server)
        iq.append(ET.Element('{jabber:iq:last}query'))
        iq.send(block=False, callback=callback)