chat.lua 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. -- Minetest: builtin/game/chat.lua
  2. local S = core.get_translator("__builtin")
  3. -- Helper function that implements search and replace without pattern matching
  4. -- Returns the string and a boolean indicating whether or not the string was modified
  5. local function safe_gsub(s, replace, with)
  6. local i1, i2 = s:find(replace, 1, true)
  7. if not i1 then
  8. return s, false
  9. end
  10. return s:sub(1, i1 - 1) .. with .. s:sub(i2 + 1), true
  11. end
  12. --
  13. -- Chat message formatter
  14. --
  15. -- Implemented in Lua to allow redefinition
  16. function core.format_chat_message(name, message)
  17. local error_str = "Invalid chat message format - missing %s"
  18. local str = core.settings:get("chat_message_format")
  19. local replaced
  20. -- Name
  21. str, replaced = safe_gsub(str, "@name", name)
  22. if not replaced then
  23. error(error_str:format("@name"), 2)
  24. end
  25. -- Timestamp
  26. str = safe_gsub(str, "@timestamp", os.date("%H:%M:%S", os.time()))
  27. -- Insert the message into the string only after finishing all other processing
  28. str, replaced = safe_gsub(str, "@message", message)
  29. if not replaced then
  30. error(error_str:format("@message"), 2)
  31. end
  32. return str
  33. end
  34. --
  35. -- Chat command handler
  36. --
  37. core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
  38. local msg_time_threshold =
  39. tonumber(core.settings:get("chatcommand_msg_time_threshold")) or 0.1
  40. core.register_on_chat_message(function(name, message)
  41. if message:sub(1,1) ~= "/" then
  42. return
  43. end
  44. local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
  45. if not cmd then
  46. core.chat_send_player(name, "-!- "..S("Empty command."))
  47. return true
  48. end
  49. param = param or ""
  50. -- Run core.registered_on_chatcommands callbacks.
  51. if core.run_callbacks(core.registered_on_chatcommands, 5, name, cmd, param) then
  52. return true
  53. end
  54. local cmd_def = core.registered_chatcommands[cmd]
  55. if not cmd_def then
  56. core.chat_send_player(name, "-!- "..S("Invalid command: @1", cmd))
  57. return true
  58. end
  59. local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
  60. if has_privs then
  61. core.set_last_run_mod(cmd_def.mod_origin)
  62. local t_before = core.get_us_time()
  63. local success, result = cmd_def.func(name, param)
  64. local delay = (core.get_us_time() - t_before) / 1000000
  65. if success == false and result == nil then
  66. core.chat_send_player(name, "-!- "..S("Invalid command usage."))
  67. local help_def = core.registered_chatcommands["help"]
  68. if help_def then
  69. local _, helpmsg = help_def.func(name, cmd)
  70. if helpmsg then
  71. core.chat_send_player(name, helpmsg)
  72. end
  73. end
  74. else
  75. if delay > msg_time_threshold then
  76. -- Show how much time it took to execute the command
  77. if result then
  78. result = result .. core.colorize("#f3d2ff", S(" (@1 s)",
  79. string.format("%.5f", delay)))
  80. else
  81. result = core.colorize("#f3d2ff", S(
  82. "Command execution took @1 s",
  83. string.format("%.5f", delay)))
  84. end
  85. end
  86. if result then
  87. core.chat_send_player(name, result)
  88. end
  89. end
  90. else
  91. core.chat_send_player(name,
  92. S("You don't have permission to run this command "
  93. .. "(missing privileges: @1).",
  94. table.concat(missing_privs, ", ")))
  95. end
  96. return true -- Handled chat message
  97. end)
  98. if core.settings:get_bool("profiler.load") then
  99. -- Run after register_chatcommand and its register_on_chat_message
  100. -- Before any chatcommands that should be profiled
  101. profiler.init_chatcommand()
  102. end
  103. -- Parses a "range" string in the format of "here (number)" or
  104. -- "(x1, y1, z1) (x2, y2, z2)", returning two position vectors
  105. local function parse_range_str(player_name, str)
  106. local p1, p2
  107. local args = str:split(" ")
  108. if args[1] == "here" then
  109. p1, p2 = core.get_player_radius_area(player_name, tonumber(args[2]))
  110. if p1 == nil then
  111. return false, S("Unable to get position of player @1.", player_name)
  112. end
  113. else
  114. local player = core.get_player_by_name(player_name)
  115. local relpos
  116. if player then
  117. relpos = player:get_pos()
  118. end
  119. p1, p2 = core.string_to_area(str, relpos)
  120. if p1 == nil or p2 == nil then
  121. return false, S("Incorrect area format. "
  122. .. "Expected: (x1,y1,z1) (x2,y2,z2)")
  123. end
  124. end
  125. return p1, p2
  126. end
  127. --
  128. -- Chat commands
  129. --
  130. core.register_chatcommand("me", {
  131. params = S("<action>"),
  132. description = S("Show chat action (e.g., '/me orders a pizza' "
  133. .. "displays '<player name> orders a pizza')"),
  134. privs = {shout=true},
  135. func = function(name, param)
  136. core.chat_send_all("* " .. name .. " " .. param)
  137. return true
  138. end,
  139. })
  140. core.register_chatcommand("admin", {
  141. description = S("Show the name of the server owner"),
  142. func = function(name)
  143. local admin = core.settings:get("name")
  144. if admin then
  145. return true, S("The administrator of this server is @1.", admin)
  146. else
  147. return false, S("There's no administrator named "
  148. .. "in the config file.")
  149. end
  150. end,
  151. })
  152. local function privileges_of(name, privs)
  153. if not privs then
  154. privs = core.get_player_privs(name)
  155. end
  156. local privstr = core.privs_to_string(privs, ", ")
  157. if privstr == "" then
  158. return S("@1 does not have any privileges.", name)
  159. else
  160. return S("Privileges of @1: @2", name, privstr)
  161. end
  162. end
  163. core.register_chatcommand("privs", {
  164. params = S("[<name>]"),
  165. description = S("Show privileges of yourself or another player"),
  166. func = function(caller, param)
  167. param = param:trim()
  168. local name = (param ~= "" and param or caller)
  169. if not core.player_exists(name) then
  170. return false, S("Player @1 does not exist.", name)
  171. end
  172. return true, privileges_of(name)
  173. end,
  174. })
  175. core.register_chatcommand("haspriv", {
  176. params = S("<privilege>"),
  177. description = S("Return list of all online players with privilege"),
  178. privs = {basic_privs = true},
  179. func = function(caller, param)
  180. param = param:trim()
  181. if param == "" then
  182. return false, S("Invalid parameters (see /help haspriv).")
  183. end
  184. if not core.registered_privileges[param] then
  185. return false, S("Unknown privilege!")
  186. end
  187. local privs = core.string_to_privs(param)
  188. local players_with_priv = {}
  189. for _, player in pairs(core.get_connected_players()) do
  190. local player_name = player:get_player_name()
  191. if core.check_player_privs(player_name, privs) then
  192. table.insert(players_with_priv, player_name)
  193. end
  194. end
  195. if #players_with_priv == 0 then
  196. return true, S("No online player has the \"@1\" privilege.",
  197. param)
  198. else
  199. table.sort(players_with_priv)
  200. return true, S("Players online with the \"@1\" privilege: @2",
  201. param,
  202. table.concat(players_with_priv, ", "))
  203. end
  204. end
  205. })
  206. local function handle_grant_command(caller, grantname, grantprivstr)
  207. local caller_privs = core.get_player_privs(caller)
  208. if not (caller_privs.privs or caller_privs.basic_privs) then
  209. return false, S("Your privileges are insufficient.")
  210. end
  211. if not core.get_auth_handler().get_auth(grantname) then
  212. return false, S("Player @1 does not exist.", grantname)
  213. end
  214. local grantprivs = core.string_to_privs(grantprivstr)
  215. if grantprivstr == "all" then
  216. grantprivs = core.registered_privileges
  217. end
  218. local privs = core.get_player_privs(grantname)
  219. local privs_unknown = ""
  220. local basic_privs =
  221. core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
  222. for priv, _ in pairs(grantprivs) do
  223. if not basic_privs[priv] and not caller_privs.privs then
  224. return false, S("Your privileges are insufficient. "..
  225. "'@1' only allows you to grant: @2",
  226. "basic_privs",
  227. core.privs_to_string(basic_privs, ', '))
  228. end
  229. if not core.registered_privileges[priv] then
  230. privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n"
  231. end
  232. privs[priv] = true
  233. end
  234. if privs_unknown ~= "" then
  235. return false, privs_unknown
  236. end
  237. core.set_player_privs(grantname, privs)
  238. for priv, _ in pairs(grantprivs) do
  239. -- call the on_grant callbacks
  240. core.run_priv_callbacks(grantname, priv, caller, "grant")
  241. end
  242. core.log("action", caller..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
  243. if grantname ~= caller then
  244. core.chat_send_player(grantname,
  245. S("@1 granted you privileges: @2", caller,
  246. core.privs_to_string(grantprivs, ', ')))
  247. end
  248. return true, privileges_of(grantname)
  249. end
  250. core.register_chatcommand("grant", {
  251. params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"),
  252. description = S("Give privileges to player"),
  253. func = function(name, param)
  254. local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
  255. if not grantname or not grantprivstr then
  256. return false, S("Invalid parameters (see /help grant).")
  257. end
  258. return handle_grant_command(name, grantname, grantprivstr)
  259. end,
  260. })
  261. core.register_chatcommand("grantme", {
  262. params = S("<privilege> [, <privilege2> [<...>]] | all"),
  263. description = S("Grant privileges to yourself"),
  264. func = function(name, param)
  265. if param == "" then
  266. return false, S("Invalid parameters (see /help grantme).")
  267. end
  268. return handle_grant_command(name, name, param)
  269. end,
  270. })
  271. local function handle_revoke_command(caller, revokename, revokeprivstr)
  272. local caller_privs = core.get_player_privs(caller)
  273. if not (caller_privs.privs or caller_privs.basic_privs) then
  274. return false, S("Your privileges are insufficient.")
  275. end
  276. if not core.get_auth_handler().get_auth(revokename) then
  277. return false, S("Player @1 does not exist.", revokename)
  278. end
  279. local privs = core.get_player_privs(revokename)
  280. local revokeprivs = core.string_to_privs(revokeprivstr)
  281. local is_singleplayer = core.is_singleplayer()
  282. local is_admin = not is_singleplayer
  283. and revokename == core.settings:get("name")
  284. and revokename ~= ""
  285. if revokeprivstr == "all" then
  286. revokeprivs = table.copy(privs)
  287. end
  288. local privs_unknown = ""
  289. local basic_privs =
  290. core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
  291. local irrevokable = {}
  292. local has_irrevokable_priv = false
  293. for priv, _ in pairs(revokeprivs) do
  294. if not basic_privs[priv] and not caller_privs.privs then
  295. return false, S("Your privileges are insufficient. "..
  296. "'@1' only allows you to revoke: @2",
  297. "basic_privs",
  298. core.privs_to_string(basic_privs, ', '))
  299. end
  300. local def = core.registered_privileges[priv]
  301. if not def then
  302. -- Old/removed privileges might still be granted to certain players
  303. if not privs[priv] then
  304. privs_unknown = privs_unknown .. S("Unknown privilege: @1", priv) .. "\n"
  305. end
  306. elseif is_singleplayer and def.give_to_singleplayer then
  307. irrevokable[priv] = true
  308. elseif is_admin and def.give_to_admin then
  309. irrevokable[priv] = true
  310. end
  311. end
  312. for priv, _ in pairs(irrevokable) do
  313. revokeprivs[priv] = nil
  314. has_irrevokable_priv = true
  315. end
  316. if privs_unknown ~= "" then
  317. return false, privs_unknown
  318. end
  319. if has_irrevokable_priv then
  320. if is_singleplayer then
  321. core.chat_send_player(caller,
  322. S("Note: Cannot revoke in singleplayer: @1",
  323. core.privs_to_string(irrevokable, ', ')))
  324. elseif is_admin then
  325. core.chat_send_player(caller,
  326. S("Note: Cannot revoke from admin: @1",
  327. core.privs_to_string(irrevokable, ', ')))
  328. end
  329. end
  330. local revokecount = 0
  331. for priv, _ in pairs(revokeprivs) do
  332. privs[priv] = nil
  333. revokecount = revokecount + 1
  334. end
  335. if revokecount == 0 then
  336. return false, S("No privileges were revoked.")
  337. end
  338. core.set_player_privs(revokename, privs)
  339. for priv, _ in pairs(revokeprivs) do
  340. -- call the on_revoke callbacks
  341. core.run_priv_callbacks(revokename, priv, caller, "revoke")
  342. end
  343. local new_privs = core.get_player_privs(revokename)
  344. core.log("action", caller..' revoked ('
  345. ..core.privs_to_string(revokeprivs, ', ')
  346. ..') privileges from '..revokename)
  347. if revokename ~= caller then
  348. core.chat_send_player(revokename,
  349. S("@1 revoked privileges from you: @2", caller,
  350. core.privs_to_string(revokeprivs, ', ')))
  351. end
  352. return true, privileges_of(revokename, new_privs)
  353. end
  354. core.register_chatcommand("revoke", {
  355. params = S("<name> (<privilege> [, <privilege2> [<...>]] | all)"),
  356. description = S("Remove privileges from player"),
  357. privs = {},
  358. func = function(name, param)
  359. local revokename, revokeprivstr = string.match(param, "([^ ]+) (.+)")
  360. if not revokename or not revokeprivstr then
  361. return false, S("Invalid parameters (see /help revoke).")
  362. end
  363. return handle_revoke_command(name, revokename, revokeprivstr)
  364. end,
  365. })
  366. core.register_chatcommand("revokeme", {
  367. params = S("<privilege> [, <privilege2> [<...>]] | all"),
  368. description = S("Revoke privileges from yourself"),
  369. privs = {},
  370. func = function(name, param)
  371. if param == "" then
  372. return false, S("Invalid parameters (see /help revokeme).")
  373. end
  374. return handle_revoke_command(name, name, param)
  375. end,
  376. })
  377. core.register_chatcommand("setpassword", {
  378. params = S("<name> <password>"),
  379. description = S("Set player's password (sent unencrypted, thus insecure)"),
  380. privs = {password=true},
  381. func = function(name, param)
  382. local toname, raw_password = string.match(param, "^([^ ]+) +(.+)$")
  383. if not toname then
  384. toname = param:match("^([^ ]+) *$")
  385. raw_password = nil
  386. end
  387. if not toname then
  388. return false, S("Name field required.")
  389. end
  390. local msg_chat, msg_log, msg_ret
  391. if not raw_password then
  392. core.set_player_password(toname, "")
  393. msg_chat = S("Your password was cleared by @1.", name)
  394. msg_log = name .. " clears password of " .. toname .. "."
  395. msg_ret = S("Password of player \"@1\" cleared.", toname)
  396. else
  397. core.set_player_password(toname,
  398. core.get_password_hash(toname,
  399. raw_password))
  400. msg_chat = S("Your password was set by @1.", name)
  401. msg_log = name .. " sets password of " .. toname .. "."
  402. msg_ret = S("Password of player \"@1\" set.", toname)
  403. end
  404. if toname ~= name then
  405. core.chat_send_player(toname, msg_chat)
  406. end
  407. core.log("action", msg_log)
  408. return true, msg_ret
  409. end,
  410. })
  411. core.register_chatcommand("clearpassword", {
  412. params = S("<name>"),
  413. description = S("Set empty password for a player"),
  414. privs = {password=true},
  415. func = function(name, param)
  416. local toname = param
  417. if toname == "" then
  418. return false, S("Name field required.")
  419. end
  420. core.set_player_password(toname, '')
  421. core.log("action", name .. " clears password of " .. toname .. ".")
  422. return true, S("Password of player \"@1\" cleared.", toname)
  423. end,
  424. })
  425. core.register_chatcommand("auth_reload", {
  426. params = "",
  427. description = S("Reload authentication data"),
  428. privs = {server=true},
  429. func = function(name, param)
  430. local done = core.auth_reload()
  431. return done, (done and S("Done.") or S("Failed."))
  432. end,
  433. })
  434. core.register_chatcommand("remove_player", {
  435. params = S("<name>"),
  436. description = S("Remove a player's data"),
  437. privs = {server=true},
  438. func = function(name, param)
  439. local toname = param
  440. if toname == "" then
  441. return false, S("Name field required.")
  442. end
  443. local rc = core.remove_player(toname)
  444. if rc == 0 then
  445. core.log("action", name .. " removed player data of " .. toname .. ".")
  446. return true, S("Player \"@1\" removed.", toname)
  447. elseif rc == 1 then
  448. return true, S("No such player \"@1\" to remove.", toname)
  449. elseif rc == 2 then
  450. return true, S("Player \"@1\" is connected, cannot remove.", toname)
  451. end
  452. return false, S("Unhandled remove_player return code @1.", tostring(rc))
  453. end,
  454. })
  455. -- pos may be a non-integer position
  456. local function find_free_position_near(pos)
  457. local tries = {
  458. vector.new( 1, 0, 0),
  459. vector.new(-1, 0, 0),
  460. vector.new( 0, 0, 1),
  461. vector.new( 0, 0, -1),
  462. }
  463. for _, d in ipairs(tries) do
  464. local p = vector.add(pos, d)
  465. local n = core.get_node_or_nil(p)
  466. if n then
  467. local def = core.registered_nodes[n.name]
  468. if def and not def.walkable then
  469. return p
  470. end
  471. end
  472. end
  473. return pos
  474. end
  475. -- Teleports player <name> to <p> if possible
  476. local function teleport_to_pos(name, p)
  477. local lm = 31007 -- equals MAX_MAP_GENERATION_LIMIT in C++
  478. if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm
  479. or p.z < -lm or p.z > lm then
  480. return false, S("Cannot teleport out of map bounds!")
  481. end
  482. local teleportee = core.get_player_by_name(name)
  483. if not teleportee then
  484. return false, S("Cannot get player with name @1.", name)
  485. end
  486. if teleportee:get_attach() then
  487. return false, S("Cannot teleport, @1 " ..
  488. "is attached to an object!", name)
  489. end
  490. teleportee:set_pos(p)
  491. return true, S("Teleporting @1 to @2.", name, core.pos_to_string(p, 1))
  492. end
  493. -- Teleports player <name> next to player <target_name> if possible
  494. local function teleport_to_player(name, target_name)
  495. if name == target_name then
  496. return false, S("One does not teleport to oneself.")
  497. end
  498. local teleportee = core.get_player_by_name(name)
  499. if not teleportee then
  500. return false, S("Cannot get teleportee with name @1.", name)
  501. end
  502. if teleportee:get_attach() then
  503. return false, S("Cannot teleport, @1 " ..
  504. "is attached to an object!", name)
  505. end
  506. local target = core.get_player_by_name(target_name)
  507. if not target then
  508. return false, S("Cannot get target player with name @1.", target_name)
  509. end
  510. local p = find_free_position_near(target:get_pos())
  511. teleportee:set_pos(p)
  512. return true, S("Teleporting @1 to @2 at @3.", name, target_name,
  513. core.pos_to_string(p, 1))
  514. end
  515. core.register_chatcommand("teleport", {
  516. params = S("<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>"),
  517. description = S("Teleport to position or player"),
  518. privs = {teleport=true},
  519. func = function(name, param)
  520. local player = core.get_player_by_name(name)
  521. local relpos
  522. if player then
  523. relpos = player:get_pos()
  524. end
  525. local p = {}
  526. p.x, p.y, p.z = string.match(param, "^([%d.~-]+)[, ] *([%d.~-]+)[, ] *([%d.~-]+)$")
  527. p = core.parse_coordinates(p.x, p.y, p.z, relpos)
  528. if p and p.x and p.y and p.z then
  529. return teleport_to_pos(name, p)
  530. end
  531. local target_name = param:match("^([^ ]+)$")
  532. if target_name then
  533. return teleport_to_player(name, target_name)
  534. end
  535. local has_bring_priv = core.check_player_privs(name, {bring=true})
  536. local missing_bring_msg = S("You don't have permission to teleport " ..
  537. "other players (missing privilege: @1).", "bring")
  538. local teleportee_name
  539. p = {}
  540. teleportee_name, p.x, p.y, p.z = param:match(
  541. "^([^ ]+) +([%d.~-]+)[, ] *([%d.~-]+)[, ] *([%d.~-]+)$")
  542. if teleportee_name then
  543. local teleportee = core.get_player_by_name(teleportee_name)
  544. if not teleportee then
  545. return
  546. end
  547. relpos = teleportee:get_pos()
  548. p = core.parse_coordinates(p.x, p.y, p.z, relpos)
  549. end
  550. p = vector.apply(p, tonumber)
  551. if teleportee_name and p.x and p.y and p.z then
  552. if not has_bring_priv then
  553. return false, missing_bring_msg
  554. end
  555. return teleport_to_pos(teleportee_name, p)
  556. end
  557. teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
  558. if teleportee_name and target_name then
  559. if not has_bring_priv then
  560. return false, missing_bring_msg
  561. end
  562. return teleport_to_player(teleportee_name, target_name)
  563. end
  564. return false
  565. end,
  566. })
  567. core.register_chatcommand("set", {
  568. params = S("([-n] <name> <value>) | <name>"),
  569. description = S("Set or read server configuration setting"),
  570. privs = {server=true},
  571. func = function(name, param)
  572. local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
  573. if arg and arg == "-n" and setname and setvalue then
  574. core.settings:set(setname, setvalue)
  575. return true, setname .. " = " .. setvalue
  576. end
  577. setname, setvalue = string.match(param, "([^ ]+) (.+)")
  578. if setname and setvalue then
  579. if setname:sub(1, 7) == "secure." then
  580. return false, S("Failed. Cannot modify secure settings. "
  581. .. "Edit the settings file manually.")
  582. end
  583. if not core.settings:get(setname) then
  584. return false, S("Failed. Use '/set -n <name> <value>' "
  585. .. "to create a new setting.")
  586. end
  587. core.settings:set(setname, setvalue)
  588. return true, S("@1 = @2", setname, setvalue)
  589. end
  590. setname = string.match(param, "([^ ]+)")
  591. if setname then
  592. setvalue = core.settings:get(setname)
  593. if not setvalue then
  594. setvalue = S("<not set>")
  595. end
  596. return true, S("@1 = @2", setname, setvalue)
  597. end
  598. return false, S("Invalid parameters (see /help set).")
  599. end,
  600. })
  601. local function emergeblocks_callback(pos, action, num_calls_remaining, ctx)
  602. if ctx.total_blocks == 0 then
  603. ctx.total_blocks = num_calls_remaining + 1
  604. ctx.current_blocks = 0
  605. end
  606. ctx.current_blocks = ctx.current_blocks + 1
  607. if ctx.current_blocks == ctx.total_blocks then
  608. core.chat_send_player(ctx.requestor_name,
  609. S("Finished emerging @1 blocks in @2ms.",
  610. ctx.total_blocks,
  611. string.format("%.2f", (os.clock() - ctx.start_time) * 1000)))
  612. end
  613. end
  614. local function emergeblocks_progress_update(ctx)
  615. if ctx.current_blocks ~= ctx.total_blocks then
  616. core.chat_send_player(ctx.requestor_name,
  617. S("emergeblocks update: @1/@2 blocks emerged (@3%)",
  618. ctx.current_blocks, ctx.total_blocks,
  619. string.format("%.1f", (ctx.current_blocks / ctx.total_blocks) * 100)))
  620. core.after(2, emergeblocks_progress_update, ctx)
  621. end
  622. end
  623. core.register_chatcommand("emergeblocks", {
  624. params = S("(here [<radius>]) | (<pos1> <pos2>)"),
  625. description = S("Load (or, if nonexistent, generate) map blocks contained in "
  626. .. "area pos1 to pos2 (<pos1> and <pos2> must be in parentheses)"),
  627. privs = {server=true},
  628. func = function(name, param)
  629. local p1, p2 = parse_range_str(name, param)
  630. if p1 == false then
  631. return false, p2
  632. end
  633. local context = {
  634. current_blocks = 0,
  635. total_blocks = 0,
  636. start_time = os.clock(),
  637. requestor_name = name
  638. }
  639. core.emerge_area(p1, p2, emergeblocks_callback, context)
  640. core.after(2, emergeblocks_progress_update, context)
  641. return true, S("Started emerge of area ranging from @1 to @2.",
  642. core.pos_to_string(p1, 1), core.pos_to_string(p2, 1))
  643. end,
  644. })
  645. core.register_chatcommand("deleteblocks", {
  646. params = S("(here [<radius>]) | (<pos1> <pos2>)"),
  647. description = S("Delete map blocks contained in area pos1 to pos2 "
  648. .. "(<pos1> and <pos2> must be in parentheses)"),
  649. privs = {server=true},
  650. func = function(name, param)
  651. local p1, p2 = parse_range_str(name, param)
  652. if p1 == false then
  653. return false, p2
  654. end
  655. if core.delete_area(p1, p2) then
  656. return true, S("Successfully cleared area "
  657. .. "ranging from @1 to @2.",
  658. core.pos_to_string(p1, 1), core.pos_to_string(p2, 1))
  659. else
  660. return false, S("Failed to clear one or more "
  661. .. "blocks in area.")
  662. end
  663. end,
  664. })
  665. core.register_chatcommand("fixlight", {
  666. params = S("(here [<radius>]) | (<pos1> <pos2>)"),
  667. description = S("Resets lighting in the area between pos1 and pos2 "
  668. .. "(<pos1> and <pos2> must be in parentheses)"),
  669. privs = {server = true},
  670. func = function(name, param)
  671. local p1, p2 = parse_range_str(name, param)
  672. if p1 == false then
  673. return false, p2
  674. end
  675. if core.fix_light(p1, p2) then
  676. return true, S("Successfully reset light in the area "
  677. .. "ranging from @1 to @2.",
  678. core.pos_to_string(p1, 1), core.pos_to_string(p2, 1))
  679. else
  680. return false, S("Failed to load one or more blocks in area.")
  681. end
  682. end,
  683. })
  684. core.register_chatcommand("mods", {
  685. params = "",
  686. description = S("List mods installed on the server"),
  687. privs = {},
  688. func = function(name, param)
  689. local mods = core.get_modnames()
  690. if #mods == 0 then
  691. return true, S("No mods installed.")
  692. else
  693. return true, table.concat(core.get_modnames(), ", ")
  694. end
  695. end,
  696. })
  697. local function handle_give_command(cmd, giver, receiver, stackstring)
  698. core.log("action", giver .. " invoked " .. cmd
  699. .. ', stackstring="' .. stackstring .. '"')
  700. local itemstack = ItemStack(stackstring)
  701. if itemstack:is_empty() then
  702. return false, S("Cannot give an empty item.")
  703. elseif (not itemstack:is_known()) or (itemstack:get_name() == "unknown") then
  704. return false, S("Cannot give an unknown item.")
  705. -- Forbid giving 'ignore' due to unwanted side effects
  706. elseif itemstack:get_name() == "ignore" then
  707. return false, S("Giving 'ignore' is not allowed.")
  708. end
  709. local receiverref = core.get_player_by_name(receiver)
  710. if receiverref == nil then
  711. return false, S("@1 is not a known player.", receiver)
  712. end
  713. local leftover = receiverref:get_inventory():add_item("main", itemstack)
  714. local partiality
  715. if leftover:is_empty() then
  716. partiality = nil
  717. elseif leftover:get_count() == itemstack:get_count() then
  718. partiality = false
  719. else
  720. partiality = true
  721. end
  722. -- The actual item stack string may be different from what the "giver"
  723. -- entered (e.g. big numbers are always interpreted as 2^16-1).
  724. stackstring = itemstack:to_string()
  725. local msg
  726. if partiality == true then
  727. msg = S("@1 partially added to inventory.", stackstring)
  728. elseif partiality == false then
  729. msg = S("@1 could not be added to inventory.", stackstring)
  730. else
  731. msg = S("@1 added to inventory.", stackstring)
  732. end
  733. if giver == receiver then
  734. return true, msg
  735. else
  736. core.chat_send_player(receiver, msg)
  737. local msg_other
  738. if partiality == true then
  739. msg_other = S("@1 partially added to inventory of @2.",
  740. stackstring, receiver)
  741. elseif partiality == false then
  742. msg_other = S("@1 could not be added to inventory of @2.",
  743. stackstring, receiver)
  744. else
  745. msg_other = S("@1 added to inventory of @2.",
  746. stackstring, receiver)
  747. end
  748. return true, msg_other
  749. end
  750. end
  751. core.register_chatcommand("give", {
  752. params = S("<name> <ItemString> [<count> [<wear>]]"),
  753. description = S("Give item to player"),
  754. privs = {give=true},
  755. func = function(name, param)
  756. local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
  757. if not toname or not itemstring then
  758. return false, S("Name and ItemString required.")
  759. end
  760. return handle_give_command("/give", name, toname, itemstring)
  761. end,
  762. })
  763. core.register_chatcommand("giveme", {
  764. params = S("<ItemString> [<count> [<wear>]]"),
  765. description = S("Give item to yourself"),
  766. privs = {give=true},
  767. func = function(name, param)
  768. local itemstring = string.match(param, "(.+)$")
  769. if not itemstring then
  770. return false, S("ItemString required.")
  771. end
  772. return handle_give_command("/giveme", name, name, itemstring)
  773. end,
  774. })
  775. core.register_chatcommand("spawnentity", {
  776. params = S("<EntityName> [<X>,<Y>,<Z>]"),
  777. description = S("Spawn entity at given (or your) position"),
  778. privs = {give=true, interact=true},
  779. func = function(name, param)
  780. local entityname, pstr = string.match(param, "^([^ ]+) *(.*)$")
  781. if not entityname then
  782. return false, S("EntityName required.")
  783. end
  784. core.log("action", ("%s invokes /spawnentity, entityname=%q")
  785. :format(name, entityname))
  786. local player = core.get_player_by_name(name)
  787. if player == nil then
  788. core.log("error", "Unable to spawn entity, player is nil")
  789. return false, S("Unable to spawn entity, player is nil.")
  790. end
  791. if not core.registered_entities[entityname] then
  792. return false, S("Cannot spawn an unknown entity.")
  793. end
  794. local p
  795. if pstr == "" then
  796. p = player:get_pos()
  797. else
  798. p = {}
  799. p.x, p.y, p.z = string.match(pstr, "^([%d.~-]+)[, ] *([%d.~-]+)[, ] *([%d.~-]+)$")
  800. local relpos = player:get_pos()
  801. p = core.parse_coordinates(p.x, p.y, p.z, relpos)
  802. if not (p and p.x and p.y and p.z) then
  803. return false, S("Invalid parameters (@1).", param)
  804. end
  805. end
  806. p.y = p.y + 1
  807. local obj = core.add_entity(p, entityname)
  808. if obj then
  809. return true, S("@1 spawned.", entityname)
  810. else
  811. return true, S("@1 failed to spawn.", entityname)
  812. end
  813. end,
  814. })
  815. core.register_chatcommand("pulverize", {
  816. params = "",
  817. description = S("Destroy item in hand"),
  818. func = function(name, param)
  819. local player = core.get_player_by_name(name)
  820. if not player then
  821. core.log("error", "Unable to pulverize, no player.")
  822. return false, S("Unable to pulverize, no player.")
  823. end
  824. local wielded_item = player:get_wielded_item()
  825. if wielded_item:is_empty() then
  826. return false, S("Unable to pulverize, no item in hand.")
  827. end
  828. core.log("action", name .. " pulverized \"" ..
  829. wielded_item:get_name() .. " " .. wielded_item:get_count() .. "\"")
  830. player:set_wielded_item(nil)
  831. return true, S("An item was pulverized.")
  832. end,
  833. })
  834. -- Key = player name
  835. core.rollback_punch_callbacks = {}
  836. core.register_on_punchnode(function(pos, node, puncher)
  837. local name = puncher and puncher:get_player_name()
  838. if name and core.rollback_punch_callbacks[name] then
  839. core.rollback_punch_callbacks[name](pos, node, puncher)
  840. core.rollback_punch_callbacks[name] = nil
  841. end
  842. end)
  843. core.register_chatcommand("rollback_check", {
  844. params = S("[<range>] [<seconds>] [<limit>]"),
  845. description = S("Check who last touched a node or a node near it "
  846. .. "within the time specified by <seconds>. "
  847. .. "Default: range = 0, seconds = 86400 = 24h, limit = 5. "
  848. .. "Set <seconds> to inf for no time limit"),
  849. privs = {rollback=true},
  850. func = function(name, param)
  851. if not core.settings:get_bool("enable_rollback_recording") then
  852. return false, S("Rollback functions are disabled.")
  853. end
  854. local range, seconds, limit =
  855. param:match("(%d+) *(%d*) *(%d*)")
  856. range = tonumber(range) or 0
  857. seconds = tonumber(seconds) or 86400
  858. limit = tonumber(limit) or 5
  859. if limit > 100 then
  860. return false, S("That limit is too high!")
  861. end
  862. core.rollback_punch_callbacks[name] = function(pos, node, puncher)
  863. local name = puncher:get_player_name()
  864. core.chat_send_player(name, S("Checking @1 ...", core.pos_to_string(pos)))
  865. local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
  866. if not actions then
  867. core.chat_send_player(name, S("Rollback functions are disabled."))
  868. return
  869. end
  870. local num_actions = #actions
  871. if num_actions == 0 then
  872. core.chat_send_player(name,
  873. S("Nobody has touched the specified "
  874. .. "location in @1 seconds.",
  875. seconds))
  876. return
  877. end
  878. local time = os.time()
  879. for i = num_actions, 1, -1 do
  880. local action = actions[i]
  881. core.chat_send_player(name,
  882. S("@1 @2 @3 -> @4 @5 seconds ago.",
  883. core.pos_to_string(action.pos),
  884. action.actor,
  885. action.oldnode.name,
  886. action.newnode.name,
  887. time - action.time))
  888. end
  889. end
  890. return true, S("Punch a node (range=@1, seconds=@2, limit=@3).",
  891. range, seconds, limit)
  892. end,
  893. })
  894. core.register_chatcommand("rollback", {
  895. params = S("(<name> [<seconds>]) | (:<actor> [<seconds>])"),
  896. description = S("Revert actions of a player. "
  897. .. "Default for <seconds> is 60. "
  898. .. "Set <seconds> to inf for no time limit"),
  899. privs = {rollback=true},
  900. func = function(name, param)
  901. if not core.settings:get_bool("enable_rollback_recording") then
  902. return false, S("Rollback functions are disabled.")
  903. end
  904. local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
  905. local rev_msg
  906. if not target_name then
  907. local player_name
  908. player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
  909. if not player_name then
  910. return false, S("Invalid parameters. "
  911. .. "See /help rollback and "
  912. .. "/help rollback_check.")
  913. end
  914. seconds = tonumber(seconds) or 60
  915. target_name = "player:"..player_name
  916. rev_msg = S("Reverting actions of player '@1' since @2 seconds.",
  917. player_name, seconds)
  918. else
  919. seconds = tonumber(seconds) or 60
  920. rev_msg = S("Reverting actions of @1 since @2 seconds.",
  921. target_name, seconds)
  922. end
  923. core.chat_send_player(name, rev_msg)
  924. local success, log = core.rollback_revert_actions_by(
  925. target_name, seconds)
  926. local response = ""
  927. if #log > 100 then
  928. response = S("(log is too long to show)").."\n"
  929. else
  930. for _, line in pairs(log) do
  931. response = response .. line .. "\n"
  932. end
  933. end
  934. if success then
  935. response = response .. S("Reverting actions succeeded.")
  936. else
  937. response = response .. S("Reverting actions FAILED.")
  938. end
  939. return success, response
  940. end,
  941. })
  942. core.register_chatcommand("status", {
  943. description = S("Show server status"),
  944. func = function(name, param)
  945. local status = core.get_server_status(name, false)
  946. if status and status ~= "" then
  947. return true, status
  948. end
  949. return false, S("This command was disabled by a mod or game.")
  950. end,
  951. })
  952. local function get_time(timeofday)
  953. local time = math.floor(timeofday * 1440)
  954. local minute = time % 60
  955. local hour = (time - minute) / 60
  956. return time, hour, minute
  957. end
  958. core.register_chatcommand("time", {
  959. params = S("[<0..23>:<0..59> | <0..24000>]"),
  960. description = S("Show or set time of day"),
  961. privs = {},
  962. func = function(name, param)
  963. if param == "" then
  964. local current_time = math.floor(core.get_timeofday() * 1440)
  965. local minutes = current_time % 60
  966. local hour = (current_time - minutes) / 60
  967. return true, S("Current time is @1:@2.",
  968. string.format("%d", hour),
  969. string.format("%02d", minutes))
  970. end
  971. local player_privs = core.get_player_privs(name)
  972. if not player_privs.settime then
  973. return false, S("You don't have permission to run "
  974. .. "this command (missing privilege: @1).", "settime")
  975. end
  976. local relative, negative, hour, minute = param:match("^(~?)(%-?)(%d+):(%d+)$")
  977. if not relative then -- checking the first capture against nil suffices
  978. local new_time = core.parse_relative_number(param, core.get_timeofday() * 24000)
  979. if not new_time then
  980. new_time = tonumber(param) or -1
  981. else
  982. new_time = new_time % 24000
  983. end
  984. if new_time ~= new_time or new_time < 0 or new_time > 24000 then
  985. return false, S("Invalid time (must be between 0 and 24000).")
  986. end
  987. core.set_timeofday(new_time / 24000)
  988. core.log("action", name .. " sets time to " .. new_time)
  989. return true, S("Time of day changed.")
  990. end
  991. local new_time
  992. hour = tonumber(hour)
  993. minute = tonumber(minute)
  994. if relative == "" then
  995. if hour < 0 or hour > 23 then
  996. return false, S("Invalid hour (must be between 0 and 23 inclusive).")
  997. elseif minute < 0 or minute > 59 then
  998. return false, S("Invalid minute (must be between 0 and 59 inclusive).")
  999. end
  1000. new_time = (hour * 60 + minute) / 1440
  1001. else
  1002. if minute < 0 or minute > 59 then
  1003. return false, S("Invalid minute (must be between 0 and 59 inclusive).")
  1004. end
  1005. local current_time = core.get_timeofday()
  1006. if negative == "-" then -- negative time
  1007. hour, minute = -hour, -minute
  1008. end
  1009. new_time = (current_time + (hour * 60 + minute) / 1440) % 1
  1010. local _
  1011. _, hour, minute = get_time(new_time)
  1012. end
  1013. core.set_timeofday(new_time)
  1014. core.log("action", ("%s sets time to %d:%02d"):format(name, hour, minute))
  1015. return true, S("Time of day changed.")
  1016. end,
  1017. })
  1018. core.register_chatcommand("days", {
  1019. description = S("Show day count since world creation"),
  1020. func = function(name, param)
  1021. return true, S("Current day is @1.", core.get_day_count())
  1022. end
  1023. })
  1024. local function parse_shutdown_param(param)
  1025. local delay, reconnect, message
  1026. local one, two, three
  1027. one, two, three = param:match("^(%S+) +(%-r) +(.*)")
  1028. if one and two and three then
  1029. -- 3 arguments: delay, reconnect and message
  1030. return one, two, three
  1031. end
  1032. -- 2 arguments
  1033. one, two = param:match("^(%S+) +(.*)")
  1034. if one and two then
  1035. if tonumber(one) then
  1036. delay = one
  1037. if two == "-r" then
  1038. reconnect = two
  1039. else
  1040. message = two
  1041. end
  1042. elseif one == "-r" then
  1043. reconnect, message = one, two
  1044. end
  1045. return delay, reconnect, message
  1046. end
  1047. -- 1 argument
  1048. one = param:match("(.*)")
  1049. if tonumber(one) then
  1050. delay = one
  1051. elseif one == "-r" then
  1052. reconnect = one
  1053. else
  1054. message = one
  1055. end
  1056. return delay, reconnect, message
  1057. end
  1058. core.register_chatcommand("shutdown", {
  1059. params = S("[<delay_in_seconds> | -1] [-r] [<message>]"),
  1060. description = S("Shutdown server (-1 cancels a delayed shutdown, -r allows players to reconnect)"),
  1061. privs = {server=true},
  1062. func = function(name, param)
  1063. local delay, reconnect, message = parse_shutdown_param(param)
  1064. local bool_reconnect = reconnect == "-r"
  1065. if not message then
  1066. message = ""
  1067. end
  1068. delay = tonumber(delay) or 0
  1069. if delay == 0 then
  1070. core.log("action", name .. " shuts down server")
  1071. core.chat_send_all("*** "..S("Server shutting down (operator request)."))
  1072. end
  1073. core.request_shutdown(message:trim(), bool_reconnect, delay)
  1074. return true
  1075. end,
  1076. })
  1077. core.register_chatcommand("ban", {
  1078. params = S("[<name>]"),
  1079. description = S("Ban the IP of a player or show the ban list"),
  1080. privs = {ban=true},
  1081. func = function(name, param)
  1082. if param == "" then
  1083. local ban_list = core.get_ban_list()
  1084. if ban_list == "" then
  1085. return true, S("The ban list is empty.")
  1086. else
  1087. return true, S("Ban list: @1", ban_list)
  1088. end
  1089. end
  1090. if core.is_singleplayer() then
  1091. return false, S("You cannot ban players in singleplayer!")
  1092. end
  1093. if not core.get_player_by_name(param) then
  1094. return false, S("Player is not online.")
  1095. end
  1096. if not core.ban_player(param) then
  1097. return false, S("Failed to ban player.")
  1098. end
  1099. local desc = core.get_ban_description(param)
  1100. core.log("action", name .. " bans " .. desc .. ".")
  1101. return true, S("Banned @1.", desc)
  1102. end,
  1103. })
  1104. core.register_chatcommand("unban", {
  1105. params = S("<name> | <IP_address>"),
  1106. description = S("Remove IP ban belonging to a player/IP"),
  1107. privs = {ban=true},
  1108. func = function(name, param)
  1109. if not core.unban_player_or_ip(param) then
  1110. return false, S("Failed to unban player/IP.")
  1111. end
  1112. core.log("action", name .. " unbans " .. param)
  1113. return true, S("Unbanned @1.", param)
  1114. end,
  1115. })
  1116. core.register_chatcommand("kick", {
  1117. params = S("<name> [<reason>]"),
  1118. description = S("Kick a player"),
  1119. privs = {kick=true},
  1120. func = function(name, param)
  1121. local tokick, reason = param:match("([^ ]+) (.+)")
  1122. tokick = tokick or param
  1123. if not core.kick_player(tokick, reason) then
  1124. return false, S("Failed to kick player @1.", tokick)
  1125. end
  1126. local log_reason = ""
  1127. if reason then
  1128. log_reason = " with reason \"" .. reason .. "\""
  1129. end
  1130. core.log("action", name .. " kicks " .. tokick .. log_reason)
  1131. return true, S("Kicked @1.", tokick)
  1132. end,
  1133. })
  1134. core.register_chatcommand("clearobjects", {
  1135. params = S("[full | quick]"),
  1136. description = S("Clear all objects in world"),
  1137. privs = {server=true},
  1138. func = function(name, param)
  1139. local options = {}
  1140. if param == "" or param == "quick" then
  1141. options.mode = "quick"
  1142. elseif param == "full" then
  1143. options.mode = "full"
  1144. else
  1145. return false, S("Invalid usage, see /help clearobjects.")
  1146. end
  1147. core.log("action", name .. " clears objects ("
  1148. .. options.mode .. " mode).")
  1149. if options.mode == "full" then
  1150. core.chat_send_all(S("Clearing all objects. This may take a long time. "
  1151. .. "You may experience a timeout. (by @1)", name))
  1152. end
  1153. core.clear_objects(options)
  1154. core.log("action", "Object clearing done.")
  1155. core.chat_send_all("*** "..S("Cleared all objects."))
  1156. return true
  1157. end,
  1158. })
  1159. core.register_chatcommand("msg", {
  1160. params = S("<name> <message>"),
  1161. description = S("Send a direct message to a player"),
  1162. privs = {shout=true},
  1163. func = function(name, param)
  1164. local sendto, message = param:match("^(%S+)%s(.+)$")
  1165. if not sendto then
  1166. return false, S("Invalid usage, see /help msg.")
  1167. end
  1168. if not core.get_player_by_name(sendto) then
  1169. return false, S("The player @1 is not online.", sendto)
  1170. end
  1171. core.log("action", "DM from " .. name .. " to " .. sendto
  1172. .. ": " .. message)
  1173. core.chat_send_player(sendto, S("DM from @1: @2", name, message))
  1174. return true, S("Message sent.")
  1175. end,
  1176. })
  1177. core.register_chatcommand("last-login", {
  1178. params = S("[<name>]"),
  1179. description = S("Get the last login time of a player or yourself"),
  1180. func = function(name, param)
  1181. if param == "" then
  1182. param = name
  1183. end
  1184. local pauth = core.get_auth_handler().get_auth(param)
  1185. if pauth and pauth.last_login and pauth.last_login ~= -1 then
  1186. -- Time in UTC, ISO 8601 format
  1187. return true, S("@1's last login time was @2.",
  1188. param,
  1189. os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login))
  1190. end
  1191. return false, S("@1's last login time is unknown.", param)
  1192. end,
  1193. })
  1194. core.register_chatcommand("clearinv", {
  1195. params = S("[<name>]"),
  1196. description = S("Clear the inventory of yourself or another player"),
  1197. func = function(name, param)
  1198. local player
  1199. if param and param ~= "" and param ~= name then
  1200. if not core.check_player_privs(name, {server=true}) then
  1201. return false, S("You don't have permission to "
  1202. .. "clear another player's inventory "
  1203. .. "(missing privilege: @1).", "server")
  1204. end
  1205. player = core.get_player_by_name(param)
  1206. core.chat_send_player(param, S("@1 cleared your inventory.", name))
  1207. else
  1208. player = core.get_player_by_name(name)
  1209. end
  1210. if player then
  1211. player:get_inventory():set_list("main", {})
  1212. player:get_inventory():set_list("craft", {})
  1213. player:get_inventory():set_list("craftpreview", {})
  1214. core.log("action", name.." clears "..player:get_player_name().."'s inventory")
  1215. return true, S("Cleared @1's inventory.", player:get_player_name())
  1216. else
  1217. return false, S("Player must be online to clear inventory!")
  1218. end
  1219. end,
  1220. })
  1221. local function handle_kill_command(killer, victim)
  1222. if core.settings:get_bool("enable_damage") == false then
  1223. return false, S("Players can't be killed, damage has been disabled.")
  1224. end
  1225. local victimref = core.get_player_by_name(victim)
  1226. if victimref == nil then
  1227. return false, S("Player @1 is not online.", victim)
  1228. elseif victimref:get_hp() <= 0 then
  1229. if killer == victim then
  1230. return false, S("You are already dead.")
  1231. else
  1232. return false, S("@1 is already dead.", victim)
  1233. end
  1234. end
  1235. if killer ~= victim then
  1236. core.log("action", string.format("%s killed %s", killer, victim))
  1237. end
  1238. -- Kill victim
  1239. victimref:set_hp(0)
  1240. return true, S("@1 has been killed.", victim)
  1241. end
  1242. core.register_chatcommand("kill", {
  1243. params = S("[<name>]"),
  1244. description = S("Kill player or yourself"),
  1245. privs = {server=true},
  1246. func = function(name, param)
  1247. return handle_kill_command(name, param == "" and name or param)
  1248. end,
  1249. })