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