Flawedspirit icon

Password API (Complex)

Flawedspirit | PRO | 03/09/14 12:04:37 AM UTC | 0 ⭐ | 257 👁️ | Never ⏰ | []
Lua |

4.41 KB

|

None

|

0 👍

/

0 👎

--[[
This is a program for ComputerCraft
 
Instructions:
1. Login to your computer's prompt
   -Your computer is usually there by default
2. Download with "pastebin get 5dNPx84w startup"
3. IMPORTANT! Make sure to view or change the default
    password by typing "edit startup" and changing line 19
   -The password can also be changed in-program
   -However, it will RESET to the default if the computer restarts!
4. Make sure to change line 18 to reflect either where
   the door is, or a redstone wire connection
5. Run by typing "startup"
   or by rebooting your computer (CTRL-R)
]]
 
local side ="left" --Side the door is on relation to the computer
local password = "password" --Make sure to change this!
local openTime = 3 --Time in seconds the door will stay open
local input = ""
local status = "Door status: CLOSED" --Default status flag DO NOT EDIT
 
function getInput(prompt)
  print(prompt)
  term.write(">")
  input = read("*")
end
 
function validate(input)
  if input == password then
      return true
  else
      return false
  end
end
 
function openDoor(time)
  status = "Door status: OPEN"
  setFooter(status)
  rs.setOutput(side, true)
  sleep(time)
  status = "Door status: CLOSED"
  setFooter(status)
  rs.setOutput(side, false)
end
 
function setFooter(text)
  term.setCursorPos(1, 19)
  term.clearLine()
  term.write(text)
end
 
function callMainMenu()
  term.clear()
  setFooter(status)
  term.setCursorPos(1, 1)
  print("Flawedspirit Imperial Dynamics Inc.")
  print("Access Mediator v1.1")
  term.setCursorPos(1, 4)
  print("1. Relock accessway")
  print("2. Change password")
  print("3. Override door")
  print("4. Open access for " .. openTime .. " seconds")
  print("5. Change activation time")
  term.write(">")
  local event, input = os.pullEvent("char")
 
  if input == "1" then
    print("Relocking...")
    sleep(1)
    start()
  elseif input == "2" then
    changePass()
  elseif input == "3" then
    overrideDoor(input)
  elseif input == "4" then
      openDoor(openTime)
      callMainMenu()
  elseif input == "5" then
      changePulse()
  else
    print("**Invalid option**")
    sleep(2)
    callMainMenu()
  end
end
 
function changePass()
  term.clear()
  setFooter(status)
  term.setCursorPos(1, 1)
  print("Change Password:")
  term.setCursorPos(1, 3)
 
  getInput("Enter current access code:")
 
  if validate(input) == true then
    getInput("Enter new access code:")
    
    --New password must be entered twice
    local newCode1 = input
    getInput("Enter new access code again:")
    local newCode2 = input
 
    --Make sure new password is correct
    if newCode1 == newCode2 then
      password = newCode2
      print("Password changed.")
      sleep(2)
      start()
    else
      print("**The new passwords did not match**")
      sleep(2)
      changePass()
    end
  else
    print("**Incorrect password**")
    sleep(2)
    start()
  end
end
 
function overrideDoor(input)
  if input == "3" and not redstone.getOutput(side) then
    status = "Door status: FORCE OPEN"
    setFooter(status)
    redstone.setOutput(side, true)
  elseif input == "3" and redstone.getOutput(side) then
    status = "Door status: CLOSED"
    setFooter(status)
    redstone.setOutput(side, false)
  end
  sleep(2)
  callMainMenu()
end
 
function changePulse()
  term.clear()
  setFooter(status)
  term.setCursorPos(1, 1)
  print("Change Activation Time:")
  term.setCursorPos(1, 3)
  print("1. Short      (3 sec)")
  print("2. Long       (5 sec)")
  print("3. Extra Long (10 sec)")
  print("4. Back")
  term.write(">")
  local event, input = os.pullEvent("char")
 
  if input == "1" then
    openTime = 3
  elseif input == "2" then
    openTime = 5
  elseif input == "3" then
    openTime = 10
  elseif input == "4" then
    callMainMenu()
  else
    print("**Invalid option**")
    sleep(2)
    changePulse()
  end
 
  print("Activation time changed.")
  sleep(2)
  callMainMenu()
end
 
function start()
  --Prevent user from terminating program early
  local pullEvent = os.pullEvent
  os.pullEvent = os.pullEventRaw
 
  term.clear()
  setFooter(status)
  term.setCursorPos(1, 1)
  getInput("Enter access code:")
 
  if validate(input) == true then
    openDoor(openTime)
    callMainMenu()
  else
    print("**Incorrect password**")
    sleep(2)
    start()
  end
end
 
--Run the thing!
start()

Comments