chatcommands.lua 22 KB

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