Advertisement
Guest User

housecloud

a guest
Nov 1st, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  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("For the login and register, you will be asked for a password.")
  34.   error()
  35. end
  36.  
  37. -- Modem checker
  38. local found, side = modemFound()
  39. if found then
  40.   rednet.open(side)
  41. else
  42.   printError("No modems detected!")
  43.   error()
  44. end
  45.  
  46. if args[1] == "register" then
  47.   write("Password: ")
  48.   pass = read("*")
  49.   print("Registering Account...")
  50.   info = {args[2], pass}
  51.   rednet.broadcast("register", info)
  52.   id, message = rednet.receive(1)
  53.   if message == "sucessful" then
  54.     print("Sucessful!")
  55.   elseif message == "userexists" then
  56.     print("Error: User Exists!")
  57.   else
  58.     print("No response, There may be no servers nearby.")
  59.   end
  60. elseif args[1] == "login" then
  61.   write("Password: ")
  62.   pass = read("*")
  63.   print("Logging in...")
  64.   info = {args[2], pass}
  65.   rednet.broadcast("login", info)
  66.   id, message = rednet.receive(1)
  67.   if message == "sucessful" then
  68.     print("Sucessful!")
  69.     file = fs.open(".login", "w")
  70.     file.writeLine(args[2])
  71.     file.writeLine(pass)
  72.     file.close()  
  73.   elseif message == "usernoexist" then
  74.        print("Error: The user does not exist!")
  75.   elseif message == "passwrong" then
  76.        print("Error: Password Wrong!")
  77.     else
  78.        print("No reponse, There may be no servers nearby.")
  79.     end
  80. elseif args[1] == "download" then
  81.   if fs.exists(".login") then
  82.     file = fs.open(".login", "r")
  83.     user = file.readLine()
  84.     pass = file.readLine()
  85.     file.close()
  86.     print("Downloading...")
  87.     info = {user, pass}
  88.     rednet.broadcast("download", info)
  89.     fs.makeDir("cloud")
  90.     while true do
  91.       id, message, info = rednet.receive()
  92.       if message == "recieve" then
  93.         file = fs.open(fs.combine("cloud", info[1]), "w")
  94.         file.write(info[2])
  95.       elseif message == "stop" then
  96.         file.close()
  97.         break
  98.       elseif message == "badauth" then
  99.         print("Invaild Session, Try re-logging")
  100.         break
  101.       end
  102.     end
  103.   else
  104.     printError("You are not logged in!")
  105.   end
  106. elseif args[1] == "upload" then
  107.    if fs.exists(".login") then
  108.      tmp = fs.open(".login", "r")
  109.      local user = tmp.readLine()
  110.      local pass = tmp.readLine()
  111.      tmp.close()
  112.      local files = fs.list("/cloud/")
  113.      info = {user, pass}
  114.      rednet.broadcast("send-start", info)
  115.      for k,v in pairs(files) do
  116.        fi = fs.open(fs.combine("cloud", v), "r")
  117.        local content = fi.readAll()
  118.        fi.close()
  119.        info = {v, content}
  120.        rednet.broadcast("send", info)
  121.      end
  122.      rednet.broadcast("send-stop")
  123.      print("Done!")
  124.    else
  125.      printError("You are not logged in!")
  126.    end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement