init.lua 6.7 KB

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