chatcommands.lua 24 KB

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