Advertisement
LDDestroier

LDD's Rednet FTP Program

May 2nd, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. protocol = "lddftp"
  2. protocolping = "lddping"
  3. tArg = {...}
  4. command = tArg[1]
  5. argument1 = tArg[2]
  6.  
  7. periphs = peripheral.getNames()
  8. for a = 1, #periphs do
  9.     if peripheral.getType(periphs[a]) == "modem" then
  10.         rednet.open(periphs[a])
  11.     end
  12. end
  13.  
  14. if command == "send" then
  15.     if argument1 ~= nil then
  16.         file = fs.open(argument1, "r")
  17.         stuff = {}
  18.         table.insert(stuff, argument1)
  19.         table.insert(stuff, {file.readAll()})
  20.         file.close()
  21.         if argument2 == nil then
  22.             rednet.broadcast(stuff, protocol)
  23.         else
  24.             rednet.send(stuff, protocol)
  25.         end
  26.     else
  27.         error("expected filename, got nil")
  28.     end
  29. elseif command == "receive" then
  30.     if argument1 == nil then
  31.         print("Waiting...")
  32.     elseif tonumber(argument1) == nil then
  33.         error("expected number for id")
  34.     else
  35.         print("Waiting for " .. argument1)
  36.     end
  37.     while true do
  38.         id, msg = rednet.receive("protocol")
  39.         if msg ~= nil then
  40.             if type(msg) == "table" then
  41.                 fileName = msg[1]
  42.                 print("Got '" .. fileName .. "' from " .. id)
  43.                 file = fs.open(fileName, "w")
  44.                 file.writeLine(stuff[2])
  45.                 file.close()
  46.                 return
  47.             end
  48.         end
  49.     end
  50. else
  51.     error("incorrect command")
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement