Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Luanub's Login Script
- local prog = ("launcher.lua") -- program to launch after sucessful password entry
- local defPass= ("password") -- default password
- local uname = {}
- local passwd = {}
- local user = ""
- local currentX = 1
- local currentY = 5
- local selection = 1
- --[[
- function os.pullEvent()
- local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
- if event == "terminate" then
- sleep(0)
- end
- return event, p1, p2, p3, p4, p5
- end
- ]]--
- if not fs.exists(".users") then
- local file = io.open(".users", "w")
- file:write("admin,")
- file:close()
- end
- if not fs.exists(".pass") then
- local file = io.open(".pass", "w")
- file:write(defPass..",")
- file:close()
- end
- function pattern(text,pattern,start)
- return string.sub(text,string.find(text,pattern,start)) end
- function split(string)
- local sep = {}
- local done = false
- local count,prevVars,tmp = 0,0,0
- string = string..",|"
- while not done do
- count = count + 1
- tmp = pattern(string,"[^,]+",count+prevVars)
- if tmp == "|" then done = true return sep end
- prevVars = prevVars + tmp:len()
- table.insert(sep,count,tmp)
- end
- return sep
- end
- function getUsers()
- local file = io.open(".users", "r")
- local usernames = file:read()
- file:close()
- uname = split(usernames)
- end
- function getPass()
- local file = io.open(".pass", "r")
- local passwords = file:read()
- file:close()
- passwd = split(passwords)
- end
- function changePass()
- while true do
- getPass()
- local a = table.maxn(uname)
- term.clear()
- term.setCursorPos(1,3)
- print ("Welcome "..user.."\n")
- write (" Enter Current Password: ")
- local pass = read("*")
- c = 0
- for x=1, a do
- c = c + 1
- if user == uname[c] and pass == passwd[c] then
- local sUser = uname[c]
- write ("\nChange your password? ")
- local input = read()
- print ()
- if input == "y" or input == "Y" then
- local test = "false"
- repeat
- write (" Enter new password: ")
- local sPass = read("*")
- write (" Verify new password: ")
- local sPass2 = read("*")
- if sPass == passwd[c] then
- term.setCursorPos(1,7)
- print ("Can not use your current password\n")
- elseif sPass == sPass2 then
- table.remove(passwd, c )
- table.insert(passwd, c, sPass)
- local newNum = table.maxn(passwd)
- local sPass = passwd[1]
- local g = newNum - 1
- local r = 2
- for x=1, g do
- sPass = sPass..","..passwd[r]
- r = r + 1
- end
- local file = io.open(".pass", "w")
- file:write(sPass..",")
- file:close()
- print ("\nPassword Updated")
- sleep(2)
- test = "true"
- else
- term.setCursorPos(1,7)
- print ("Passwords entered do not match, try again\n")
- end
- until test == "true"
- end
- if sUser == "admin" then
- adminMenu()
- return
- end
- loginMenu()
- return
- end
- end
- print ("Invalid User/Password Entered")
- sleep(2)
- end
- end
- function drawCursor()
- term.setCursorPos(currentX, currentY)
- write("->")
- end
- function addUser()
- term.clear()
- term.setCursorPos(1,6)
- write ("Username: ")
- local sName = read()
- local file = io.open(".users", "a")
- file:write(sName..",")
- file:close()
- term.clear()
- term.setCursorPos(1,6)
- local file = io.open(".pass", "a")
- file:write(defPass..",")
- file:close()
- print ("User "..sName.." has been added")
- sleep(2)
- end
- function adminUser()
- currentX = 1
- currentY = 5
- selection = 1
- while true do
- getUsers()
- getPass()
- local num = table.maxn(uname)
- term.clear()
- term.setCursorPos(1, 3)
- print ("User Administration Menu")
- print ("------------------------")
- local n = 1
- for x=1, num do
- print (" "..uname[n])
- n = n + 1
- end
- print ("\n Add User")
- print (" Menu")
- local c = num + 1
- local d = num + 2
- if num == 0 then
- selection = c
- currentY = 6
- end
- drawCursor()
- local e,key, msg = os.pullEvent()
- if e == "key" then
- if key == 17 or key == 200 then
- if selection - 1 == num then
- selection = selection - 1
- currentY = currentY - 2
- elseif selection - 1 >= 1 then
- selection = selection - 1
- currentY = currentY - 1
- end
- elseif key == 31 or key == 208 then
- if selection + 1 <= num then
- selection = selection + 1
- currentY = currentY + 1
- elseif selection + 1 == c then
- selection = selection + 1
- currentY = currentY + 2
- elseif selection + 1 == d then
- selection = selection + 1
- currentY = currentY + 1
- end
- elseif key == 28 then
- if selection == c then
- addUser()
- currentX = 1
- currentY = 5
- selection = 1
- elseif selection == d then
- adminMenu()
- break
- elseif selection == 1 then
- term.clear()
- term.setCursorPos(1,6)
- print ("Unable to delete user")
- sleep (2)
- elseif selection >=2 or selection <= num then
- term.clear()
- term.setCursorPos(1,6)
- local sPlayer = uname[selection]
- write ("Delete User "..sPlayer.." y/n? ")
- local input = read()
- if input == "y" or input == "Y" then
- table.remove(uname, selection )
- table.remove(passwd, selection )
- local newNum = table.maxn(uname)
- local sUsers = uname[1]
- local f = newNum - 1
- local q = 2
- for x=1, f do
- sUsers = sUsers..","..uname[q]
- q = q + 1
- end
- local sPass = passwd[1]
- local g = newNum - 1
- local r = 2
- for x=1, g do
- sPass = sPass..","..passwd[r]
- r = r + 1
- end
- local file1 = io.open(".users", "w")
- file1:write(sUsers..",")
- file1:close()
- local file2 = io.open(".pass", "w")
- file2:write(sPass..",")
- file2:close()
- print ("User "..sPlayer.." has been deleted.")
- sleep(2)
- end
- currentX = 1
- currentY = 5
- selection = 1
- else
- print("Error: Selection out of bounds: ", selection)
- read()
- end
- end
- end
- end
- end
- function adminPass()
- currentX = 1
- currentY = 5
- selection = 1
- while true do
- getUsers()
- getPass()
- local num = table.maxn(passwd)
- term.clear()
- term.setCursorPos(1, 3)
- print ("Password Administration Menu")
- print ("----------------------------")
- local n = 1
- for x=1, num do
- print (" "..uname[n])
- n = n + 1
- end
- print ("\n Menu")
- print("\nSelect User to reset password")
- local c = num + 1
- drawCursor()
- if num == 0 then
- selection = c
- currentY = 6
- end
- drawCursor()
- local e,key, msg = os.pullEvent()
- if e == "key" then
- if key == 17 or key == 200 then
- if selection - 1 == num then
- selection = selection - 1
- currentY = currentY - 2
- elseif selection - 1 >= 1 then
- selection = selection - 1
- currentY = currentY - 1
- end
- elseif key == 31 or key == 208 then
- if selection + 1 <= num then
- selection = selection + 1
- currentY = currentY + 1
- elseif selection + 1 == c then
- selection = selection + 1
- currentY = currentY + 2
- end
- elseif key == 28 then
- if selection == c then
- adminMenu()
- break
- elseif selection >=1 or selection <= num then
- term.clear()
- term.setCursorPos(1,6)
- local sPlayer = uname[selection]
- write ("Reset "..sPlayer.."'s password y/n? ")
- local input = read()
- if input == "y" or input == "Y" then
- table.remove(passwd, selection)
- table.insert(passwd, selection, defPass)
- local newNum = table.maxn(passwd)
- local sPass = passwd[1]
- local g = newNum - 1
- local r = 2
- for x=1, g do
- sPass = sPass..","..passwd[r]
- r = r + 1
- end
- local file = io.open(".pass", "w")
- file:write(sPass..",")
- file:close()
- print ("User "..sPlayer.." password has been reset.")
- sleep(2)
- end
- currentX = 1
- currentY = 5
- selection = 1
- else
- print("Error: Selection out of bounds: ", selection)
- read()
- end
- end
- end
- end
- end
- function adminMenu()
- currentX = 1
- currentY = 5
- selection = 1
- while true do
- term.clear()
- term.setCursorPos(1, 3)
- print (" Select Action")
- print ("---------------------")
- print ("| User Maintenance |")
- print ("| Reset Passwords |")
- print ("| Change Password |")
- print ("| Exit |")
- print ("---------------------")
- drawCursor()
- local e, key = os.pullEvent("key")
- if key == 17 or key == 200 then
- if selection - 1 >= 1 then
- selection = selection - 1
- currentY = currentY -1
- end
- elseif key == 31 or key == 208 then
- if selection + 1 <= 4 then
- selection = selection + 1
- currentY = currentY +1
- end
- elseif key == 28 then
- if selection == 1 then
- adminUser()
- break
- elseif selection == 2 then
- adminPass()
- break
- elseif selection == 3 then
- changePass()
- break
- elseif selection == 4 then
- loginMenu()
- break
- else
- print("Error: Selection out of bounds: ", selection)
- read()
- end
- end
- end
- end
- function loginMenu()
- while true do
- getUsers()
- getPass()
- local a = table.maxn(uname)
- term.clear()
- term.setCursorPos(1, 3)
- write (" Username: ")
- user = read()
- write (" Password: ")
- local pass = read("*")
- c = 0
- for x=1, a do
- c = c + 1
- if user == uname[c] and pass == passwd[c] then
- print ("\nLogin Sucessful")
- sleep(1)
- if user == "admin" then
- adminMenu()
- return
- end
- write ("\nChange Password y/n? ")
- local input = read()
- if input == "y" or input == "Y" then
- changePass()
- return
- end
- shell.run(prog)
- return
- end
- end
- print ("Invalid Username or Password Entered")
- sleep(2)
- end
- end
- loginMenu()
Add Comment
Please, Sign In to add comment