This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheEisbaer's profile picture

[Lua][Error][Question] ComputerCraft Wiki Program

Started by TheEisbaer, 23 September 2012 - 05:36 PM
TheEisbaer #1
Posted 23 September 2012 - 07:36 PM
Hello people,

I copied this http://computercraft...oaming_Profiles
I edited it so: Password Server is Computer No. 3
I have 2 Clients, Computer 4 and 5.

Every time i want to log in on a client it says "Not authorised" (or so) ..

Here the codes:

Password Server
Spoilerterm.clear()
term.setCursorPos(1,1)
print("This is a password server. There is no user interaction here.")
print("Please find a computer and login there.")
local firstCycle = true
local validSender = false
local modemSide = "left"
local valid = false
users = { test, test1 }
passwords = { test, test1 }
senders = { 4, 5 }
function bootUp()
rednet.open(modemSide)
end
while true do
validSender = false
if firstCycle then
bootUp()
firstCycle = false
end
senderId, message, distance = rednet.receive()
for i,v in ipairs(senders) do
if v == senderId then
validSender = true
break
end
end
if validSender then
for i,v in ipairs(users) do
if message == v then
valid = true
password = passwords
else
valid = false
end
end
if valid then
rednet.send(senderId, password)
else
rednet.send(senderId, "Not Valid")
end
end
end
Client 1 & 2
Spoilerlocal locker = true
local failed = true
local attempted_login = true
local password_server = 3
rednet.open("left")
while locker do
attempted_login = false
term.clear()
term.setCursorPos(1,1)
print("Welcome to a USERS PC : Roaming Profile Enabled")
print("What would you like to do?")
print("[1] Login (*)")
print("[2] Shutdown")
write("> ")
local input = read()
if input == "2" then
os.shutdown()
elseif input == "1" then
attempted_login = true
print("Please login…")
write("Username: ")
local username = read()
write("Password: ")
local password = read("*")
rednet.send(password_server, username)
senderId, message, distance = rednet.receive(5)
if password == message then
failed = false
locker = false
term.clear()
term.setCursorPos(1,1)
print("Welcome ", username)
else
print("Not authorised.")
sleep(1)
end
else
print("Command not recognised…")
sleep(2)
end
end
Hope you can help
MysticT #2
Posted 23 September 2012 - 07:40 PM
And… what exactly you need help with?
TheEisbaer #3
Posted 23 September 2012 - 07:42 PM
And… what exactly you need help with?
Edited sry i fogot o:
MysticT #4
Posted 23 September 2012 - 07:45 PM
The usernames and passwords must be strings, so put quotes arround them, that should fix it.
TheEisbaer #5
Posted 23 September 2012 - 07:48 PM
The usernames and passwords must be strings, so put quotes arround them, that should fix it.
sry i'm stupid in english are quotes this " " ??
MysticT #6
Posted 23 September 2012 - 07:49 PM
The usernames and passwords must be strings, so put quotes arround them, that should fix it.
sry i'm stupid in english are quotes this " " ??
Yes, put them arround the usernames and passwords, like this:

users = { "test", "test1" }
passwords = { "test", "test1" }
TheEisbaer #7
Posted 23 September 2012 - 07:57 PM
The usernames and passwords must be strings, so put quotes arround them, that should fix it.
sry i'm stupid in english are quotes this " " ??
Yes, put them arround the usernames and passwords, like this:

users = { "test", "test1" }
passwords = { "test", "test1" }
still don't work :P/>/>
and it's on a server btw :3
MysticT #8
Posted 23 September 2012 - 08:07 PM
Ok, found the error:

for i,v in ipairs(users) do
  if message == v then
    valid = true
    password = passwords[i]
    break -- add break here
  else
    valid = false
  end
end
Someone should change the code in the wiki.
TheEisbaer #9
Posted 23 September 2012 - 08:21 PM
Ok, found the error:

for i,v in ipairs(users) do
  if message == v then
	valid = true
	password = passwords[i]
	break -- add break here
  else
	valid = false
  end
end
Someone should change the code in the wiki.
hmm still don't work ://
MysticT #10
Posted 23 September 2012 - 08:38 PM
Ok, made new versions of the programs:
Server:
Spoiler

local users = { "user1" = "password1", "user2" = "password2" }
local senders = { 4, 5 }

local function clear()
	term.clear()
	term.setCursorPos(1, 1)
end

local function connect()
	for _,s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false
end

local function isValid(sender)
	for _,id in ipairs(senders) do
		if id == sender then
			return true
		end
	end
	return false
end

if not connect() then
	print("No modem found")
	return
end

clear()
print("This is a password server. There is no user interaction here.")
print("Please find a computer and login there.")

while true do
	local id, msg = rednet.receive()
	if isValid(id) then
		local usr = textutils.unserialize(msg)
		if usr then
			if users[usr.username] == usr.password then
				rednet.send(id, "PASS_OK")
			else
				rednet.send(id, "PASS_WRONG")
			end
		end
	end
end
Client:
Spoiler

local password_server = 3

local function clear()
    term.clear()
    term.setCursorPos(1, 1)
end

local function connect()
    for _,s in ipairs(rs.getSides()) do
        if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
            rednet.open(s)
            return true
        end
    end
    return false
end

local function validateUsr(usr, timeout)
    rednet.send(password_server, textutils.serialize(usr))
    local t0 = os.clock()
    while os.clock() - t0 < timeout do
        local id, msg = rednet.receive(timeout - (os.clock() - t0))
        if id and msg then
            if id == password_server then
                if msg == "PASS_OK" then
                    return true
                else
                    print("Not authorised")
                    sleep(1)
                    return false
                end
            end
        else
            print("Server not responding")
            print("Try again")
            sleep(1)
            return false
        end
    end
    return false
end

if not connect() then
    print("No modem found")
    print("Shutting down")
    os.shutdown()
end

while true do
    clear()
    print("Welcome to a USERS PC : Roaming Profile Enabled")
    local usr = {}
    write("Username: ")
    usr.username = read()
    write("Password: ")
    usr.password = read("*")
    if validateUsr(usr, 5) then
        clear()
        print("Welcome ", usr.username)
        break
    end
end
I didn't test them, so let me know if they work.

Edit: made some changes to the client
TheEisbaer #11
Posted 23 September 2012 - 08:53 PM
Ok, made new versions of the programs:
Server:
Spoiler

local users = { "user1" = "password1", "user2" = "password2" }
local senders = { 4, 5 }

local function clear()
	term.clear()
	term.setCursorPos(1, 1)
end

local function connect()
	for _,s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false
end

local function isValid(sender)
	for _,id in ipairs(senders) do
		if id == sender then
			return true
		end
	end
	return false
end

if not connect() then
	print("No modem found")
	return
end

clear()
print("This is a password server. There is no user interaction here.")
print("Please find a computer and login there.")

while true do
	local id, msg = rednet.receive()
	if isValid(id) then
		local usr = textutils.unserialize(msg)
		if usr then
			if users[usr.username] == usr.password then
				rednet.send(id, "PASS_OK")
			else
				rednet.send(id, "PASS_WRONG")
			end
		end
	end
end
Client:
Spoiler

local password_server = 3

local function clear()
	term.clear()
	term.setCursorPos(1, 1)
end

local function connect()
	for _,s in ipairs(rs.getSides()) do
		if peripheral.isPresent(s) and peripheral.getType(s) == "modem" then
			rednet.open(s)
			return true
		end
	end
	return false
end

local function validateUsr(usr, timeout)
	rednet.send(password_server, textutils.serialize(usr))
	local t0 = os.clock()
	while os.clock() - t0 < timeout do
		local id, msg = rednet.receive(timeout - (os.clock() - t0))
		if id and msg then
			if id == password_server then
				if msg == "PASS_OK" then
					return true
				else
					print("Not authorised")
					sleep(1)
					return false
				end
			end
		else
			print("Server not responding")
			print("Try again")
			sleep(1)
			return false
		end
	end
	return false
end

if not connect() then
	print("No modem found")
	print("Shutting down")
	os.shutdown()
end

while true do
	clear()
	print("Welcome to a USERS PC : Roaming Profile Enabled")
	local usr = {}
	write("Username: ")
	usr.username = read()
	write("Password: ")
	usr.password = read("*")
	if validateUsr(usr, 5) then
		clear()
		print("Welcome ", usr.username)
		break
	end
end
I didn't test them, so let me know if they work.

Edit: made some changes to the client
in the server one i get "bios:206: [string "startup"]:1: '}' expected
MysticT #12
Posted 23 September 2012 - 08:59 PM
Change the first line to:

local users = { ["user1"] = "password1", ["user2"] = "password2" }
or

local users = { user1 = "password1", user2 = "password2" }
TheEisbaer #13
Posted 23 September 2012 - 09:02 PM
Change the first line to:

local users = { ["user1"] = "password1", ["user2"] = "password2" }
or

local users = { user1 = "password1", user2 = "password2" }
Next error: bios:206: [string "startup"]:6: '<eof>' expected
MysticT #14
Posted 23 September 2012 - 09:09 PM
Did you copy the exact same code?
TheEisbaer #15
Posted 23 September 2012 - 09:19 PM
Did you copy the exact same code?
First not. But now it works :P/>/>
i love you so much! <3 ;)/>/>
TheEisbaer #16
Posted 23 September 2012 - 09:37 PM
Did you copy the exact same code?
Now i have a little offtopic question: I want the computer to do something if it get's a blue cable input from a bundeled cable how do i "code" this? ^^
MysticT #17
Posted 23 September 2012 - 09:39 PM
You need to use the redstone api. It will be something like this:

if rs.testBundledInput("side", colors.blue) then
  -- do something here
end