init.lua 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. status = status or {}
  2. status.modpath = minetest.get_modpath("status")
  3. status.motd = minetest.setting_get("motd") or ""
  4. -- Get text color.
  5. local STATUS_COLOR = core.get_color_escape_sequence("#0d9b7b")
  6. status.color = STATUS_COLOR
  7. function status.chat_players(user, param)
  8. do
  9. -- Serialize player names to string.
  10. local players = minetest.get_connected_players()
  11. local clients = "{"
  12. local num_clients = 0
  13. for k, v in ipairs(players) do
  14. local hide = minetest.check_player_privs(v:get_player_name(), {statushide=true})
  15. if hide == false then
  16. clients = clients .. rename.gpn(v:get_player_name()) .. ", "
  17. num_clients = num_clients + 1
  18. end
  19. end
  20. clients = clients .. "}"
  21. clients = string.gsub(clients, ", }", "}")
  22. -- Build status string.
  23. local final =
  24. STATUS_COLOR .. "# Server: Players Online (" .. num_clients .. "): " .. clients .. "."
  25. minetest.chat_send_player(user, final)
  26. end
  27. do
  28. -- Serialize player names to string.
  29. local channel = shout.player_channel(user)
  30. if channel and channel ~= "" then
  31. local players = shout.channel_players(channel)
  32. local clients = "{"
  33. local num_clients = 0
  34. for k, v in ipairs(players) do
  35. local hide = minetest.check_player_privs(v, {statushide=true})
  36. if hide == false then
  37. clients = clients .. rename.gpn(v) .. ", "
  38. num_clients = num_clients + 1
  39. end
  40. end
  41. clients = clients .. "}"
  42. clients = string.gsub(clients, ", }", "}")
  43. -- Build status string.
  44. local final =
  45. STATUS_COLOR .. "# Server: Players In Channel '" .. channel .. "' (" .. num_clients .. "): " .. clients .. "."
  46. minetest.chat_send_player(user, final)
  47. end
  48. end
  49. return true
  50. end
  51. function status.chat_status(user, param)
  52. local p1, p2
  53. -- Get uptime.
  54. local status_str = status.original_status()
  55. p1, p2 = string.find(status_str, "uptime:[^|]+")
  56. local uptime = "uptime: unknown"
  57. if p1 and p2 then
  58. uptime = string.sub(status_str, p1, p2)
  59. uptime = string.gsub(uptime, "uptime: ", "Uptime: ")
  60. uptime = string.trim(uptime)
  61. end
  62. p1, p2 = string.find(status_str, "max lag:[^|]+")
  63. local max_lag = "max lag: unknown"
  64. if p1 and p2 then
  65. max_lag = string.sub(status_str, p1, p2)
  66. max_lag = string.gsub(max_lag, "max lag: ", "Max Lag: ")
  67. max_lag = string.trim(max_lag)
  68. end
  69. -- Get MoTD.
  70. local motd2 = status.motd
  71. if not motd2 then
  72. motd2 = "Daily message has not been set!"
  73. end
  74. -- Get version string.
  75. local version = "version: unknown"
  76. p1, p2 = string.find(status_str, "version:[^|]+")
  77. if p1 and p2 then
  78. version = string.sub(status_str, p1, p2)
  79. version = string.trim(version)
  80. end
  81. version = string.gsub(version, "version: ", "Version: ")
  82. version = string.gsub(version, "-dev", "-DEV")
  83. -- Build status string.
  84. local final =
  85. STATUS_COLOR .. "# Server: " .. version .. ", " .. uptime .. ", " .. max_lag .. ".\n" ..
  86. STATUS_COLOR .. "# Server: " .. motd2 .. "\n" ..
  87. STATUS_COLOR .. "# Server: More info can be found at http://arklegacy.duckdns.org/."
  88. minetest.chat_send_player(user, final)
  89. return true
  90. end
  91. function status.chat_short_status(user, param)
  92. -- Serialize player names to string.
  93. local players = minetest.get_connected_players()
  94. local clients = "{"
  95. local num_clients = 0
  96. for k, v in ipairs(players) do
  97. local pname = v:get_player_name()
  98. local hide = minetest.check_player_privs(pname, {statushide=true})
  99. if pname == user then
  100. hide = true
  101. end
  102. if hide == false then
  103. clients = clients .. rename.gpn(pname) .. ", "
  104. num_clients = num_clients + 1
  105. end
  106. end
  107. clients = clients .. "}"
  108. clients = string.gsub(clients, ", }", "}")
  109. local sare = "are"
  110. local splayers = "players"
  111. if num_clients == 1 then
  112. sare = "is"
  113. splayers = "player"
  114. end
  115. local tail = ": " .. clients .. "."
  116. if num_clients == 0 then
  117. tail = "."
  118. end
  119. local final = STATUS_COLOR .. "# Server: " ..
  120. "Welcome back, <" .. rename.gpn(user) .. ">! There " .. sare ..
  121. " currently " ..
  122. num_clients .. " " .. splayers .. " online" .. tail
  123. minetest.chat_send_player(user, final)
  124. end
  125. function status.chat_admin(name, param)
  126. minetest.chat_send_player(name,
  127. STATUS_COLOR .. "# Server: The administrator of this server is <MustTest>. " ..
  128. "You may send me messages using the email interface found through the Key of Citizenship.")
  129. return true
  130. end
  131. function status.on_joinplayer(player)
  132. local pname = player:get_player_name()
  133. -- Don't show /status info to registered players rejoining the server.
  134. -- It's just noise, if they want it they can type /status manually.
  135. if not passport.player_registered(pname) then
  136. status.chat_status(pname, "")
  137. else
  138. status.chat_short_status(pname, "")
  139. end
  140. end
  141. if not status.registered then
  142. -- Save original function.
  143. status.original_status = minetest.get_server_status
  144. -- Override builtin status function to prevent showing this message on player join!
  145. function minetest.get_server_status(pname, joined)
  146. return nil
  147. end
  148. -- Override the /status chat command.
  149. minetest.override_chatcommand("status", {
  150. params = "",
  151. description = "Show the server's current status.",
  152. privs = {},
  153. func = function(...) return status.chat_status(...) end,
  154. })
  155. minetest.register_chatcommand("players", {
  156. params = "",
  157. description = "Show list of players currently online.",
  158. privs = {},
  159. func = function(...) return status.chat_players(...) end,
  160. })
  161. minetest.override_chatcommand("admin", {
  162. params = "",
  163. description = "Show the name of the server owner and primary operator.",
  164. privs = {},
  165. func = function(...) return status.chat_admin(...) end,
  166. })
  167. local timefunc = minetest.registered_chatcommands["time"].func
  168. assert(type(timefunc) == "function")
  169. minetest.override_chatcommand("time", {
  170. description = "Set or get the current time of day.",
  171. func = function(name, param)
  172. local result, message = timefunc(name, param)
  173. message = STATUS_COLOR .. "# Server: " .. message
  174. return result, message
  175. end,
  176. })
  177. local daysfunc = minetest.registered_chatcommands["days"].func
  178. assert(type(daysfunc) == "function")
  179. minetest.override_chatcommand("days", {
  180. description = "Display day count.",
  181. func = function(name, param)
  182. local result, message = daysfunc(name, param)
  183. message = STATUS_COLOR ..
  184. "# Server: " .. message .. ". " ..
  185. string.gsub(hud_clock.get_date_string(), "\n+", ", ") .. "."
  186. return result, message
  187. end,
  188. })
  189. minetest.register_privilege("statushide", {
  190. description = "Player's name will not be shown in the server's status string.",
  191. give_to_singleplayer = false,
  192. })
  193. -- Send status string to players on join.
  194. -- The engine status string is disabled in server config.
  195. minetest.register_on_joinplayer(function(...) return status.on_joinplayer(...) end)
  196. reload.register_file("status:core", status.modpath .. "/init.lua", false)
  197. status.registered = true
  198. end