LDShadowLord

CRF Custom File Client

Sep 1st, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.     return false, "This action is prohibited on your system"
  81. end
  82.  
  83. function mkDir(server, dir)
  84.     LANClient.send(server, textutils.serialize({"FileTransfer", "mkdir", dir}))
  85.     local sender, data = LANClient.receive(5, server)
  86.     if not sender then
  87.         return false, "Server timed out"
  88.     else
  89.         data = textutils.unserialize(data)
  90.         if type(data) == "table" then
  91.             if data[1] == "FileTransfer" then
  92.                 if data[2] == "Error" then
  93.                     return false, data[3]
  94.                 elseif data[2] == "Okay" then
  95.                     return true
  96.                 else
  97.                     return false, "Unknown response"
  98.                 end
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. if shell then
  105.     local file = fs.open(shell.getRunningProgram(), "r")
  106.     if file then
  107.         local l = file.readLine()
  108.         file.close()
  109.         if string.match(l, "File Transfers") then
  110.             local args = {...}
  111.             if #args == 0 then
  112.                 print("FTP -- File transfer interface and API")
  113.                 print(string.rep("-", term.getSize()))
  114.                 print("Usage: ftp [action] [server] [target] [params]")
  115.                 print("Actions:")
  116.                 print("read (\"ftp read [server] [remoteFile] [localFile]\"): Get a file.")
  117.                 print("write (\"ftp write [server] [localFile] [remoteFile]\"): Send a file.")
  118.                 print("mkdir (\"ftp mkdir [server] [remoteDir]\"): Make a directory.")
  119.                 print("delete (\"ftp delete [server] [remoteFile]\"): Delete a file.")
  120.                 print("list (\"ftp list [server] [remoteDir]\"): Get a directory listing.")
  121.             elseif #args == 4 then
  122.                 if args[1] == "read" then
  123.                     local stat, data = readFile(tonumber(args[2]), args[3], false)
  124.                     if stat then
  125.                         local f = fs.open(args[4], "w")
  126.                         f.write(data)
  127.                         f.close()
  128.                         print("Data written to "..args[4])
  129.                     else
  130.                         print("Could not get file: "..data)
  131.                     end
  132.                 elseif args[1] == "write" then
  133.                     local f = fs.open(args[3], "r")
  134.                     data = f.readAll()
  135.                     f.close()
  136.                     local stat, err = writeFile(tonumber(args[2]), args[4], data, false)
  137.                     if stat then
  138.                         print("File successfully transferred.")
  139.                     else
  140.                         print("Could not transfer file: "..err)
  141.                     end
  142.                 end
  143.             elseif #args == 3 then
  144.                 if args[1] == "mkdir" then
  145.                     local stat, err = mkDir(tonumber(args[2]), args[3])
  146.                     if stat then
  147.                         print("Directory successfully created.")
  148.                     else
  149.                         print("Could not make directory: "..err)
  150.                     end
  151.                 elseif args[1] == "delete" then
  152.                     local stat, err = deleteFile(tonumber(args[2]), args[3])
  153.                     if stat then
  154.                         print("File successfully deleted.")
  155.                     else
  156.                         print("Could not delete file: "..err)
  157.                     end
  158.                 elseif args[1] == "list" then
  159.                     local stat, list = listFiles(tonumber(args[2]), args[3])
  160.                     if stat then
  161.                         textutils.tabulate(list)
  162.                     else
  163.                         print("Could not get directory listing: "..err)
  164.                     end
  165.                 end
  166.             end
  167.         end
  168.     end
  169. end
Advertisement
Add Comment
Please, Sign In to add comment