Advertisement
Guest User

startup

a guest
Nov 13th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. --functions
  2. function clear()
  3.   term.clear()
  4.   term.setCursorPos(1,1)
  5. end
  6.  
  7. function Init()
  8.   print("Initializing...")
  9.   --open rednet
  10.   if fs.exists(".rnconfig") == false then
  11.     local file = fs.open(".rnconfig","w")
  12.     file.write("back")
  13.     file.close()
  14.     print(".rnconfig file generated")
  15.   end
  16.   local config = fs.open(".rnconfig","r").readAll()
  17.   rednet.open(tostring(config))
  18.   if rednet.isOpen() == false then
  19.     print("Could Not Open Rednet, Check .rnconfig")
  20.   else
  21.     print("Rednet Opened On Channel "..os.computerID())
  22.     sleep(1)
  23.   end
  24.   --end rn check
  25.   -- user database
  26.   print("Loading Username Database")
  27.   if fs.exists(".users") == false then
  28.     local unames = fs.open(".users","w")
  29.     unames.write('{"admin"}')
  30.     unames.close()
  31.     print("Creating User Database")
  32.   else
  33.     print("User Database Found")
  34.     sleep(1)
  35.   end
  36.   --end user database check
  37.   -- password database check
  38.   print("Loading Password Database")
  39.   if fs.exists(".users") == false then
  40.     local passwds = fs.open(".passwords","w")
  41.     passwds.write('{"admin", "password"}')
  42.     passwds.close()
  43.     print("Creating Password Database")
  44.   else
  45.   sleep(1)
  46.   print("Password Database Found")
  47.   end  
  48.   --end password check
  49. end
  50.  
  51. function load(name)
  52.   local f = fs.open(name, "r")
  53.   local data = f.readAll()
  54.   f.close()
  55.   return textutils.unserialise(data)
  56. end
  57.  
  58. function Host()
  59.   while true do
  60.     local client,message,protocol = rednet.receive()
  61.     print(client..": "..message.." : "..protocol)
  62.     local num = tonumber(client)
  63.     local i = tonumber(UID)
  64.     --UID
  65.     if protocol == "UID" then
  66.       UID = message
  67.       print("UID set to :"..message)
  68.       print(client)
  69.       sleep(1)
  70.       rednet.send(num, "confirm")
  71.     end
  72.     --USERNAME
  73.     if protocol == "username" then
  74.        
  75.         if message == UserData[i] then
  76.          print("Username: "..message)
  77.          rednet.send(num,"confirm")
  78.         else
  79.           print("Username Rejected")
  80.           rednet.send(num, "denied")
  81.         end
  82.     end
  83.     --PASSWORD
  84.     if protocol == "password" then
  85.       if message == PassData[i] then
  86.         print("Password: "..message)
  87.         rednet.send(num, "confirm")
  88.       else
  89.         print("Password Denied")
  90.         rednet.send(num, "denied")
  91.       end
  92.     end
  93.   end
  94. end
  95.  
  96. --Start
  97. Init()
  98. print("Init Complete!")
  99. sleep(1)
  100. clear()
  101. UserData = load(".users")
  102. print("Loaded User Database!")
  103. sleep(1)
  104. PassData = load(".passwords")
  105. print("Loaded Password Database")
  106. sleep(1)
  107. clear()
  108. print("Hosting...")
  109. local UID = 1
  110. sleep(1)
  111. clear()
  112. Host()
  113.  
  114. --test
  115. --print("Test:")
  116. --print(UserData[2])
  117. --print(PassData[2])
  118. --sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement