X-Git-Url: https://git.enpas.org/?a=blobdiff_plain;f=plugins%2Fmod_uptime.lua;h=2e369b16f38a25ab21d363bfe7ebb90171eda64f;hb=73e841ddd082009e3f19502b207231bbddfa0dbb;hp=f0a428d2f1312029e115708221de27842e32395e;hpb=ba596f33614438d0f42713ca39423bdc88eb224d;p=prosody.git diff --git a/plugins/mod_uptime.lua b/plugins/mod_uptime.lua index f0a428d2..2e369b16 100644 --- a/plugins/mod_uptime.lua +++ b/plugins/mod_uptime.lua @@ -1,33 +1,48 @@ -- Prosody IM --- Copyright (C) 2008-2009 Matthew Wild --- Copyright (C) 2008-2009 Waqas Hussain --- +-- Copyright (C) 2008-2010 Matthew Wild +-- Copyright (C) 2008-2010 Waqas Hussain +-- -- This project is MIT/X11 licensed. Please see the -- COPYING file in the source package for more information. -- +local st = require "util.stanza"; +local start_time = prosody.start_time; +module:hook_global("server-started", function() start_time = prosody.start_time end); -local st = require "util.stanza" +-- XEP-0012: Last activity +module:add_feature("jabber:iq:last"); -local jid_split = require "util.jid".split; -local t_concat = table.concat; +module:hook("iq/host/jabber:iq:last:query", function(event) + local origin, stanza = event.origin, event.stanza; + if stanza.attr.type == "get" then + origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:last", seconds = tostring(os.difftime(os.time(), start_time))})); + return true; + end +end); -local start_time = prosody.start_time; +-- Ad-hoc command +local adhoc_new = module:require "adhoc".new; -module:add_feature("jabber:iq:last"); +function uptime_text() + local t = os.time()-prosody.start_time; + local seconds = t%60; + t = (t - seconds)/60; + local minutes = t%60; + t = (t - minutes)/60; + local hours = t%24; + t = (t - hours)/24; + local days = t; + return string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)", + days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "", + minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time)); +end + +function uptime_command_handler (self, data, state) + return { info = uptime_text(), status = "completed" }; +end + +local descriptor = adhoc_new("Get uptime", "uptime", uptime_command_handler); -module:add_iq_handler({"c2s", "s2sin"}, "jabber:iq:last", - function (origin, stanza) - if stanza.tags[1].name == "query" then - if stanza.attr.type == "get" then - local node, host, resource = jid_split(stanza.attr.to); - if node or resource then - -- TODO - else - origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:last", seconds = tostring(os.difftime(os.time(), start_time))})); - return true; - end - end - end - end); +module:add_item ("adhoc", descriptor);