Advertisement
ComputerMan123

HouseCloud

Nov 1st, 2016
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- HouseCloud
  2. -- Made by houseofkraft
  3. -- Contributions by matimati433
  4. local sides = rs.getSides()
  5. local args = { ... }
  6. local loggedIn = false
  7.  
  8. -- Functions
  9. local function modemFound()
  10.   local modemFound, side
  11.   modemFound = false
  12.   for _,v in pairs(sides) do
  13.     if peripheral.getType(v) == "modem" then
  14.       modemFound = true
  15.       side = v
  16.     end
  17.   end
  18.   if modemFound then
  19.     return modemFound, side
  20.   else
  21.     return modemFound
  22.   end
  23. end    
  24.    
  25.  
  26. if #args < 1 then
  27.   -- Show usages
  28.   printError("Usages:")
  29.   printError("housecloud register <user>")
  30.   printError("housecloud login <user>")
  31.   printError("housecloud download")
  32.   printError("housecloud upload")
  33.   printError("housecloud logout")
  34.   printError("For the login and register, you will be asked for a password.")
  35.   error()
  36. end
  37.  
  38. -- Modem checker
  39. local found, side = modemFound()
  40. if found then
  41.   rednet.open(side)
  42. else
  43.   printError("No modems detected!")
  44.   error()
  45. end
  46.  
  47. if args[1] == "register" then
  48.   write("Password: ")
  49.   pass = read("*")
  50.   print("Registering Account...")
  51.   info = {args[2], pass}
  52.   rednet.broadcast("register", info)
  53.   id, message = rednet.receive(1)
  54.   if message == "sucessful" then
  55.     print("Sucessful!")
  56.   elseif message == "userexists" then
  57.     print("Error: User Exists!")
  58.   else
  59.     print("No response, There may be no servers nearby.")
  60.   end
  61. elseif args[1] == "login" then
  62.   write("Password: ")
  63.   pass = read("*")
  64.   print("Logging in...")
  65.   info = {args[2], pass}
  66.   rednet.broadcast("login", info)
  67.   id, message = rednet.receive(1)
  68.   if message == "sucessful" then
  69.     print("Sucessful!")
  70.     file = fs.open(".login", "w")
  71.     file.writeLine(args[2])
  72.     file.writeLine(pass)
  73.     file.close()  
  74.   elseif message == "usernoexist" then
  75.        print("Error: The user does not exist!")
  76.   elseif message == "passwrong" then
  77.        print("Error: Password Wrong!")
  78.     else
  79.        print("No reponse, There may be no servers nearby.")
  80.     end
  81. elseif args[1] == "download" then
  82.   if fs.exists(".login") then
  83.     file = fs.open(".login", "r")
  84.     user = file.readLine()
  85.     pass = file.readLine()
  86.     file.close()
  87.     print("Downloading...")
  88.     info = {user, pass}
  89.     rednet.broadcast("download", info)
  90.     fs.makeDir("cloud")
  91.     while true do
  92.       id, message, info = rednet.receive()
  93.       if message == "recieve" then
  94.         file = fs.open(fs.combine("cloud", info[1]), "w")
  95.         file.write(info[2])
  96.       elseif message == "stop" then
  97.         file.close()
  98.         break
  99.       elseif message == "badauth" then
  100.         print("Invaild Session, Try re-logging")
  101.         break
  102.       end
  103.     end
  104.   else
  105.     printError("You are not logged in!")
  106.   end
  107. elseif args[1] == "upload" then
  108.    if fs.exists(".login") then
  109.      tmp = fs.open(".login", "r")
  110.      local user = tmp.readLine()
  111.      local pass = tmp.readLine()
  112.      tmp.close()
  113.      local files = fs.list("/cloud/")
  114.      info = {user, pass}
  115.      rednet.broadcast("send-start", info)
  116.      for k,v in pairs(files) do
  117.        fi = fs.open(fs.combine("cloud", v), "r")
  118.        local content = fi.readAll()
  119.        fi.close()
  120.        info = {v, content}
  121.        rednet.broadcast("send", info)
  122.      end
  123.      rednet.broadcast("send-stop")
  124.      print("Done!")
  125.    else
  126.      printError("You are not logged in!")
  127.    end
  128. elseif args[1] == "logout" then
  129.     if fs.exists(".login") == false then
  130.       printError("You are not logged in!")
  131.       error()
  132.     else
  133.       fs.delete(".login")
  134.       print("Successfully Logged out.")
  135.     end
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement