Advertisement
DOGGYWOOF

Untitled

Dec 22nd, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.73 KB | None | 0 0
  1. -- Function to clear the terminal and set the cursor position
  2. local function clearScreen()
  3. term.clear()
  4. term.setCursorPos(1, 1)
  5. end
  6.  
  7. -- Function to display centered ASCII art
  8. local function displayCenteredArt(art, text)
  9. clearScreen()
  10. local lines = {}
  11. for line in art:gmatch("[^\n]+") do
  12. table.insert(lines, line)
  13. end
  14.  
  15. local _, h = term.getSize()
  16. local startY = math.floor((h - #lines) / 2) + 1
  17. for i, line in ipairs(lines) do
  18. local w, _ = term.getSize()
  19. local startX = math.floor((w - #line) / 2) + 1
  20. term.setCursorPos(startX, startY + i - 1)
  21. print(line)
  22. end
  23.  
  24. if text then
  25. local textX = math.floor((term.getSize() - #text) / 2) + 1
  26. term.setCursorPos(textX, startY + #lines + 1)
  27. print(text)
  28. end
  29. end
  30.  
  31. -- Function to draw a menu system
  32. local function drawMenu(title, options)
  33. clearScreen()
  34. local w, h = term.getSize()
  35. local boxWidth = 40
  36. local boxHeight = #options + 6
  37. local startX = math.floor((w - boxWidth) / 2) + 1
  38. local startY = math.floor((h - boxHeight) / 2) + 1
  39.  
  40. -- Draw menu background and header
  41. paintutils.drawBox(startX, startY, startX + boxWidth - 1, startY + boxHeight - 1, colors.blue)
  42. paintutils.drawFilledBox(startX + 1, startY + 1, startX + boxWidth - 2, startY + boxHeight - 2, colors.lightBlue)
  43. term.setTextColor(colors.white)
  44. term.setCursorPos(startX + 2, startY + 1)
  45. print(title)
  46.  
  47. -- Draw options
  48. for i, option in ipairs(options) do
  49. term.setCursorPos(startX + 2, startY + 2 + i)
  50. print(i .. ". " .. option)
  51. end
  52. end
  53.  
  54. -- Function to handle menu selection
  55. local function menuSelection(title, options)
  56. drawMenu(title, options)
  57. local w, h = term.getSize()
  58. local startX = math.floor((w - 40) / 2) + 2
  59. local startY = math.floor((h - (#options + 6)) / 2) + #options + 5
  60. term.setCursorPos(startX, startY)
  61. term.write("Select an option: ")
  62. local choice = tonumber(read())
  63. if choice and choice > 0 and choice <= #options then
  64. return choice
  65. else
  66. return nil
  67. end
  68. end
  69.  
  70.  
  71. local MAX_ATTEMPTS = 3 -- Maximum number of incorrect password attempts allowed
  72. local LOCKOUT_TIME = 30 -- Lockout time in seconds after reaching maximum attempts
  73.  
  74. local USERS_FOLDER = "/disk/users/"
  75. local ERROR_FOLDER = "/disk/error/"
  76. local BSOD_PROGRAM = "BSOD.lua"
  77. local CURRENT_USER_FILE = ".currentusr"
  78. local SHOW_ALL_USERS_FILE = "/disk/config/security/login/ShowAllUsers.cfg"
  79.  
  80. -- Utility function to draw a centered popup window with a text-based border
  81. local function drawPopupWindow(headerText, contentLines, windowWidth, windowHeight)
  82. term.clear() -- Clear the screen before drawing the popup window
  83.  
  84. local w, h = term.getSize()
  85. local maxLength = #headerText
  86. for _, line in ipairs(contentLines) do
  87. maxLength = math.max(maxLength, #line)
  88. end
  89.  
  90. -- Calculate the window dimensions if not provided
  91. windowWidth = windowWidth or (maxLength + 4)
  92. windowHeight = windowHeight or (#contentLines + 4)
  93.  
  94. local xStart = math.floor(w / 2 - windowWidth / 2)
  95. local yStart = math.floor(h / 2 - windowHeight / 2)
  96.  
  97. -- Draw border
  98. term.setCursorPos(xStart, yStart)
  99. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  100. for i = 1, windowHeight - 2 do
  101. term.setCursorPos(xStart, yStart + i)
  102. term.write("|" .. string.rep(" ", windowWidth - 2) .. "|")
  103. end
  104. term.setCursorPos(xStart, yStart + windowHeight - 1)
  105. term.write("+" .. string.rep("-", windowWidth - 2) .. "+")
  106.  
  107. -- Draw header
  108. term.setCursorPos(xStart + 2, yStart + 1)
  109. term.write(headerText)
  110.  
  111. -- Draw content
  112. for i, line in ipairs(contentLines) do
  113. term.setCursorPos(xStart + 2, yStart + 1 + i + 1)
  114. term.write(line)
  115. end
  116. end
  117.  
  118. -- Function to list all users
  119. local function listUsers()
  120. local users = fs.list(USERS_FOLDER)
  121. local usernames = {}
  122. for _, user in ipairs(users) do
  123. local userDir = fs.combine(USERS_FOLDER, user)
  124. if fs.isDir(userDir) then
  125. table.insert(usernames, user)
  126. end
  127. end
  128. return usernames
  129. end
  130.  
  131. -- Function to handle user selection from the list
  132. local function drawUsersScreen(usernames, selectedIndex)
  133. local w, h = term.getSize()
  134. local contentLines = {}
  135. for i, username in ipairs(usernames) do
  136. if i == selectedIndex then
  137. table.insert(contentLines, "> " .. username)
  138. else
  139. table.insert(contentLines, " " .. username)
  140. end
  141. end
  142.  
  143. -- Increase the border size for a larger window
  144. drawPopupWindow("Select a User", contentLines, 30, #contentLines + 4)
  145. term.setCursorPos(1, h) -- Move cursor out of the way after drawing
  146. end
  147.  
  148. -- Function to handle arrow key navigation and selection
  149. local function selectUserFromList()
  150. local usernames = listUsers()
  151. local selectedIndex = 1
  152.  
  153. while true do
  154. drawUsersScreen(usernames, selectedIndex)
  155.  
  156. local event, key = os.pullEvent("key")
  157. if key == keys.up then
  158. selectedIndex = math.max(1, selectedIndex - 1)
  159. elseif key == keys.down then
  160. selectedIndex = math.min(#usernames, selectedIndex + 1)
  161. elseif key == keys.enter then
  162. return usernames[selectedIndex]
  163. end
  164. end
  165. end
  166.  
  167. -- Function to get user credentials
  168. local function getUserCredentials(username)
  169. local passwordFile = fs.combine(USERS_FOLDER .. username, "password.txt")
  170.  
  171. if fs.exists(passwordFile) then
  172. local file = fs.open(passwordFile, "r")
  173. local storedPassword = file.readLine()
  174. file.close()
  175. return storedPassword
  176. else
  177. return nil -- User does not exist
  178. end
  179. end
  180.  
  181. -- Function to draw login screen
  182. local function drawLoginScreen(username, attemptsLeft)
  183. local contentLines = {
  184. "Username: " .. username,
  185. "Attempts left: " .. attemptsLeft,
  186. "",
  187. "Enter password:"
  188. }
  189. drawPopupWindow("Login to Doggy OS", contentLines)
  190. term.setCursorPos(math.floor(term.getSize() / 2) - 2, math.floor(term.getSize() / 2) + 1) -- Position cursor for password input
  191. end
  192.  
  193. -- Function to lockout user after failed attempts
  194. local function lockoutUser(username)
  195. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  196. local file = fs.open(disabledFile, "w")
  197. file.close()
  198. end
  199.  
  200. -- Function to check if user is disabled
  201. local function checkDisabled(username)
  202. local disabledFile = fs.combine(USERS_FOLDER .. username, "disabled.txt")
  203. return fs.exists(disabledFile)
  204. end
  205.  
  206. -- Function to save the current user
  207. local function saveCurrentUser(username)
  208. if fs.exists(CURRENT_USER_FILE) then
  209. fs.delete(CURRENT_USER_FILE) -- Delete existing file
  210. end
  211. local file = fs.open(CURRENT_USER_FILE, "w")
  212. file.write(username)
  213. file.close()
  214. end
  215.  
  216. -- Function to check credentials and login
  217. local function checkCredentials(username)
  218. if checkDisabled(username) then
  219. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons.", "", "Contact your administrator for help."})
  220. os.sleep(5) -- Display the disabled message for 5 seconds
  221. shell.run("/disk/os/lock.lua") -- Run the lock.lua program
  222. return false
  223. end
  224.  
  225. local storedPassword = getUserCredentials(username)
  226.  
  227. if not storedPassword then
  228. return false -- User does not exist
  229. end
  230.  
  231. local attempts = 0
  232.  
  233. repeat
  234. drawLoginScreen(username, MAX_ATTEMPTS - attempts)
  235.  
  236. local enteredPassword = read("*")
  237. attempts = attempts + 1
  238.  
  239. if enteredPassword == storedPassword then
  240. saveCurrentUser(username) -- Save the logged-in user
  241. return true
  242. else
  243. drawPopupWindow("Access Denied", {"Incorrect password. Please try again."})
  244. os.sleep(2) -- Display the error message for 2 seconds
  245. end
  246. until attempts > MAX_ATTEMPTS
  247.  
  248. drawPopupWindow("Account Disabled", {"Too many incorrect attempts. User has been disabled."})
  249. lockoutUser(username)
  250. os.sleep(2) -- Display the lockout message for 2 seconds
  251.  
  252. return false
  253. end
  254.  
  255. -- Function to handle user login process
  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. -- If the ShowAllUsers.cfg file exists, show all users
  263. if fs.exists(SHOW_ALL_USERS_FILE) then
  264. local username = selectUserFromList()
  265. if checkCredentials(username) then
  266. drawPopupWindow("Welcome", {"Access granted. Welcome, " .. username .. "!"})
  267. os.sleep(2) -- Display the success message for 2 seconds
  268. shell.run("/disk/os/gui")
  269. else
  270. drawPopupWindow("Access Denied", {"Incorrect credentials."})
  271. os.sleep(2) -- Display the access denied message for 2 seconds
  272. shell.run("/disk/os/lock.lua")
  273. end
  274. else
  275. drawPopupWindow("Protected by Doggy OS Security", {"Enter username:"})
  276. local enteredUsername = read()
  277.  
  278. if checkDisabled(enteredUsername) then
  279. drawPopupWindow("Access Denied", {"This user has been disabled due to security reasons."})
  280. os.sleep(5)
  281. shell.run("/disk/os/lock.lua")
  282. elseif checkCredentials(enteredUsername) then
  283. drawPopupWindow("Welcome", {"Access granted. Welcome, " .. enteredUsername .. "!"})
  284. os.sleep(2)
  285. shell.run("/disk/os/gui")
  286. else
  287. drawPopupWindow("Access Denied", {"Incorrect credentials."})
  288. os.sleep(2)
  289. shell.run("/disk/os/lock.lua")
  290. end
  291. end
  292. end
  293.  
  294. -- Start the login process
  295. main()
  296.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement