plugins/mod_uptime.lua

changeset 3232
c47bfd62701c
parent 2923
b7049746bd29
child 3540
bc139431830b
equal deleted inserted replaced
3231:ad3fbed1dda5 3232:c47bfd62701c
9 local st = require "util.stanza"; 9 local st = require "util.stanza";
10 10
11 local start_time = prosody.start_time; 11 local start_time = prosody.start_time;
12 prosody.events.add_handler("server-started", function() start_time = prosody.start_time end); 12 prosody.events.add_handler("server-started", function() start_time = prosody.start_time end);
13 13
14 -- XEP-0012: Last activity
14 module:add_feature("jabber:iq:last"); 15 module:add_feature("jabber:iq:last");
15 16
16 module:hook("iq/host/jabber:iq:last:query", function(event) 17 module:hook("iq/host/jabber:iq:last:query", function(event)
17 local origin, stanza = event.origin, event.stanza; 18 local origin, stanza = event.origin, event.stanza;
18 if stanza.attr.type == "get" then 19 if stanza.attr.type == "get" then
19 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:last", seconds = tostring(os.difftime(os.time(), start_time))})); 20 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:last", seconds = tostring(os.difftime(os.time(), start_time))}));
20 return true; 21 return true;
21 end 22 end
22 end); 23 end);
24
25 -- Ad-hoc command
26 local adhoc_new = module:require "adhoc".new;
27
28 function uptime_text()
29 local t = os.time()-prosody.start_time;
30 local seconds = t%60;
31 t = (t - seconds)/60;
32 local minutes = t%60;
33 t = (t - minutes)/60;
34 local hours = t%24;
35 t = (t - hours)/24;
36 local days = t;
37 return string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
38 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
39 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
40 end
41
42 function uptime_command_handler (self, data, state)
43 return { info = uptime_text(), status = "completed" };
44 end
45
46 local descriptor = adhoc_new("Get uptime", "uptime", uptime_command_handler);
47
48 module:add_item ("adhoc", descriptor);

mercurial