diff --git a/mods/terminal/init.lua b/mods/terminal/init.lua index 305cb07..c5553c3 100644 --- a/mods/terminal/init.lua +++ b/mods/terminal/init.lua @@ -41,22 +41,22 @@ local commands = { end, append = function(output, params, c) if not c.rw then - return output .. "\nno write access" + return output .. "\nError: No write access" end local what, _ = get_cmd_params(params) if what == "" then - return output .. "\nMissing file name" + return output .. "\nError: Missing file name" end c.writing = what - return output .. "\n" .. "writing \"" .. what .. "\". Enter STOP on a line by itself to finish" + return output .. "\nWriting \"" .. what .. "\". Enter STOP on a line by itself to finish" end, write = function(output, params, c) if not c.rw then - return output .. "\nno write access" + return output .. "\nError: No write access" end local what, _ = get_cmd_params(params) if what == "" then - return output .. "\nMissing file name" + return output .. "\nError: Missing file name" end c.writing = what local meta = minetest.get_meta(c.pos) @@ -68,37 +68,37 @@ local commands = { meta:set_string("files", minetest.write_json(files)) end end - return output .. "\n" .. "writing \"" .. what .. "\". Enter STOP on a line by itself to finish" + return output .. "\nWriting \"" .. what .. "\". Enter STOP on a line by itself to finish" end, remove = function(output, params, c) if not c.rw then - return output .. "\nno write access" + return output .. "\nError: No write access" end local meta = minetest.get_meta(c.pos) local meta_files = meta:get_string("files") if not meta_files or meta_files == "" then - return output .. "\nNo such file" + return output .. "\nError: No such file" end local files = minetest.parse_json(meta_files) or {} local first, _ = get_cmd_params(params) if files[first] then files[first] = nil else - return output .. "\n" .. "No such file" + return output .. "\nError: No such file" end meta:set_string("files", minetest.write_json(files)) - return output .. "\n" .. "removed \"" .. first .. "\"" + return output .. "\nRemoved \"" .. first .. "\"" end, list = function(output, params, c) local meta = minetest.get_meta(c.pos) local meta_files = meta:get_string("files") local files if not meta_files or meta_files == "" then - return output .. "\nNo files found" + return output .. "\nError: No files found" end files = minetest.parse_json(meta_files) or {} if not files then - return output .. "\nNo files found" + return output .. "\nError: No files found" end for k, _ in pairs(files) do output = output .. "\n" .. k @@ -112,26 +112,26 @@ local commands = { local meta = minetest.get_meta(c.pos) local meta_files = meta:get_string("files") if not meta_files or meta_files == "" then - return output .. "\nNo such file" + return output .. "\nError: No such file" end local files = minetest.parse_json(meta_files) or {} local first, _ = get_cmd_params(params) if files[first] then return output .. "\n" .. files[first] else - return output .. "\n" .. "No such file" + return output .. "\nError: No such file" end end, lock = function(output, params, c) if not c.rw then - return output .. "\nno write access" + return output .. "\nError: no write access" end local meta = minetest.get_meta(c.pos) meta:set_int("locked", 1) - return output .. "\n" .. "terminal locked" + return output .. "\n" .. "Terminal locked" end, unlock = function(output, params, c) - return output .. "\n" .. "unable to connect to authentication service" + return output .. "\n" .. "Error: unable to connect to authentication service" end, help = function(output, params, c) if params ~= "" then @@ -139,11 +139,14 @@ local commands = { if help[h] then return output .. "\n" .. help[h] else - return output .. "\nno help for \"" .. h .. "\"" + return output .. "\nError: No help for \"" .. h .. "\"" end end return output .. "\n" .. - "append clear echo help list lock read remove unlock write" + "Available commands:\n" .. + " append\n clear\n echo\n help\n list\n lock\n" .. + " read\n remove\n unlock\n write\n" .. + "Type `help ` for more help about that command" end, } @@ -172,7 +175,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) -- input validation local name = player:get_player_name() local c = context[name] - if not context[name] then + if not c then minetest.log("error", name .. " sends invalid formspec data") return true end @@ -187,7 +190,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if not c.rw then c.writing = nil output = output .. "\n" .. line - output = output .. "\nno write access" + output = output .. "\nError: no write access" c.output = output minetest.show_formspec(name, "terminal:", make_formspec(output, "> ")) return true @@ -214,8 +217,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) files[c.writing] = files[c.writing] .. "\n" .. line end end - meta:set_string("files", minetest.write_json(files)) - output = output .. "\n" .. line + if string.len(files[c.writing]) < 16384 then + local json = minetest.write_json(files) + if string.len(json) < 49152 then + meta:set_string("files", json) + output = output .. "\n" .. line + else + output = output .. "\n" .. "Error: no space left on device" + end + else + output = output .. "\n" .. "Error: maximum file length exceeded" + end c.output = output minetest.show_formspec(name, "terminal:", make_formspec(output, "")) else @@ -225,7 +237,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) local meta = minetest.get_meta(c.pos) local cmd, params = get_cmd_params(line) if meta:get_int("locked") == 1 and cmd ~= "unlock" then - output = output .. "\nTerminal locked, type \"unlock\" to unlock it" + output = output .. "\nError: Terminal locked, type \"unlock\" to unlock it" c.output = output minetest.show_formspec(name, "terminal:", make_formspec(output, "> ")) return true @@ -235,13 +247,17 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fn then output = fn(output, params, c) else - output = output .. "\n" .. "Syntax Error. Try \"help\"" + output = output .. "\n" .. "Error: Syntax Error. Try \"help\"" end c.output = output minetest.show_formspec(name, "terminal:", make_formspec(output, "> ")) end elseif fields.quit then + context[name] = nil minetest.sound_play("terminal_power_off", {pos = c.pos}) + else + minetest.log("error", name .. " sends invalid formspec data") + return true end return true end)