Mercurial > prosody-modules
annotate mod_lastlog/mod_lastlog.lua @ 1047:38781835c911
mod_lastlog: Keep track of last logout time
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 03 Jun 2013 19:03:42 +0200 |
parents | 3f91f17ddaca |
children | 50bed746aa16 |
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)); |
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
|
30 local lastlog = datamanager.load(user, host, "lastlog") or {}; |
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
|
31 print("Last login: "..(lastlog and os.date("%Y-%m-%d %H:%m:%s", datamanager.load(user, host, "lastlog").time) or "<unknown>")); |
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
|
32 if lastlog.ip then |
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
|
33 print("IP address: "..lastlog.ip); |
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
|
34 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
|
35 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
|
36 end |