init.lua 4.5 KB

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