init.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. chat_logging = chat_logging or {}
  2. chat_logging.modpath = minetest.get_modpath("chat_logging")
  3. chat_logging.worldpath = minetest.get_worldpath()
  4. -- Localize for performance.
  5. local vector_round = vector.round
  6. -- Register this file as reloadable, if not already done.
  7. if minetest.get_modpath("reload") then
  8. local c = "chat_logging:core"
  9. local f = chat_logging.modpath .. "/init.lua"
  10. if not reload.file_registered(c) then
  11. reload.register_file(c, f, false)
  12. end
  13. end
  14. local get_time = function(pname)
  15. return os.date("%Y-%m-%d, %H:%M")
  16. end
  17. local get_time_and_place = function(pname)
  18. local place = "N/A"
  19. local player = minetest.get_player_by_name(pname)
  20. if player and player:is_player() then
  21. place = minetest.pos_to_string(vector_round(player:get_pos()))
  22. end
  23. return os.date("%Y-%m-%d, %H:%M @ " .. place)
  24. end
  25. local get_public_time = function()
  26. return os.date("!%Y/%m/%d, %H:%M:%S UTC")
  27. end
  28. local generate_whitespace = function(msg)
  29. local len = 50 - string.len(msg)
  30. if len < 0 then len = 0 end
  31. local space = string.rep(" ", len)
  32. return space
  33. end
  34. local generate_shortspace = function(msg)
  35. local len = 30 - string.len(msg)
  36. if len < 0 then len = 0 end
  37. local space = string.rep(" ", len)
  38. return space
  39. end
  40. chat_logging.on_shutdown = function()
  41. minetest.chat_send_all("# Server: Normal shutdown. Everybody off!")
  42. if chat_logging.logfile then
  43. chat_logging.logfile:flush()
  44. chat_logging.logfile:close()
  45. chat_logging.logfile2:flush()
  46. chat_logging.logfile2:close()
  47. end
  48. end
  49. chat_logging.on_joinplayer = function(obj)
  50. --[[
  51. local pname = obj:get_player_name()
  52. local prefix = "[" .. get_time_and_place(pname) .. "] "
  53. local prefix2 = "[" .. get_public_time() .. "] "
  54. local wspace = generate_whitespace(prefix)
  55. local wspace2 = generate_shortspace(prefix2)
  56. prefix = prefix .. wspace
  57. prefix2 = prefix2 .. wspace2
  58. local msg = prefix .. "*** <" .. rename.gpn(pname) .. "> joined the game.\n"
  59. local msg2 = prefix2 .. "*** <" .. rename.gpn(pname) .. "> joined the game.\n"
  60. chat_logging.logfile:write(msg)
  61. if not chat_colorize.should_suppress(pname) then
  62. chat_logging.logfile2:write(msg2)
  63. end
  64. chat_logging.logfile:flush()
  65. chat_logging.logfile2:flush()
  66. --]]
  67. end
  68. chat_logging.report_leavejoin_player = function(pname, message)
  69. local prefix = "[" .. get_time_and_place(pname) .. "] "
  70. local prefix2 = "[" .. get_public_time() .. "] "
  71. local wspace = generate_whitespace(prefix)
  72. local wspace2 = generate_shortspace(prefix2)
  73. prefix = prefix .. wspace
  74. prefix2 = prefix2 .. wspace2
  75. local msg = prefix .. message .. "\n"
  76. local msg2 = prefix2 .. message .. "\n"
  77. chat_logging.logfile:write(msg)
  78. if not chat_colorize.should_suppress(pname) then
  79. chat_logging.logfile2:write(msg2)
  80. end
  81. chat_logging.logfile:flush()
  82. chat_logging.logfile2:flush()
  83. end
  84. chat_logging.on_leaveplayer = function(obj, timeout)
  85. --[[
  86. local pname = obj:get_player_name()
  87. local prefix = "[" .. get_time_and_place(pname) .. "] "
  88. local prefix2 = "[" .. get_public_time() .. "] "
  89. local wspace = generate_whitespace(prefix)
  90. local wspace2 = generate_shortspace(prefix2)
  91. prefix = prefix .. wspace
  92. prefix2 = prefix2 .. wspace2
  93. if timeout then
  94. local msg = prefix .. "*** <" .. rename.gpn(pname) .. "> left the game. (Broken connection.)\n"
  95. local msg2 = prefix2 .. "*** <" .. rename.gpn(pname) .. "> left the game. (Broken connection.)\n"
  96. chat_logging.logfile:write(msg)
  97. if not chat_colorize.should_suppress(pname) then
  98. chat_logging.logfile2:write(msg2)
  99. end
  100. else
  101. local msg = prefix .. "*** <" .. rename.gpn(pname) .. "> left the game.\n"
  102. local msg2 = prefix2 .. "*** <" .. rename.gpn(pname) .. "> left the game.\n"
  103. chat_logging.logfile:write(msg)
  104. if not chat_colorize.should_suppress(pname) then
  105. chat_logging.logfile2:write(msg2)
  106. end
  107. end
  108. chat_logging.logfile:flush()
  109. chat_logging.logfile2:flush()
  110. --]]
  111. end
  112. -- Open logfile if not already done.
  113. if not chat_logging.opened then
  114. local path = chat_logging.worldpath .. "/chat.txt"
  115. chat_logging.logfile = io.open(path, "a")
  116. local path2 = chat_logging.worldpath .. "/chat-public.txt"
  117. chat_logging.logfile2 = io.open(path2, "a")
  118. minetest.register_on_shutdown(function(...)
  119. return chat_logging.on_shutdown(...) end)
  120. --minetest.register_on_joinplayer(function(...)
  121. -- return chat_logging.on_joinplayer(...) end)
  122. --minetest.register_on_leaveplayer(function(...)
  123. -- return chat_logging.on_leaveplayer(...) end)
  124. chat_logging.opened = true
  125. end
  126. -- Public API functions.
  127. chat_logging.log_public_shout = function(pname, msg, loc)
  128. local prefix = "[" .. get_time_and_place(pname) .. "] "
  129. local prefix2 = "[" .. get_public_time() .. "] "
  130. local wspace = generate_whitespace(prefix)
  131. local wspace2 = generate_shortspace(prefix2)
  132. prefix = prefix .. wspace .. "<!" .. rename.gpn(pname) .. loc .. "!> " .. msg .. "\n"
  133. prefix2 = prefix2 .. wspace2 .. "<!" .. rename.gpn(pname) .. loc .. "!> " .. msg .. "\n"
  134. chat_logging.logfile:write(prefix)
  135. chat_logging.logfile2:write(prefix2)
  136. chat_logging.logfile:flush()
  137. chat_logging.logfile2:flush()
  138. end
  139. chat_logging.log_public_chat = function(pname, msg, loc)
  140. local prefix = "[" .. get_time_and_place(pname) .. "] "
  141. local prefix2 = "[" .. get_public_time() .. "] "
  142. local wspace = generate_whitespace(prefix)
  143. local wspace2 = generate_shortspace(prefix2)
  144. prefix = prefix .. wspace .. "<" .. rename.gpn(pname) .. loc .. "> " .. msg .. "\n"
  145. prefix2 = prefix2 .. wspace2 .. "<" .. rename.gpn(pname) .. loc .. "> " .. msg .. "\n"
  146. chat_logging.logfile:write(prefix)
  147. chat_logging.logfile2:write(prefix2)
  148. chat_logging.logfile:flush()
  149. chat_logging.logfile2:flush()
  150. end
  151. chat_logging.log_public_action = function(pname, act, loc)
  152. local prefix = "[" .. get_time_and_place(pname) .. "] "
  153. local prefix2 = "[" .. get_public_time() .. "] "
  154. local wspace = generate_whitespace(prefix)
  155. local wspace2 = generate_shortspace(prefix2)
  156. prefix = prefix .. wspace .. "* <" .. rename.gpn(pname) .. loc .. "> " .. act .. "\n"
  157. prefix2 = prefix2 .. wspace2 .. "* <" .. rename.gpn(pname) .. loc .. "> " .. act .. "\n"
  158. chat_logging.logfile:write(prefix)
  159. chat_logging.logfile2:write(prefix2)
  160. chat_logging.logfile:flush()
  161. chat_logging.logfile2:flush()
  162. end
  163. chat_logging.log_private_message = function(from, to, message)
  164. local prefix = "[" .. get_time_and_place(from) .. "] "
  165. local wspace = generate_whitespace(prefix)
  166. prefix = prefix .. wspace .. "<" .. rename.gpn(from) .. " -- " .. rename.gpn(to) .. "> " .. message .. "\n"
  167. chat_logging.logfile:write(prefix)
  168. chat_logging.logfile:flush()
  169. end
  170. chat_logging.log_team_chat = function(from, message, team)
  171. local prefix = "[" .. get_time_and_place(from) .. "] "
  172. local wspace = generate_whitespace(prefix)
  173. prefix = prefix .. wspace .. "<" .. rename.gpn(from) .. " x:" .. team .. "> " .. message .. "\n"
  174. chat_logging.logfile:write(prefix)
  175. chat_logging.logfile:flush()
  176. end
  177. chat_logging.log_server_message = function(message)
  178. local prefix = "[" .. get_time() .. "] "
  179. local prefix2 = "[" .. get_public_time() .. "] "
  180. local wspace = generate_whitespace(prefix)
  181. local wspace2 = generate_shortspace(prefix2)
  182. prefix = prefix .. wspace .. message .. "\n"
  183. prefix2 = prefix2 .. wspace2 .. message .. "\n"
  184. chat_logging.logfile:write(prefix)
  185. chat_logging.logfile2:write(prefix2)
  186. chat_logging.logfile:flush()
  187. chat_logging.logfile2:flush()
  188. end