init.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. chat_colorize = chat_colorize or {}
  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. return chat_colorize.old_chat_send_player(name, color .. msg)
  75. end
  76. local should_suppress = function(msg)
  77. --if string.sub(msg, 1, 4) == "*** " then
  78. local strs = string.split(msg, " ")
  79. local name = strs[2]
  80. if type(name) == "string" and string.len(name) > 0 then
  81. local hide = minetest.check_player_privs(name, {nojoinmsg=true})
  82. if hide then return true end
  83. end
  84. --end
  85. end
  86. chat_colorize.should_suppress = function(pname)
  87. local hide = minetest.check_player_privs(pname, {nojoinmsg=true})
  88. if hide then return true end
  89. end
  90. local ragequit = {
  91. "Rage-quit",
  92. "Sneaked out",
  93. "Left in a huff",
  94. "Quit",
  95. "Couldn't take the heat",
  96. "Embarrassed",
  97. "Stormed out",
  98. "Stormed off",
  99. "Walked out",
  100. "Ugh",
  101. "This place stinks",
  102. "Gone home",
  103. "This server sucks, I'm going home",
  104. "Getting out",
  105. "Escaped",
  106. }
  107. chat_colorize.send_all = function(message)
  108. local color = ""
  109. local is_server_message = false
  110. if string.sub(message, 1, 1) == "#" then
  111. color = chat_colorize.COLOR_CYAN
  112. is_server_message = true
  113. end
  114. if is_server_message then
  115. chat_logging.log_server_message(message)
  116. end
  117. return chat_colorize.old_chat_send_all(color .. message)
  118. end
  119. if not chat_colorize.registered then
  120. minetest.chat_send_all = function(...) return chat_colorize.send_all(...) end
  121. minetest.chat_send_player = function(...) return chat_colorize.send_player(...) end
  122. function core.send_join_message(player_name)
  123. if not core.is_singleplayer() and not chat_colorize.should_suppress(player_name) then
  124. local color = chat_colorize.COLOR_GRAY
  125. local prefix = "*** "
  126. local alias = "<" .. rename.gpn(player_name) .. ">"
  127. local message = " joined the game."
  128. -- Send colored, prefixed, translatable message to chat using player's alias.
  129. chat_colorize.old_chat_send_all(color .. prefix .. S("@1" .. message, alias))
  130. -- Send bare prefix + alias + message to log.
  131. chat_logging.report_leavejoin_player(player_name, prefix .. alias .. message)
  132. end
  133. end
  134. function core.send_leave_message(player_name, timed_out)
  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 = " left the game."
  140. local to_spc, timeout_suffix = "", ""
  141. local rq_spc, ragequit_suffix = "", ""
  142. if timed_out then
  143. to_spc, timeout_suffix = " ", "(Broken connection.)"
  144. end
  145. if chat_colorize.is_ragequit(player_name) then
  146. rq_spc, ragequit_suffix = " ", "(" .. ragequit[math_random(1, #ragequit)] .. ".)"
  147. end
  148. -- Send colored, prefixed, translatable message [ + translatable timeout_suffix ]
  149. -- [ + translatable ragequit_suffix ] to chat using player's alias.
  150. chat_colorize.old_chat_send_all(color .. prefix .. S("@1" .. message, alias) ..
  151. to_spc .. S(timeout_suffix) .. rq_spc .. S(ragequit_suffix))
  152. -- Send bare prefix + alias + message [ + timeout suffix ] [ + ragequit suffix ] to log.
  153. chat_logging.report_leavejoin_player(player_name, prefix .. alias .. message ..
  154. to_spc .. timeout_suffix .. rq_spc .. ragequit_suffix)
  155. end
  156. end
  157. chat_colorize.registered = true
  158. end