Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setHost()
- modemState = rednet.isOpen(checkForModem_modem)
- if modemState == false or nil then
- setHost_fail = true
- else
- rednet.host(server1_protocol, server1_host)
- setHost_fail = false
- end
- end
- function userCheck() --Logs in an user, then returns the username to be used as the hostname
- local isRegistered = fs.exists("/user/localuser.txt")
- if isRegistered == false or nil then
- fs.makeDir("/user/")
- local userData = fs.open("/user/localuser", "w")
- if userData == nil then userCheck_fail = true
- else
- customMessage(nil, "Input Username")
- local newUsername = read('')
- print(newUsername)
- return newUsername
- end
- elseif isRegistered == true then
- local userData = fs.open("/user/localuser.txt", "r")
- local username = userData.readLine()
- userData.close()
- return username
- end
- isRegistered.close()
- end
- function logIn() --Fancy security measures
- local passwordRead = fs.open("/user/localpassword", "w")
- local password = passwordRead.readLine()
- passwordRead.close()
- if password == nil then
- local newPassword = read('*')
- print(newPassword)
- return true
- else
- inputPassword = read('*')
- if inputPassword == password then
- return true
- else
- logIn_attempts = logIn_attempts + 1
- logIn_retry()
- end
- end
- end
- function logIn_retry()
- local writeRetryTime = fs.open("/user/localpasswordretry", "w")
- retryTime = writeRetryTime.readLine()
- if retryTime == nil then
- retryTime = 2
- else
- print(retryTime)
- writeRetryTime.close
- end
- if logIn_attempts ~= 3 then
- logIn_attempts = logIn_attempts + 1
- customMessage(1, "Failed to Login. Try again. "..logIn_attempts.." / 3")
- logIn()
- else
- if retryTime >= 51 then retryTime = 50 end
- customMessage(1, "Failed to login. Too many attempts issued. waiting "..retryTime.." seconds and trying again")
- os.sleep(retryTime)
- retryTime = retryTime + retryTime
- logIn()
- end
- end
- function peerCheck(peerCheck_protocol, peerCheck_host, peerCheck_var) --Dynamically stores other devices (Sort of)
- local id = rednet.lookup(peerCheck_protocol, peerCheck_host)
- return id
- end
- function checkForModem()
- checkForModem_fail = false
- checkForModem_modem = "back"
- end
- function customMessage(mode, message)
- if aY == 20 then
- term.clear()
- term.setCursorPos(1, 1)
- end
- if mode == nil then mode = "System"
- elseif mode == 1 then mode = "Error"
- elseif mode == cmd0 then mode = "Ping" message = "Pings the Main Server"
- end
- aX, aY = term.getCursorPos()
- term.write("["..tostring(mode).."]: "..tostring(message))
- term.setCursorPos(1, aY+1)
- end
- function errorHandle()
- if setHost_fail == true then
- customMessage(1, "The function setHost() failed.")
- errorCount = errorCount + 1
- end
- if checkForModem_fail == true then
- customMessage(1, "The function checkForModem() failed.")
- errorCount = errorCount + 1
- end
- if userCheck_fail == true then
- customMessage(1, "The function userCheck() failed.")
- errorCount = errorCount + 1
- end
- end
- --[[Action Functions]]--
- function issueCommand(command)
- rednet.send(houseServer, command)
- local id, message, protocol = rednet.receive(protocolLv1, 20)
- if id or message or protocol not nil then
- return message, true
- else
- return nil, false
- end
- end
- function inputCommand()
- customMessage(nil, "Available commands are: ")
- customMessage(cmd0, nil)
- customMessage(nil, "Input a command")
- local command = read('')
- return command
- end
- function act()
- local command = inputCommand()
- local reply, success = issueCommand(command)
- if reply == nil then
- customMessage(1, "Success = "..tostring(success))
- end
- end
- --[[Action Functions end]]--
- if setHost_fail or checkForModem_fail == true then
- startup_fail = true
- end
- function startup()
- -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
- protocolLv1 = "alpha"
- protocolLv2 = "beta"
- protocolLv3 = "gamma"
- protocolLv4 = "delta"
- -- end
- -- local host settings MUST BE SECOND ROW IN EVERY startup() FUNCTION
- server1_protocol = protocolLv2
- server1_host = tostring(userCheck())
- -- end
- -- static hosts settings MUST BE THIRD ROW IN EVERY startup() FUNCTION
- houseServer = rednet.lookup(protocolLv1,"houseServer")
- if houseServer == nil then
- houseServer_fail = true
- end
- -- end
- -- global variables MUST BE FOURTH ROW IN EVERY startup() FUNCTION
- errorCount = 0
- checkForCommand_attempts = 0
- logIn_attempts = 0
- -- end
- --peripherals
- term.clear()
- term.setCursorPos(1,1)
- checkForModem()
- rednet.open(checkForModem_modem)
- -- end
- errorHandle()
- if errorCount == 1 then
- customMessage(nil, "Succesfully started with "..errorCount.." error.")
- elseif errorCount == 0 then
- customMessage(nil, "Succesfully started with no errors")
- elseif errorCount >= 2 then
- customMessage(nil, "Succesfully started with "..errorCount.." errors.")
- end
- loggedIn = logIn()
- if loggedIn == false or nil then
- print("You're not logged in. Shutting Down...")
- os.shutdown()
- end
- act()
- end
- if startup_fail == true then errorCount = errorCount + 1
- else startup()
- end
- end
- os.shutdown()
- --DiegoG
- --Base Phone OS A.1.1.0
- --13/10/2016
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement