chatcommands.lua 22 KB

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