Mercurial > prosody-modules
comparison mod_list_inactive/mod_list_inactive.lua @ 1497:d1836dfa4ae4
mod_list_inactive: Add different output formats
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 26 Aug 2014 16:15:31 +0200 |
| parents | cccb151a4cc5 |
| children | e4c3d335b07f |
comparison
equal
deleted
inserted
replaced
| 1496:cccb151a4cc5 | 1497:d1836dfa4ae4 |
|---|---|
| 10 w = 604800, -- week | 10 w = 604800, -- week |
| 11 m = 2629746, -- month | 11 m = 2629746, -- month |
| 12 y = 31556952, -- year | 12 y = 31556952, -- year |
| 13 } | 13 } |
| 14 | 14 |
| 15 local output_formats = { | |
| 16 default = "%s", | |
| 17 event = "%s %s", | |
| 18 delete = "user:delete%q -- %s" | |
| 19 } | |
| 20 | |
| 15 function module.command(arg) | 21 function module.command(arg) |
| 16 local items = {}; | 22 local items = {}; |
| 17 local host = arg[1]; | 23 local host = arg[1]; |
| 18 assert(hosts[host], "Host "..tostring(host).." does not exist"); | 24 assert(hosts[host], "Host "..tostring(host).." does not exist"); |
| 19 sm.initialize_host(host); | 25 sm.initialize_host(host); |
| 20 um.initialize_host(host); | 26 um.initialize_host(host); |
| 21 | 27 |
| 22 local max_age, unit = assert(arg[2], "No time range given"):match("^(%d*)%s*([dwmy]?)"); | 28 local max_age, unit = assert(arg[2], "No time range given"):match("^(%d*)%s*([dwmy]?)"); |
| 23 max_age = os.time() - ( tonumber(max_age) or 1 ) * ( multipliers[unit] or 1 ); | 29 max_age = os.time() - ( tonumber(max_age) or 1 ) * ( multipliers[unit] or 1 ); |
| 30 | |
| 31 local output = assert(output_formats[arg[3] or "default"], "No such output format: "..tostring(arg[3] or "default")); | |
| 32 | |
| 24 for user in um.users(host) do | 33 for user in um.users(host) do |
| 25 local last_active = dm_load(user, host, "lastlog"); | 34 local last_active = dm_load(user, host, "lastlog"); |
| 35 local last_action = last_active and last_active.event or "?" | |
| 26 last_active = last_active and last_active.timestamp or 0; | 36 last_active = last_active and last_active.timestamp or 0; |
| 27 if last_active < max_age then | 37 if last_active < max_age then |
| 28 print(("user:delete%q"):format(jid_join(user, host))); | 38 print(output:format(jid_join(user, host), last_action)); |
| 29 end | 39 end |
| 30 end | 40 end |
| 31 end | 41 end |
| 32 | 42 |
