Advertisement
Alakazard12

Cloud client

Nov 11th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. -- [[ CREATED BY ALAKAZARD12 ]] --
  2.  
  3. --[[
  4.     EDITTING THIS PROGRAM IN ANY WAY IS SCRITLY PROHIBITED
  5.     CREDITS: ALAKAZARD12
  6. ]]  
  7.  
  8. ---------- [[ Script ]] ----------
  9.  
  10. local stx, sty = term.getSize()
  11.  
  12. function clear()
  13.     term.clear()
  14.     term.setCursorPos(1, 1)
  15.     term.setTextColor(1)
  16. end
  17.  
  18. function ports()
  19.     rednet.open("back")
  20.     rednet.open("top")
  21.     rednet.open("front")
  22.     rednet.open("bottom")
  23.     rednet.open("left")
  24.     rednet.open("right")
  25. end
  26.  
  27. server = 0
  28.  
  29. function scan(user, path, prog)
  30.     local hasp = "/"
  31.     for i,v in pairs(sep(path, "/")) do
  32.         if not fs.exists("/files/"..tostring(user)..hasp..v) then
  33.             if i == #sep(path, "/") and prog then
  34.                 if #prog > 100000 then
  35.                     print(path.." is to big!")
  36.                 else
  37.                     pcall(function()
  38.                         print("Creating \"/files/"..tostring(user)..hasp..v.."\"")
  39.                         local newf = fs.open("/files/"..tostring(user)..hasp..v, "w")
  40.                         newf.write(prog)
  41.                     end)
  42.                    
  43.                 end
  44.             else
  45.                 print("Creating \"/files/"..tostring(user)..hasp..v.."\"")
  46.                 fs.makeDir("/files/"..tostring(user)..hasp..v)
  47.             end
  48.         end
  49.         hasp = hasp..v.."/"
  50.     end
  51. end
  52.  
  53. function sep(list, by)
  54.     local newlist = {}
  55.     while true do
  56.         local on = nil
  57.         local done = false
  58.         for i = 1, #list do
  59.             if done == false then
  60.                 if string.sub(list, i, i) == by then
  61.                     on = i
  62.                     done = true
  63.                 end
  64.             end
  65.         end
  66.         if on then
  67.             table.insert(newlist, string.sub(list, 1, on - 1))
  68.             list = string.sub(list, on + 1)
  69.         else
  70.             break
  71.         end
  72.     end
  73.     table.insert(newlist, list)
  74.     return newlist
  75. end
  76.  
  77. function rec()
  78.     if not fs.exists("/files") then
  79.         fs.makeDir("/files")
  80.     end
  81.     local id, msg = rednet.receive()
  82.     pcall(function()
  83.         msg = textutils.unserialize(msg)
  84.     end)
  85.     if id == server then
  86.         local hasp = "/"
  87.         for i,v in pairs(sep(msg[1], "/")) do
  88.             if not fs.exists("/files"..hasp..v) then
  89.                 if i == #sep(msg[1], "/") and msg[2] then
  90.                     if #msg[1] > 100000 then
  91.                         print(msg[1].." is to big!")
  92.                     else
  93.                         print("Creating \"/files"..hasp..v.."\"")
  94.                         local newf = fs.open("/files"..hasp..v, "w")
  95.                         newf.write(msg[1])
  96.                     end
  97.                 else
  98.                     print("Creating \"/files"..hasp..v.."\"")
  99.                     fs.makeDir("/files"..hasp..v)
  100.                 end
  101.             end
  102.             hasp = hasp..v.."/"
  103.         end
  104.     end
  105.     rec()
  106. end
  107.  
  108. function send(path)
  109.     sleep(0.05)
  110.     if fs.isDir(path) then
  111.         print("Sending "..path)
  112.         rednet.send(server, textutils.serialize({"CLOUD:UP:"..path}))
  113.         for i,v in pairs(fs.list(path)) do
  114.             send(path.."/"..v)
  115.         end
  116.     else
  117.         print("Sending "..path)
  118.         pcall(function()
  119.             local fl = fs.open(path, "r")
  120.             local rd = fl.readAll()
  121.             fl.close()
  122.             rednet.send(server, textutils.serialize({"CLOUD:UP:"..path, rd}))
  123.         end)
  124.     end
  125. end
  126.  
  127. function drawlist(title, listr, num)
  128.         clear()
  129.         term.setTextColor(colors.lime)
  130.         local list = {}
  131.         print(title.."\n")
  132.         for i,v in pairs(listr) do
  133.                 list[i] = v
  134.         end
  135.         local start = 1
  136.         local last = #list
  137.         if num > sty - 4 then
  138.                 start = num - (sty - 4)
  139.         end
  140.         local cnt = 0
  141.         for i = start, #list do
  142.                 if cnt > sty - 4 then
  143.                         last = last - 1
  144.                         cnt = cnt - 1
  145.                 end
  146.                 cnt = cnt + 1
  147.         end
  148.         for m = start, last do
  149.                 local tor = true
  150.                 if tor == true then
  151.                         if m == num then
  152.                                 term.setTextColor(colors.white)
  153.                                 write("[")
  154.                                 term.setTextColor(colors.lime)
  155.                                 write("-")
  156.                                 term.setTextColor(colors.white)
  157.                                 write("]")
  158.                                 term.setTextColor(colors.lime)
  159.                                 print(" "..list[m])
  160.                         else
  161.                                 term.setTextColor(colors.white)
  162.                                 print("[ ] "..list[m])
  163.                         end
  164.                 end
  165.         end
  166. end
  167.  
  168. function getKey()
  169.     local rt = nil
  170.     function lop()
  171.         local e, a1 = os.pullEvent("key")
  172.         if a1 == 200 then
  173.             rt = "up"
  174.         elseif a1 == 208 then
  175.             rt = "down"
  176.         elseif a1 == 28 then
  177.             rt = "enter"
  178.         else
  179.             lop()
  180.         end
  181.     end
  182.     lop()
  183.     repeat sleep(0) until rt ~= nil
  184.     return rt
  185. end
  186.  
  187. function ch(title, list)
  188.     clear()
  189.     local ind = 1
  190.     drawlist(title, list, ind)
  191.     local rt = nil
  192.     cycle = function()
  193.         local ke = getKey()
  194.         if ke == "up" then
  195.             if ind > 1 then
  196.                 ind = ind - 1
  197.             else
  198.                 ind = #list
  199.             end
  200.             drawlist(title, list, ind)
  201.             cycle()
  202.         elseif ke == "down" then
  203.             if ind < #list then
  204.                 ind = ind + 1
  205.             else
  206.                 ind = 1
  207.             end
  208.             drawlist(title, list, ind)
  209.             cycle()
  210.         elseif ke == "enter" then
  211.             rt = ind
  212.         end
  213.     end
  214.     cycle()
  215.     repeat sleep(0) until rt ~= nil
  216.     return ind
  217. end
  218.  
  219. function main()
  220.     ports()
  221.     clear()
  222.     local ont = ch("What would you like to do?", {"Upload files", "Download files"})
  223.     clear()
  224.     write("Server ID: ")
  225.     server = tonumber(read())
  226.     if ont == 1 then
  227.         write("Path to file: ")
  228.         local tof = read()
  229.         if fs.exists(tof) then
  230.             send(tof)
  231.         else
  232.             print("File does not exist!")
  233.         end
  234.     elseif ont == 2 then
  235.         print("Requesting files...")
  236.         rednet.send(server, textutils.serialize({"CLOUD:REC:/"}))
  237.         rec()
  238.     end
  239. end
  240.  
  241. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement