init.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. -- Localize for performance.
  5. local math_random = math.random
  6. -- Support for hot reloading.
  7. local mp = chat_colorize.modpath .. "/init.lua"
  8. local rn = "chat_colorize:core"
  9. if minetest.get_modpath("reload") then
  10. if not reload.file_registered(rn) then
  11. reload.register_file(rn, mp, false)
  12. end
  13. end
  14. mp = nil
  15. rn = nil
  16. function chat_colorize.notify_death(pname)
  17. if chat_colorize.player_just_died[pname] then
  18. return
  19. end
  20. chat_colorize.player_just_died[pname] = true
  21. minetest.after(math_random(30, 60), function()
  22. chat_colorize.player_just_died[pname] = nil
  23. end)
  24. end
  25. function chat_colorize.is_ragequit(pname)
  26. if chat_colorize.player_just_died[pname] then
  27. return true
  28. end
  29. end
  30. if not chat_colorize.gotten then
  31. chat_colorize.old_chat_send_player = minetest.chat_send_player
  32. chat_colorize.old_chat_send_all = minetest.chat_send_all
  33. chat_colorize.gotten = true
  34. end
  35. -- Using cyan color, at request of sorcerykid. This color is easy to see.
  36. chat_colorize.COLOR_CYAN = core.get_color_escape_sequence("#00e0ff")
  37. chat_colorize.COLOR_OLIVE = core.get_color_escape_sequence("#9a7122")
  38. chat_colorize.COLOR_YELLOW = core.get_color_escape_sequence("#ffff00")
  39. chat_colorize.COLOR_ORANGE = core.get_color_escape_sequence("#ffae00")
  40. chat_colorize.COLOR_GRAY = core.get_color_escape_sequence("#aaaaaaff")
  41. if not chat_colorize.registered then
  42. minetest.register_privilege("nojoinmsg", {
  43. description = "Player's join/leave messages will not be announced.",
  44. give_to_singleplayer = false,
  45. })
  46. end
  47. chat_colorize.send_player = function(name, message)
  48. local color = ""
  49. if string.sub(message, 1, 1) == "#" then
  50. color = chat_colorize.COLOR_OLIVE
  51. elseif string.find(message, "^%-!%- Invalid command") then
  52. message = "# Server: Invalid command. See '/help all' for a list of valid commands."
  53. easyvend.sound_error(name)
  54. color = chat_colorize.COLOR_OLIVE
  55. end
  56. return chat_colorize.old_chat_send_player(name, color .. message)
  57. end
  58. local should_suppress = function(msg)
  59. --if string.sub(msg, 1, 4) == "*** " then
  60. local strs = string.split(msg, " ")
  61. local name = strs[2]
  62. if type(name) == "string" and string.len(name) > 0 then
  63. local hide = minetest.check_player_privs(name, {nojoinmsg=true})
  64. if hide then return true end
  65. end
  66. --end
  67. end
  68. chat_colorize.should_suppress = function(pname)
  69. local hide = minetest.check_player_privs(pname, {nojoinmsg=true})
  70. if hide then return true end
  71. end
  72. local ragequit = {
  73. "Rage-quit",
  74. "Sneaked out",
  75. "Left in a huff",
  76. "Quit",
  77. "Couldn't take the heat",
  78. "Embarrassed",
  79. "Stormed out",
  80. "Stormed off",
  81. "Walked out",
  82. "Ugh",
  83. "This place stinks",
  84. "Gone home",
  85. "This server sucks, I'm going home",
  86. "Getting out",
  87. "Escaped",
  88. }
  89. chat_colorize.send_all = function(message)
  90. local color = ""
  91. local is_server_message = false
  92. if string.sub(message, 1, 1) == "#" then
  93. color = chat_colorize.COLOR_CYAN
  94. is_server_message = true
  95. --elseif string.sub(message, 1, 2) == "* " then
  96. -- color = chat_colorize.COLOR_ORANGE
  97. elseif string.sub(message, 1, 3) == "***" then
  98. -- Some players can have join/leave messages hidden.
  99. if should_suppress(message) then return end
  100. color = chat_colorize.COLOR_GRAY
  101. -- Add <> around player's name.
  102. message = string.gsub(message, "%*%*%* ([%w_%-]+) ", "*** <%1> ")
  103. -- Get player's internal name, so we can substitute their nick.
  104. local nick = string.match(message, "<([%w_%-]+)>")
  105. if nick then
  106. message = string.gsub(message, "<[%w_%-]+>", "<" .. rename.gpn(nick) .. ">")
  107. end
  108. -- Rewrite the timeout message.
  109. -- March 20, 2018: changed "timed out" to "connection broke" for better understanding.
  110. message = string.gsub(message, ". %(timed out%)$", ". (Broken connection.)")
  111. -- Check whether this is a player-leave-game message.
  112. local is_leaveplayer = message:find("left the game")
  113. if nick and is_leaveplayer and chat_colorize.is_ragequit(nick) then
  114. message = message .. " (" .. ragequit[math_random(1, #ragequit)] .. ".)"
  115. end
  116. if nick then
  117. chat_logging.report_leavejoin_player(nick, message)
  118. end
  119. end
  120. if is_server_message then
  121. chat_logging.log_server_message(message)
  122. end
  123. return chat_colorize.old_chat_send_all(color .. message)
  124. end
  125. if not chat_colorize.registered then
  126. minetest.chat_send_all = function(...) return chat_colorize.send_all(...) end
  127. minetest.chat_send_player = function(...) return chat_colorize.send_player(...) end
  128. chat_colorize.registered = true
  129. end