91b685fdf85a4e34ca7af595b14428e04b70c820
[oweals/minetest.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 priv ~= "interact_extra" 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 actstr = "?"
233                 if not raw_password then
234                         core.set_player_password(toname, "")
235                         actstr = "cleared"
236                 else
237                         core.set_player_password(toname,
238                                         core.get_password_hash(toname,
239                                                         raw_password))
240                         actstr = "set"
241                 end
242                 if toname ~= name then
243                         core.chat_send_player(toname, "Your password was "
244                                         .. actstr .. " by " .. name)
245                 end
246                 return true, "Password of player \"" .. toname .. "\" " .. actstr
247         end,
248 })
249
250 core.register_chatcommand("clearpassword", {
251         params = "<name>",
252         description = "set empty password",
253         privs = {password=true},
254         func = function(name, param)
255                 local toname = param
256                 if toname == "" then
257                         return false, "Name field required"
258                 end
259                 core.set_player_password(toname, '')
260                 return true, "Password of player \"" .. toname .. "\" cleared"
261         end,
262 })
263
264 core.register_chatcommand("auth_reload", {
265         params = "",
266         description = "reload authentication data",
267         privs = {server=true},
268         func = function(name, param)
269                 local done = core.auth_reload()
270                 return done, (done and "Done." or "Failed.")
271         end,
272 })
273
274 core.register_chatcommand("teleport", {
275         params = "<X>,<Y>,<Z> | <to_name> | <name> <X>,<Y>,<Z> | <name> <to_name>",
276         description = "teleport to given position",
277         privs = {teleport=true},
278         func = function(name, param)
279                 -- Returns (pos, true) if found, otherwise (pos, false)
280                 local function find_free_position_near(pos)
281                         local tries = {
282                                 {x=1,y=0,z=0},
283                                 {x=-1,y=0,z=0},
284                                 {x=0,y=0,z=1},
285                                 {x=0,y=0,z=-1},
286                         }
287                         for _, d in ipairs(tries) do
288                                 local p = {x = pos.x+d.x, y = pos.y+d.y, z = pos.z+d.z}
289                                 local n = core.get_node_or_nil(p)
290                                 if n and n.name then
291                                         local def = core.registered_nodes[n.name]
292                                         if def and not def.walkable then
293                                                 return p, true
294                                         end
295                                 end
296                         end
297                         return pos, false
298                 end
299
300                 local teleportee = nil
301                 local p = {}
302                 p.x, p.y, p.z = string.match(param, "^([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
303                 p.x = tonumber(p.x)
304                 p.y = tonumber(p.y)
305                 p.z = tonumber(p.z)
306                 teleportee = core.get_player_by_name(name)
307                 if teleportee and p.x and p.y and p.z then
308                         teleportee:setpos(p)
309                         return true, "Teleporting to "..core.pos_to_string(p)
310                 end
311                 
312                 local teleportee = nil
313                 local p = nil
314                 local target_name = nil
315                 target_name = param:match("^([^ ]+)$")
316                 teleportee = core.get_player_by_name(name)
317                 if target_name then
318                         local target = core.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                         teleportee:setpos(p)
326                         return true, "Teleporting to " .. target_name
327                                         .. " at "..core.pos_to_string(p)
328                 end
329
330                 if not core.check_player_privs(name, {bring=true}) then
331                         return false, "You don't have permission to teleport other players (missing bring privilege)"
332                 end
333
334                 local teleportee = nil
335                 local p = {}
336                 local teleportee_name = nil
337                 teleportee_name, p.x, p.y, p.z = param:match(
338                                 "^([^ ]+) +([%d.-]+)[, ] *([%d.-]+)[, ] *([%d.-]+)$")
339                 p.x, p.y, p.z = tonumber(p.x), tonumber(p.y), tonumber(p.z)
340                 if teleportee_name then
341                         teleportee = core.get_player_by_name(teleportee_name)
342                 end
343                 if teleportee and p.x and p.y and p.z then
344                         teleportee:setpos(p)
345                         return true, "Teleporting " .. teleportee_name
346                                         .. " to " .. core.pos_to_string(p)
347                 end
348                 
349                 local teleportee = nil
350                 local p = nil
351                 local teleportee_name = nil
352                 local target_name = nil
353                 teleportee_name, target_name = string.match(param, "^([^ ]+) +([^ ]+)$")
354                 if teleportee_name then
355                         teleportee = core.get_player_by_name(teleportee_name)
356                 end
357                 if target_name then
358                         local target = core.get_player_by_name(target_name)
359                         if target then
360                                 p = target:getpos()
361                         end
362                 end
363                 if teleportee and p then
364                         p = find_free_position_near(p)
365                         teleportee:setpos(p)
366                         return true, "Teleporting " .. teleportee_name
367                                         .. " to " .. target_name
368                                         .. " at " .. core.pos_to_string(p)
369                 end
370                 
371                 return false, 'Invalid parameters ("' .. param
372                                 .. '") or player not found (see /help teleport)'
373         end,
374 })
375
376 core.register_chatcommand("set", {
377         params = "[-n] <name> <value> | <name>",
378         description = "set or read server configuration setting",
379         privs = {server=true},
380         func = function(name, param)
381                 local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
382                 if arg and arg == "-n" and setname and setvalue then
383                         core.setting_set(setname, setvalue)
384                         return true, setname .. " = " .. setvalue
385                 end
386                 local setname, setvalue = string.match(param, "([^ ]+) (.+)")
387                 if setname and setvalue then
388                         if not core.setting_get(setname) then
389                                 return false, "Failed. Use '/set -n <name> <value>' to create a new setting."
390                         end
391                         core.setting_set(setname, setvalue)
392                         return true, setname .. " = " .. setvalue
393                 end
394                 local setname = string.match(param, "([^ ]+)")
395                 if setname then
396                         local setvalue = core.setting_get(setname)
397                         if not setvalue then
398                                 setvalue = "<not set>"
399                         end
400                         return true, setname .. " = " .. setvalue
401                 end
402                 return false, "Invalid parameters (see /help set)."
403         end,
404 })
405
406 core.register_chatcommand("deleteblocks", {
407         params = "[here] [<pos1> <pos2>]",
408         description = "delete map blocks contained in area pos1 to pos2",
409         privs = {server=true},
410         func = function(name, param)
411                 local p1 = {}
412                 local p2 = {}
413                 if param == "here" then
414                         local player = core.get_player_by_name(name)
415                         if player == nil then
416                                 core.log("error", "player is nil")
417                                 return false, "Unable to get current position; player is nil"
418                         end
419                         p1 = player:getpos()
420                         p2 = p1
421                 else
422                         local pos1, pos2 = unpack(param:split(") ("))
423                         if pos1 == nil or pos2 == nil then
424                                 return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
425                         end
426
427                         p1 = core.string_to_pos(pos1 .. ")")
428                         p2 = core.string_to_pos("(" .. pos2)
429
430                         if p1 == nil or p2 == nil then
431                                 return false, "Incorrect area format. Expected: (x1,y1,z1) (x2,y2,z2)"
432                         end
433                 end
434
435                 if core.delete_area(p1, p2) then
436                         return true, "Successfully cleared area ranging from " ..
437                                 core.pos_to_string(p1, 1) .. " to " .. core.pos_to_string(p2, 1)
438                 else
439                         return false, "Failed to clear one or more blocks in area"
440                 end
441         end,
442 })
443
444 core.register_chatcommand("mods", {
445         params = "",
446         description = "List mods installed on the server",
447         privs = {},
448         func = function(name, param)
449                 return true, table.concat(core.get_modnames(), ", ")
450         end,
451 })
452
453 local function handle_give_command(cmd, giver, receiver, stackstring)
454         core.log("action", giver .. " invoked " .. cmd
455                         .. ', stackstring="' .. stackstring .. '"')
456         local itemstack = ItemStack(stackstring)
457         if itemstack:is_empty() then
458                 return false, "Cannot give an empty item"
459         elseif not itemstack:is_known() then
460                 return false, "Cannot give an unknown item"
461         end
462         local receiverref = core.get_player_by_name(receiver)
463         if receiverref == nil then
464                 return false, receiver .. " is not a known player"
465         end
466         local leftover = receiverref:get_inventory():add_item("main", itemstack)
467         local partiality
468         if leftover:is_empty() then
469                 partiality = ""
470         elseif leftover:get_count() == itemstack:get_count() then
471                 partiality = "could not be "
472         else
473                 partiality = "partially "
474         end
475         -- The actual item stack string may be different from what the "giver"
476         -- entered (e.g. big numbers are always interpreted as 2^16-1).
477         stackstring = itemstack:to_string()
478         if giver == receiver then
479                 return true, ("%q %sadded to inventory.")
480                                 :format(stackstring, partiality)
481         else
482                 core.chat_send_player(receiver, ("%q %sadded to inventory.")
483                                 :format(stackstring, partiality))
484                 return true, ("%q %sadded to %s's inventory.")
485                                 :format(stackstring, partiality, receiver)
486         end
487 end
488
489 core.register_chatcommand("give", {
490         params = "<name> <ItemString>",
491         description = "give item to player",
492         privs = {give=true},
493         func = function(name, param)
494                 local toname, itemstring = string.match(param, "^([^ ]+) +(.+)$")
495                 if not toname or not itemstring then
496                         return false, "Name and ItemString required"
497                 end
498                 return handle_give_command("/give", name, toname, itemstring)
499         end,
500 })
501
502 core.register_chatcommand("giveme", {
503         params = "<ItemString>",
504         description = "give item to yourself",
505         privs = {give=true},
506         func = function(name, param)
507                 local itemstring = string.match(param, "(.+)$")
508                 if not itemstring then
509                         return false, "ItemString required"
510                 end
511                 return handle_give_command("/giveme", name, name, itemstring)
512         end,
513 })
514
515 core.register_chatcommand("spawnentity", {
516         params = "<EntityName>",
517         description = "Spawn entity at your position",
518         privs = {give=true, interact=true},
519         func = function(name, param)
520                 local entityname = string.match(param, "(.+)$")
521                 if not entityname then
522                         return false, "EntityName required"
523                 end
524                 core.log("action", ("/spawnentity invoked, entityname=%q")
525                                 :format(entityname))
526                 local player = core.get_player_by_name(name)
527                 if player == nil then
528                         core.log("error", "Unable to spawn entity, player is nil")
529                         return false, "Unable to spawn entity, player is nil"
530                 end
531                 local p = player:getpos()
532                 p.y = p.y + 1
533                 core.add_entity(p, entityname)
534                 return true, ("%q spawned."):format(entityname)
535         end,
536 })
537
538 core.register_chatcommand("pulverize", {
539         params = "",
540         description = "Destroy item in hand",
541         func = function(name, param)
542                 local player = core.get_player_by_name(name)
543                 if not player then
544                         core.log("error", "Unable to pulverize, no player.")
545                         return false, "Unable to pulverize, no player."
546                 end
547                 if player:get_wielded_item():is_empty() then
548                         return false, "Unable to pulverize, no item in hand."
549                 end
550                 player:set_wielded_item(nil)
551                 return true, "An item was pulverized."
552         end,
553 })
554
555 -- Key = player name
556 core.rollback_punch_callbacks = {}
557
558 core.register_on_punchnode(function(pos, node, puncher)
559         local name = puncher:get_player_name()
560         if core.rollback_punch_callbacks[name] then
561                 core.rollback_punch_callbacks[name](pos, node, puncher)
562                 core.rollback_punch_callbacks[name] = nil
563         end
564 end)
565
566 core.register_chatcommand("rollback_check", {
567         params = "[<range>] [<seconds>] [limit]",
568         description = "Check who has last touched a node or near it,"
569                         .. " max. <seconds> ago (default range=0,"
570                         .. " seconds=86400=24h, limit=5)",
571         privs = {rollback=true},
572         func = function(name, param)
573                 if not core.setting_getbool("enable_rollback_recording") then
574                         return false, "Rollback functions are disabled."
575                 end
576                 local range, seconds, limit =
577                         param:match("(%d+) *(%d*) *(%d*)")
578                 range = tonumber(range) or 0
579                 seconds = tonumber(seconds) or 86400
580                 limit = tonumber(limit) or 5
581                 if limit > 100 then
582                         return false, "That limit is too high!"
583                 end
584
585                 core.rollback_punch_callbacks[name] = function(pos, node, puncher)
586                         local name = puncher:get_player_name()
587                         core.chat_send_player(name, "Checking " .. core.pos_to_string(pos) .. "...")
588                         local actions = core.rollback_get_node_actions(pos, range, seconds, limit)
589                         if not actions then
590                                 core.chat_send_player(name, "Rollback functions are disabled")
591                                 return
592                         end
593                         local num_actions = #actions
594                         if num_actions == 0 then
595                                 core.chat_send_player(name, "Nobody has touched"
596                                                 .. " the specified location in "
597                                                 .. seconds .. " seconds")
598                                 return
599                         end
600                         local time = os.time()
601                         for i = num_actions, 1, -1 do
602                                 local action = actions[i]
603                                 core.chat_send_player(name,
604                                         ("%s %s %s -> %s %d seconds ago.")
605                                                 :format(
606                                                         core.pos_to_string(action.pos),
607                                                         action.actor,
608                                                         action.oldnode.name,
609                                                         action.newnode.name,
610                                                         time - action.time))
611                         end
612                 end
613
614                 return true, "Punch a node (range=" .. range .. ", seconds="
615                                 .. seconds .. "s, limit=" .. limit .. ")"
616         end,
617 })
618
619 core.register_chatcommand("rollback", {
620         params = "<player name> [<seconds>] | :<actor> [<seconds>]",
621         description = "revert actions of a player; default for <seconds> is 60",
622         privs = {rollback=true},
623         func = function(name, param)
624                 if not core.setting_getbool("enable_rollback_recording") then
625                         return false, "Rollback functions are disabled."
626                 end
627                 local target_name, seconds = string.match(param, ":([^ ]+) *(%d*)")
628                 if not target_name then
629                         local player_name = nil
630                         player_name, seconds = string.match(param, "([^ ]+) *(%d*)")
631                         if not player_name then
632                                 return false, "Invalid parameters. See /help rollback"
633                                                 .. " and /help rollback_check."
634                         end
635                         target_name = "player:"..player_name
636                 end
637                 seconds = tonumber(seconds) or 60
638                 core.chat_send_player(name, "Reverting actions of "
639                                 .. target_name .. " since "
640                                 .. seconds .. " seconds.")
641                 local success, log = core.rollback_revert_actions_by(
642                                 target_name, seconds)
643                 local response = ""
644                 if #log > 100 then
645                         response = "(log is too long to show)\n"
646                 else
647                         for _, line in pairs(log) do
648                                 response = response .. line .. "\n"
649                         end
650                 end
651                 response = response .. "Reverting actions "
652                                 .. (success and "succeeded." or "FAILED.")
653                 return success, response
654         end,
655 })
656
657 core.register_chatcommand("status", {
658         description = "Print server status",
659         func = function(name, param)
660                 return true, core.get_server_status()
661         end,
662 })
663
664 core.register_chatcommand("time", {
665         params = "<0...24000>",
666         description = "set time of day",
667         privs = {settime=true},
668         func = function(name, param)
669                 if param == "" then
670                         return false, "Missing time."
671                 end
672                 local newtime = tonumber(param)
673                 if newtime == nil then
674                         return false, "Invalid time."
675                 end
676                 core.set_timeofday((newtime % 24000) / 24000)
677                 core.log("action", name .. " sets time " .. newtime)
678                 return true, "Time of day changed."
679         end,
680 })
681
682 core.register_chatcommand("shutdown", {
683         description = "shutdown server",
684         privs = {server=true},
685         func = function(name, param)
686                 core.log("action", name .. " shuts down server")
687                 core.request_shutdown()
688                 core.chat_send_all("*** Server shutting down (operator request).")
689         end,
690 })
691
692 core.register_chatcommand("ban", {
693         params = "<name>",
694         description = "Ban IP of player",
695         privs = {ban=true},
696         func = function(name, param)
697                 if param == "" then
698                         return true, "Ban list: " .. core.get_ban_list()
699                 end
700                 if not core.get_player_by_name(param) then
701                         return false, "No such player."
702                 end
703                 if not core.ban_player(param) then
704                         return false, "Failed to ban player."
705                 end
706                 local desc = core.get_ban_description(param)
707                 core.log("action", name .. " bans " .. desc .. ".")
708                 return true, "Banned " .. desc .. "."
709         end,
710 })
711
712 core.register_chatcommand("unban", {
713         params = "<name/ip>",
714         description = "remove IP ban",
715         privs = {ban=true},
716         func = function(name, param)
717                 if not core.unban_player_or_ip(param) then
718                         return false, "Failed to unban player/IP."
719                 end
720                 core.log("action", name .. " unbans " .. param)
721                 return true, "Unbanned " .. param
722         end,
723 })
724
725 core.register_chatcommand("kick", {
726         params = "<name> [reason]",
727         description = "kick a player",
728         privs = {kick=true},
729         func = function(name, param)
730                 local tokick, reason = param:match("([^ ]+) (.+)")
731                 tokick = tokick or param
732                 if not core.kick_player(tokick, reason) then
733                         return false, "Failed to kick player " .. tokick
734                 end
735                 core.log("action", name .. " kicked " .. tokick)
736                 return true, "Kicked " .. tokick
737         end,
738 })
739
740 core.register_chatcommand("clearobjects", {
741         description = "clear all objects in world",
742         privs = {server=true},
743         func = function(name, param)
744                 core.log("action", name .. " clears all objects.")
745                 core.chat_send_all("Clearing all objects.  This may take long."
746                                 .. "  You may experience a timeout.  (by "
747                                 .. name .. ")")
748                 core.clear_objects()
749                 core.log("action", "Object clearing done.")
750                 core.chat_send_all("*** Cleared all objects.")
751         end,
752 })
753
754 core.register_chatcommand("msg", {
755         params = "<name> <message>",
756         description = "Send a private message",
757         privs = {shout=true},
758         func = function(name, param)
759                 local sendto, message = param:match("^(%S+)%s(.+)$")
760                 if not sendto then
761                         return false, "Invalid usage, see /help msg."
762                 end
763                 if not core.get_player_by_name(sendto) then
764                         return false, "The player " .. sendto
765                                         .. " is not online."
766                 end
767                 core.log("action", "PM from " .. name .. " to " .. sendto
768                                 .. ": " .. message)
769                 core.chat_send_player(sendto, "PM from " .. name .. ": "
770                                 .. message)
771                 return true, "Message sent."
772         end,
773 })
774
775 core.register_chatcommand("last-login", {
776         params = "[name]",
777         description = "Get the last login time of a player",
778         func = function(name, param)
779                 if param == "" then
780                         param = name
781                 end
782                 local pauth = core.get_auth_handler().get_auth(param)
783                 if pauth and pauth.last_login then
784                         -- Time in UTC, ISO 8601 format
785                         return true, "Last login time was " ..
786                                 os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
787                 end
788                 return false, "Last login time is unknown"
789         end,
790 })
791