init.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. --------------------------------------------------------------------------------
  2. -- Core Chat System for Must Test Survival
  3. -- Author: GoldFireUn
  4. -- License: MIT
  5. --------------------------------------------------------------------------------
  6. chat_core = chat_core or {}
  7. chat_core.modpath = minetest.get_modpath("chat_core")
  8. chat_core.players = chat_core.players or {}
  9. -- Localize for performance.
  10. local vector_distance = vector.distance
  11. local vector_round = vector.round
  12. function chat_core.on_joinplayer(player)
  13. local pname = player:get_player_name()
  14. chat_core.players[pname] = {
  15. last_pm_from = "",
  16. }
  17. end
  18. function chat_core.on_leaveplayer(player, timeout)
  19. local pname = player:get_player_name()
  20. chat_core.players[pname] = nil
  21. end
  22. if minetest.get_modpath("reload") then
  23. local c = "chat_core:core"
  24. local f = chat_core.modpath .. "/init.lua"
  25. if not reload.file_registered(c) then
  26. reload.register_file(c, f, false)
  27. end
  28. end
  29. local color_green = core.get_color_escape_sequence("#00ff00")
  30. local color_dark_green = core.get_color_escape_sequence("#00d000")
  31. local color_dark_cyan = core.get_color_escape_sequence("#88ffff")
  32. local color_nametag = core.get_color_escape_sequence("#ffd870")
  33. local color_white = core.get_color_escape_sequence("#ffffff")
  34. --local color_cyan = core.get_color_escape_sequence("#00e0ff")
  35. chat_core.nametag_color = color_nametag -- make public to other mods
  36. -- Used in PMs.
  37. local color_magenta = core.get_color_escape_sequence("#ff50ff")
  38. local color_dark_magenta = core.get_color_escape_sequence("#c800c8")
  39. chat_core.rewrite_message = function(chat2)
  40. -- Prevent players from including zero-bytes or control characters in their chat.
  41. local sub = string.gsub
  42. local chat = chat2
  43. chat = sub(chat, "[%z%c]", "") -- Zero byte & control bytes.
  44. chat = sub(chat, " +", " ") -- Excess spaces.
  45. --chat = sub(chat, "[qQ]", "k")
  46. return chat
  47. end
  48. -- Send regular chat from a player to all other players.
  49. -- This is called by this mod after validation checks pass.
  50. chat_core.send_all = function(from, prename, actname, postname, message, alwaysecho)
  51. -- `alwaysecho` is true in the case of a /me command.
  52. -- The client never echoes this command by itself.
  53. local player = minetest.get_player_by_name(from)
  54. if not player then
  55. return
  56. end
  57. local ppos = player:get_pos()
  58. local allplayers = minetest.get_connected_players()
  59. local mlower = string.lower(message)
  60. for k, v in ipairs(allplayers) do
  61. local pname = v:get_player_name()
  62. local plower = string.lower(rename.gpn(pname))
  63. local plowero = string.lower(pname)
  64. local tpos = v:get_pos()
  65. if pname ~= from then
  66. local chosen_color = ""
  67. local should_send = true
  68. local should_beep = false
  69. local ignored = false
  70. -- Execute chat filters. Order is relevant!
  71. -- Hide chat from players who are too far away (if feature is enabled).
  72. if chat_controls.player_too_far(pname, from) then
  73. should_send = false
  74. end
  75. -- Whitelisted player chat can be see even if far away.
  76. if chat_controls.player_whitelisted(pname, from) then
  77. should_send = true
  78. end
  79. -- Ignore list takes precedence over whitelist.
  80. if chat_controls.player_ignored(pname, from) then
  81. should_send = false
  82. ignored = true
  83. end
  84. -- Chat from nearby players is highlighted.
  85. -- Even ignored players may talk if they are close enough.
  86. if vector_distance(ppos, tpos) < 64 then
  87. -- Highlight chat from nearby player only if originating player is not invisible.
  88. if not gdac_invis.is_invisible(from) then
  89. chosen_color = color_dark_cyan
  90. end
  91. should_send = true
  92. end
  93. -- Finally, check highlighting filters. But only if not ignored!
  94. if not ignored then
  95. if chat_controls.check_highlighting_filters(pname, from, message) then
  96. chosen_color = color_dark_green
  97. should_send = true
  98. end
  99. end
  100. if should_send then
  101. -- Colorize message for players if their name or alt is used.
  102. -- This overrides any previous coloring.
  103. if string.find(mlower, plower) or string.find(mlower, plowero) then
  104. chosen_color = color_green
  105. should_beep = true
  106. end
  107. -- If /me, use correct color.
  108. if alwaysecho then
  109. chosen_color = chat_colorize.COLOR_ORANGE
  110. end
  111. -- Finally send the message.
  112. if should_beep then
  113. chat_core.alert_player_sound(pname)
  114. end
  115. minetest.chat_send_player(pname, prename .. color_nametag .. actname .. color_white .. postname .. chosen_color .. message)
  116. end
  117. else -- Message being echoed back to player that sent it.
  118. if alwaysecho then
  119. -- It should be a /me command.
  120. minetest.chat_send_player(pname, prename .. color_nametag .. actname .. color_white .. postname .. chat_colorize.COLOR_ORANGE .. message)
  121. else
  122. -- Send chat to self if echo enabled.
  123. chat_echo.echo_chat(pname, prename .. color_nametag .. actname .. color_white .. postname .. message)
  124. end
  125. end
  126. end
  127. end
  128. -- Check player's language, and kick them if they are not protected by the PoC/KoC.
  129. -- Or, mute them and send a message to other players that they were muted.
  130. -- This function can be called from other mods.
  131. function chat_core.check_language(name, message)
  132. -- Players with anticurse bypass priv cannot be kicked by this mod.
  133. local nokick = minetest.check_player_privs(name, {anticurse_bypass=true})
  134. if nokick then
  135. if anticurse.check(name, message, "foul") then
  136. anticurse.log(name, message)
  137. elseif anticurse.check(name, message, "curse") then
  138. anticurse.log(name, message)
  139. end
  140. return false -- Has bypass.
  141. end
  142. if anticurse.check(name, message, "foul") then
  143. anticurse.log(name, message)
  144. -- Players who have registered (and therefore have probably played
  145. -- on the server more than a few days) are warned but not kicked.
  146. if passport.player_registered(name) then
  147. local ext = anticurse.get_kick_message("foul")
  148. minetest.chat_send_all("# Server: Talk from someone hidden in case of uninteresting language.")
  149. minetest.chat_send_player(name, "# Server: " .. ext)
  150. else
  151. anticurse.kick(name, "foul")
  152. end
  153. return true -- Blocked.
  154. elseif anticurse.check(name, message, "curse") then
  155. anticurse.log(name, message)
  156. -- Players who have registered (and therefore have probably played
  157. -- on the server more than a few days) are warned but not kicked.
  158. if passport.player_registered(name) then
  159. local ext = anticurse.get_kick_message("curse")
  160. minetest.chat_send_all("# Server: Talk from someone hidden in case of uninteresting language.")
  161. minetest.chat_send_player(name, "# Server: " .. ext)
  162. else
  163. anticurse.kick(name, "foul")
  164. end
  165. return true -- Blocked.
  166. end
  167. return false -- Nothing found.
  168. end
  169. local generate_coord_string = function(name)
  170. local coord_string = ""
  171. local entity = minetest.get_player_by_name(name)
  172. if not entity then
  173. return coord_string
  174. end
  175. local pos = entity:get_pos()
  176. if command_tokens.mark.player_marked(name) or
  177. (sheriff.is_suspected_cheater(name) and city_block:in_city(pos)) then
  178. local pstr = rc.pos_to_string(vector_round(pos))
  179. pstr = string.gsub(pstr, "[%(%)]", "")
  180. -- Remember to include leading space!
  181. coord_string = " [" .. rc.realm_description_at_pos(pos) .. ": " .. pstr .. "]"
  182. -- If server is not echoing player's chat back to the player, then their
  183. -- client is old (or they have chat-echo turned off). In this case, the
  184. -- player will need a special info message sent to them in order for them to
  185. -- know if they're marked.
  186. if not chat_echo.get_echo(name) then
  187. minetest.chat_send_player(name, "# Server: You are marked (" .. pstr .. ")!")
  188. end
  189. end
  190. return coord_string
  191. end
  192. chat_core.generate_coord_string = generate_coord_string
  193. chat_core.on_chat_message = function(name, message)
  194. -- Trim input.
  195. message = string.trim(message)
  196. if message:sub(1, 1) == "/" then
  197. minetest.chat_send_player(name, "# Server: Invalid command. See '/help all' for a list of valid commands.")
  198. easyvend.sound_error(name)
  199. -- It's a special command, and not one that was registered.
  200. -- This is actually never called?
  201. return
  202. end
  203. if not minetest.check_player_privs(name, {shout=true}) then
  204. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  205. -- Player doesn't have shout priv.
  206. return
  207. end
  208. local player_muted = false
  209. if command_tokens.mute.player_muted(name) then
  210. minetest.chat_send_player(name, "# Server: You are currently gagged.")
  211. -- Player is muted.
  212. return
  213. end
  214. -- Shouts can be executed by prepending a '!' to your chat.
  215. if string.find(message, "^!") then
  216. -- Handled by the shout mod.
  217. shout.shout(name, string.sub(message, 2))
  218. return
  219. end
  220. -- Check for accidents.
  221. local pm_pos = string.find(message, "msg")
  222. if not pm_pos then
  223. pm_pos = string.find(message, "MSG")
  224. end
  225. if not pm_pos then
  226. pm_pos = string.find(message, "pm")
  227. end
  228. if not pm_pos then
  229. pm_pos = string.find(message, "PM")
  230. end
  231. if pm_pos and pm_pos <= 3 then -- Space for 2 symbols.
  232. minetest.chat_send_player(name,
  233. "# Server: Did you mean to send a PM? The command format is \"/msg <player> <message>\" (without quotes). Chat not sent.")
  234. return
  235. end
  236. if chat_core.check_language(name, message) then return end
  237. local coord_string = generate_coord_string(name)
  238. player_labels.on_chat_message(name, message)
  239. chat_core.send_all(name, "<", rename.gpn(name), coord_string .. "> ", message)
  240. chat_logging.log_public_chat(name, message, coord_string)
  241. afk_removal.reset_timeout(name)
  242. end
  243. chat_core.handle_command_me = function(name, param)
  244. if not minetest.check_player_privs(name, {shout=true}) then
  245. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  246. return -- Player doesn't have shout priv.
  247. end
  248. if command_tokens.mute.player_muted(name) then
  249. minetest.chat_send_player(name, "# Server: You can't do that while gagged, sorry.")
  250. return -- Player is muted.
  251. end
  252. param = string.trim(param)
  253. if #param < 1 then
  254. minetest.chat_send_player(name, "# Server: No action specified.")
  255. return
  256. end
  257. if chat_core.check_language(name, param) then return end
  258. local coord_string = generate_coord_string(name)
  259. player_labels.on_chat_message(name, param)
  260. chat_core.send_all(name, "* <", rename.gpn(name), coord_string .. "> ", param, true)
  261. chat_logging.log_public_action(name, param, coord_string)
  262. afk_removal.reset_timeout(name)
  263. end
  264. chat_core.handle_command_msg = function(name, param)
  265. if not minetest.check_player_privs(name, {shout=true}) then
  266. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  267. easyvend.sound_error(name)
  268. return -- Player doesn't have shout priv.
  269. end
  270. -- Gagged players cannot polute global chat, but PMs are allowed.
  271. -- If an annoying pervert is PM'ing someone, that person should take advantage
  272. -- of the ignore-list in their Key. Or use F2 if they don't have a Key.
  273. --[[
  274. if command_tokens.mute.player_muted(name) then
  275. minetest.chat_send_player(name, "# Server: You are gagged at the moment.")
  276. easyvend.sound_error(name)
  277. return -- Player is muted.
  278. end
  279. --]]
  280. local coord_string = generate_coord_string(name)
  281. -- Split command arguments.
  282. local p = string.find(param, " ")
  283. local to, newmsg
  284. if p then
  285. newmsg = string.trim(param:sub(p+1))
  286. to = string.trim(param:sub(1, p-1))
  287. end
  288. if type(to)=="string" and type(newmsg)=="string" and string.len(newmsg) > 0 and string.len(to) > 0 then
  289. to = rename.grn(to)
  290. if gdac_invis.is_invisible(to) and to ~= name then -- If target is invisible, and player sending is not same as target ...
  291. if chat_core.players[name] and (chat_core.players[name].last_pm_from or "") ~= to then -- Do not permit, if player did not receive a PM from this target.
  292. if to == "MustTest" then
  293. minetest.chat_send_player(name, "# Server: The server admin is not available at this time! If it's important, send mail instead.")
  294. else
  295. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not available at this time! If it's important, send mail instead.")
  296. end
  297. return
  298. end
  299. end
  300. if minetest.get_player_by_name(to) then
  301. -- Bad words in PMs.
  302. if chat_core.check_language(name, newmsg) then
  303. return
  304. end
  305. -- Cannot PM player if being ignored.
  306. if chat_controls.player_ignored_pm(to, name) and to ~= name then
  307. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not available for private messaging!")
  308. easyvend.sound_error(name)
  309. return
  310. end
  311. minetest.after(0, function()
  312. chat_core.alert_player_sound(to)
  313. minetest.chat_send_player(to, color_magenta .. "# PM: FROM <" .. rename.gpn(name) .. coord_string .. ">: " .. newmsg)
  314. -- Record name of last player to send this player a PM.
  315. if chat_core.players[to] then
  316. chat_core.players[to].last_pm_from = name
  317. end
  318. end)
  319. minetest.chat_send_player(name, color_dark_magenta .. "# PM: TO <" .. rename.gpn(to) .. coord_string .. ">: " .. newmsg)
  320. chat_logging.log_private_message(name, to, newmsg)
  321. afk_removal.reset_timeout(name)
  322. else minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not online.") end
  323. else minetest.chat_send_player(name, "# Server: Usage: '/msg <playername> <message>'.") end
  324. end
  325. function chat_core.handle_command_r(name, param)
  326. local to = chat_core.players[name].last_pm_from or ""
  327. if to == "" then
  328. minetest.chat_send_player(name, "# Server: No one has sent you a PM yet that can be replied to. Use /msg instead to specify the player.")
  329. return
  330. end
  331. return chat_core.handle_command_msg(name, to .. " " .. param) -- Prepend target name, and call normal /msg function.
  332. end
  333. function chat_core.alert_player_sound(to)
  334. if chat_controls.beep_enabled(to) then
  335. if afk_removal.is_afk(to) then
  336. minetest.sound_play("chat_alert", {to_player = to, gain = 1})
  337. else
  338. if afk_removal.seconds_since_action(to) > 60*2 then
  339. minetest.sound_play("chat_alert", {to_player = to, gain = 1})
  340. else
  341. minetest.sound_play("chat_alert", {to_player = to, gain = 0.4})
  342. end
  343. end
  344. local pref = minetest.get_player_by_name(to)
  345. if pref then
  346. local pos = pref:get_pos()
  347. ambiance.sound_play("chat_alert", pos, 0.5, 20, to)
  348. end
  349. end
  350. end
  351. if not chat_core.registered then
  352. minetest.register_chatcommand("me", {
  353. params = "<action>",
  354. description = "Send an 'action' message beginning with your name.",
  355. privs = {shout=true},
  356. func = function(name, param)
  357. chat_core.handle_command_me(name, chat_core.rewrite_message(param))
  358. return true
  359. end,
  360. })
  361. minetest.register_chatcommand("msg", {
  362. params = "<player> <message>",
  363. description = "Send a private message to another player.",
  364. privs = {shout=true},
  365. func = function(name, param)
  366. chat_core.handle_command_msg(name, chat_core.rewrite_message(param))
  367. return true
  368. end,
  369. })
  370. minetest.register_chatcommand("r", {
  371. params = "<message>",
  372. description = "Reply via PM to the last player to send you a PM.",
  373. privs = {shout=true},
  374. func = function(name, param)
  375. chat_core.handle_command_r(name, chat_core.rewrite_message(param))
  376. return true
  377. end,
  378. })
  379. -- This should be the only handler registered. Only one handler can be registered.
  380. minetest.register_on_chat_message(function(name, message)
  381. chat_core.on_chat_message(name, chat_core.rewrite_message(message))
  382. return true -- Don't send message automatically, we already did this.
  383. end)
  384. minetest.register_on_joinplayer(function(...) return chat_core.on_joinplayer(...) end)
  385. minetest.register_on_leaveplayer(function(...) return chat_core.on_leaveplayer(...) end)
  386. chat_core.registered = true
  387. end