Advertisement
DOGGYWOOF

Select user system

Nov 27th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.19 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 getUsers()
  125. local users = {}
  126. local allFiles = fs.list(USERS_FOLDER)
  127.  
  128. -- Filter the list to only include directories (exclude root directory)
  129. for _, file in ipairs(allFiles) do
  130. local filePath = fs.combine(USERS_FOLDER, file)
  131.  
  132. -- Exclude the root directory
  133. if fs.isDir(filePath) and file ~= "root" then
  134. table.insert(users, file)
  135. end
  136. end
  137.  
  138. return users
  139. end
  140.  
  141. local function checkDiskIDs()
  142. -- Get a list of all connected peripherals
  143. local peripherals = peripheral.getNames()
  144.  
  145. -- Array to store disk IDs
  146. local diskIDs = {}
  147.  
  148. -- Loop through all peripherals
  149. for _, name in ipairs(peripherals) do
  150. -- Check if the peripheral is a disk drive
  151. if peripheral.getType(name) == "drive" then
  152. -- Get the disk ID from the disk drive
  153. local diskID = disk.getID(name)
  154.  
  155. -- If a disk is inserted, add its ID to the array
  156. if diskID then
  157. table.insert(diskIDs, {id = diskID, name = name})
  158. end
  159. end
  160. end
  161.  
  162. -- Check if any disks were found
  163. if #diskIDs > 0 then
  164. return diskIDs
  165. else
  166. return nil
  167. end
  168. end
  169.  
  170. local function drawSecurityCardPrompt()
  171. local contentLines = {
  172. "Please insert your security card.",
  173. "Press ENTER to use password instead."
  174. }
  175. drawPopupWindow("Insert Security Card", contentLines)
  176. end
  177.  
  178. local function drawErrorMessage(message)
  179. drawPopupWindow("Error", {message})
  180. end
  181.  
  182. local function drawMessage(header, message)
  183. drawPopupWindow(header, {message})
  184. end
  185.  
  186. local function ejectDisk(diskName)
  187. peripheral.call(diskName, "ejectDisk")
  188. end
  189.  
  190. local function insertSecurityCard(username)
  191. local idFolder = fs.combine(USERS_FOLDER .. username, "ID")
  192. if not fs.exists(idFolder) then
  193. return false
  194. end
  195.  
  196. while true do
  197. drawSecurityCardPrompt()
  198.  
  199. local event, key = os.pullEvent()
  200.  
  201. if event == "key" and key == keys.enter then
  202. return false -- Allow password login if enter is pressed
  203. elseif event == "disk" or event == "disk_insert" then
  204. local diskIDs = checkDiskIDs()
  205. if diskIDs then
  206. for _, disk in ipairs(diskIDs) do
  207. ejectDisk(disk.name) -- Eject the disk after checking
  208. local idFile = fs.combine(idFolder, tostring(disk.id) .. ".file")
  209. if fs.exists(idFile) then
  210. return true -- Allow access if a valid security ID is found
  211. end
  212. end
  213. drawErrorMessage("Error: Unregistered security key.")
  214. os.sleep(2)
  215. end
  216. end
  217. end
  218. end
  219.  
  220. local function selectUserFromList(users)
  221. local selectedIndex = 1
  222. local totalUsers = #users
  223.  
  224. while true do
  225. local contentLines = {
  226. "Select your user account"
  227. }
  228.  
  229. for i, user in ipairs(users) do
  230. if i == selectedIndex then
  231. term.setTextColor(HIGHLIGHT_COLOR)
  232. table.insert(contentLines, "> " .. user)
  233. else
  234. term.setTextColor(colors.white)
  235. table.insert(contentLines, " " .. user)
  236. end
  237. end
  238.  
  239. term.setTextColor(colors.white) -- Reset color after loop
  240. drawPopupWindow("Select User", contentLines)
  241.  
  242. local event, key = os.pullEvent()
  243.  
  244. if event == "key" then
  245. if key == keys.down then
  246. selectedIndex = math.min(selectedIndex + 1, totalUsers)
  247. elseif key == keys.up then
  248. selectedIndex = math.max(selectedIndex - 1, 1)
  249. elseif key == keys.enter then
  250. return users[selectedIndex]
  251. end
  252. end
  253. end
  254. end
  255.  
  256. local function main()
  257. -- Set the text color to white and the background color to black
  258. term.setTextColor(colors.white)
  259. term.setBackgroundColor(colors.black)
  260. term.clear()
  261.  
  262. local users = getUsers()
  263.  
  264. if not users or #users == 0 then
  265. -- No users detected, run the BSOD program
  266. shell.run(ERROR_FOLDER .. BSOD_PROGRAM)
  267. else
  268. -- Allow the user to select a username
  269. local selectedUser = selectUserFromList(users)
  270. if selectedUser then
  271. if insertSecurityCard(selectedUser) or checkCredentials(selectedUser) then
  272. drawMessage("Success", "Welcome back, " .. selectedUser .. "!")
  273. os.sleep(2)
  274. -- Proceed to the main Doggy OS interface
  275. shell.run("/disk/os/gui")
  276. else
  277. drawMessage("Access Denied", "Incorrect credentials or no security card found.")
  278. os.sleep(2)
  279. -- Proceed to lock the system
  280. shell.run("/disk/os/lock.lua")
  281. end
  282. end
  283. end
  284. end
  285.  
  286. main()
  287.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement