Alright Kolpa, as requested (kinda^^) I took a look at it and rewrote some stuff. I tried to leave the original code as much as possible.
I moved the password check to the server though. Because checking valid credentials should always happen server-side, or else someone could just rewrite their client to ignore the password-check.^^
Other smaller changes include setting locals wherever I could, removing redundant table accesses (os & io), etc.
If you have any questions about something in the code, don't hesitate to ask.
K, hope it's of any use, here goes:
Client:
Spoiler
local server = changeme
rednet.open("back")
while true do
textutils.slowPrint("please log in")
textutils.slowPrint("Username:")
local user = read()
textutils.slowPrint("Password:")
local pass = read()
rednet.send(server, user.."n"..pass)
textutils.slowPrint("waiting for server")
local id, access
repeat
id, access = rednet.receive()
until id == server
if access == "granted" then
textutils.slowPrint("access granted for 10 seconds")
redstone.setOutput("right", true)
sleep(10)
redstone.setOutput("right", false)
term.clear()
term.setCursorPos(1, 1)
else
textutils.slowPrint("access denied")
sleep(2)
term.clear()
term.setCursorPos(1, 1)
end
end
Server:
Spoiler
local client = changeme
local usr = "changeme"
local pass = "changeme"
rednet.open("back")
-- Helper function to split strings at the first line-break ("n").
-- Returns: Two strings representing the first and second half of the given string.
local function splitAtLineBreak( toSplit )
for i = 1, #toSplit do
local currChar = string.sub( toSplit, i, i )
if currChar == "n" then
firstHalf = string.sub( toSplit, 1, i - 1 )
secondHalf = string.sub( toSplit, i + 1 )
return firstHalf, secondHalf
end -- of IF
end -- of FOR
end
while true do
local id, loginDetails
repeat
id, loginDetails = rednet.receive()
until id == client
local receivedUser, receivedPass = splitAtLineBreak( loginDetails )
if receivedUser == usr and receivedPass == pass then
sleep(5)
rednet.send(client, "granted")
else
sleep(5)
rednet.send(client, "denied")
end
end
Cheers :(/>/>