Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function split (s, splitter)
- result = {};
- for match in (s..splitter):gmatch("(.-)"..splitter) do
- table.insert(result, match);
- end
- return result;
- end
- function verify(username, password, modem)
- if fs.exists("/data/"..username) == false then
- return "404";
- else
- --the user exists
- local file = fs.open("/data/"..username,"r")
- if password == file.readLine() then
- --correct password
- modem.transmit(1,1,"Logging into "..username)
- else
- --incorrect password
- return "403";
- end
- end
- end
- local modem = peripheral.wrap("top")
- textutils.slowPrint("Starting Server...")
- while true do
- modem.open(1)
- local _, side, freq, rfreq, message = os.pullEvent("modem_message")
- msg = split(message, "`")
- for key, value in pairs(msg) do
- if key == 1 then
- action = value
- elseif key == 2 then
- var1 = value
- elseif key == 3 then
- var2 = value
- end
- end
- if fs.exists("/data") == false then
- fs.makeDir("/data")
- end
- --Do the requested action
- if action == "balance" then
- print(var1..","..var2)
- user = verify(var1, var2, modem)
- if user == "0" then
- --Correct user just a fake balance for now because we do not actually store it
- modem.transmit(1,1,"1200")
- elseif user == "403" then
- print("Failed login attempt for user "..var1)
- modem.transmit(1,1,"403")
- elseif user == "404" then
- print("Non existant "..var1.." just tried to login")
- modem.transmit(1,1,"404")
- end
- elseif action == "register" then
- --Checking if the user exists
- if verify(var1,"",modem) == "404" then
- --its a new user!
- local file = fs.open("/data/"..var1,"a")
- file.writeLine(var2)
- file.close()
- modem.transmit(1,1,"0")
- print("Registered user "..var1)
- else
- --user already exists
- modem.transmit(1,1,"409")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement