Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- args = {...}
- if not rednet.isOpen() then
- print("ftp requires rednet to function. Please open rednet before using")
- return false
- end
- send = function( id, file, sendAs )
- if type(id)=="number" and type(file)=="string" then
- if not fs.exists(file) then
- print("file "..file.." does not exist")
- return false
- end
- if fs.isDir(file) then
- ft = addDirToFileTable({},file,sendAs)
- rednet.send(id,{type="send", files = ft},"ftp")
- return true
- end
- f = fs.open(file,"r")
- m = f.readAll()
- ft = {}
- if sendAs and type(sendAs) == "string" then
- ft[sendAs] = m
- else
- ft[file] = m
- end
- t = {type = "send", files = ft}
- rednet.send(id,t,"ftp")
- elseif type(id) == "string" then
- id = rednet.lookup("ftp",id)
- if id then
- send(id,file,sendAs)
- end
- end
- end
- receive = function( id, file, receiveAs )
- print("called receive")
- if type(id)=="number" and type(file) == "string" then
- rednet.send(id,{type="get",files={file}},"ftp")
- done = false
- while not done do
- print("waiting for response")
- s,m,p = rednet.receive("ftp")
- if s == id then
- if type(m) == "table" and m.type == "send" and type(m.files) == "table" then
- f = {}
- if receiveAs then
- f = fs.open(receiveAs,"w")
- else
- f = fs.open(file,"w")
- end
- f.write(m.files[file])
- f.flush()
- f.close()
- done = true
- end
- end
- end
- elseif type(id) == "string" then
- id = rednet.lookup("ftp",id)
- if id then
- receive(id,file,receiveAs)
- end
- end
- end
- getDirectoryListing = function( id, dir)
- if type(id) == "string" then
- id = rednet.lookup("ftp",id)
- end
- if type(id) == "number" and type(dir) == "string" then
- rednet.send(id,{type="listRequest",dir=dir},"ftp")
- done = false
- while not done do
- s,m,p = rednet.receive("ftp")
- if s == id and type(m) == "table" and m.type == "listAnswer" and m.dir == dir then
- return m.files
- end
- end
- end
- end
- addDirToFileTable = function( ft, dir, aliasDir)
- for i,path in pairs(fs.list(dir)) do
- if fs.isDir(fs.combine(dir,path)) then
- addDirToFileTable(ft, fs.combine(dir,path), fs.combine(aliasDir,path))
- else
- f = fs.open(fs.combine(dir,path),"r")
- if aliasDir then
- ft[fs.combine(aliasDir,path)] = f.readAll()
- else
- ft[fs.combine(dir,path)] = f.readAll()
- end
- f.close()
- end
- end
- end
- sendMultiple = function( id, t , dir )
- if type(id) == "string" then
- id = rednet.lookup("ftp",id)
- end
- ft = {}
- for i,path in pairs(t) do
- if fs.exists(fs.combine(dir,path)) then
- if not fs.isDir(fs.combine(dir,path)) then
- f = fs.open(fs.combine(dir,path),"r")
- ft[path] = f.readAll()
- f.close()
- else
- addDirToFileTable(ft,fs.combine(dir,path),path)
- end
- else
- ft[path] = ""
- end
- end
- t = { type = "send", files = ft}
- rednet.send(id,t,"ftp")
- end
- receiveMultiple = function(id, t, dir)
- if type(id) == "string" then
- id = rednet.lookup("ftp",id)
- end
- rednet.send(id,t,"ftp")
- done = false
- while not done do
- s,m,p = rednet.receive("ftp")
- if s == id and type(m) == "table" and m.type == "send" and type(m.files) == "table" then
- saveMultiple(m.files, dir)
- end
- end
- end
- saveMultiple = function( ft, dir )
- for path,file in pairs(ft) do
- f = fs.open(fs.combine(dir,path),"w")
- f.write(file)
- f.flush()
- f.close()
- end
- end
- sendListOfFiles = function( id, path, dir)
- ft = {}
- for i,j in pairs(fs.list(fs.combine(dir,path))) do
- ft[j] = fs.isDir(j)
- end
- rednet.send(id,{type="listAnswer",dir=path,files=ft},"ftp")
- end
- server = function( args )
- dir = "/"
- if args[2] and type(args[2]) == "string" then
- if not fs.isDir(args[2]) then
- fs.makeDir(args[2])
- end
- print("setting dir to "..args[2])
- dir = args[2]
- end
- if args[3] and type(args[3]) == "string" then
- rednet.host("ftp",args[3])
- print("Hostname is "..args[3])
- end
- while true do
- print("waiting for calls")
- s,m,p = rednet.receive("ftp")
- if type(m) == "table" then
- if m.type == "send" and type(m.files) == "table" then
- saveMultiple(m.files, dir)
- end
- if m.type == "get" and type(m.files) == "table" then
- sendMultiple(s, m.files, dir)
- end
- if m.type == "listRequest" and type(m.dir) == "string" then
- sendListOfFiles(s,m.dir,dir)
- end
- end
- end
- end
- if args[1] then
- if args[1] == "server" then
- server(args)
- end
- if args[1] == "send" then
- send(tonumber(args[2]),args[3],args[4])
- end
- if args[1] == "receive" then
- receive(tonumber(args[2]),args[3],args[4])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment