init.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 player doesn't have priv, check if their XP is high enough.
  135. -- (Remember, the AC mod is only intended to discourage "drive-by" pollution from people randomly joining.)
  136. -- By the time someone gets to this amount of XP, they probably understand that we like clean air, here. :-)
  137. if not nokick then
  138. nokick = (xp.get_xp(name, "digxp") >= 5000)
  139. end
  140. if nokick then
  141. if anticurse.check(name, message, "foul") then
  142. anticurse.log(name, message)
  143. elseif anticurse.check(name, message, "curse") then
  144. anticurse.log(name, message)
  145. end
  146. return false -- Has bypass.
  147. end
  148. if anticurse.check(name, message, "foul") then
  149. anticurse.log(name, message)
  150. -- Players who have registered (and therefore have probably played
  151. -- on the server more than a few days) are warned but not kicked.
  152. if passport.player_registered(name) then
  153. local ext = anticurse.get_kick_message("foul")
  154. minetest.chat_send_all("# Server: Talk from someone hidden in case of uninteresting language.")
  155. minetest.chat_send_player(name, "# Server: " .. ext)
  156. else
  157. anticurse.kick(name, "foul")
  158. end
  159. return true -- Blocked.
  160. elseif anticurse.check(name, message, "curse") then
  161. anticurse.log(name, message)
  162. -- Players who have registered (and therefore have probably played
  163. -- on the server more than a few days) are warned but not kicked.
  164. if passport.player_registered(name) then
  165. local ext = anticurse.get_kick_message("curse")
  166. minetest.chat_send_all("# Server: Talk from someone hidden in case of uninteresting language.")
  167. minetest.chat_send_player(name, "# Server: " .. ext)
  168. else
  169. anticurse.kick(name, "foul")
  170. end
  171. return true -- Blocked.
  172. end
  173. return false -- Nothing found.
  174. end
  175. local generate_coord_string = function(name)
  176. local coord_string = ""
  177. local entity = minetest.get_player_by_name(name)
  178. if not entity then
  179. return coord_string
  180. end
  181. local pos = entity:get_pos()
  182. if command_tokens.mark.player_marked(name) or
  183. (sheriff.is_suspected_cheater(name) and city_block:in_city(pos)) then
  184. local pstr = rc.pos_to_string(vector_round(pos))
  185. pstr = string.gsub(pstr, "[%(%)]", "")
  186. -- Remember to include leading space!
  187. coord_string = " [" .. rc.realm_description_at_pos(pos) .. ": " .. pstr .. "]"
  188. -- If server is not echoing player's chat back to the player, then their
  189. -- client is old (or they have chat-echo turned off). In this case, the
  190. -- player will need a special info message sent to them in order for them to
  191. -- know if they're marked.
  192. if not chat_echo.get_echo(name) then
  193. minetest.chat_send_player(name, "# Server: You are marked (" .. pstr .. ")!")
  194. end
  195. end
  196. return coord_string
  197. end
  198. chat_core.generate_coord_string = generate_coord_string
  199. chat_core.on_chat_message = function(name, message)
  200. -- Trim input.
  201. message = string.trim(message)
  202. if message:sub(1, 1) == "/" then
  203. minetest.chat_send_player(name, "# Server: Invalid command. See '/help all' for a list of valid commands.")
  204. easyvend.sound_error(name)
  205. -- It's a special command, and not one that was registered.
  206. -- This is actually never called?
  207. return
  208. end
  209. if not minetest.check_player_privs(name, {shout=true}) then
  210. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  211. -- Player doesn't have shout priv.
  212. return
  213. end
  214. local player_muted = false
  215. if command_tokens.mute.player_muted(name) then
  216. minetest.chat_send_player(name, "# Server: You are currently gagged.")
  217. -- Player is muted.
  218. return
  219. end
  220. -- Shouts can be executed by prepending a '!' to your chat.
  221. if string.find(message, "^!") then
  222. -- Handled by the shout mod.
  223. shout.shout(name, string.sub(message, 2))
  224. return
  225. end
  226. -- Check for accidents.
  227. local pm_pos = string.find(message, "msg")
  228. if not pm_pos then
  229. pm_pos = string.find(message, "MSG")
  230. end
  231. if not pm_pos then
  232. pm_pos = string.find(message, "pm")
  233. end
  234. if not pm_pos then
  235. pm_pos = string.find(message, "PM")
  236. end
  237. if pm_pos and pm_pos <= 3 then -- Space for 2 symbols.
  238. minetest.chat_send_player(name,
  239. "# Server: Did you mean to send a PM? The command format is \"/msg <player> <message>\" (without quotes). Chat not sent.")
  240. return
  241. end
  242. if chat_core.check_language(name, message) then return end
  243. local coord_string = generate_coord_string(name)
  244. player_labels.on_chat_message(name, message)
  245. chat_core.send_all(name, "<", rename.gpn(name), coord_string .. "> ", message)
  246. chat_logging.log_public_chat(name, message, coord_string)
  247. afk.reset_timeout(name)
  248. end
  249. chat_core.handle_command_me = function(name, param)
  250. if not minetest.check_player_privs(name, {shout=true}) then
  251. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  252. return -- Player doesn't have shout priv.
  253. end
  254. if command_tokens.mute.player_muted(name) then
  255. minetest.chat_send_player(name, "# Server: You can't do that while gagged, sorry.")
  256. return -- Player is muted.
  257. end
  258. param = string.trim(param)
  259. if #param < 1 then
  260. minetest.chat_send_player(name, "# Server: No action specified.")
  261. return
  262. end
  263. if chat_core.check_language(name, param) then return end
  264. local coord_string = generate_coord_string(name)
  265. player_labels.on_chat_message(name, param)
  266. chat_core.send_all(name, "* <", rename.gpn(name), coord_string .. "> ", param, true)
  267. chat_logging.log_public_action(name, param, coord_string)
  268. afk.reset_timeout(name)
  269. end
  270. chat_core.handle_command_msg = function(name, param)
  271. if not minetest.check_player_privs(name, {shout=true}) then
  272. minetest.chat_send_player(name, "# Server: You do not have 'shout' priv.")
  273. easyvend.sound_error(name)
  274. return -- Player doesn't have shout priv.
  275. end
  276. -- Gagged players cannot polute global chat, but PMs are allowed.
  277. -- If an annoying pervert is PM'ing someone, that person should take advantage
  278. -- of the ignore-list in their Key. Or use F2 if they don't have a Key.
  279. --[[
  280. if command_tokens.mute.player_muted(name) then
  281. minetest.chat_send_player(name, "# Server: You are gagged at the moment.")
  282. easyvend.sound_error(name)
  283. return -- Player is muted.
  284. end
  285. --]]
  286. local coord_string = generate_coord_string(name)
  287. -- Split command arguments.
  288. local p = string.find(param, " ")
  289. local to, newmsg
  290. if p then
  291. newmsg = string.trim(param:sub(p+1))
  292. to = string.trim(param:sub(1, p-1))
  293. end
  294. if type(to)=="string" and type(newmsg)=="string" and string.len(newmsg) > 0 and string.len(to) > 0 then
  295. to = rename.grn(to)
  296. if gdac_invis.is_invisible(to) and to ~= name then -- If target is invisible, and player sending is not same as target ...
  297. 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.
  298. if to == "MustTest" then
  299. minetest.chat_send_player(name, "# Server: The server admin is not available at this time! If it's important, send mail instead.")
  300. else
  301. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not available at this time! If it's important, send mail instead.")
  302. end
  303. return
  304. end
  305. end
  306. if minetest.get_player_by_name(to) then
  307. -- Bad words in PMs.
  308. if chat_core.check_language(name, newmsg) then
  309. return
  310. end
  311. -- Cannot PM player if being ignored.
  312. if chat_controls.player_ignored_pm(to, name) and to ~= name then
  313. minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not available for private messaging!")
  314. easyvend.sound_error(name)
  315. return
  316. end
  317. minetest.after(0, function()
  318. chat_core.alert_player_sound(to)
  319. minetest.chat_send_player(to, color_magenta .. "# PM: FROM <" .. rename.gpn(name) .. coord_string .. ">: " .. newmsg)
  320. -- Record name of last player to send this player a PM.
  321. if chat_core.players[to] then
  322. chat_core.players[to].last_pm_from = name
  323. end
  324. end)
  325. minetest.chat_send_player(name, color_dark_magenta .. "# PM: TO <" .. rename.gpn(to) .. coord_string .. ">: " .. newmsg)
  326. chat_logging.log_private_message(name, to, newmsg)
  327. afk.reset_timeout(name)
  328. else minetest.chat_send_player(name, "# Server: <" .. rename.gpn(to) .. "> is not online.") end
  329. else minetest.chat_send_player(name, "# Server: Usage: '/msg <playername> <message>'.") end
  330. end
  331. function chat_core.handle_command_r(name, param)
  332. local to = chat_core.players[name].last_pm_from or ""
  333. if to == "" then
  334. 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.")
  335. return
  336. end
  337. return chat_core.handle_command_msg(name, to .. " " .. param) -- Prepend target name, and call normal /msg function.
  338. end
  339. function chat_core.alert_player_sound(to)
  340. if chat_controls.beep_enabled(to) then
  341. if afk.is_afk(to) then
  342. minetest.sound_play("chat_alert", {to_player = to, gain = 1}, true)
  343. else
  344. if afk.seconds_since_action(to) > 60*2 then
  345. minetest.sound_play("chat_alert", {to_player = to, gain = 1}, true)
  346. else
  347. minetest.sound_play("chat_alert", {to_player = to, gain = 0.4}, true)
  348. end
  349. end
  350. local pref = minetest.get_player_by_name(to)
  351. if pref then
  352. local pos = pref:get_pos()
  353. ambiance.sound_play("chat_alert", pos, 0.5, 20, to)
  354. end
  355. end
  356. end
  357. if not chat_core.registered then
  358. minetest.register_chatcommand("me", {
  359. params = "<action>",
  360. description = "Send an 'action' message beginning with your name.",
  361. privs = {shout=true},
  362. func = function(name, param)
  363. chat_core.handle_command_me(name, chat_core.rewrite_message(param))
  364. return true
  365. end,
  366. })
  367. minetest.register_chatcommand("msg", {
  368. params = "<player> <message>",
  369. description = "Send a private message to another player.",
  370. privs = {shout=true},
  371. func = function(name, param)
  372. chat_core.handle_command_msg(name, chat_core.rewrite_message(param))
  373. return true
  374. end,
  375. })
  376. minetest.register_chatcommand("r", {
  377. params = "<message>",
  378. description = "Reply via PM to the last player to send you a PM.",
  379. privs = {shout=true},
  380. func = function(name, param)
  381. chat_core.handle_command_r(name, chat_core.rewrite_message(param))
  382. return true
  383. end,
  384. })
  385. -- This should be the only handler registered. Only one handler can be registered.
  386. minetest.register_on_chat_message(function(name, message)
  387. chat_core.on_chat_message(name, chat_core.rewrite_message(message))
  388. return true -- Don't send message automatically, we already did this.
  389. end)
  390. minetest.register_on_joinplayer(function(...) return chat_core.on_joinplayer(...) end)
  391. minetest.register_on_leaveplayer(function(...) return chat_core.on_leaveplayer(...) end)
  392. chat_core.registered = true
  393. end