Advertisement
DOGGYWOOF

Untitled

Jul 25th, 2024 (edited)
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. -- Function to draw the title bar with the red "X"
  2. local function drawTitleBar()
  3. local width, _ = term.getSize()
  4. term.setBackgroundColor(colors.blue)
  5. term.setTextColor(colors.white)
  6. term.setCursorPos(1, 1)
  7. term.write("Doggy OS Browser")
  8. term.setCursorPos(width, 1)
  9. term.write("X")
  10. term.setBackgroundColor(colors.black)
  11. term.setTextColor(colors.white)
  12. end
  13.  
  14. -- Function to check if a mouse click is on the red "X" in the title bar
  15. local function isCloseButtonClicked(x, y)
  16. local width, _ = term.getSize()
  17. return x == width and y == 1
  18. end
  19.  
  20. -- Function to wait for a mouse click event
  21. local function waitForClick()
  22. while true do
  23. local event, button, x, y = os.pullEvent("mouse_click")
  24. if isCloseButtonClicked(x, y) then
  25. return true
  26. end
  27. end
  28. end
  29.  
  30. -- Function to display an error message and wait
  31. local function displayError(message)
  32. term.clear()
  33. drawTitleBar()
  34. print("Error: " .. message)
  35. print("Press any key to return to the main menu.")
  36. os.pullEvent("key") -- Wait for any key press
  37. end
  38.  
  39. -- Function to get URL from user
  40. local function getURL()
  41. term.clear()
  42. drawTitleBar()
  43. print("Enter URL:")
  44. return read()
  45. end
  46.  
  47. -- Automatically find and open the first available modem
  48. local function openModem()
  49. local sides = {"top", "bottom", "left", "right", "front", "back"}
  50. for _, side in ipairs(sides) do
  51. if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
  52. rednet.open(side)
  53. return side
  54. end
  55. end
  56. displayError("No modem found.")
  57. return nil
  58. end
  59.  
  60. -- Load DNS Provider ID from file
  61. local function loadDNSProviderID()
  62. local filePath = "DNS-Provider.txt"
  63. if not fs.exists(filePath) then
  64. displayError("DNS-Provider.txt not found.")
  65. return nil
  66. end
  67. local file = fs.open(filePath, "r")
  68. local id = file.readAll()
  69. file.close()
  70. return tonumber(id)
  71. end
  72.  
  73. -- Save DNS Provider ID to file
  74. local function saveDNSProviderID(id)
  75. local filePath = "DNS-Provider.txt"
  76. local file = fs.open(filePath, "w")
  77. file.write(id)
  78. file.close()
  79. print("DNS Provider ID updated.")
  80. end
  81.  
  82. -- Connect to DNS and get IP address
  83. local function connectToDNS(url, dnsID)
  84. local modemSide = openModem()
  85. if not modemSide then return nil end
  86.  
  87. term.clear()
  88. drawTitleBar()
  89. print("Connecting to DNS...")
  90.  
  91. rednet.send(dnsID, url)
  92. local timer = os.startTimer(5)
  93. while true do
  94. local event, p1, p2 = os.pullEvent()
  95. if event == "rednet_message" and p1 == dnsID then
  96. rednet.close(modemSide)
  97. return p2
  98. elseif event == "timer" and p1 == timer then
  99. rednet.close(modemSide)
  100. displayError("DNS Timeout.")
  101. return nil
  102. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  103. rednet.close(modemSide)
  104. return nil
  105. end
  106. end
  107. end
  108.  
  109. -- Connect to webserver and get website code
  110. local function connectToWebserver(ip)
  111. local modemSide = openModem()
  112. if not modemSide then return nil end
  113.  
  114. term.clear()
  115. drawTitleBar()
  116. print("Connecting to Webserver...")
  117.  
  118. rednet.send(tonumber(ip), "GET")
  119. local timer = os.startTimer(5)
  120. while true do
  121. local event, p1, p2 = os.pullEvent()
  122. if event == "rednet_message" and p1 == tonumber(ip) then
  123. rednet.close(modemSide)
  124. return p2
  125. elseif event == "timer" and p1 == timer then
  126. rednet.close(modemSide)
  127. displayError("Connection timed out.")
  128. return nil
  129. elseif event == "mouse_click" and isCloseButtonClicked(p1, p2) then
  130. rednet.close(modemSide)
  131. return nil
  132. end
  133. end
  134. end
  135.  
  136. -- Execute website code and render it
  137. local function runWebsite(code)
  138. local func, err = load(code, "website", "t", _ENV)
  139. if func then
  140. term.clear()
  141. drawTitleBar()
  142. func() -- Run the website code
  143. -- Continuously check for user interaction (mouse clicks) to close the browser
  144. while not waitForClick() do
  145. -- Keep checking for click events to close the browser
  146. end
  147. term.clear()
  148. else
  149. displayError("Error loading website: " .. err)
  150. end
  151. end
  152.  
  153. -- Main menu function
  154. local function mainMenu()
  155. term.clear()
  156. drawTitleBar()
  157. print("Select an option:")
  158. print("1. Enter URL")
  159. print("2. Edit DNS Provider ID")
  160. print("3. Exit")
  161. local choice = tonumber(read())
  162.  
  163. if choice == 1 then
  164. -- Enter URL
  165. while true do
  166. local url = getURL()
  167. if url == nil then
  168. break
  169. end
  170.  
  171. local dnsID = loadDNSProviderID()
  172. if not dnsID then
  173. displayError("Failed to load DNS Provider ID.")
  174. return
  175. end
  176.  
  177. local ip = connectToDNS(url, dnsID)
  178. if ip == "404_NOT_FOUND" then
  179. displayError("Error 404: Webpage cannot be found!")
  180. return
  181. elseif not ip then
  182. displayError("Failed to connect to DNS.")
  183. return
  184. end
  185.  
  186. local code = connectToWebserver(ip)
  187. if not code then
  188. displayError("Failed to connect to Webserver.")
  189. return
  190. end
  191.  
  192. -- Write code to file and then delete it
  193. local tempFile = "received_code.lua"
  194. local tempFileHandle = fs.open(tempFile, "w")
  195. tempFileHandle.write(code)
  196. tempFileHandle.close()
  197.  
  198. -- Immediately delete the file after writing
  199. local success, err = pcall(function() fs.delete(tempFile) end)
  200. if success then
  201. print("Temporary file deleted successfully.")
  202. else
  203. displayError("Error deleting temporary file: " .. err)
  204. end
  205.  
  206. runWebsite(code)
  207. -- After running the website, return to the main menu
  208. term.clear()
  209. drawTitleBar()
  210. print("Returning to main menu...")
  211. os.sleep(2) -- Pause to allow the user to see the message
  212. break
  213. end
  214. elseif choice == 2 then
  215. -- Edit DNS Provider ID
  216. term.clear()
  217. drawTitleBar()
  218. print("Current DNS Provider ID: " .. (loadDNSProviderID() or "Not set"))
  219. print("Enter new DNS Provider ID:")
  220. local newID = tonumber(read())
  221. if newID then
  222. saveDNSProviderID(newID)
  223. else
  224. displayError("Invalid DNS Provider ID.")
  225. end
  226. elseif choice == 3 then
  227. print("Exiting...")
  228. return true -- Indicate exit
  229. else
  230. displayError("Invalid option. Please select 1, 2, or 3.")
  231. end
  232.  
  233. return false -- Continue showing menu
  234. end
  235.  
  236. -- Main execution loop
  237. while true do
  238. local exit = mainMenu()
  239. if exit then
  240. break -- Exit the loop if the user chose to exit
  241. end
  242. end
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement