chatcommands.lua 27 KB

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