Rynano

Door Host

Feb 10th, 2021 (edited)
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. local m_FolderName = "."..shell.getRunningProgram()
  2.  
  3. -- Interaction code
  4. function AskForInput(msg)
  5.     write(msg)
  6.     l_Input = read()
  7.     return l_Input
  8. end
  9. -- Data stuff
  10. function Store(Data, Filename)
  11.     l_File = fs.open(m_FolderName.."/"..Filename, "w")
  12.     l_File.write(Data)
  13.     l_File.close()
  14. end
  15.  
  16. function Read(Filename)
  17.     l_File = fs.open(m_FolderName.."/"..Filename, "r")
  18.     l_ReturnText = l_File.readAll()
  19.     l_File.close()
  20.     return l_ReturnText
  21. end
  22.  
  23. function Exists(Filename)
  24.     return fs.exists(m_FolderName.."/"..Filename)
  25. end
  26. -- Screen Visuals
  27. function ResetCursor()
  28.     term.setCursorPos(1,1)
  29. end
  30.  
  31. function DisplayInfo(Message)
  32.     term.clear()
  33.     ResetCursor()
  34.     write(Message)
  35. end
  36. -- Computer Configuration at Startup
  37. function ConfigureRouter()
  38.     l_Value = AskForInput("Enter the router side:")
  39.     Store(l_Value, "rtsd")
  40.     l_Value = AskForInput("Enter the redstone side:")
  41.     Store(l_Value, "rssd")
  42. end
  43.  
  44. function ConfigureComputer()
  45.     l_Value = AskForInput("Enter the REMOTE computer ID: ")
  46.     Store(l_Value, "rmid")
  47.     l_Value = AskForInput("Enter the PASSWORD to unlock the REMOTE computer.")
  48.     Store(l_Value, "rmpw")
  49. end
  50.  
  51. function AutoSetup() --Autosetups the user and the password upon first execution.
  52.     rmid, rmpw = rednet.receive()
  53.    
  54. end
  55. -- Main Iteration
  56. function CheckForMessages()
  57.     rmid, rmpw = rednet.receive()
  58.     return IsUser(rmpw, rmid)
  59. end
  60. -- Security??? stuff?
  61. function IsUser(PW, ID)
  62.     rmid = tonumber(Read("rmid"))
  63.     rmpw = Read("rmpw")
  64.     return rmid == ID and rmpw == PW
  65. end
  66. -- Small Code.
  67. function ToggleRedstone(Enabled)
  68.     redstone.setOutput(Read("rssd"), Enabled)
  69. end
  70.  
  71. -- Start of main program.
  72. function main()
  73.     m_Running = true
  74.     if not fs.exists(m_FolderName) then
  75.         ConfigureRouter()
  76.         ConfigureComputer()
  77.         DisplayInfo("All set!")
  78.         sleep(2)
  79.         os.reboot()
  80.     else if not Exists("rmid") then --Do the autosetup if it hasnt been done before.
  81.         while(not Exists("rmid")) do
  82.             AutoSetup()
  83.         end
  84.         DisplayInfo("All setup!")
  85.         read()
  86.     else -- Enter here when its all setup
  87.         local l_Router = Read("rtsd")
  88.         rednet.open(l_Router)
  89.         while m_Running do
  90.             DisplayInfo("Running!")
  91.             if CheckForMessages() then
  92.                 DisplayInfo("User Confirmed!")
  93.                 ToggleRedstone(true)
  94.                 sleep(3)
  95.                 ToggleRedstone(false)            
  96.                 end
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. -- Actual Code Executing ---
  103. local no_error, errorMSG = pcall(main)
  104.  
  105. if not no_error then
  106.   DisplayInfo(errorMSG)
  107. end
  108. -- Pretty clean huh? -------
Advertisement
Add Comment
Please, Sign In to add comment