Posted 22 October 2012 - 09:58 AM
Hey all,
I have a program designed to login a person, and let them do things (like open a door, or if root, add users.. etc). My LoadAccounts function is causing ComputerCraft to shutdown the computer, and it seems as though there's a bad pointer somewhere. Lua then leaves a thread open and causes any further running to return the number 25.. then after a while of messing with it it output 16. I don't know what those values mean, but they seem to be error numbers?
Thanks in advance for any help anyone may be able to give.
EDIT: Fixed indent in code. For some reason, 2 space indent wasn't even showing up at the first indentation level
EDIT2: You can substitute the SHA1 function call with your own hashing function.. or just use a plaintext string of any sort.
I have a program designed to login a person, and let them do things (like open a door, or if root, add users.. etc). My LoadAccounts function is causing ComputerCraft to shutdown the computer, and it seems as though there's a bad pointer somewhere. Lua then leaves a thread open and causes any further running to return the number 25.. then after a while of messing with it it output 16. I don't know what those values mean, but they seem to be error numbers?
Thanks in advance for any help anyone may be able to give.
local accounts={root="e63a0ab2f8e7ecde486b42ebfec16d4434840af4"}
loggedIn = false
name = ""
function Shutdown()
SaveAccounts()
print("Shutting down...")
sleep(2)
os.shutdown()
end
function SaveAccounts()
if not fs.exists("data") then fs.makeDir("data") end
local f = fs.open("/data/accounts", "w")
for k,v in pairs(accounts) do
f.writeLine(k.."="..v)
end
f.close()
end
function LoadAccounts()
if not fs.exists("data/accounts") then return end
local f = fs.open("/data/accounts", "r")
local line = f.readLine()
while line do
local found = line:find("=")
local key = line:sub(0, found-1)
local val = line:sub(found+1, -1)
accounts[key] = val
end
end
function RawRead()
local event, p = os.pullEvent("key")
if event == "key" then
return p
end
end
nMenu={"Login", "Shutdown"}
lMenu={"Open Door", "Logout", "Shutdown"}
rMenu={"Open Door", "New User", "Del User", "Save Accounts", "Load Accounts",
"Logout", "Shutdown"}
function CUI(m)
local n=1
local l=#m
while true do
term.clear()
term.setCursorPos(1, 1)
print("Select an option [arrow up/arrow down]")
term.setCursorPos(1, 3)
for i=1, l, 1 do
if i==n then
print("["..m[i].."]")
else
print(" ", m[i])
end
end
key = RawRead()
if key==200 and n > 1 then n=n-1 end
if key==208 and n <= l then n=n+1 end
if key==28 then break end
end
term.clear() term.setCursorPos(1, 1)
return n
end
function Menu()
if not loggedIn then
local opt = CUI(nMenu)
if opt == 1 then Login() end
if opt == 2 then Shutdown() end
else
if name == "root" then
local opt = CUI(rMenu)
if opt == 1 then OpenDoor() end
if opt == 2 then NewUser() end
if opt == 3 then DelUser() end
if opt == 4 then SaveAccounts() end
if opt == 5 then LoadAccounts() end
if opt == 6 then Logout() end
if opt == 7 then Shutdown() end
else
local opt = CUI(lMenu)
if opt == 1 then OpenDoor() end
if opt == 2 then Logout() end
if opt == 3 then Shutdown() end
end
end
end
function Login()
local TextPass = function()
term.clear()
term.setCursorPos(1, 1)
write("Username: ")
local user = read()
write("Password: ")
local pass = read("*")
pass = StrUtils.SHA1(pass)
for k,v in pairs(accounts) do
if user==k and pass==v then
loggedIn = true;
name = user
print("Welcome to 351.10, "..name.."!")
sleep(2)
Menu()
end
end
print("ERROR: Bad Username or Password!")
sleep(3)
Menu()
end
TextPass()
end
function Start()
--LoadAccounts()
--For the meantime, just setup two accounts
accounts["root"] = StrUtils.SHA1("rootpass")
accounts["bob"] = StrUtils.SHA1("poop")
Menu()
end
Start()
EDIT: Fixed indent in code. For some reason, 2 space indent wasn't even showing up at the first indentation level
EDIT2: You can substitute the SHA1 function call with your own hashing function.. or just use a plaintext string of any sort.