chatcommands.lua 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. minetest.log("action", '/spawnentity invoked, entityname="'..entityname..'"')
  459. local player = minetest.get_player_by_name(name)
  460. if player == nil then
  461. minetest.log("error", "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. minetest.log("error", "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>] [limit]",
  500. description = "check who has last touched a node or near it, "..
  501. "max. <seconds> ago (default range=0, seconds=86400=24h, limit=5)",
  502. privs = {rollback=true},
  503. func = function(name, param)
  504. local range, seconds, limit =
  505. param:match("(%d+) *(%d*) *(%d*)")
  506. range = tonumber(range) or 0
  507. seconds = tonumber(seconds) or 86400
  508. limit = tonumber(limit) or 5
  509. if limit > 100 then
  510. minetest.chat_send_player(name, "That limit is too high!")
  511. return
  512. end
  513. minetest.chat_send_player(name, "Punch a node (range="..
  514. range..", seconds="..seconds.."s, limit="..limit..")")
  515. minetest.rollback_punch_callbacks[name] = function(pos, node, puncher)
  516. local name = puncher:get_player_name()
  517. minetest.chat_send_player(name, "Checking "..minetest.pos_to_string(pos).."...")
  518. local actions = minetest.rollback_get_node_actions(pos, range, seconds, limit)
  519. local num_actions = #actions
  520. if num_actions == 0 then
  521. minetest.chat_send_player(name, "Nobody has touched the "..
  522. "specified location in "..seconds.." seconds")
  523. return
  524. end
  525. local time = os.time()
  526. for i = num_actions, 1, -1 do
  527. local action = actions[i]
  528. minetest.chat_send_player(name,
  529. ("%s %s %s -> %s %d seconds ago.")
  530. :format(
  531. minetest.pos_to_string(action.pos),
  532. action.actor,
  533. action.oldnode.name,
  534. action.newnode.name,
  535. time - action.time))
  536. end
  537. end
  538. end,
  539. })
  540. minetest.register_chatcommand("rollback", {
  541. params = "<player name> [<seconds>] | :<actor> [<seconds>]",
  542. description = "revert actions of a player; default for <seconds> is 60",
  543. privs = {rollback=true},
  544. func = function(name, param)
  545. local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
  546. if not target_name then
  547. local player_name = nil
  548. player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
  549. if not player_name then
  550. minetest.chat_send_player(name, "Invalid parameters. See /help rollback and /help rollback_check")
  551. return
  552. end
  553. target_name = "player:"..player_name
  554. end
  555. seconds = tonumber(seconds) or 60
  556. minetest.chat_send_player(name, "Reverting actions of "..
  557. target_name.." since "..seconds.." seconds.")
  558. local success, log = minetest.rollback_revert_actions_by(
  559. target_name, seconds)
  560. if #log > 100 then
  561. minetest.chat_send_player(name, "(log is too long to show)")
  562. else
  563. for _, line in pairs(log) do
  564. minetest.chat_send_player(name, line)
  565. end
  566. end
  567. if success then
  568. minetest.chat_send_player(name, "Reverting actions succeeded.")
  569. else
  570. minetest.chat_send_player(name, "Reverting actions FAILED.")
  571. end
  572. end,
  573. })
  574. minetest.register_chatcommand("status", {
  575. params = "",
  576. description = "print server status line",
  577. privs = {},
  578. func = function(name, param)
  579. minetest.chat_send_player(name, minetest.get_server_status())
  580. end,
  581. })
  582. minetest.register_chatcommand("time", {
  583. params = "<0...24000>",
  584. description = "set time of day",
  585. privs = {settime=true},
  586. func = function(name, param)
  587. if param == "" then
  588. minetest.chat_send_player(name, "Missing parameter")
  589. return
  590. end
  591. local newtime = tonumber(param)
  592. if newtime == nil then
  593. minetest.chat_send_player(name, "Invalid time")
  594. else
  595. minetest.set_timeofday((newtime % 24000) / 24000)
  596. minetest.chat_send_player(name, "Time of day changed.")
  597. minetest.log("action", name .. " sets time " .. newtime)
  598. end
  599. end,
  600. })
  601. minetest.register_chatcommand("shutdown", {
  602. params = "",
  603. description = "shutdown server",
  604. privs = {server=true},
  605. func = function(name, param)
  606. minetest.log("action", name .. " shuts down server")
  607. minetest.request_shutdown()
  608. minetest.chat_send_all("*** Server shutting down (operator request).")
  609. end,
  610. })
  611. minetest.register_chatcommand("ban", {
  612. params = "<name>",
  613. description = "ban IP of player",
  614. privs = {ban=true},
  615. func = function(name, param)
  616. if param == "" then
  617. minetest.chat_send_player(name, "Ban list: " .. minetest.get_ban_list())
  618. return
  619. end
  620. if not minetest.get_player_by_name(param) then
  621. minetest.chat_send_player(name, "No such player")
  622. return
  623. end
  624. if not minetest.ban_player(param) then
  625. minetest.chat_send_player(name, "Failed to ban player")
  626. else
  627. local desc = minetest.get_ban_description(param)
  628. minetest.chat_send_player(name, "Banned " .. desc .. ".")
  629. minetest.log("action", name .. " bans " .. desc .. ".")
  630. end
  631. end,
  632. })
  633. minetest.register_chatcommand("unban", {
  634. params = "<name/ip>",
  635. description = "remove IP ban",
  636. privs = {ban=true},
  637. func = function(name, param)
  638. if not minetest.unban_player_or_ip(param) then
  639. minetest.chat_send_player(name, "Failed to unban player/IP")
  640. else
  641. minetest.chat_send_player(name, "Unbanned " .. param)
  642. minetest.log("action", name .. " unbans " .. param)
  643. end
  644. end,
  645. })
  646. minetest.register_chatcommand("kick", {
  647. params = "<name> [reason]",
  648. description = "kick a player",
  649. privs = {kick=true},
  650. func = function(name, param)
  651. local tokick, reason = string.match(param, "([^ ]+) (.+)")
  652. if not tokick then
  653. tokick = param
  654. end
  655. if not minetest.kick_player(tokick, reason) then
  656. minetest.chat_send_player(name, "Failed to kick player " .. tokick)
  657. else
  658. minetest.chat_send_player(name, "kicked " .. tokick)
  659. minetest.log("action", name .. " kicked " .. tokick)
  660. end
  661. end,
  662. })
  663. minetest.register_chatcommand("clearobjects", {
  664. params = "",
  665. description = "clear all objects in world",
  666. privs = {server=true},
  667. func = function(name, param)
  668. minetest.log("action", name .. " clears all objects")
  669. minetest.chat_send_all("Clearing all objects. This may take long. You may experience a timeout. (by " .. name .. ")")
  670. minetest.clear_objects()
  671. minetest.log("action", "object clearing done")
  672. minetest.chat_send_all("*** Cleared all objects.")
  673. end,
  674. })
  675. minetest.register_chatcommand("msg", {
  676. params = "<name> <message>",
  677. description = "Send a private message",
  678. privs = {shout=true},
  679. func = function(name, param)
  680. local found, _, sendto, message = param:find("^([^%s]+)%s(.+)$")
  681. if found then
  682. if minetest.get_player_by_name(sendto) then
  683. minetest.log("action", "PM from "..name.." to "..sendto..": "..message)
  684. minetest.chat_send_player(sendto, "PM from "..name..": "..message)
  685. minetest.chat_send_player(name, "Message sent")
  686. else
  687. minetest.chat_send_player(name, "The player "..sendto.." is not online")
  688. end
  689. else
  690. minetest.chat_send_player(name, "Invalid usage, see /help msg")
  691. end
  692. end,
  693. })