Advertisement
DOGGYWOOF

Untitled

Nov 25th, 2024 (edited)
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  2. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  3.  
  4. local USERS_FOLDER = "/disk/users/"
  5. local ERROR_FOLDER = "/disk/error/"
  6. local BSOD_PROGRAM = "BSOD.lua"
  7.  
  8. -- Doggy OS branding and constants
  9. local OS_NAME = "Doggy OS"
  10. local OS_VERSION = "13.0"
  11. local HEADER_COLOR = colors.blue
  12. local HIGHLIGHT_COLOR = colors.blue -- Set highlight color to blue
  13.  
  14. -- Utility function to draw a centered popup window with a text-based border
  15. local function drawPopupWindow(headerText, contentLines)
  16. term.clear() -- Clear the screen before drawing the popup window
  17.  
  18. local w, h = term.getSize()
  19. local maxLength = #headerText
  20. for _, line in ipairs(contentLines) do
  21. maxLength = math.max(maxLength, #line)
  22. end
  23.  
  24. local windowWidth = maxLength + 4 -- Width of the popup window
  25. local windowHeight = #contentLines + 4 -- Height of the popup window
  26. local xStart = math.floor(w / 2 - windowWidth / 2)
  27. local yStart = math.floor(h / 2 - windowHeight / 2)
  28.  
  29. -- Draw border
  30. term.setCursorPos(xStart, yStart)
  31. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  32. for i = 1, windowHeight - 2 do
  33. term.setCursorPos(xStart, yStart + i)
  34. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  35. end
  36. term.setCursorPos(xStart, yStart + windowHeight - 1)
  37. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  38.  
  39. -- Draw header
  40. term.setCursorPos(xStart + 2, yStart + 1)
  41. term.setTextColor(HEADER_COLOR)
  42. term.write(headerText)
  43. term.setTextColor(colors.white)
  44.  
  45. -- Draw content
  46. for i, line in ipairs(contentLines) do
  47. term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
  48. term.write(line)
  49. end
  50. end
  51.  
  52. local function getUserCredentials(username)
  53. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  54.  
  55. if fs.exists(passwordFile) then
  56. local file = fs.open(passwordFile, "r")
  57. local storedPassword = file.readLine()
  58. file.close()
  59. return storedPassword
  60. else
  61. return nil -- User does not exist
  62. end
  63. end
  64.  
  65. local function drawLoginScreen(username, attemptsLeft)
  66. local contentLines = {
  67. "Username: " .. username,
  68. "Attempts left: " .. attemptsLeft,
  69. "",
  70. "Enter password:"
  71. }
  72. drawPopupWindow("Login", contentLines)
  73. term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
  74. end
  75.  
  76. local function lockoutUser(username)
  77. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  78. local file = fs.open(disabledFile, "w")
  79. file.close()
  80. end
  81.  
  82. local function checkDisabled(username)
  83. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  84. return fs.exists(disabledFile)
  85. end
  86.  
  87. local function checkCredentials(username)
  88. if checkDisabled(username) then
  89. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
  90. os.sleep(5) -- Display the disabled message for 5 seconds
  91. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  92. return false
  93. end
  94.  
  95. local storedPassword = getUserCredentials(username)
  96.  
  97. if not storedPassword then
  98. return false -- User does not exist
  99. end
  100.  
  101. local attempts = 0
  102.  
  103. repeat
  104. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  105.  
  106. local enteredPassword = read("*")
  107. attempts = attempts + 1
  108.  
  109. if enteredPassword == storedPassword then
  110. return true
  111. else
  112. drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
  113. os.sleep(2) -- Display the error message for 2 seconds
  114. end
  115. until attempts > MAX_ATTEMPTS
  116.  
  117. drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
  118. lockoutUser(username)
  119. os.sleep(2) -- Display the lockout message for 2 seconds
  120.  
  121. return false
  122. end
  123.  
  124. local function checkDiskIDs()
  125. -- Get a list of all connected peripherals
  126. local peripherals = peripheral.getNames()
  127.  
  128. -- Array to store disk IDs
  129. local diskIDs = {}
  130.  
  131. -- Loop through all peripherals
  132. for _, name in ipairs(peripherals) do
  133. -- Check if the peripheral is a disk drive
  134. if peripheral.getType(name) == "drive" then
  135. -- Get the disk ID from the disk drive
  136. local diskID = disk.getID(name)
  137.  
  138. -- If a disk is inserted, add its ID to the array
  139. if diskID then
  140. table.insert(diskIDs, {id = diskID, name = name})
  141. end
  142. end
  143. end
  144.  
  145. -- Check if any disks were found
  146. if #diskIDs > 0 then
  147. return diskIDs
  148. else
  149. return nil
  150. end
  151. end
  152.  
  153. local function drawSecurityCardPrompt()
  154. local contentLines = {
  155. "Please insert your security card.",
  156. "Press ENTER to use password instead."
  157. }
  158. drawPopupWindow("Insert Security Card", contentLines)
  159. end
  160.  
  161. local function drawErrorMessage(message)
  162. drawPopupWindow("Error", {message})
  163. end
  164.  
  165. local function drawMessage(header, message)
  166. drawPopupWindow(header, {message})
  167. end
  168.  
  169. local function ejectDisk(diskName)
  170. peripheral.call(diskName, "ejectDisk")
  171. end
  172.  
  173. local function insertSecurityCard(username)
  174. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  175. if not fs.exists(idFolder) then
  176. return false
  177. end
  178.  
  179. while true do
  180. drawSecurityCardPrompt()
  181.  
  182. local event, key = os.pullEvent()
  183.  
  184. if event == "key" and key == keys.enter then
  185. return false -- Allow password login if enter is pressed
  186. elseif event == "disk" or event == "disk_insert" then
  187. local diskIDs = checkDiskIDs()
  188. if diskIDs then
  189. for _, disk in ipairs(diskIDs) do
  190. ejectDisk(disk.name) -- Eject the disk after checking
  191. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  192. if fs.exists(idFile) then
  193. return true -- Allow access if a valid security ID is found
  194. end
  195. end
  196. drawErrorMessage("Error: Unregistered security key.")
  197. os.sleep(2)
  198. end
  199. end
  200. end
  201. end
  202.  
  203. local function selectUserFromList(users)
  204. local selectedIndex = 1
  205. local totalUsers = #users
  206.  
  207. while true do
  208. local contentLines = {
  209. "Select your user account"
  210. }
  211.  
  212. for i, user in ipairs(users) do
  213. if i == selectedIndex then
  214. term.setTextColor(HIGHLIGHT_COLOR)
  215. table.insert(contentLines, "> " .. user)
  216. else
  217. term.setTextColor(colors.white)
  218. table.insert(contentLines, " " .. user)
  219. end
  220. end
  221.  
  222. term.setTextColor(colors.white) -- Reset color after loop
  223. drawPopupWindow("Select User", contentLines)
  224.  
  225. local event, key = os.pullEvent()
  226.  
  227. if event == "key" then
  228. if key == keys.down then
  229. selectedIndex = math.min(selectedIndex + 1, totalUsers)
  230. elseif key == keys.up then
  231. selectedIndex = math.max(selectedIndex - 1, 1)
  232. elseif key == keys.enter then
  233. return users[selectedIndex]
  234. end
  235. end
  236. end
  237. end
  238.  
  239. local function main()
  240. -- Set the text color to white and the background color to black
  241. term.setTextColor(colors.white)
  242. term.setBackgroundColor(colors.black)
  243. term.clear()
  244.  
  245. local users = fs.list(USERS_FOLDER)
  246.  
  247. if not users or #users == 0 then
  248. -- No users detected, run the BSOD program
  249. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  250. else
  251. -- Allow the user to select a username
  252. local selectedUser = selectUserFromList(users)
  253. if selectedUser then
  254. if insertSecurityCard(selectedUser) or checkCredentials(selectedUser) then
  255. drawMessage("Success", "Welcome back, " .. selectedUser .. "!")
  256. os.sleep(2)
  257. -- Proceed to the main Doggy OS interface
  258. shell.run("/disk/os/gui")
  259. else
  260. drawMessage("Login Failed", "Invalid username or password.")
  261. os.sleep(2)
  262. shell.run("/disk/os/gui")
  263. end
  264. end
  265. end
  266. end
  267.  
  268. main()
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement