Mercurial > prosody-modules
comparison mod_lastlog2/mod_lastlog2.lua @ 4012:fd582067c732
mod_lastlog2: Store last timestamp per account event
Incompatible with mod_lastlog and other modules using its data due to
use of map store
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 29 Mar 2020 15:09:25 +0200 |
parents | mod_lastlog/mod_lastlog.lua@d5e8758d391d |
children | 4e7ff27c212c |
comparison
equal
deleted
inserted
replaced
4011:de40686ae9c8 | 4012:fd582067c732 |
---|---|
1 local jid = require "util.jid"; | |
2 local time = os.time; | |
3 local log_ip = module:get_option_boolean("lastlog_ip_address", false); | |
4 | |
5 local store; | |
6 if module.host ~= "*" then -- workaround for prosodyctl loading into global context | |
7 store = module:open_store(nil, "map"); | |
8 end | |
9 | |
10 module:hook("authentication-success", function(event) | |
11 local session = event.session; | |
12 if session.username then | |
13 store:set(session.username, "login", { | |
14 timestamp = time(), | |
15 ip = log_ip and session and session.ip or nil, | |
16 }); | |
17 end | |
18 end); | |
19 | |
20 module:hook("resource-unbind", function(event) | |
21 local session = event.session; | |
22 if session.username then | |
23 store:set(session.username, "logout", { | |
24 timestamp = time(), | |
25 ip = log_ip and session and session.ip or nil, | |
26 }); | |
27 end | |
28 end); | |
29 | |
30 module:hook("user-registered", function(event) | |
31 local session = event.session; | |
32 store:set(event.username, "lastlog", { | |
33 event = "registered"; | |
34 timestamp = time(), | |
35 ip = log_ip and session and session.ip or nil, | |
36 }); | |
37 end); | |
38 | |
39 | |
40 if module:get_host_type() == "component" then | |
41 module:hook("message/bare", function(event) | |
42 local room = jid.split(event.stanza.attr.to); | |
43 if room then | |
44 store:set(room, module.host, "message", { | |
45 timestamp = time(), | |
46 }); | |
47 end | |
48 end); | |
49 end | |
50 | |
51 function module.command(arg) | |
52 if not arg[1] or arg[1] == "--help" then | |
53 require"util.prosodyctl".show_usage([[mod_lastlog <user@host>]], [[Show when user last logged in or out]]); | |
54 return 1; | |
55 end | |
56 local user, host = jid.prepped_split(table.remove(arg, 1)); | |
57 require"core.storagemanager".initialize_host(host); | |
58 store = module:context(host):open_store(); | |
59 local lastlog = store:get(user); | |
60 if lastlog then | |
61 for event, data in pairs(lastlog) do | |
62 print(("Last %s: %s"):format(event, | |
63 data.timestamp and os.date("%Y-%m-%d %H:%M:%S", data.timestamp) or "<unknown>")); | |
64 if lastlog.ip then | |
65 print("IP address: "..lastlog.ip); | |
66 end | |
67 end | |
68 else | |
69 print("No record found"); | |
70 return 1; | |
71 end | |
72 return 0; | |
73 end |