Annotate

mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua @ 121:a9898f13c89e

mod_adhoc: Major refactoring. Actuall data exchange happens here now mod_adhoc_cmd_*: Update to work with aforementioned change
author Florian Zeitz <florob@babelmonkeys.de>
date Fri, 22 Jan 2010 04:25:58 +0100
parent 109:9b63fd1196c0
child 124:843cadf36306
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
121
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 109
diff changeset
26 function uptime_command_handler (self, data, sessid)
a9898f13c89e mod_adhoc: Major refactoring. Actuall data exchange happens here now
Florian Zeitz <florob@babelmonkeys.de>
parents: 109
diff changeset
27 return { info = uptime(), status = "completed" }, uuid.generate();
38
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
28 end
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
29
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
30 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
31
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
32 module:add_item ("adhoc", descriptor);
b9bf8a35b064 mod_adhoc_cmd_uptime: Initial commit
Florian Zeitz <florob@babelmonkeys.de>
parents:
diff changeset
33