init.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. auth2 = auth2 or {}
  2. auth2.modpath = minetest.get_modpath("auth2")
  3. function auth2.last_login(name, param)
  4. if param == "" then
  5. param = name
  6. end
  7. -- Disallow getting the administrator's last login time.
  8. if not gdac.player_is_admin(param) then
  9. local pauth = minetest.get_auth_handler().get_auth(param)
  10. if pauth and pauth.last_login and pauth.last_login ~= -1 then
  11. local hours = math.floor((os.time() - pauth.last_login) / (60 * 60))
  12. local time_since = ""
  13. if hours == 1 then
  14. time_since = "Was " .. hours .. " hour ago."
  15. else
  16. time_since = "Was " .. hours .. " hours ago."
  17. end
  18. -- Time in UTC, ISO 8601 format
  19. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(param) ..
  20. ">'s last login time was " ..
  21. os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login) .. ". " .. time_since)
  22. return true
  23. end
  24. end
  25. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(param) ..
  26. ">'s last login time is unknown.")
  27. return true
  28. end
  29. if not auth2.run_once then
  30. -- Override this function before 'sauth' calls it.
  31. -- Sauth must depend on this mod for this to work.
  32. local old_auth_register = minetest.register_authentication_handler
  33. function minetest.register_authentication_handler(handler)
  34. local get_auth = handler.get_auth
  35. function handler.get_auth(name)
  36. return get_auth(rename.grn(name))
  37. end
  38. local create_auth = handler.create_auth
  39. function handler.create_auth(name, password)
  40. return create_auth(rename.grn(name), password)
  41. end
  42. local delete_auth = handler.delete_auth
  43. function handler.delete_auth(name)
  44. return delete_auth(rename.grn(name))
  45. end
  46. local set_password = handler.set_password
  47. function handler.set_password(name, password)
  48. return set_password(rename.grn(name), password)
  49. end
  50. local set_privileges = handler.set_privileges
  51. function handler.set_privileges(name, privileges)
  52. return set_privileges(rename.grn(name), privileges)
  53. end
  54. local reload = handler.reload
  55. function handler.reload()
  56. return reload()
  57. end
  58. local record_login = handler.record_login
  59. function handler.record_login(name)
  60. -- Delay the login record so that we can get the previous login
  61. -- time from within another `on_playerjoin` handler.
  62. minetest.after(0.5, function() record_login(rename.grn(name)) end)
  63. end
  64. local name_search = handler.name_search
  65. function handler.name_search(name)
  66. return name_search(rename.grn(name))
  67. end
  68. -- Log activity.
  69. minetest.log("action", "Wrapped auth handler in live-rename functions!")
  70. -- Call the original engine function.
  71. return old_auth_register(handler)
  72. end
  73. -- Override some global functions, for completeness.
  74. local set_password = core.set_player_password
  75. function core.set_player_password(name, password)
  76. return set_password(rename.grn(name), password)
  77. end
  78. local set_privileges = core.set_player_privs
  79. function core.set_player_privs(name, privileges)
  80. return set_privileges(rename.grn(name), privileges)
  81. end
  82. minetest.register_chatcommand("last-login", {
  83. params = "[name]",
  84. description = "Get the last login time of a player or yourself.",
  85. func = function(...)
  86. return auth2.last_login(...)
  87. end,
  88. })
  89. local c = "auth2:core"
  90. local f = auth2.modpath .. "/init.lua"
  91. reload.register_file(c, f, false)
  92. auth2.run_once = true
  93. end