Ibot02

FTP

Mar 29th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.86 KB | None | 0 0
  1. args = {...}
  2. if not rednet.isOpen() then
  3.   print("ftp requires rednet to function. Please open rednet before using")
  4.   return false
  5. end
  6.  
  7. send = function( id, file, sendAs )
  8.   if type(id)=="number" and type(file)=="string" then
  9.     if not fs.exists(file) then
  10.       print("file "..file.." does not exist")
  11.       return false
  12.     end
  13.     if fs.isDir(file) then
  14.       ft = addDirToFileTable({},file,sendAs)
  15.       rednet.send(id,{type="send", files = ft},"ftp")
  16.       return true
  17.     end
  18.     f = fs.open(file,"r")
  19.     m = f.readAll()
  20.     ft = {}
  21.     if sendAs and type(sendAs) == "string" then
  22.       ft[sendAs] = m
  23.     else
  24.       ft[file] = m
  25.     end
  26.     t = {type = "send", files = ft}
  27.     rednet.send(id,t,"ftp")
  28.   elseif type(id) == "string" then
  29.     id = rednet.lookup("ftp",id)
  30.     if id then
  31.       send(id,file,sendAs)
  32.     end
  33.   end
  34. end
  35.  
  36. receive = function( id, file, receiveAs )
  37.   print("called receive")
  38.   if type(id)=="number" and type(file) == "string" then
  39.     rednet.send(id,{type="get",files={file}},"ftp")
  40.     done = false
  41.     while not done do
  42.       print("waiting for response")
  43.       s,m,p = rednet.receive("ftp")
  44.       if s == id then
  45.         if type(m) == "table" and m.type == "send" and type(m.files) == "table" then
  46.           f = {}
  47.           if receiveAs then
  48.             f = fs.open(receiveAs,"w")
  49.           else
  50.             f = fs.open(file,"w")
  51.           end
  52.           f.write(m.files[file])
  53.           f.flush()
  54.           f.close()
  55.           done = true
  56.         end
  57.       end
  58.     end
  59.   elseif type(id) == "string" then
  60.     id = rednet.lookup("ftp",id)
  61.     if id then
  62.       receive(id,file,receiveAs)
  63.     end
  64.   end
  65. end
  66.  
  67. getDirectoryListing = function( id, dir)
  68.   if type(id) == "string" then
  69.     id = rednet.lookup("ftp",id)
  70.   end
  71.   if type(id) == "number" and type(dir) == "string" then
  72.     rednet.send(id,{type="listRequest",dir=dir},"ftp")
  73.     done = false
  74.     while not done do
  75.       s,m,p = rednet.receive("ftp")
  76.       if s == id and type(m) == "table" and m.type == "listAnswer" and m.dir == dir then
  77.         return m.files
  78.       end
  79.     end
  80.   end
  81. end
  82.  
  83. addDirToFileTable = function( ft, dir, aliasDir)
  84.   for i,path in pairs(fs.list(dir)) do
  85.     if fs.isDir(fs.combine(dir,path)) then
  86.       addDirToFileTable(ft, fs.combine(dir,path), fs.combine(aliasDir,path))
  87.     else
  88.       f = fs.open(fs.combine(dir,path),"r")
  89.       if aliasDir then
  90.         ft[fs.combine(aliasDir,path)] = f.readAll()
  91.       else
  92.         ft[fs.combine(dir,path)] = f.readAll()
  93.       end
  94.       f.close()
  95.     end
  96.   end
  97. end
  98.  
  99. sendMultiple = function( id, t , dir )
  100.   if type(id) == "string" then
  101.     id = rednet.lookup("ftp",id)
  102.   end
  103.   ft = {}
  104.   for i,path in pairs(t) do
  105.     if fs.exists(fs.combine(dir,path)) then
  106.       if not fs.isDir(fs.combine(dir,path)) then
  107.         f = fs.open(fs.combine(dir,path),"r")
  108.         ft[path] = f.readAll()
  109.         f.close()
  110.       else
  111.         addDirToFileTable(ft,fs.combine(dir,path),path)
  112.       end
  113.     else
  114.       ft[path] = ""
  115.     end
  116.   end
  117.   t = { type = "send", files = ft}
  118.   rednet.send(id,t,"ftp")
  119. end
  120.  
  121. receiveMultiple = function(id, t, dir)
  122.   if type(id) == "string" then
  123.     id = rednet.lookup("ftp",id)
  124.   end
  125.   rednet.send(id,t,"ftp")
  126.   done = false
  127.   while not done do
  128.     s,m,p = rednet.receive("ftp")
  129.     if s == id and type(m) == "table" and m.type == "send" and type(m.files) == "table" then
  130.       saveMultiple(m.files, dir)
  131.     end
  132.   end
  133. end
  134.  
  135. saveMultiple = function( ft, dir )
  136.   for path,file in pairs(ft) do
  137.     f = fs.open(fs.combine(dir,path),"w")
  138.     f.write(file)
  139.     f.flush()
  140.     f.close()
  141.   end
  142. end
  143.  
  144. sendListOfFiles = function( id, path, dir)
  145.   ft = {}
  146.   for i,j in pairs(fs.list(fs.combine(dir,path))) do
  147.     ft[j] = fs.isDir(j)
  148.   end
  149.   rednet.send(id,{type="listAnswer",dir=path,files=ft},"ftp")
  150. end
  151.  
  152. server = function( args )
  153.   dir = "/"
  154.   if args[2] and type(args[2]) == "string" then
  155.     if not fs.isDir(args[2]) then
  156.       fs.makeDir(args[2])
  157.     end
  158.     print("setting dir to "..args[2])
  159.     dir = args[2]
  160.   end
  161.   if args[3] and type(args[3]) == "string" then
  162.     rednet.host("ftp",args[3])
  163.     print("Hostname is "..args[3])
  164.   end
  165.   while true do
  166.     print("waiting for calls")
  167.     s,m,p = rednet.receive("ftp")
  168.     if type(m) == "table" then
  169.       if m.type == "send" and type(m.files) == "table" then
  170.         saveMultiple(m.files, dir)
  171.       end
  172.       if m.type == "get" and type(m.files) == "table" then
  173.         sendMultiple(s, m.files, dir)
  174.       end
  175.       if m.type == "listRequest" and type(m.dir) == "string" then
  176.         sendListOfFiles(s,m.dir,dir)
  177.       end
  178.     end
  179.   end
  180. end
  181.  
  182. if args[1] then
  183.   if args[1] == "server" then
  184.     server(args)
  185.   end
  186.   if args[1] == "send" then
  187.     send(tonumber(args[2]),args[3],args[4])
  188.   end
  189.   if args[1] == "receive" then
  190.     receive(tonumber(args[2]),args[3],args[4])
  191.   end
  192. end
Advertisement
Add Comment
Please, Sign In to add comment