mod_list_active/mod_list_active.lua
changeset 2641 39c39844cd4c
parent 1770 e4c3d335b07f
child 2644 c06c59b99b3c
equal deleted inserted replaced
2640:9ed6d44b9fed 2641:39c39844cd4c
       
     1 -- Copyright (C) 2012-2013 Kim Alvefur
       
     2 
       
     3 local um = require "core.usermanager";
       
     4 local sm = require "core.storagemanager";
       
     5 local dm = require "util.datamanager";
       
     6 local jid_join = require"util.jid".join;
       
     7 
       
     8 local multipliers = {
       
     9 	d = 86400, -- day
       
    10 	w = 604800, -- week
       
    11 	m = 2629746, -- month
       
    12 	y = 31556952, -- year
       
    13 }
       
    14 
       
    15 local output_formats = {
       
    16 	default = "%s",
       
    17 	event = "%s %s",
       
    18 }
       
    19 
       
    20 function module.command(arg)
       
    21 	if #arg < 2 then
       
    22 		print("usage: prosodyctl mod_list_active example.net time [format]");
       
    23 		print("time is a number followed by 'day', 'week', 'month' or 'year'");
       
    24 		print("formats are:");
       
    25 		for name, fmt in pairs(output_formats) do
       
    26 			print(name, fmt:format("user@example.com", "last action"))
       
    27 		end
       
    28 		return;
       
    29 	end
       
    30 	local items = {};
       
    31 	local host = arg[1];
       
    32 	assert(hosts[host], "Host "..tostring(host).." does not exist");
       
    33 	sm.initialize_host(host);
       
    34 	um.initialize_host(host);
       
    35 
       
    36 	local max_age, unit = assert(arg[2], "No time range given"):match("^(%d*)%s*([dwmy]?)");
       
    37 	max_age = os.time() - ( tonumber(max_age) or 1 ) * ( multipliers[unit] or 1 );
       
    38 
       
    39 	local output = assert(output_formats[arg[3] or "default"], "No such output format: "..tostring(arg[3] or "default"));
       
    40 
       
    41 	for user in dm.users(host, "lastlog") do
       
    42 		local last_active = dm.load(user, host, "lastlog");
       
    43 		local last_action = last_active and last_active.event or "?"
       
    44 		last_active = last_active and last_active.timestamp or 0;
       
    45 		if last_active < max_age then
       
    46 			print(output:format(jid_join(user, host), last_action));
       
    47 		end
       
    48 	end
       
    49 end
       
    50