Software /
code /
prosody-modules
Diff
mod_list_active/mod_list_active.lua @ 2637:39c39844cd4c
mod_list_active: Inverse of mod_list_inactive (closes #705)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 22 Mar 2017 14:53:15 +0100 |
parent | 1766:e4c3d335b07f |
child | 2640:c06c59b99b3c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_list_active/mod_list_active.lua Wed Mar 22 14:53:15 2017 +0100 @@ -0,0 +1,50 @@ +-- Copyright (C) 2012-2013 Kim Alvefur + +local um = require "core.usermanager"; +local sm = require "core.storagemanager"; +local dm = require "util.datamanager"; +local jid_join = require"util.jid".join; + +local multipliers = { + d = 86400, -- day + w = 604800, -- week + m = 2629746, -- month + y = 31556952, -- year +} + +local output_formats = { + default = "%s", + event = "%s %s", +} + +function module.command(arg) + if #arg < 2 then + print("usage: prosodyctl mod_list_active example.net time [format]"); + print("time is a number followed by 'day', 'week', 'month' or 'year'"); + print("formats are:"); + for name, fmt in pairs(output_formats) do + print(name, fmt:format("user@example.com", "last action")) + end + return; + end + local items = {}; + local host = arg[1]; + assert(hosts[host], "Host "..tostring(host).." does not exist"); + sm.initialize_host(host); + um.initialize_host(host); + + local max_age, unit = assert(arg[2], "No time range given"):match("^(%d*)%s*([dwmy]?)"); + max_age = os.time() - ( tonumber(max_age) or 1 ) * ( multipliers[unit] or 1 ); + + local output = assert(output_formats[arg[3] or "default"], "No such output format: "..tostring(arg[3] or "default")); + + for user in dm.users(host, "lastlog") do + local last_active = dm.load(user, host, "lastlog"); + local last_action = last_active and last_active.event or "?" + last_active = last_active and last_active.timestamp or 0; + if last_active < max_age then + print(output:format(jid_join(user, host), last_action)); + end + end +end +