commands = {} function CommandHandler (line, client) -- warning - this will disregard lines which do not start with alpha stuff local s, e, command = string.find (line, "^[ ]*(%a+)") -- blank line will result in nothing found if not s then return end -- no command local args = string.sub (line, e + 1) f = commands [command] -- find command handler if f then f.handler (client, trim (args)) -- send command and args to handler else Send (client, config.unknown_command) end -- command not known end -- CommandHandler function addcommand (name, handler, help) assert (type (name) == "string", "command name must be string") assert (type (handler) == "function", "command handler must be function") commands [name] = {} commands [name].handler = handler commands [name].help = help end -- addcommand function DoQuit (client, args) RemoveClient (client.socket) end -- DoQuit function DoSay (client, args) if args == "" then Send (client, "Say what?") return end -- if nothing to say Send (client, "You say, '" .. args .. "'") end -- DoSay function DoHelp (client, args) local maxlen = 15 for k, v in pairs (commands) do local len = math.min (string.len (k), maxlen) -- how many dots Send (client, k .. string.rep (".", maxlen - len) .. " " .. v.help) end -- for end -- DoHelp addcommand ("quit", DoQuit, "Logs off from the MUD") addcommand ("say", DoSay, "Say something to people in the same room as you") addcommand ("help", DoHelp, "Get help on commands")