Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
- local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
- local USERS_FOLDER = "/disk/users/"
- local ERROR_FOLDER = "/disk/error/"
- local BSOD_PROGRAM = "BSOD.lua"
- -- Doggy OS branding and constants
- local OS_NAME = "Doggy OS"
- local OS_VERSION = "13.0"
- local HEADER_COLOR = colors.blue
- local HIGHLIGHT_COLOR = colors.blue -- Set highlight color to blue
- -- Utility function to draw a centered popup window with a text-based border
- local function drawPopupWindow(headerText, contentLines)
- term.clear() -- Clear the screen before drawing the popup window
- local w, h = term.getSize()
- local maxLength = #headerText
- for _, line in ipairs(contentLines) do
- maxLength = math.max(maxLength, #line)
- end
- local windowWidth = maxLength + 4 -- Width of the popup window
- local windowHeight = #contentLines + 4 -- Height of the popup window
- local xStart = math.floor(w / 2 - windowWidth / 2)
- local yStart = math.floor(h / 2 - windowHeight / 2)
- -- Draw border
- term.setCursorPos(xStart, yStart)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- for i = 1, windowHeight - 2 do
- term.setCursorPos(xStart, yStart + i)
- term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
- end
- term.setCursorPos(xStart, yStart + windowHeight - 1)
- term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
- -- Draw header
- term.setCursorPos(xStart + 2, yStart + 1)
- term.setTextColor(HEADER_COLOR)
- term.write(headerText)
- term.setTextColor(colors.white)
- -- Draw content
- for i, line in ipairs(contentLines) do
- term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
- term.write(line)
- end
- end
- local function getUserCredentials(username)
- local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
- if fs.exists(passwordFile) then
- local file = fs.open(passwordFile, "r")
- local storedPassword = file.readLine()
- file.close()
- return storedPassword
- else
- return nil -- User does not exist
- end
- end
- local function drawLoginScreen(username, attemptsLeft)
- local contentLines = {
- "Username: " .. username,
- "Attempts left: " .. attemptsLeft,
- "",
- "Enter password:"
- }
- drawPopupWindow("Login", contentLines)
- term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
- end
- local function lockoutUser(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- local file = fs.open(disabledFile, "w")
- file.close()
- end
- local function checkDisabled(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- return fs.exists(disabledFile)
- end
- local function checkCredentials(username)
- if checkDisabled(username) then
- drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
- os.sleep(5) -- Display the disabled message for 5 seconds
- shell.run("/disk/os/lock.lua") -- Run the lock.lua program
- return false
- end
- local storedPassword = getUserCredentials(username)
- if not storedPassword then
- return false -- User does not exist
- end
- local attempts = 0
- repeat
- drawLoginScreen(username, MAX_ATTEMPTS - attempts)
- local enteredPassword = read("*")
- attempts = attempts + 1
- if enteredPassword == storedPassword then
- return true
- else
- drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
- os.sleep(2) -- Display the error message for 2 seconds
- end
- until attempts > MAX_ATTEMPTS
- drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
- lockoutUser(username)
- os.sleep(2) -- Display the lockout message for 2 seconds
- return false
- end
- local function checkDiskIDs()
- -- Get a list of all connected peripherals
- local peripherals = peripheral.getNames()
- -- Array to store disk IDs
- local diskIDs = {}
- -- Loop through all peripherals
- for _, name in ipairs(peripherals) do
- -- Check if the peripheral is a disk drive
- if peripheral.getType(name) == "drive" then
- -- Get the disk ID from the disk drive
- local diskID = disk.getID(name)
- -- If a disk is inserted, add its ID to the array
- if diskID then
- table.insert(diskIDs, {id = diskID, name = name})
- end
- end
- end
- -- Check if any disks were found
- if #diskIDs > 0 then
- return diskIDs
- else
- return nil
- end
- end
- local function drawSecurityCardPrompt()
- local contentLines = {
- "Please insert your security card.",
- "Press ENTER to use password instead."
- }
- drawPopupWindow("Insert Security Card", contentLines)
- end
- local function drawErrorMessage(message)
- drawPopupWindow("Error", {message})
- end
- local function drawMessage(header, message)
- drawPopupWindow(header, {message})
- end
- local function ejectDisk(diskName)
- peripheral.call(diskName, "ejectDisk")
- end
- local function insertSecurityCard(username)
- local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
- if not fs.exists(idFolder) then
- return false
- end
- while true do
- drawSecurityCardPrompt()
- local event, key = os.pullEvent()
- if event == "key" and key == keys.enter then
- return false -- Allow password login if enter is pressed
- elseif event == "disk" or event == "disk_insert" then
- local diskIDs = checkDiskIDs()
- if diskIDs then
- for _, disk in ipairs(diskIDs) do
- ejectDisk(disk.name) -- Eject the disk after checking
- local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
- if fs.exists(idFile) then
- return true -- Allow access if a valid security ID is found
- end
- end
- drawErrorMessage("Error: Unregistered security key.")
- os.sleep(2)
- end
- end
- end
- end
- local function selectUserFromList(users)
- local selectedIndex = 1
- local totalUsers = #users
- while true do
- local contentLines = {
- "Select your user account"
- }
- for i, user in ipairs(users) do
- if i == selectedIndex then
- term.setTextColor(HIGHLIGHT_COLOR)
- table.insert(contentLines, "> " .. user)
- else
- term.setTextColor(colors.white)
- table.insert(contentLines, " " .. user)
- end
- end
- term.setTextColor(colors.white) -- Reset color after loop
- drawPopupWindow("Select User", contentLines)
- local event, key = os.pullEvent()
- if event == "key" then
- if key == keys.down then
- selectedIndex = math.min(selectedIndex + 1, totalUsers)
- elseif key == keys.up then
- selectedIndex = math.max(selectedIndex - 1, 1)
- elseif key == keys.enter then
- return users[selectedIndex]
- end
- end
- end
- end
- local function main()
- -- Set the text color to white and the background color to black
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.clear()
- local users = fs.list(USERS_FOLDER)
- if not users or #users == 0 then
- -- No users detected, run the BSOD program
- shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
- else
- -- Allow the user to select a username
- local selectedUser = selectUserFromList(users)
- if selectedUser then
- if insertSecurityCard(selectedUser) or checkCredentials(selectedUser) then
- drawMessage("Success", "Welcome back, " .. selectedUser .. "!")
- os.sleep(2)
- -- Proceed to the main Doggy OS interface
- shell.run("/disk/os/gui")
- else
- drawMessage("Login Failed", "Invalid username or password.")
- os.sleep(2)
- shell.run("/disk/os/gui")
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement