Annotate

mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua @ 79:34f5818c90e9

mod_proxy65: answer with correct disco#info data on a disco#info request
author Thilo Cestonaro <thilo@cestona.ro>
date Sun, 01 Nov 2009 14:37:28 +0100
parent 38:b9bf8a35b064
child 109:9b63fd1196c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
1 -- Copyright (C) 2009 Florian Zeitz
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
2 --
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
3 -- This file is MIT/X11 licensed. Please see the
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
4 -- COPYING file in the source package for more information.
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
5 --
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
6
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
7 local _G = _G;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
8 local prosody = _G.prosody;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
9 local st, uuid = require "util.stanza", require "util.uuid";
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
10 local adhoc_new = module:require "adhoc".new;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
11
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
12 function uptime()
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
13 local t = os.time()-prosody.start_time;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
14 local seconds = t%60;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
15 t = (t - seconds)/60;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
16 local minutes = t%60;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
17 t = (t - minutes)/60;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
18 local hours = t%24;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
19 t = (t - hours)/24;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
20 local days = t;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
21 return string.format("This server has been running for %d day%s, %d hour%s and %d minute%s (since %s)",
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
22 days, (days ~= 1 and "s") or "", hours, (hours ~= 1 and "s") or "",
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
23 minutes, (minutes ~= 1 and "s") or "", os.date("%c", prosody.start_time));
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
24 end
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
25
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
26 function uptime_command_handler (item, origin, stanza)
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
27 origin.send(st.reply(stanza):add_child(item:cmdtag("completed", uuid.generate()):tag("note", {type="info"}):text(uptime())));
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 return true;
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29 end
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
31 local descriptor = adhoc_new("Get uptime", "uptime", uptime_command_handler);
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
32
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33 function module.unload()
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
34 module:remove_item("adhoc", descriptor);
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
35 end
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
36
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
37 module:add_item ("adhoc", descriptor);
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
38