init.lua 16 KB

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