Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- This is a challenge, the goal is to read arbitary files on the machine running this script
- local dangerous_functions = { 'io.open', 'io.popen', 'io.input', 'io.lines', 'io.write', 'io.output', 'dofile', 'loadfile', 'loadstring', 'load', 'require', 'module', 'os.execute'};
- -- remove every dangerous function from the global environment :D
- for k,v in pairs(dangerous_functions) do
- _G[v] = nil;
- end
- account = { };
- function account.greet()
- print"Greetings";
- end
- local function print_menu()
- print'\n\nAccount Management Service';
- print'You are free to do whatever you want with your object';
- print'[0] display current account\n[1] add a key-value pair to the account\n[2] display attribute / call method on the current account';
- end
- local function inspect_account()
- print'Account properties:'
- for k,v in pairs(account) do
- print('\t' .. k .. ' : ' .. type(v));
- end
- end
- local function parse_value(value)
- if value:find'^%d+$' then
- return tonumber(value); -- its a number
- elseif value:find"^'[^']+'$" or value:find'^"[^"]+"$' then
- return value:sub(2, -2); -- its a string, what is between the quotes
- else
- return _G[value]; -- return the value of the global variable that has the name value
- end
- end
- local function add_entry_to_account()
- print'Enter the name of the key you want to add to your account';
- key_name = io.read'l';
- print'Enter the value you want to associate with that key';
- print'It can be an integer, a string, or the name of a global variable';
- value = parse_value(io.read'l');
- account[key_name] = value;
- end
- local function access_entry_from_account()
- inspect_account();
- print'Enter the name of the attribute that you want to read, or the method that you want to call';
- local key_name = io.read'l';
- if type(account[key_name]) == 'function' then
- local arguments = { };
- -- read the arguments from the user, they can be strings, numbers, or references to global variables!
- while true do
- print'Please specify the arguments, enter an empty line to stop entering arguments';
- local argum = io.read'l';
- if argum == '' then break;
- end
- arguments[#arguments+1] = parse_value(argum);
- end
- account[key_name](table.unpack(arguments)); -- call the function and pass it the given arguments, no dangerous functions can be called Haha!
- else
- if type(account[key_name]) == 'string' or type(account[key_name]) == 'number' then
- print('The value associated with the key ' .. key_name .. ' is ' .. account[key_name]);
- else
- -- the value is not string and not number, just print its type
- print('The value associated with the key ' .. key_name .. ' is a ' .. type(account[key_name]) .. ' object');
- end
- end
- end
- while true do
- print_menu();
- local choice = tonumber(io.read'l');
- if choice == 0 then
- inspect_account();
- elseif choice == 1 then
- add_entry_to_account();
- elseif choice == 2 then
- access_entry_from_account();
- else
- print'Unexpected input, enter a number between 0 and 2';
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement