Guest User

ftp

a guest
Jan 13th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. tArgs = {...}
  2. local function printUsage()
  3.   print("Usage: ")
  4.   print(shell.getRunningProgram().." send <file> <id>")
  5.   print(shell.getRunningProgram().." receive <id>")
  6. end
  7.  
  8. local function receive()
  9.   id, msg = rednet.receive(10)
  10.   if msg == nil then
  11.     error("Connection timed out.")
  12.   else
  13.     if id == tArgs[2] then
  14.       local data = textutils.unserialize(msg)
  15.       local handle = fs.open(data[1], "w")
  16.       handle.write(data[2])
  17.       handle.close()
  18.       print("File saved as "..data[1])
  19.       error()
  20.     end
  21.   end
  22. end
  23.  
  24. local function send()
  25.   local handle = fs.open(tArgs[2], "r")
  26.   local handleData = handle.readAll()  
  27.   local data = {}
  28.   table.insert(data, 1, tArgs[2])
  29.   table.insert(data, 2, handleData)
  30.   local msg = textutils.serialize(data)
  31.   rednet.send(tonumber(tArgs[3]), msg)
  32. end
  33.  
  34. local function openAll()
  35.   for _,v in pairs(rs.getSides()) do
  36.     if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
  37.       rednet.open(v)
  38.     end
  39.   end
  40. end
  41.  
  42. if #tArgs < 2 then
  43.   printUsage()
  44.   error()
  45. end
  46.  
  47. openAll()
  48.  
  49. if tArgs[1] == "send" then send() elseif tArgs[1] == "receive" then receive() end
Advertisement
Add Comment
Please, Sign In to add comment