Advertisement
Alakazard12

Cloud Server

Nov 10th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 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. function clear()
  11.     term.clear()
  12.     term.setCursorPos(1, 1)
  13.     term.setTextColor(1)
  14. end
  15.  
  16. function ports()
  17.     rednet.open("back")
  18.     rednet.open("top")
  19.     rednet.open("front")
  20.     rednet.open("bottom")
  21.     rednet.open("left")
  22.     rednet.open("right")
  23. end
  24.  
  25. function sep(list, by)
  26.     local newlist = {}
  27.     while true do
  28.         local on = nil
  29.         local done = false
  30.         for i = 1, #list do
  31.             if done == false then
  32.                 if string.sub(list, i, i) == by then
  33.                     on = i
  34.                     done = true
  35.                 end
  36.             end
  37.         end
  38.         if on then
  39.             table.insert(newlist, string.sub(list, 1, on - 1))
  40.             list = string.sub(list, on + 1)
  41.         else
  42.             break
  43.         end
  44.     end
  45.     table.insert(newlist, list)
  46.     return newlist
  47. end
  48.  
  49. function rec()
  50.     local id, msg = rednet.receive()
  51.     pcall(function()
  52.         msg = textutils.unserialize(msg)
  53.     end)
  54.     local type = nil
  55.     if string.sub(msg[1], 1, 6) == "CLOUD:" then
  56.         if string.sub(msg[1], 7, 10) == "REC:" then
  57.             return id, string.sub(msg[1], 11), nil, 2
  58.         elseif string.sub(msg[1], 7, 9) == "UP:" then
  59.             local prog = nil
  60.             if msg[2] then
  61.                 prog = msg[2]
  62.             end
  63.             return id, string.sub(msg[1], 10), prog, 1
  64.         else
  65.             return nil
  66.         end
  67.     else
  68.         return nil
  69.     end
  70. end
  71.  
  72. function checkf()
  73.     if not fs.exists("/files") then
  74.         print("Creating \"/files\"")
  75.         fs.makeDir("/files")
  76.     end
  77. end
  78.  
  79. function scan(user, path, prog)
  80.     local hasp = "/"
  81.     for i,v in pairs(sep(path, "/")) do
  82.         if not fs.exists("/files/"..tostring(user)..hasp..v) then
  83.             if i == #sep(path, "/") and prog then
  84.                 if #prog > 100000 then
  85.                     print(path.." is to big!")
  86.                 else
  87.                     pcall(function()
  88.                         print("Creating \"/files/"..tostring(user)..hasp..v.."\"")
  89.                         local newf = fs.open("/files/"..tostring(user)..hasp..v, "w")
  90.                         newf.write(prog)
  91.                     end)
  92.                    
  93.                 end
  94.             else
  95.                 print("Creating \"/files/"..tostring(user)..hasp..v.."\"")
  96.                 fs.makeDir("/files/"..tostring(user)..hasp..v)
  97.             end
  98.         end
  99.         hasp = hasp..v.."/"
  100.     end
  101. end
  102.  
  103. function rem(pth, rrats)
  104.     local found = 0
  105.     for i = 1, #pth do
  106.         if string.sub(pth, 1, i) == rrats then
  107.             pth = string.sub(pth, i+2)
  108.             break
  109.         end
  110.     end
  111.     return pth
  112. end
  113.  
  114. function send(path, id)
  115.     sleep(0.05)
  116.     path = resolve2(path)
  117.     path = resolve(path)
  118.     local remedpath = rem(path, "files/"..tostring(id))
  119.     if fs.isDir(path) then
  120.         print("Sending "..remedpath)
  121.         rednet.send(id, textutils.serialize({remedpath}))
  122.         for i,v in pairs(fs.list(path)) do
  123.             send(path.."/"..v, id)
  124.         end
  125.     else
  126.         print("Sending "..remedpath)
  127.         local fl = fs.open(path, "r")
  128.         local rd = fl.readAll()
  129.         fl.close()
  130.         rednet.send(id, textutils.serialize({remedpath, rd}))
  131.         print("Sent "..remedpath)
  132.     end
  133. end
  134.  
  135. function resolve( path )
  136.     for i = 1, #path do
  137.         if string.sub(path, 1 ,1) == "/" then
  138.             path = string.sub(path, 2)
  139.         end
  140.     end
  141.     return path
  142. end
  143.  
  144. function resolve2( path )
  145.     for i = 1, #path do
  146.         if string.sub(path,#path, #path) == "/" then
  147.             path = string.sub(path, 1, #path-1)
  148.         end
  149.     end
  150.     return path
  151. end
  152.  
  153.  
  154. function loop()
  155.     local id, files, prog, type = rec()
  156.     if id and files then
  157.         if not fs.exists("/files/"..tostring(id)) then
  158.             fs.makeDir("/files/"..tostring(id))
  159.         end
  160.         if type == 1 then
  161.             if not prog then
  162.                 scan(id, files)
  163.             else
  164.                 scan(id, files, prog)
  165.             end
  166.         elseif type == 2 then
  167.             local path = "/files/"..tostring(id).."/"..resolve(files)
  168.             if fs.exists(path) then
  169.                 print("Requested: "..path)
  170.                 send(path, id)
  171.             else
  172.                 print(path.." does not exist!")
  173.             end
  174.         end
  175.     end
  176.     loop()
  177. end
  178.  
  179. function main()
  180.     clear()
  181.     print("Cloud server running on "..os.getComputerID())
  182.     ports()
  183.     checkf()
  184.     loop()
  185. end
  186.  
  187.  
  188. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement