Mercurial > prosody-modules
annotate mod_lastlog/mod_lastlog.lua @ 1048:50bed746aa16
mod_lastlog: Fix command
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 03 Jun 2013 19:35:13 +0200 |
parents | 38781835c911 |
children | 59f031d1cd38 |
rev | line source |
---|---|
615
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local datamanager = require "util.datamanager"; |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local time = os.time; |
616
884ae37d76bf
mod_lastlog: Add option to also log the users IP address.
Kim Alvefur <zash@zash.se>
parents:
615
diff
changeset
|
3 local log_ip = module:get_option_boolean("lastlog_ip_address", false); |
1047
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
4 local host = module.host; |
615
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 module:hook("authentication-success", function(event) |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local session = event.session; |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 if session.username then |
1047
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
9 datamanager.store(session.username, host, "lastlog", { |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
10 event = "login"; |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
11 timestamp = time(), |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
12 ip = log_ip and session.ip or nil, |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
13 }); |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
14 end |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
15 end); |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
16 |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
17 module:hook("resource-unbind", function(event) |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
18 local session = event.session; |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
19 if session.username then |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
20 datamanager.store(session.username, host, "lastlog", { |
38781835c911
mod_lastlog: Keep track of last logout time
Kim Alvefur <zash@zash.se>
parents:
1039
diff
changeset
|
21 event = "logout"; |
615
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 timestamp = time(), |
616
884ae37d76bf
mod_lastlog: Add option to also log the users IP address.
Kim Alvefur <zash@zash.se>
parents:
615
diff
changeset
|
23 ip = log_ip and session.ip or nil, |
615
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 }); |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 end |
4134d0e25242
mod_lastlog: New module to record last time a user authenticated
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end); |
1039
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
27 |
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
28 function module.command(arg) |
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
29 local user, host = require "util.jid".prepped_split(table.remove(arg, 1)); |
1048 | 30 require"core.storagemanager".initialize_host(host); |
31 local lastlog = assert(datamanager.load(user, host, "lastlog")); | |
32 if lastlog then | |
33 print(("Last %s: %s"):format(lastlog.event or "login", | |
34 lastlog.timestamp and os.date("%Y-%m-%d %H:%M:%S", lastlog.timestamp) or "<unknown>")); | |
35 if lastlog.ip then | |
36 print("IP address: "..lastlog.ip); | |
37 end | |
38 else | |
39 print("No record found"); | |
1039
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
40 end |
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
41 return 0; |
3f91f17ddaca
mod_lastlog: Add prosodyctl command (prosodyctl mod_lastlog JID) to show last login time and IP of user (if available)
Matthew Wild <mwild1@gmail.com>
parents:
616
diff
changeset
|
42 end |