init.lua 17 KB

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