chatcommands.lua 29 KB

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