Here's a vague restructuring with some work left to be done, but including file operations to store the user data in separate files:
local function loadData()
local userNames = {}
local passwords = {}
userHandle = io.open("/.logins", "r")
if userHandle and passHandle then
for line in userHandle:lines() do
table.insert(userNames, line)
end
userHandle:close()
else
print("Cannot find username file!")
return nil
end
passHandle = io.open("/.passwords", "r")
if passHandle then
for line in passHandle:lines() do
table.insert(passwords, line)
end
passHandle:close()
else
print("Cannot find passwords file!")
return nil
end
local userData = {}
for i=1,#userNames do
userData[userNames[i]] = passwords[i]
end
return userData
end
local function saveData(userData)
userHandle = io.open("/.logins", "w")
passHandle = io.open("/.passwords", "w")
if userHandle and passHandle then
for name, pass in pairs(userData) do
userHandle:write(name.."\n")
passHandle:write(pass.."\n")
end
userHandle:close()
passHandle:close()
else
print("Could not save data!")
return nil
end
return true
end
-- Rednet Login Server
term.clear()
term.setCursorPos(1,1)
print("This Is A Password Server!")
print("No User Interaction Here!")
clients = { [1] = true, [2] = true, [3] = true } -- The allowed client computer id's
local modem_Side = "back"
local loginData = loadData()
while true do
rednet.open(modem_Side)
senderId, message, distance = rednet.receive()
if clients[senderID] and userData[message] then
rednet.send(senderId, userData[message], true)
elseif clients[senderID] and message == "!changePass" then
--you need to keep track of who's logged in, then just loginData[whoIsLoggedInAt[senderID]] = newPasswordWhateverItIs
saveData(loginData)
end
end