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

Nil problem!

Started by LNETeam, 31 December 2012 - 05:09 PM
LNETeam #1
Posted 31 December 2012 - 06:09 PM
Hello!,

I'm having problems with a logon script. I have the server read an account array (multiple passwords for each computer/user) serialize the table and send it to the client, the client recieves it, unserializes it and creates a new table from that. It then checks the password entered by the user against all values in the table to check if any match. Unfourtunately it seems that the program is not recieving the table correctly please help!! Server and Client code included. P.S I use this with CC-Get.

Server:
Spoiler

function os.pullEvent()
	local cEvent, p1, p2, p3, p4, p5 = os.pullEventRaw()
		if cEvent == "terminate" then
		end
	return cEvent, p1, p2, p3, p4, p5
end

serverRun = true

dCheck = {}
dCheck[1] = "top"
dCheck[2] = "bottom"
dCheck[3] = "left"
dCheck[4] = "right"
dCheck[5] = "front"
dCheck[6] = "back"

for n = 1,#dCheck do
	rednet.open(dCheck[n])
end

function checkServer(yn)
	write("Start with maintainence?: ")
	it = read()
	if it == "y" or it == "Y" then
		term.clear()
		return "main"
	end
	if yn == "n" then
		return false
	end
	if fs.exists("/LNEX/Server/CompServe") then
		if fs.exists("/LNEX/Server/APIS") then
			if fs.exists("/LNEX/Server/APIS/WFCI") and fs.exists("/LNEX/Server/APIS/WPCI") and fs.exists("/LNEX/Server/APIS/WAPI") and fs.exists("/LNEX/Server/APIS/WNIL") then
				if serverRun == true then
					return true
				else
					return false
				end
			else
				return false
			end
		else
			return false
		end
	else
		return false
	end
end

function placeAcc(pass , compi)
	if fs.exists("/LNEX/Server/CompServe/" .. compi) then
		hpath = "/LNEX/Server/CompServe/" .. compi
		shell.run(hpath)  
		if #passw == 0 then
			file = io.open(hpath , "w")
			local nnum = #passw + 1
			file:write("passw = {}")
			file:write("\npassw[" ..nnum.. "] = '".. pass .."'")
			file:close()
		else
			file = io.open(hpath , "w")
			nnum = #passw + 1
			file:write("passw = {}")
			for i = 1,#passw do
				local curpass = passw[i]
				file:write("\npassw[" ..i.. "] = '" .. curpass.."'")
			end
			file:write("\npassw[" ..nnum.. "] = '".. pass .."'")
		end
		file:close()
		shell.run("/LNEX/Server/CompServe/" .. compi)
		if passw[nnum] == pass then
			return true
		else
			return false
		end
	else
		file = io.open("/LNEX/Server/CompServe/" .. compi , "w")
		file:write("passw = {}")
		file:close()
		placeAcc(pass , compi)
	end
end

function sendAcc(compi)
	ting = readAccData(compi)
	sv = textutils.serialize(ting)
	rednet.send(compi , sv)
end

function acce(compi)
	if fs.exists("/LNEX/Server/CompServe/" .. compi) == true then
		shell.run("/LNEX/Server/CompServe/" .. compi)
		if fs.exists("/LNEX/Server/CompServe/" .. compi) == true and #passw >= 5 then
			return true
		else
			return false
		end
	else
		return false
	end
end

function edCount(pass , edvar , compi)
	if edvar == 1 then
		local file = io.open("/LNEX/Server/accounts/" .. compi , "r")
		local cp = file:read()
		file:close()
		if pass == cp then
			return true
		else
			return false
		end
	elseif edvar == 2 then
		local file = io.open("/LNEX/Server/accounts/" .. compi , "w+")
		file:write(pass)
		file:close()
		return true
	end
end

function readAccData(init)
	shell.run("/LNEX/Server/CompServe/" .. init)
	if #passw >= 1 then
		comp = {}
		for i = 1,#passw do
			comp[i] = passw[i]
		end
		return comp
	end
end

function server()
	id  = os.getComputerID()
	sm = checkServer("y")
		if sm == true then
			state = true
			term.clear()
		elseif sm == "main" then
			state = false
			term.clear()
		else
			state = false
		end
	while serverRun == true do
		term.setTextColor(colors.white)
		output = state and "Open and Servicing" or "Closed for Maintainence or Error"
		textColor = state and colors.green or colors.red
		term.setCursorPos(1,1)
		term.write("Server ID: " ..id)
		print(" ")
		print(" ")
		term.setTextColor(colors.white)
		term.write("Server status: ")
		term.setTextColor(textColor)
		term.write(output)
		if state == false then
			os.pullEvent("key")
			term.clear()
			term.setCursorPos(1,1)
			serverRun = false
			return
		end
		event, p1, p2 = os.pullEvent("rednet_message")
			sendid = p1
			if p2 == "$-*" then
				p2 = nil
				rednet.send(sendid , "get")
				event, p1, p2, p3 = os.pullEvent("rednet_message")
				newacc = p2
				sendid = nil
				sendid = p1
				tem = tonumber(sendid)
				if placeAcc(newacc , tem) == true then
					rednet.send(sendid , "success")
				else
					rednet.send(sendid , "failure")
				end
			elseif p2 == "req" then
				if acce(p1) == true then --Fixed
					rednet.send(sendid , "true")
				else
					rednet.send(sendid , "false")
				end
				event, p1, p2  = os.pullEvent("rednet_message") --Add new policy
				if acce(p1) == true then
					sendAcc(sendid)  --WIP
				end
			elseif p2 == "is" then
				if acce(p1) == true then
					rednet.send(p1 , "true")
				else
					rednet.send(p1 , "false")
				end
			elseif p2 == "edit" then
				event , p1 , p2 = os.pullEvent("rednet_message")
				curpass = p2
				if edCount(curpass , 1 , p1) == true then
					rednet.send(p1 , "true")
				else
					rednet.send(p1 , "false")
				end
				event , p1 , p2 = os.pullEvent("rednet_message")
				npass = p2
				if edCount(npass , 2 , p1) == true then
					rednet.send(p1 , "true")
				end
			end
		sleep(.0000000001)
	end
end

if serverRun == false and maintRun == false then
	os.pullEvent("key")
	os.reboot()
end

server()

Client:
Spoiler

function os.pullEvent()
	local cEvent, p1, p2, p3, p4, p5 = os.pullEventRaw()
		if cEvent == "terminate" then
		end
	return cEvent, p1, p2, p3, p4, p5
end

term.clear()

file = io.open("/LNEX/Client-DB/connect-info/id" , "r")
ci = file:read()
file:close()
cs = tonumber(ci)



function getRight(sLin)
	local nWidth = term.getSize()
	local x = math.floor(nWidth) - string.len(sLin)
	return x + 1
end

function getExist() --Send comp id, server checks if passuid exists with id, if not return false if yes return true. client reqests access to that file
	file = io.open("/LNEX/Client-DB/connect-info/id" , "r")
	ci = file:read()
	file:close()
	cs = tonumber(ci)
	if rednet.send(cs , "req") == false then
		term.clear()
		term.setCursorPos(1,1)
		print("Server connection error")
		os.pullEvent("key")
		os.reboot()
	end
	event , p1 , p2 = os.pullEvent("rednet_message")
	if p2 == "true" then
		rednet.send(cs , "cred")
		event , p1 , p2 = os.pullEvent("rednet_message")
		passes = textutils.unserialize(p2)
		pr = true
		return passes
	elseif p2 == "false" then
		pr = false
	else
		term.clear()
		term.setCursorPos(1,1)
		print("Server recommunication error")
		os.pullEvent("key")
		os.reboot()
	end
end

kim = getExist()
term.setCursorPos(1,2)
print(kim)

if  pr == false then
	pass = ""
end

function termRight(sLin)
	local nWidth, nHeight = term.getSize()
	local y = nHeight
	local x = getRight(sLin)
	term.setCursorPos(x, y)
	term.write(sLin)


end

termRight("powered by: LNEX")
term.setCursorPos(1,1)
term.clearLine()
write "Password: "
input = read("*")
for i = 1,#kim do
	if input == kim[i] then
		term.setTextColor(colors.green)
		print "Logged in"
		sleep(2)
		rednet.open("right")
		shell.run("cc-get" , "update" , "lnexi")
		shell.run("lnexi")
		term.clear()
		term.setCursorPos(1,1)
		term.setTextColor(colors.yellow)
		term.write("LNET SECURITY SYSTEM: Client")
		term.setCursorPos(1,2)
	else
		print "Password Incorrect"
		sleep(2)
		os.shutdown()
	end
end
remiX #2
Posted 01 January 2013 - 03:29 AM
This should be in Ask a Pro

Can you please provide the whole error (so we can see what line is causing the error), and post the codes on pastebin because they're long and counting the lines is not fun.
GeniusName3 #3
Posted 03 January 2013 - 04:10 PM
Nice program,I will check it later on
1lann #4
Posted 03 January 2013 - 06:31 PM
Ok so I looked through your code quickly, and found the problems relating to what you said wasn't working

In your code in this part:

kim = getExist()
term.setCursorPos(1,2)
print(kim)
It looks like you're trying to debug 'kim' which is a table, so you can't print it out. You'll need to remove that print(kim)

Here (which is most likely your problem):

for i = 1,#kim do
  if input == kim[i] then
	term.setTextColor(colors.green)
	print "Logged in"
	sleep(2)
	rednet.open("right")
	shell.run("cc-get" , "update" , "lnexi")
	shell.run("lnexi")
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.yellow)
	term.write("LNET SECURITY SYSTEM: Client")
	term.setCursorPos(1,2)
  else
	print "Password Incorrect"
	sleep(2)
	os.shutdown()
  end
end
Your if statement's logic doesn't really work in that for loop, you should change it to:

for i = 1,#kim do
  if input == kim[i] then
	term.setTextColor(colors.green)
	print "Logged in"
	sleep(2)
	rednet.open("right")
	shell.run("cc-get" , "update" , "lnexi")
	shell.run("lnexi")
	term.clear()
	term.setCursorPos(1,1)
	term.setTextColor(colors.yellow)
	term.write("LNET SECURITY SYSTEM: Client")
	term.setCursorPos(1,2)
    break
  end
end
print "Password Incorrect"
sleep(2)
os.shutdown()
LNETeam #5
Posted 04 January 2013 - 09:16 AM
You can try the code to help better know the error. You only need cc-get installed, the use
cc-get install lnexi
Then simply type "lnexi" to launch setup