Annotate

mod_adhoc_cmd_uptime/mod_adhoc_cmd_uptime.lua @ 312:31deafcbf623

mod_auth_dovecot: Fix various global variable sets/gets, log levels, unclear variable names and change coding style to match more closely the rest of the Prosody code.
author Matthew Wild <mwild1@gmail.com>
date Sat, 08 Jan 2011 17:57:03 +0000
parent 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;
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
9 local st = require "util.stanza";
38
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
124
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
26 function uptime_command_handler (self, data, state)
843cadf36306 mod_adhoc*: Move state handling to mod_adhoc itself
Florian Zeitz <florob@babelmonkeys.de>
parents: 121
diff changeset
27 return { info = uptime(), status = "completed" };
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