tools/ejabberd2prosody: Disable generating a config, as the format it generates is...
[prosody.git] / plugins / mod_uptime.lua
index 69662ef03d0bd4a911b3a42a230324862f5dbb48..3f275b2fe7efcdce55c7612c5fe8722080bedc11 100644 (file)
@@ -1,33 +1,48 @@
--- Prosody IM v0.3
--- Copyright (C) 2008-2009 Matthew Wild
--- Copyright (C) 2008-2009 Waqas Hussain
+-- Prosody IM
+-- 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";
 
-\r
-local st = require "util.stanza"\r
-\r
-local jid_split = require "util.jid".split;\r
-local t_concat = table.concat;\r
-\r
-local start_time = os.time();\r
-\r
-module:add_feature("jabber:iq:last");\r
-\r
-module:add_iq_handler({"c2s", "s2sin"}, "jabber:iq:last", \r
-       function (origin, stanza)\r
-               if stanza.tags[1].name == "query" then\r
-                       if stanza.attr.type == "get" then\r
-                               local node, host, resource = jid_split(stanza.attr.to);\r
-                               if node or resource then\r
-                                       -- TODO\r
-                               else\r
-                                       origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:last", seconds = tostring(os.difftime(os.time(), start_time))}));\r
-                                       return true;\r
-                               end\r
-                       end\r
-               end\r
-       end);\r
+local start_time = prosody.start_time;
+module:hook_global("server-started", function() start_time = prosody.start_time end);
+
+-- XEP-0012: Last activity
+module:add_feature("jabber:iq:last");
+
+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);
+
+-- Ad-hoc command
+local adhoc_new = module:require "adhoc".new;
+
+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_item ("adhoc", descriptor);