Advertisement
Tatantyler

CRFv3.1 File Transfer API

Aug 17th, 2013
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. -- File Transfers over CRFv3.1
  2.  
  3. -- Packet structure:
  4. -- [1] : Ident string
  5. -- [2] : Action
  6. -- [3] : Target
  7. -- [4] : Data Parameter
  8. -- [5] : Binary Flag
  9.  
  10. if not LANClient then
  11.     error("LAN Client API not loaded.")
  12. end
  13.  
  14. function readFile(server, file, binary)
  15.     LANClient.send(server, textutils.serialize({"FileTransfer", "read", file, binary}))
  16.     while true do
  17.         local sender, data = LANClient.receive(5)
  18.         if not sender then
  19.             return false, "Server timed out"
  20.         elseif sender == server then
  21.             data = textutils.unserialize(data)
  22.             if type(data) == "table" then
  23.                 if data[1] == "FileTransfer" then
  24.                     if data[2] == "Error" then
  25.                         return false, data[3]
  26.                     elseif data[2] == "data" then
  27.                         return true, data[3]
  28.                     else
  29.                         return false, "Unknown response"
  30.                     end
  31.                 end
  32.             end
  33.         end
  34.     end
  35. end
  36.  
  37. function listFiles(server, dir)
  38.     LANClient.send(server, textutils.serialize({"FileTransfer", "list", file}))
  39.     local sender, data = LANClient.receive(5, server)
  40.     if not sender then
  41.         return false, "Server timed out"
  42.     else
  43.         data = textutils.unserialize(data)
  44.         if type(data) == "table" then
  45.             if data[1] == "FileTransfer" then
  46.                 if data[2] == "Error" then
  47.                     return false, data[3]
  48.                 elseif data[2] == "list" then
  49.                     return true, data[3]
  50.                 else
  51.                     return false, "Unknown response"
  52.                 end
  53.             end
  54.         end
  55.     end
  56. end
  57.  
  58. function writeFile(server, file, data, binary)
  59.     LANClient.send(server, textutils.serialize({"FileTransfer", "write", file, binary}))
  60.     local sender, data = LANClient.receive(5, server)
  61.     if not sender then
  62.         return false, "Server timed out"
  63.     else
  64.         data = textutils.unserialize(data)
  65.         if type(data) == "table" then
  66.             if data[1] == "FileTransfer" then
  67.                 if data[2] == "Error" then
  68.                     return false, data[3]
  69.                 elseif data[2] == "Okay" then
  70.                     return true
  71.                 else
  72.                     return false, "Unknown response"
  73.                 end
  74.             end
  75.         end
  76.     end
  77. end
  78.  
  79. function deleteFile(server, file)
  80.     LANClient.send(server, textutils.serialize({"FileTransfer", "delete", file}))
  81.     local sender, data = LANClient.receive(5, server)
  82.     if not sender then
  83.         return false, "Server timed out"
  84.     else
  85.         data = textutils.unserialize(data)
  86.         if type(data) == "table" then
  87.             if data[1] == "FileTransfer" then
  88.                 if data[2] == "Error" then
  89.                     return false, data[3]
  90.                 elseif data[2] == "Okay" then
  91.                     return true
  92.                 else
  93.                     return false, "Unknown response"
  94.                 end
  95.             end
  96.         end
  97.     end
  98. end
  99.  
  100. function mkDir(server, dir)
  101.     LANClient.send(server, textutils.serialize({"FileTransfer", "mkdir", dir}))
  102.     local sender, data = LANClient.receive(5, server)
  103.     if not sender then
  104.         return false, "Server timed out"
  105.     else
  106.         data = textutils.unserialize(data)
  107.         if type(data) == "table" then
  108.             if data[1] == "FileTransfer" then
  109.                 if data[2] == "Error" then
  110.                     return false, data[3]
  111.                 elseif data[2] == "Okay" then
  112.                     return true
  113.                 else
  114.                     return false, "Unknown response"
  115.                 end
  116.             end
  117.         end
  118.     end
  119. end
  120.  
  121. if shell then
  122.     local file = fs.open(shell.getRunningProgram(), "r")
  123.     if file then
  124.         local l = file.readLine()
  125.         file.close()
  126.         if string.match(l, "File Transfers") then
  127.             local args = {...}
  128.             if #args == 0 then
  129.                 print("FTP -- File transfer interface and API")
  130.                 print(string.rep("-", term.getSize()))
  131.                 print("Usage: ftp [action] [server] [target] [params]")
  132.                 print("Actions:")
  133.                 print("read (\"ftp read [server] [remoteFile] [localFile]\"): Get a file.")
  134.                 print("write (\"ftp write [server] [localFile] [remoteFile]\"): Send a file.")
  135.                 print("mkdir (\"ftp mkdir [server] [remoteDir]\"): Make a directory.")
  136.                 print("delete (\"ftp delete [server] [remoteFile]\"): Delete a file.")
  137.                 print("list (\"ftp list [server] [remoteDir]\"): Get a directory listing.")
  138.             elseif #args == 4 then
  139.                 if args[1] == "read" then
  140.                     local stat, data = readFile(tonumber(args[2]), args[3], false)
  141.                     if stat then
  142.                         local f = fs.open(args[4], "w")
  143.                         f.write(data)
  144.                         f.close()
  145.                         print("Data written to "..args[4])
  146.                     else
  147.                         print("Could not get file: "..data)
  148.                     end
  149.                 elseif args[1] == "write" then
  150.                     if fs.exists(args[3]) then
  151.                         local f = fs.open(args[3], "r")
  152.                         data = f.readAll()
  153.                         f.close()
  154.                         local stat, err = writeFile(tonumber(args[2]), args[4], data, false)
  155.                         if stat then
  156.                             print("File successfully transferred.")
  157.                         else
  158.                             print("Could not transfer file: "..err)
  159.                         end
  160.                     else
  161.                         print("File does not exist: "..args[3])
  162.                     end
  163.                 end
  164.             elseif #args == 3 then
  165.                 if args[1] == "mkdir" then
  166.                     local stat, err = mkDir(tonumber(args[2]), args[3])
  167.                     if stat then
  168.                         print("Directory successfully created.")
  169.                     else
  170.                         print("Could not make directory: "..err)
  171.                     end
  172.                 elseif args[1] == "delete" then
  173.                     local stat, err = deleteFile(tonumber(args[2]), args[3])
  174.                     if stat then
  175.                         print("File successfully deleted.")
  176.                     else
  177.                         print("Could not delete file: "..err)
  178.                     end
  179.                 elseif args[1] == "list" then
  180.                     local stat, list = listFiles(tonumber(args[2]), args[3])
  181.                     if stat then
  182.                         textutils.tabulate(list)
  183.                     else
  184.                         print("Could not get directory listing: "..err)
  185.                     end
  186.                 end
  187.             end
  188.         end
  189.     end
  190. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement