Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to clear the terminal and set the cursor position
- local function clearScreen()
- term.clear()
- term.setCursorPos(1, 1)
- end
- -- Function to display centered ASCII art
- local function displayCenteredArt(art, text)
- clearScreen()
- local lines = {}
- for line in art:gmatch("[^\n]+") do
- table.insert(lines, line)
- end
- local _, h = term.getSize()
- local startY = math.floor((h - #lines) / 2) + 1
- for i, line in ipairs(lines) do
- local w, _ = term.getSize()
- local startX = math.floor((w - #line) / 2) + 1
- term.setCursorPos(startX, startY + i - 1)
- print(line)
- end
- if text then
- local textX = math.floor((term.getSize() - #text) / 2) + 1
- term.setCursorPos(textX, startY + #lines + 1)
- print(text)
- end
- end
- -- Function to draw a menu system
- local function drawMenu(title, options)
- clearScreen()
- local w, h = term.getSize()
- local boxWidth = 40
- local boxHeight = #options + 6
- local startX = math.floor((w - boxWidth) / 2) + 1
- local startY = math.floor((h - boxHeight) / 2) + 1
- -- Draw menu background and header
- paintutils.drawBox(startX, startY, startX + boxWidth - 1, startY + boxHeight - 1, colors.blue)
- paintutils.drawFilledBox(startX + 1, startY + 1, startX + boxWidth - 2, startY + boxHeight - 2, colors.lightBlue)
- term.setTextColor(colors.white)
- term.setCursorPos(startX + 2, startY + 1)
- print(title)
- -- Draw options
- for i, option in ipairs(options) do
- term.setCursorPos(startX + 2, startY + 2 + i)
- print(i .. ". " .. option)
- end
- end
- -- Function to handle menu selection
- local function menuSelection(title, options)
- drawMenu(title, options)
- local w, h = term.getSize()
- local startX = math.floor((w - 40) / 2) + 2
- local startY = math.floor((h - (#options + 6)) / 2) + #options + 5
- term.setCursorPos(startX, startY)
- term.write("Select an option: ")
- local choice = tonumber(read())
- if choice and choice > 0 and choice <= #options then
- return choice
- else
- return nil
- end
- end
- 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"
- local CURRENT_USER_FILE = ".currentusr"
- local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
- -- Utility function to draw a centered popup window with a text-based border
- local function drawPopupWindow(headerText, contentLines, windowWidth, windowHeight)
- 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
- -- Calculate the window dimensions if not provided
- windowWidth = windowWidth or (maxLength + 4)
- windowHeight = windowHeight or (#contentLines + 4)
- 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.write(headerText)
- -- Draw content
- for i, line in ipairs(contentLines) do
- term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
- term.write(line)
- end
- end
- -- Function to list all users
- local function listUsers()
- local users = fs.list(USERS_FOLDER)
- local usernames = {}
- for _, user in ipairs(users) do
- local userDir = fs.combine(USERS_FOLDER, user)
- if fs.isDir(userDir) then
- table.insert(usernames, user)
- end
- end
- return usernames
- end
- -- Function to handle user selection from the list
- local function drawUsersScreen(usernames, selectedIndex)
- local w, h = term.getSize()
- local contentLines = {}
- for i, username in ipairs(usernames) do
- if i == selectedIndex then
- table.insert(contentLines, "> " .. username)
- else
- table.insert(contentLines, " " .. username)
- end
- end
- -- Increase the border size for a larger window
- drawPopupWindow("Select a User", contentLines, 30, #contentLines + 4)
- term.setCursorPos(1, h) -- Move cursor out of the way after drawing
- end
- -- Function to handle arrow key navigation and selection
- local function selectUserFromList()
- local usernames = listUsers()
- local selectedIndex = 1
- while true do
- drawUsersScreen(usernames, selectedIndex)
- local event, key = os.pullEvent("key")
- if key == keys.up then
- selectedIndex = math.max(1, selectedIndex - 1)
- elseif key == keys.down then
- selectedIndex = math.min(#usernames, selectedIndex + 1)
- elseif key == keys.enter then
- return usernames[selectedIndex]
- end
- end
- end
- -- Function to get user credentials
- 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
- -- Function to draw login screen
- local function drawLoginScreen(username, attemptsLeft)
- local contentLines = {
- "Username: " .. username,
- "Attempts left: " .. attemptsLeft,
- "",
- "Enter password:"
- }
- drawPopupWindow("Login to Doggy OS", contentLines)
- term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
- end
- -- Function to lockout user after failed attempts
- local function lockoutUser(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- local file = fs.open(disabledFile, "w")
- file.close()
- end
- -- Function to check if user is disabled
- local function checkDisabled(username)
- local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
- return fs.exists(disabledFile)
- end
- -- Function to save the current user
- local function saveCurrentUser(username)
- if fs.exists(CURRENT_USER_FILE) then
- fs.delete(CURRENT_USER_FILE) -- Delete existing file
- end
- local file = fs.open(CURRENT_USER_FILE, "w")
- file.write(username)
- file.close()
- end
- -- Function to check credentials and login
- 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
- saveCurrentUser(username) -- Save the logged-in user
- 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
- -- Function to handle user login process
- 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()
- -- If the ShowAllUsers.cfg file exists, show all users
- if fs.exists(SHOW_ALL_USERS_FILE) then
- local username = selectUserFromList()
- if checkCredentials(username) then
- drawPopupWindow("Welcome", {"Access granted. Welcome, " .. username .. "!"})
- os.sleep(2) -- Display the success message for 2 seconds
- shell.run("/disk/os/gui")
- else
- drawPopupWindow("Access Denied", {"Incorrect credentials."})
- os.sleep(2) -- Display the access denied message for 2 seconds
- shell.run("/disk/os/lock.lua")
- end
- else
- drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
- local enteredUsername = read()
- if checkDisabled(enteredUsername) then
- drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons."})
- os.sleep(5)
- shell.run("/disk/os/lock.lua")
- elseif checkCredentials(enteredUsername) then
- drawPopupWindow("Welcome", {"Access granted. Welcome, " .. enteredUsername .. "!"})
- os.sleep(2)
- shell.run("/disk/os/gui")
- else
- drawPopupWindow("Access Denied", {"Incorrect credentials."})
- os.sleep(2)
- shell.run("/disk/os/lock.lua")
- end
- end
- end
- -- Start the login process
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement