Advertisement
Guest User

PhoneStartup.lua

a guest
Oct 18th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 KB | None | 0 0
  1. function setHost()
  2. modemState = rednet.isOpen(checkForModem_modem)
  3. if modemState ==  false or nil then
  4.     setHost_fail = true
  5. else
  6.  
  7. rednet.host(server1_protocol, server1_host)
  8. setHost_fail = false
  9. end
  10. end
  11.  
  12. function userCheck() --Logs in an user, then returns the username to be used as the hostname
  13. local isRegistered = fs.exists("/user/localuser.txt")
  14.     if isRegistered == false or nil then
  15.         fs.makeDir("/user/")
  16.         local userData = fs.open("/user/localuser", "w")
  17.             if userData == nil then userCheck_fail = true
  18.             else
  19.                 customMessage(nil, "Input Username")
  20.                 local newUsername = read('')
  21.                 print(newUsername)
  22.                 return newUsername
  23.             end
  24.     elseif isRegistered == true then
  25.         local userData = fs.open("/user/localuser.txt", "r")
  26.         local username = userData.readLine()
  27.         userData.close()
  28.         return username
  29.     end
  30. isRegistered.close()
  31. end
  32.  
  33. function logIn() --Fancy security measures
  34. local passwordRead = fs.open("/user/localpassword", "w")
  35.     local password = passwordRead.readLine()
  36.     passwordRead.close()
  37.         if password == nil then
  38.             local newPassword = read('*')
  39.             print(newPassword)
  40.             return true
  41.         else
  42.             inputPassword = read('*')
  43.                 if inputPassword == password then
  44.                     return true
  45.                 else
  46.             logIn_attempts = logIn_attempts + 1
  47.             logIn_retry()
  48.                 end
  49.         end
  50. end
  51.  
  52. function logIn_retry()
  53.        
  54.         local writeRetryTime = fs.open("/user/localpasswordretry", "w")
  55.         retryTime = writeRetryTime.readLine()
  56.         if retryTime == nil then
  57.             retryTime = 2
  58.         else
  59.             print(retryTime)
  60.             writeRetryTime.close   
  61.         end
  62.        
  63.     if logIn_attempts ~= 3 then
  64.         logIn_attempts = logIn_attempts + 1
  65.         customMessage(1, "Failed to Login. Try again. "..logIn_attempts.." / 3")
  66.         logIn()
  67.     else
  68.         if retryTime >= 51 then retryTime = 50 end
  69.         customMessage(1, "Failed to login. Too many attempts issued. waiting "..retryTime.." seconds and trying again")
  70.         os.sleep(retryTime)
  71.         retryTime = retryTime + retryTime
  72.         logIn()
  73.     end
  74.    
  75. end
  76.  
  77. function peerCheck(peerCheck_protocol, peerCheck_host, peerCheck_var) --Dynamically stores other devices (Sort of)
  78. local id = rednet.lookup(peerCheck_protocol, peerCheck_host)
  79. return id
  80. end
  81.  
  82. function checkForModem()
  83.     checkForModem_fail = false
  84.     checkForModem_modem = "back"
  85. end
  86.  
  87. function customMessage(mode, message)
  88. if aY == 20 then
  89. term.clear()
  90. term.setCursorPos(1, 1)
  91. end
  92. if mode == nil then mode = "System"
  93. elseif mode == 1 then mode = "Error"
  94. elseif mode == cmd0 then mode = "Ping" message = "Pings the Main Server"
  95. end
  96. aX, aY = term.getCursorPos()
  97. term.write("["..tostring(mode).."]: "..tostring(message))
  98. term.setCursorPos(1, aY+1)
  99. end
  100.  
  101. function errorHandle()
  102.  
  103. if setHost_fail == true then
  104. customMessage(1, "The function setHost() failed.")
  105. errorCount = errorCount + 1
  106. end
  107.  
  108. if checkForModem_fail == true then
  109. customMessage(1, "The function checkForModem() failed.")
  110. errorCount = errorCount + 1
  111. end
  112.  
  113. if userCheck_fail == true then
  114. customMessage(1, "The function userCheck() failed.")
  115. errorCount = errorCount + 1
  116. end
  117.  
  118. end
  119.  
  120. --[[Action Functions]]--
  121.  
  122. function issueCommand(command)
  123.     rednet.send(houseServer, command)
  124.     local id, message, protocol = rednet.receive(protocolLv1, 20)
  125.         if id or message or protocol not nil then
  126.             return message, true
  127.         else
  128.             return nil, false
  129.         end
  130. end
  131.  
  132. function inputCommand()
  133.     customMessage(nil, "Available commands are: ")
  134.     customMessage(cmd0, nil)
  135.     customMessage(nil, "Input a command")
  136.     local command = read('')
  137.     return command
  138. end
  139.  
  140. function act()
  141.     local command = inputCommand()
  142.     local reply, success = issueCommand(command)
  143.         if reply == nil then
  144.             customMessage(1, "Success = "..tostring(success))
  145.         end
  146. end
  147. --[[Action Functions end]]--
  148.  
  149. if setHost_fail or checkForModem_fail == true then
  150. startup_fail = true
  151. end
  152.  
  153. function startup()
  154.     -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
  155.         protocolLv1 = "alpha"
  156.         protocolLv2 = "beta"
  157.         protocolLv3 = "gamma"
  158.         protocolLv4 = "delta"
  159.     -- end
  160.  
  161.     -- local host settings MUST BE SECOND ROW IN EVERY startup() FUNCTION
  162.         server1_protocol = protocolLv2
  163.         server1_host = tostring(userCheck())
  164.     -- end
  165.  
  166.     -- static hosts settings MUST BE THIRD ROW IN EVERY startup() FUNCTION
  167.         houseServer = rednet.lookup(protocolLv1,"houseServer")
  168.             if houseServer == nil then
  169.             houseServer_fail = true
  170.             end
  171.     -- end
  172.  
  173.     -- global variables MUST BE FOURTH ROW IN EVERY startup() FUNCTION
  174.         errorCount = 0
  175.         checkForCommand_attempts = 0
  176.         logIn_attempts = 0
  177.     -- end
  178.  
  179.     --peripherals
  180.         term.clear()
  181.         term.setCursorPos(1,1)
  182.         checkForModem()
  183.         rednet.open(checkForModem_modem)
  184.     -- end
  185.  
  186.     errorHandle()
  187.         if errorCount == 1 then
  188.         customMessage(nil, "Succesfully started with "..errorCount.." error.")
  189.     elseif errorCount == 0 then
  190.         customMessage(nil, "Succesfully started with no errors")
  191.     elseif errorCount >= 2 then
  192.         customMessage(nil, "Succesfully started with "..errorCount.." errors.")
  193.         end
  194.  
  195.         loggedIn = logIn()
  196.         if loggedIn == false or nil then
  197.         print("You're not logged in. Shutting Down...")
  198.         os.shutdown()
  199.         end
  200. act()
  201.  
  202. end
  203.  
  204. if startup_fail == true then errorCount = errorCount + 1
  205.     else startup()
  206.     end
  207. end
  208.  
  209. os.shutdown()
  210.  
  211. --DiegoG
  212. --Base Phone OS A.1.1.0
  213. --13/10/2016
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement