init.lua 7.2 KB

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