chatcommands.lua 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. -- Minetest: builtin/game/chatcommands.lua
  2. --
  3. -- Chat command handler
  4. --
  5. core.chatcommands = core.registered_chatcommands -- BACKWARDS COMPATIBILITY
  6. core.register_on_chat_message(function(name, message)
  7. if message:sub(1,1) ~= "/" then
  8. return
  9. end
  10. local cmd, param = string.match(message, "^/([^ ]+) *(.*)")
  11. if not cmd then
  12. core.chat_send_player(name, "-!- Empty command")
  13. return true
  14. end
  15. param = param or ""
  16. local cmd_def = core.registered_chatcommands[cmd]
  17. if not cmd_def then
  18. core.chat_send_player(name, "-!- Invalid command: " .. cmd)
  19. return true
  20. end
  21. local has_privs, missing_privs = core.check_player_privs(name, cmd_def.privs)
  22. if has_privs then
  23. core.set_last_run_mod(cmd_def.mod_origin)
  24. local success, message = cmd_def.func(name, param)
  25. if message then
  26. core.chat_send_player(name, message)
  27. end
  28. else
  29. core.chat_send_player(name, "You don't have permission"
  30. .. " to run this command (missing privileges: "
  31. .. table.concat(missing_privs, ", ") .. ")")
  32. end
  33. return true -- Handled chat message
  34. end)
  35. if core.settings:get_bool("profiler.load") then
  36. -- Run after register_chatcommand and its register_on_chat_message
  37. -- Before any chattcommands that should be profiled
  38. profiler.init_chatcommand()
  39. end
  40. -- Parses a "range" string in the format of "here (number)" or
  41. -- "(x1, y1, z1) (x2, y2, z2)", returning two position vectors
  42. local function parse_range_str(player_name, str)
  43. local p1, p2
  44. local args = str:split(" ")
  45. if args[1] == "here" then
  46. p1, p2 = core.get_player_radius_area(player_name, tonumber(args[2]))
  47. if p1 == nil then
  48. return false, "Unable to get player " .. player_name .. " position"
  49. end
  50. else
  51. p1, p2 = core.string_to_area(str)
  52. if p1 == nil then
  53. return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
  54. end
  55. end
  56. return p1, p2
  57. end
  58. --
  59. -- Chat commands
  60. --
  61. core.register_chatcommand("me", {
  62. params = "<action>",
  63. description = "Display chat action (e.g., '/me orders a pizza' displays"
  64. .. " '<player name> orders a pizza')",
  65. privs = {shout=true},
  66. func = function(name, param)
  67. core.chat_send_all("* " .. name .. " " .. param)
  68. end,
  69. })
  70. core.register_chatcommand("admin", {
  71. description = "Show the name of the server owner",
  72. func = function(name)
  73. local admin = minetest.settings:get("name")
  74. if admin then
  75. return true, "The administrator of this server is "..admin.."."
  76. else
  77. return false, "There's no administrator named in the config file."
  78. end
  79. end,
  80. })
  81. core.register_chatcommand("privs", {
  82. params = "[<name>]",
  83. description = "Print privileges of player",
  84. func = function(caller, param)
  85. param = param:trim()
  86. local name = (param ~= "" and param or caller)
  87. return true, "Privileges of " .. name .. ": "
  88. .. core.privs_to_string(
  89. core.get_player_privs(name), ' ')
  90. end,
  91. })
  92. local function handle_grant_command(caller, grantname, grantprivstr)
  93. local caller_privs = minetest.get_player_privs(caller)
  94. if not (caller_privs.privs or caller_privs.basic_privs) then
  95. return false, "Your privileges are insufficient."
  96. end
  97. if not core.get_auth_handler().get_auth(grantname) then
  98. return false, "Player " .. grantname .. " does not exist."
  99. end
  100. local grantprivs = core.string_to_privs(grantprivstr)
  101. if grantprivstr == "all" then
  102. grantprivs = core.registered_privileges
  103. end
  104. local privs = core.get_player_privs(grantname)
  105. local privs_unknown = ""
  106. local basic_privs =
  107. core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
  108. for priv, _ in pairs(grantprivs) do
  109. if not basic_privs[priv] and not caller_privs.privs then
  110. return false, "Your privileges are insufficient."
  111. end
  112. if not core.registered_privileges[priv] then
  113. privs_unknown = privs_unknown .. "Unknown privilege: " .. priv .. "\n"
  114. end
  115. privs[priv] = true
  116. end
  117. if privs_unknown ~= "" then
  118. return false, privs_unknown
  119. end
  120. core.set_player_privs(grantname, privs)
  121. core.log("action", caller..' granted ('..core.privs_to_string(grantprivs, ', ')..') privileges to '..grantname)
  122. if grantname ~= caller then
  123. core.chat_send_player(grantname, caller
  124. .. " granted you privileges: "
  125. .. core.privs_to_string(grantprivs, ' '))
  126. end
  127. return true, "Privileges of " .. grantname .. ": "
  128. .. core.privs_to_string(
  129. core.get_player_privs(grantname), ' ')
  130. end
  131. core.register_chatcommand("grant", {
  132. params = "<name> (<privilege> | all)",
  133. description = "Give privilege to player",
  134. func = function(name, param)
  135. local grantname, grantprivstr = string.match(param, "([^ ]+) (.+)")
  136. if not grantname or not grantprivstr then
  137. return false, "Invalid parameters (see /help grant)"
  138. end
  139. return handle_grant_command(name, grantname, grantprivstr)
  140. end,
  141. })
  142. core.register_chatcommand("grantme", {
  143. params = "<privilege> | all",
  144. description = "Grant privileges to yourself",
  145. func = function(name, param)
  146. if param == "" then
  147. return false, "Invalid parameters (see /help grantme)"
  148. end
  149. return handle_grant_command(name, name, param)
  150. end,
  151. })
  152. core.register_chatcommand("revoke", {
  153. params = "<name> (<privilege> | all)",
  154. description = "Remove privilege from player",
  155. privs = {},
  156. func = function(name, param)
  157. if not core.check_player_privs(name, {privs=true}) and
  158. not core.check_player_privs(name, {basic_privs=true}) then
  159. return false, "Your privileges are insufficient."
  160. end
  161. local revoke_name, revoke_priv_str = string.match(param, "([^ ]+) (.+)")
  162. if not revoke_name or not revoke_priv_str then
  163. return false, "Invalid parameters (see /help revoke)"
  164. elseif not core.get_auth_handler().get_auth(revoke_name) then
  165. return false, "Player " .. revoke_name .. " does not exist."
  166. end
  167. local revoke_privs = core.string_to_privs(revoke_priv_str)
  168. local privs = core.get_player_privs(revoke_name)
  169. local basic_privs =
  170. core.string_to_privs(core.settings:get("basic_privs") or "interact,shout")
  171. for priv, _ in pairs(revoke_privs) do
  172. if not basic_privs[priv] and
  173. not core.check_player_privs(name, {privs=true}) then
  174. return false, "Your privileges are insufficient."
  175. end
  176. end
  177. if revoke_priv_str == "all" then
  178. privs = {}
  179. else
  180. for priv, _ in pairs(revoke_privs) do
  181. privs[priv] = nil
  182. end
  183. end
  184. core.set_player_privs(revoke_name, privs)
  185. core.log("action", name..' revoked ('
  186. ..core.privs_to_string(revoke_privs, ', ')
  187. ..') privileges from '..revoke_name)
  188. if revoke_name ~= name then
  189. core.chat_send_player(revoke_name, name
  190. .. " revoked privileges from you: "
  191. .. core.privs_to_string(revoke_privs, ' '))
  192. end
  193. return true, "Privileges of " .. revoke_name .. ": "
  194. .. core.privs_to_string(
  195. core.get_player_privs(revoke_name), ' ')
  196. end,
  197. })
  198. core.register_chatcommand("setpassword", {
  199. params = "<name> <password>",
  200. description = "Set player's password",
  201. privs = {password=true},
  202. func = function(name, param)
  203. local toname, raw_password = string.match(param, "^([^ ]+) +(.+)$")
  204. if not toname then
  205. toname = param:match("^([^ ]+) *$")
  206. raw_password = nil
  207. end
  208. if not toname then
  209. return false, "Name field required"
  210. end
  211. local act_str_past = "?"
  212. local act_str_pres = "?"
  213. if not raw_password then
  214. core.set_player_password(toname, "")
  215. act_str_past = "cleared"
  216. act_str_pres = "clears"
  217. else
  218. core.set_player_password(toname,
  219. core.get_password_hash(toname,
  220. raw_password))
  221. act_str_past = "set"
  222. act_str_pres = "sets"
  223. end
  224. if toname ~= name then
  225. core.chat_send_player(toname, "Your password was "
  226. .. act_str_past .. " by " .. name)
  227. end
  228. core.log("action", name .. " " .. act_str_pres
  229. .. " password of " .. toname .. ".")
  230. return true, "Password of player \"" .. toname .. "\" " .. act_str_past
  231. end,
  232. })
  233. core.register_chatcommand("clearpassword", {
  234. params = "<name>",
  235. description = "Set empty password",
  236. privs = {password=true},
  237. func = function(name, param)
  238. local toname = param
  239. if toname == "" then
  240. return false, "Name field required"
  241. end
  242. core.set_player_password(toname, '')
  243. core.log("action", name .. " clears password of " .. toname .. ".")
  244. return true, "Password of player \"" .. toname .. "\" cleared"
  245. end,
  246. })
  247. core.register_chatcommand("auth_reload", {
  248. params = "",
  249. description = "Reload authentication data",
  250. privs = {server=true},
  251. func = function(name, param)
  252. local done = core.auth_reload()
  253. return done, (done and "Done." or "Failed.")
  254. end,
  255. })
  256. core.register_chatcommand("remove_player", {
  257. params = "<name>",
  258. description = "Remove player data",
  259. privs = {server=true},
  260. func = function(name, param)
  261. local toname = param
  262. if toname == "" then
  263. return false, "Name field required"
  264. end
  265. local rc = core.remove_player(toname)
  266. if rc == 0 then
  267. core.log("action", name .. " removed player data of " .. toname .. ".")
  268. return true, "Player \"" .. toname .. "\" removed."
  269. elseif rc == 1 then
  270. return true, "No such player \"" .. toname .. "\" to remove."
  271. elseif rc == 2 then
  272. return true, "Player \"" .. toname .. "\" is connected, cannot remove."
  273. end
  274. return false, "Unhandled remove_player return code " .. rc .. ""
  275. end,
  276. })
  277. core.register_chatcommand("teleport", {
  278. params = "<X>,<Y>,<Z> | <to_name> | (<name> <X>,<Y>,<Z>) | (<name> <to_name>)",
  279. description = "Teleport to player or position",
  280. privs = {teleport=true},
  281. func = function(name, param)
  282. -- Returns (pos, true) if found, otherwise (pos, false)
  283. local function find_free_position_near(pos)
  284. local tries = {
  285. {x=1,y=0,z=0},
  286. {x=-1,y=0,z=0},
  287. {x=0,y=0,z=1},
  288. {x=0,y=0,z=-1},
  289. }
  290. for _, d in ipairs(tries) do
  291. local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
  292. local n = core.get_node_or_nil(p)
  293. if n and n.name then
  294. local def = core.registered_nodes[n.name]
  295. if def and not def.walkable then
  296. return p, true
  297. end
  298. end
  299. end
  300. return pos, false
  301. end
  302. local teleportee = nil
  303. local p = {}
  304. p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  305. p.x = tonumber(p.x)
  306. p.y = tonumber(p.y)
  307. p.z = tonumber(p.z)
  308. if p.x and p.y and p.z then
  309. local lm = 31000
  310. if p.x < -lm or p.x > lm or p.y < -lm or p.y > lm or p.z < -lm or p.z > lm then
  311. return false, "Cannot teleport out of map bounds!"
  312. end
  313. teleportee = core.get_player_by_name(name)
  314. if teleportee then
  315. teleportee:setpos(p)
  316. return true, "Teleporting to "..core.pos_to_string(p)
  317. end
  318. end
  319. local teleportee = nil
  320. local p = nil
  321. local target_name = nil
  322. target_name = param:match("^([^ ]+)$")
  323. teleportee = core.get_player_by_name(name)
  324. if target_name then
  325. local target = core.get_player_by_name(target_name)
  326. if target then
  327. p = target:getpos()
  328. end
  329. end
  330. if teleportee and p then
  331. p = find_free_position_near(p)
  332. teleportee:setpos(p)
  333. return true, "Teleporting to " .. target_name
  334. .. " at "..core.pos_to_string(p)
  335. end
  336. if not core.check_player_privs(name, {bring=true}) then
  337. return false, "You don't have permission to teleport other players (missing bring privilege)"
  338. end
  339. local teleportee = nil
  340. local p = {}
  341. local teleportee_name = nil
  342. teleportee_name, p.x, p.y, p.z = param:match(
  343. "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
  344. p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
  345. if teleportee_name then
  346. teleportee = core.get_player_by_name(teleportee_name)
  347. end
  348. if teleportee and p.x and p.y and p.z then
  349. teleportee:setpos(p)
  350. return true, "Teleporting " .. teleportee_name
  351. .. " to " .. core.pos_to_string(p)
  352. end
  353. local teleportee = nil
  354. local p = nil
  355. local teleportee_name = nil
  356. local target_name = nil
  357. teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
  358. if teleportee_name then
  359. teleportee = core.get_player_by_name(teleportee_name)
  360. end
  361. if target_name then
  362. local target = core.get_player_by_name(target_name)
  363. if target then
  364. p = target:getpos()
  365. end
  366. end
  367. if teleportee and p then
  368. p = find_free_position_near(p)
  369. teleportee:setpos(p)
  370. return true, "Teleporting " .. teleportee_name
  371. .. " to " .. target_name
  372. .. " at " .. core.pos_to_string(p)
  373. end
  374. return false, 'Invalid parameters ("' .. param
  375. .. '") or player not found (see /help teleport)'
  376. end,
  377. })
  378. core.register_chatcommand("set", {
  379. params = "([-n] <name> <value>) | <name>",
  380. description = "Set or read server configuration setting",
  381. privs = {server=true},
  382. func = function(name, param)
  383. local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
  384. if arg and arg == "-n" and setname and setvalue then
  385. core.settings:set(setname, setvalue)
  386. return true, setname .. " = " .. setvalue
  387. end
  388. local setname, setvalue = string.match(param, "([^ ]+) (.+)")
  389. if setname and setvalue then
  390. if not core.settings:get(setname) then
  391. return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
  392. end
  393. core.settings:set(setname, setvalue)
  394. return true, setname .. " = " .. setvalue
  395. end
  396. local setname = string.match(param, "([^ ]+)")
  397. if setname then
  398. local setvalue = core.settings:get(setname)
  399. if not setvalue then
  400. setvalue = "<not set>"
  401. end
  402. return true, setname .. " = " .. setvalue
  403. end
  404. return false, "Invalid parameters (see /help set)."
  405. end,
  406. })
  407. local function emergeblocks_callback(pos, action, num_calls_remaining, ctx)
  408. if ctx.total_blocks == 0 then
  409. ctx.total_blocks = num_calls_remaining + 1
  410. ctx.current_blocks = 0
  411. end
  412. ctx.current_blocks = ctx.current_blocks + 1
  413. if ctx.current_blocks == ctx.total_blocks then
  414. core.chat_send_player(ctx.requestor_name,
  415. string.format("Finished emerging %d blocks in %.2fms.",
  416. ctx.total_blocks, (os.clock() - ctx.start_time) * 1000))
  417. end
  418. end
  419. local function emergeblocks_progress_update(ctx)
  420. if ctx.current_blocks ~= ctx.total_blocks then
  421. core.chat_send_player(ctx.requestor_name,
  422. string.format("emergeblocks update: %d/%d blocks emerged (%.1f%%)",
  423. ctx.current_blocks, ctx.total_blocks,
  424. (ctx.current_blocks / ctx.total_blocks) * 100))
  425. core.after(2, emergeblocks_progress_update, ctx)
  426. end
  427. end
  428. core.register_chatcommand("emergeblocks", {
  429. params = "(here [<radius>]) | (<pos1> <pos2>)",
  430. description = "Load (or, if nonexistent, generate) map blocks "
  431. .. "contained in area pos1 to pos2 (<pos1> and <pos2> must be in parentheses)",
  432. privs = {server=true},
  433. func = function(name, param)
  434. local p1, p2 = parse_range_str(name, param)
  435. if p1 == false then
  436. return false, p2
  437. end
  438. local context = {
  439. current_blocks = 0,
  440. total_blocks = 0,
  441. start_time = os.clock(),
  442. requestor_name = name
  443. }
  444. core.emerge_area(p1, p2, emergeblocks_callback, context)
  445. core.after(2, emergeblocks_progress_update, context)
  446. return true, "Started emerge of area ranging from " ..
  447. core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
  448. end,
  449. })
  450. core.register_chatcommand("deleteblocks", {
  451. params = "(here [<radius>]) | (<pos1> <pos2>)",
  452. description = "Delete map blocks contained in area pos1 to pos2 "
  453. .. "(<pos1> and <pos2> must be in parentheses)",
  454. privs = {server=true},
  455. func = function(name, param)
  456. local p1, p2 = parse_range_str(name, param)
  457. if p1 == false then
  458. return false, p2
  459. end
  460. if core.delete_area(p1, p2) then
  461. return true, "Successfully cleared area ranging from " ..
  462. core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
  463. else
  464. return false, "Failed to clear one or more blocks in area"
  465. end
  466. end,
  467. })
  468. core.register_chatcommand("fixlight", {
  469. params = "(here [<radius>]) | (<pos1> <pos2>)",
  470. description = "Resets lighting in the area between pos1 and pos2 "
  471. .. "(<pos1> and <pos2> must be in parentheses)",
  472. privs = {server = true},
  473. func = function(name, param)
  474. local p1, p2 = parse_range_str(name, param)
  475. if p1 == false then
  476. return false, p2
  477. end
  478. if core.fix_light(p1, p2) then
  479. return true, "Successfully reset light in the area ranging from " ..
  480. core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
  481. else
  482. return false, "Failed to load one or more blocks in area"
  483. end
  484. end,
  485. })
  486. core.register_chatcommand("mods", {
  487. params = "",
  488. description = "List mods installed on the server",
  489. privs = {},
  490. func = function(name, param)
  491. return true, table.concat(core.get_modnames(), ", ")
  492. end,
  493. })
  494. local function handle_give_command(cmd, giver, receiver, stackstring)
  495. core.log("action", giver .. " invoked " .. cmd
  496. .. ', stackstring="' .. stackstring .. '"')
  497. local itemstack = ItemStack(stackstring)
  498. if itemstack:is_empty() then
  499. return false, "Cannot give an empty item"
  500. elseif not itemstack:is_known() then
  501. return false, "Cannot give an unknown item"
  502. end
  503. local receiverref = core.get_player_by_name(receiver)
  504. if receiverref == nil then
  505. return false, receiver .. " is not a known player"
  506. end
  507. local leftover = receiverref:get_inventory():add_item("main", itemstack)
  508. local partiality
  509. if leftover:is_empty() then
  510. partiality = ""
  511. elseif leftover:get_count() == itemstack:get_count() then
  512. partiality = "could not be "
  513. else
  514. partiality = "partially "
  515. end
  516. -- The actual item stack string may be different from what the "giver"
  517. -- entered (e.g. big numbers are always interpreted as 2^16-1).
  518. stackstring = itemstack:to_string()
  519. if giver == receiver then
  520. return true, ("%q %sadded to inventory.")
  521. :format(stackstring, partiality)
  522. else
  523. core.chat_send_player(receiver, ("%q %sadded to inventory.")
  524. :format(stackstring, partiality))
  525. return true, ("%q %sadded to %s's inventory.")
  526. :format(stackstring, partiality, receiver)
  527. end
  528. end
  529. core.register_chatcommand("give", {
  530. params = "<name> <ItemString>",
  531. description = "Give item to player",
  532. privs = {give=true},
  533. func = function(name, param)
  534. local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
  535. if not toname or not itemstring then
  536. return false, "Name and ItemString required"
  537. end
  538. return handle_give_command("/give", name, toname, itemstring)
  539. end,
  540. })
  541. core.register_chatcommand("giveme", {
  542. params = "<ItemString>",
  543. description = "Give item to yourself",
  544. privs = {give=true},
  545. func = function(name, param)
  546. local itemstring = string.match(param, "(.+)$")
  547. if not itemstring then
  548. return false, "ItemString required"
  549. end
  550. return handle_give_command("/giveme", name, name, itemstring)
  551. end,
  552. })
  553. core.register_chatcommand("spawnentity", {
  554. params = "<EntityName> [<X>,<Y>,<Z>]",
  555. description = "Spawn entity at given (or your) position",
  556. privs = {give=true, interact=true},
  557. func = function(name, param)
  558. local entityname, p = string.match(param, "^([^ ]+) *(.*)$")
  559. if not entityname then
  560. return false, "EntityName required"
  561. end
  562. core.log("action", ("%s invokes /spawnentity, entityname=%q")
  563. :format(name, entityname))
  564. local player = core.get_player_by_name(name)
  565. if player == nil then
  566. core.log("error", "Unable to spawn entity, player is nil")
  567. return false, "Unable to spawn entity, player is nil"
  568. end
  569. if p == "" then
  570. p = player:getpos()
  571. else
  572. p = core.string_to_pos(p)
  573. if p == nil then
  574. return false, "Invalid parameters ('" .. param .. "')"
  575. end
  576. end
  577. p.y = p.y + 1
  578. core.add_entity(p, entityname)
  579. return true, ("%q spawned."):format(entityname)
  580. end,
  581. })
  582. core.register_chatcommand("pulverize", {
  583. params = "",
  584. description = "Destroy item in hand",
  585. func = function(name, param)
  586. local player = core.get_player_by_name(name)
  587. if not player then
  588. core.log("error", "Unable to pulverize, no player.")
  589. return false, "Unable to pulverize, no player."
  590. end
  591. if player:get_wielded_item():is_empty() then
  592. return false, "Unable to pulverize, no item in hand."
  593. end
  594. player:set_wielded_item(nil)
  595. return true, "An item was pulverized."
  596. end,
  597. })
  598. -- Key = player name
  599. core.rollback_punch_callbacks = {}
  600. core.register_on_punchnode(function(pos, node, puncher)
  601. local name = puncher:get_player_name()
  602. if core.rollback_punch_callbacks[name] then
  603. core.rollback_punch_callbacks[name](pos, node, puncher)
  604. core.rollback_punch_callbacks[name] = nil
  605. end
  606. end)
  607. core.register_chatcommand("rollback_check", {
  608. params = "[<range>] [<seconds>] [<limit>]",
  609. description = "Check who last touched a node or a node near it"
  610. .. " within the time specified by <seconds>. Default: range = 0,"
  611. .. " seconds = 86400 = 24h, limit = 5",
  612. privs = {rollback=true},
  613. func = function(name, param)
  614. if not core.settings:get_bool("enable_rollback_recording") then
  615. return false, "Rollback functions are disabled."
  616. end
  617. local range, seconds, limit =
  618. param:match("(%d+) *(%d*) *(%d*)")
  619. range = tonumber(range) or 0
  620. seconds = tonumber(seconds) or 86400
  621. limit = tonumber(limit) or 5
  622. if limit > 100 then
  623. return false, "That limit is too high!"
  624. end
  625. core.rollback_punch_callbacks[name] = function(pos, node, puncher)
  626. local name = puncher:get_player_name()
  627. core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
  628. local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
  629. if not actions then
  630. core.chat_send_player(name, "Rollback functions are disabled")
  631. return
  632. end
  633. local num_actions = #actions
  634. if num_actions == 0 then
  635. core.chat_send_player(name, "Nobody has touched"
  636. .. " the specified location in "
  637. .. seconds .. " seconds")
  638. return
  639. end
  640. local time = os.time()
  641. for i = num_actions, 1, -1 do
  642. local action = actions[i]
  643. core.chat_send_player(name,
  644. ("%s %s %s -> %s %d seconds ago.")
  645. :format(
  646. core.pos_to_string(action.pos),
  647. action.actor,
  648. action.oldnode.name,
  649. action.newnode.name,
  650. time - action.time))
  651. end
  652. end
  653. return true, "Punch a node (range=" .. range .. ", seconds="
  654. .. seconds .. "s, limit=" .. limit .. ")"
  655. end,
  656. })
  657. core.register_chatcommand("rollback", {
  658. params = "(<name> [<seconds>]) | (:<actor> [<seconds>])",
  659. description = "Revert actions of a player. Default for <seconds> is 60",
  660. privs = {rollback=true},
  661. func = function(name, param)
  662. if not core.settings:get_bool("enable_rollback_recording") then
  663. return false, "Rollback functions are disabled."
  664. end
  665. local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
  666. if not target_name then
  667. local player_name = nil
  668. player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
  669. if not player_name then
  670. return false, "Invalid parameters. See /help rollback"
  671. .. " and /help rollback_check."
  672. end
  673. target_name = "player:"..player_name
  674. end
  675. seconds = tonumber(seconds) or 60
  676. core.chat_send_player(name, "Reverting actions of "
  677. .. target_name .. " since "
  678. .. seconds .. " seconds.")
  679. local success, log = core.rollback_revert_actions_by(
  680. target_name, seconds)
  681. local response = ""
  682. if #log > 100 then
  683. response = "(log is too long to show)\n"
  684. else
  685. for _, line in pairs(log) do
  686. response = response .. line .. "\n"
  687. end
  688. end
  689. response = response .. "Reverting actions "
  690. .. (success and "succeeded." or "FAILED.")
  691. return success, response
  692. end,
  693. })
  694. core.register_chatcommand("status", {
  695. description = "Print server status",
  696. func = function(name, param)
  697. return true, core.get_server_status()
  698. end,
  699. })
  700. core.register_chatcommand("time", {
  701. params = "<0..23>:<0..59> | <0..24000>",
  702. description = "Set time of day",
  703. privs = {},
  704. func = function(name, param)
  705. if param == "" then
  706. local current_time = math.floor(core.get_timeofday() * 1440)
  707. local minutes = current_time % 60
  708. local hour = (current_time - minutes) / 60
  709. return true, ("Current time is %d:%02d"):format(hour, minutes)
  710. end
  711. local player_privs = core.get_player_privs(name)
  712. if not player_privs.settime then
  713. return false, "You don't have permission to run this command " ..
  714. "(missing privilege: settime)."
  715. end
  716. local hour, minute = param:match("^(%d+):(%d+)$")
  717. if not hour then
  718. local new_time = tonumber(param)
  719. if not new_time then
  720. return false, "Invalid time."
  721. end
  722. -- Backward compatibility.
  723. core.set_timeofday((new_time % 24000) / 24000)
  724. core.log("action", name .. " sets time to " .. new_time)
  725. return true, "Time of day changed."
  726. end
  727. hour = tonumber(hour)
  728. minute = tonumber(minute)
  729. if hour < 0 or hour > 23 then
  730. return false, "Invalid hour (must be between 0 and 23 inclusive)."
  731. elseif minute < 0 or minute > 59 then
  732. return false, "Invalid minute (must be between 0 and 59 inclusive)."
  733. end
  734. core.set_timeofday((hour * 60 + minute) / 1440)
  735. core.log("action", ("%s sets time to %d:%02d"):format(name, hour, minute))
  736. return true, "Time of day changed."
  737. end,
  738. })
  739. core.register_chatcommand("days", {
  740. description = "Display day count",
  741. func = function(name, param)
  742. return true, "Current day is " .. core.get_day_count()
  743. end
  744. })
  745. core.register_chatcommand("shutdown", {
  746. params = "[<delay_in_seconds> | -1] [reconnect] [<message>]",
  747. description = "Shutdown server (-1 cancels a delayed shutdown)",
  748. privs = {server=true},
  749. func = function(name, param)
  750. local delay, reconnect, message = param:match("([^ ][-]?[0-9]+)([^ ]+)(.*)")
  751. message = message or ""
  752. if delay ~= "" then
  753. delay = tonumber(param) or 0
  754. else
  755. delay = 0
  756. core.log("action", name .. " shuts down server")
  757. core.chat_send_all("*** Server shutting down (operator request).")
  758. end
  759. core.request_shutdown(message:trim(), core.is_yes(reconnect), delay)
  760. end,
  761. })
  762. core.register_chatcommand("ban", {
  763. params = "<name>",
  764. description = "Ban IP of player",
  765. privs = {ban=true},
  766. func = function(name, param)
  767. if param == "" then
  768. return true, "Ban list: " .. core.get_ban_list()
  769. end
  770. if not core.get_player_by_name(param) then
  771. return false, "No such player."
  772. end
  773. if not core.ban_player(param) then
  774. return false, "Failed to ban player."
  775. end
  776. local desc = core.get_ban_description(param)
  777. core.log("action", name .. " bans " .. desc .. ".")
  778. return true, "Banned " .. desc .. "."
  779. end,
  780. })
  781. core.register_chatcommand("unban", {
  782. params = "<name> | <IP_address>",
  783. description = "Remove IP ban",
  784. privs = {ban=true},
  785. func = function(name, param)
  786. if not core.unban_player_or_ip(param) then
  787. return false, "Failed to unban player/IP."
  788. end
  789. core.log("action", name .. " unbans " .. param)
  790. return true, "Unbanned " .. param
  791. end,
  792. })
  793. core.register_chatcommand("kick", {
  794. params = "<name> [<reason>]",
  795. description = "Kick a player",
  796. privs = {kick=true},
  797. func = function(name, param)
  798. local tokick, reason = param:match("([^ ]+) (.+)")
  799. tokick = tokick or param
  800. if not core.kick_player(tokick, reason) then
  801. return false, "Failed to kick player " .. tokick
  802. end
  803. local log_reason = ""
  804. if reason then
  805. log_reason = " with reason \"" .. reason .. "\""
  806. end
  807. core.log("action", name .. " kicks " .. tokick .. log_reason)
  808. return true, "Kicked " .. tokick
  809. end,
  810. })
  811. core.register_chatcommand("clearobjects", {
  812. params = "[full | quick]",
  813. description = "Clear all objects in world",
  814. privs = {server=true},
  815. func = function(name, param)
  816. local options = {}
  817. if param == "" or param == "full" then
  818. options.mode = "full"
  819. elseif param == "quick" then
  820. options.mode = "quick"
  821. else
  822. return false, "Invalid usage, see /help clearobjects."
  823. end
  824. core.log("action", name .. " clears all objects ("
  825. .. options.mode .. " mode).")
  826. core.chat_send_all("Clearing all objects. This may take long."
  827. .. " You may experience a timeout. (by "
  828. .. name .. ")")
  829. core.clear_objects(options)
  830. core.log("action", "Object clearing done.")
  831. core.chat_send_all("*** Cleared all objects.")
  832. end,
  833. })
  834. core.register_chatcommand("msg", {
  835. params = "<name> <message>",
  836. description = "Send a private message",
  837. privs = {shout=true},
  838. func = function(name, param)
  839. local sendto, message = param:match("^(%S+)%s(.+)$")
  840. if not sendto then
  841. return false, "Invalid usage, see /help msg."
  842. end
  843. if not core.get_player_by_name(sendto) then
  844. return false, "The player " .. sendto
  845. .. " is not online."
  846. end
  847. core.log("action", "PM from " .. name .. " to " .. sendto
  848. .. ": " .. message)
  849. core.chat_send_player(sendto, "PM from " .. name .. ": "
  850. .. message)
  851. return true, "Message sent."
  852. end,
  853. })
  854. core.register_chatcommand("last-login", {
  855. params = "[<name>]",
  856. description = "Get the last login time of a player",
  857. func = function(name, param)
  858. if param == "" then
  859. param = name
  860. end
  861. local pauth = core.get_auth_handler().get_auth(param)
  862. if pauth and pauth.last_login then
  863. -- Time in UTC, ISO 8601 format
  864. return true, "Last login time was " ..
  865. os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
  866. end
  867. return false, "Last login time is unknown"
  868. end,
  869. })
  870. core.register_chatcommand("clearinv", {
  871. params = "[<name>]",
  872. description = "Clear the inventory of yourself or another player",
  873. func = function(name, param)
  874. local player
  875. if param and param ~= "" and param ~= name then
  876. if not core.check_player_privs(name, {server=true}) then
  877. return false, "You don't have permission"
  878. .. " to run this command (missing privilege: server)"
  879. end
  880. player = core.get_player_by_name(param)
  881. core.chat_send_player(param, name.." cleared your inventory.")
  882. else
  883. player = core.get_player_by_name(name)
  884. end
  885. if player then
  886. player:get_inventory():set_list("main", {})
  887. player:get_inventory():set_list("craft", {})
  888. player:get_inventory():set_list("craftpreview", {})
  889. core.log("action", name.." clears "..player:get_player_name().."'s inventory")
  890. return true, "Cleared "..player:get_player_name().."'s inventory."
  891. else
  892. return false, "Player must be online to clear inventory!"
  893. end
  894. end,
  895. })