Change command prefix to "." and add "help" command.
[oweals/minetest.git] / builtin / common / chatcommands.lua
1 -- Minetest: builtin/common/chatcommands.lua
2
3 core.registered_chatcommands = {}
4
5 function core.register_chatcommand(cmd, def)
6         def = def or {}
7         def.params = def.params or ""
8         def.description = def.description or ""
9         def.privs = def.privs or {}
10         def.mod_origin = core.get_current_modname() or "??"
11         core.registered_chatcommands[cmd] = def
12 end
13
14 function core.unregister_chatcommand(name)
15         if core.registered_chatcommands[name] then
16                 core.registered_chatcommands[name] = nil
17         else
18                 core.log("warning", "Not unregistering chatcommand " ..name..
19                         " because it doesn't exist.")
20         end
21 end
22
23 function core.override_chatcommand(name, redefinition)
24         local chatcommand = core.registered_chatcommands[name]
25         assert(chatcommand, "Attempt to override non-existent chatcommand "..name)
26         for k, v in pairs(redefinition) do
27                 rawset(chatcommand, k, v)
28         end
29         core.registered_chatcommands[name] = chatcommand
30 end
31
32 local cmd_marker = "/"
33
34 if INIT == "client" then
35         cmd_marker = "."
36 end
37
38 local function do_help_cmd(name, param)
39         local function format_help_line(cmd, def)
40                 local msg = core.colorize("#00ffff", cmd_marker .. cmd)
41                 if def.params and def.params ~= "" then
42                         msg = msg .. " " .. def.params
43                 end
44                 if def.description and def.description ~= "" then
45                         msg = msg .. ": " .. def.description
46                 end
47                 return msg
48         end
49         if param == "" then
50                 local cmds = {}
51                 for cmd, def in pairs(core.registered_chatcommands) do
52                         if INIT == "client" or core.check_player_privs(name, def.privs) then
53                                 cmds[#cmds + 1] = cmd
54                         end
55                 end
56                 table.sort(cmds)
57                 return true, "Available commands: " .. table.concat(cmds, " ") .. "\n"
58                                 .. "Use '"..cmd_marker.."help <cmd>' to get more information,"
59                                 .. " or '"..cmd_marker.."help all' to list everything."
60         elseif param == "all" then
61                 local cmds = {}
62                 for cmd, def in pairs(core.registered_chatcommands) do
63                         if INIT == "client" or core.check_player_privs(name, def.privs) then
64                                 cmds[#cmds + 1] = format_help_line(cmd, def)
65                         end
66                 end
67                 table.sort(cmds)
68                 return true, "Available commands:\n"..table.concat(cmds, "\n")
69         elseif INIT == "game" and param == "privs" then
70                 local privs = {}
71                 for priv, def in pairs(core.registered_privileges) do
72                         privs[#privs + 1] = priv .. ": " .. def.description
73                 end
74                 table.sort(privs)
75                 return true, "Available privileges:\n"..table.concat(privs, "\n")
76         else
77                 local cmd = param
78                 local def = core.registered_chatcommands[cmd]
79                 if not def then
80                         return false, "Command not available: "..cmd
81                 else
82                         return true, format_help_line(cmd, def)
83                 end
84         end
85 end
86
87 if INIT == "client" then
88         core.register_chatcommand("help", {
89                 params = "[all/<cmd>]",
90                 description = "Get help for commands",
91                 func = function(param)
92                         return do_help_cmd(nil, param)
93                 end,
94         })
95 else
96         core.register_chatcommand("help", {
97                 params = "[all/privs/<cmd>]",
98                 description = "Get help for commands or list privileges",
99                 func = do_help_cmd,
100         })
101 end