init.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. if not minetest.global_exists("chat_colorize") then chat_colorize = {} end
  2. chat_colorize.player_just_died = chat_colorize.player_just_died or {}
  3. chat_colorize.modpath = minetest.get_modpath("chat_colorize")
  4. local S = core.get_translator("chat_colorize")
  5. -- Localize for performance.
  6. local math_random = math.random
  7. -- Support for hot reloading.
  8. local mp = chat_colorize.modpath .. "/init.lua"
  9. local rn = "chat_colorize:core"
  10. if minetest.get_modpath("reload") then
  11. if not reload.file_registered(rn) then
  12. reload.register_file(rn, mp, false)
  13. end
  14. end
  15. mp = nil
  16. rn = nil
  17. function chat_colorize.notify_death(pname)
  18. if chat_colorize.player_just_died[pname] then
  19. return
  20. end
  21. chat_colorize.player_just_died[pname] = true
  22. minetest.after(math_random(30, 60), function()
  23. chat_colorize.player_just_died[pname] = nil
  24. end)
  25. end
  26. function chat_colorize.is_ragequit(pname)
  27. if chat_colorize.player_just_died[pname] then
  28. return true
  29. end
  30. end
  31. if not chat_colorize.gotten then
  32. chat_colorize.old_chat_send_player = minetest.chat_send_player
  33. chat_colorize.old_chat_send_all = minetest.chat_send_all
  34. chat_colorize.gotten = true
  35. end
  36. -- Using cyan color, at request of sorcerykid. This color is easy to see.
  37. chat_colorize.COLOR_CYAN = core.get_color_escape_sequence("#00e0ff")
  38. chat_colorize.COLOR_OLIVE = core.get_color_escape_sequence("#9a7122")
  39. chat_colorize.COLOR_YELLOW = core.get_color_escape_sequence("#ffff00")
  40. chat_colorize.COLOR_ORANGE = core.get_color_escape_sequence("#ffae00")
  41. chat_colorize.COLOR_GRAY = core.get_color_escape_sequence("#aaaaaaff")
  42. if not chat_colorize.registered then
  43. minetest.register_privilege("nojoinmsg", {
  44. description = "Player's join/leave messages will not be announced.",
  45. give_to_singleplayer = false,
  46. })
  47. end
  48. -- Must be careful not to trigger on chat sent by players.
  49. chat_colorize.send_player = function(name, msg)
  50. local color = ""
  51. if msg:sub(1, 1) == "#" then
  52. color = chat_colorize.COLOR_OLIVE
  53. elseif msg:find("^-!-") and msg:find("Invalid command usage") then
  54. msg = "# Server: Invalid command usage."
  55. easyvend.sound_error(name)
  56. color = chat_colorize.COLOR_OLIVE
  57. elseif msg:find("^-!-") and msg:find("Invalid command:") then
  58. msg = "# Server: Invalid command. See '/help' for a list of valid commands."
  59. easyvend.sound_error(name)
  60. color = chat_colorize.COLOR_OLIVE
  61. elseif msg:find("^-!-") and msg:find("Empty command") then
  62. msg = "# Server: Empty command. See '/help' for a list of valid commands."
  63. easyvend.sound_error(name)
  64. color = chat_colorize.COLOR_OLIVE
  65. elseif msg:sub(1, 1) ~= "<" and msg:find("Command execution took") then
  66. -- Not an error.
  67. msg = "# Server: " .. minetest.strip_colors(msg)
  68. color = chat_colorize.COLOR_OLIVE
  69. elseif msg:sub(1, 1) ~= "<" and msg:find("You don't have permission to run this command") then
  70. msg = "# Server: " .. minetest.strip_colors(msg)
  71. easyvend.sound_error(name)
  72. color = chat_colorize.COLOR_OLIVE
  73. end
  74. --[[
  75. -- Make it less verbose.
  76. if msg:find("^# Server: ") then
  77. msg = "#" .. msg:sub(10)
  78. end
  79. --]]
  80. return chat_colorize.old_chat_send_player(name, color .. msg)
  81. end
  82. local should_suppress = function(msg)
  83. --if string.sub(msg, 1, 4) == "*** " then
  84. local strs = string.split(msg, " ")
  85. local name = strs[2]
  86. if type(name) == "string" and string.len(name) > 0 then
  87. local hide = minetest.check_player_privs(name, {nojoinmsg=true})
  88. if hide then return true end
  89. end
  90. --end
  91. end
  92. chat_colorize.should_suppress = function(pname)
  93. local hide = minetest.check_player_privs(pname, {nojoinmsg=true})
  94. if hide then return true end
  95. end
  96. local ragequit = {
  97. "Rage-quit",
  98. "Sneaked out",
  99. "Left in a huff",
  100. "Quit",
  101. "Couldn't take the heat",
  102. "Embarrassed",
  103. "Stormed out",
  104. "Stormed off",
  105. "Walked out",
  106. "Ugh",
  107. "This place stinks",
  108. "Gone home",
  109. "This server sucks, I'm going home",
  110. "Getting out",
  111. "Escaped",
  112. }
  113. chat_colorize.send_all = function(message)
  114. local color = ""
  115. local is_server_message = false
  116. if string.sub(message, 1, 1) == "#" then
  117. color = chat_colorize.COLOR_CYAN
  118. is_server_message = true
  119. end
  120. --[[
  121. -- Make it less verbose.
  122. if message:find("^# Server: ") then
  123. message = "#" .. message:sub(10)
  124. end
  125. --]]
  126. if is_server_message then
  127. chat_logging.log_server_message(message)
  128. end
  129. return chat_colorize.old_chat_send_all(color .. message)
  130. end
  131. if not chat_colorize.registered then
  132. minetest.chat_send_all = function(...) return chat_colorize.send_all(...) end
  133. minetest.chat_send_player = function(...) return chat_colorize.send_player(...) end
  134. function core.send_join_message(player_name)
  135. if not core.is_singleplayer() and not chat_colorize.should_suppress(player_name) then
  136. local color = chat_colorize.COLOR_GRAY
  137. local prefix = "*** "
  138. local alias = "<" .. rename.gpn(player_name) .. ">"
  139. local message = " joined the game."
  140. -- Send colored, prefixed, translatable message to chat using player's alias.
  141. chat_colorize.old_chat_send_all(color .. prefix .. S("@1" .. message, alias))
  142. -- Send bare prefix + alias + message to log.
  143. chat_logging.report_leavejoin_player(player_name, prefix .. alias .. message)
  144. end
  145. end
  146. function core.send_leave_message(player_name, timed_out)
  147. if not core.is_singleplayer() and not chat_colorize.should_suppress(player_name) then
  148. local color = chat_colorize.COLOR_GRAY
  149. local prefix = "*** "
  150. local alias = "<" .. rename.gpn(player_name) .. ">"
  151. local message = " left the game."
  152. local to_spc, timeout_suffix = "", ""
  153. local rq_spc, ragequit_suffix = "", ""
  154. if timed_out then
  155. to_spc, timeout_suffix = " ", "(Broken connection.)"
  156. end
  157. if chat_colorize.is_ragequit(player_name) then
  158. rq_spc, ragequit_suffix = " ", "(" .. ragequit[math_random(1, #ragequit)] .. ".)"
  159. end
  160. -- Send colored, prefixed, translatable message [ + translatable timeout_suffix ]
  161. -- [ + translatable ragequit_suffix ] to chat using player's alias.
  162. chat_colorize.old_chat_send_all(color .. prefix .. S("@1" .. message, alias) ..
  163. to_spc .. S(timeout_suffix) .. rq_spc .. S(ragequit_suffix))
  164. -- Send bare prefix + alias + message [ + timeout suffix ] [ + ragequit suffix ] to log.
  165. chat_logging.report_leavejoin_player(player_name, prefix .. alias .. message ..
  166. to_spc .. timeout_suffix .. rq_spc .. ragequit_suffix)
  167. end
  168. end
  169. chat_colorize.registered = true
  170. end