I have made a simple ftp program and a simple ftp client. Again, very simple and basic. But it might be of use to you…
Commands is:
download - download a file from the server
goto - go to a page (if there is a lot of files/folders in a directory)
up - move on directory up.
exit - Exit program and clear ftp server.
FTP Client:
local running = true
local selected = 1
settings={}
--[[Load settings]]--
test = io.open("settings.txt", "r")
if test == nil then
file = io.open("settings.txt", "w")
while X~="top" and X~="bottom" and X~="left" and X~="right" and X~="back" and X~="front" do
term.clear()
term.setCursorPos(1,1)
print("Which side is the router on?:")
X=io.read()
end
file:write(X)
file:write("n")
file:write("stop")
file:write("n")
file:close()
print("Settings completed...")
print("Please restart the program...")
sleep(4)
os.reboot()
else
test:close()
file = io.open("settings.txt", "r")
running=true
while running do
X = file:read()
if X~="stop" then
table.insert(settings,X)
else
running=false
end
end
file:close()
end
--[[Download]]--
function Download()
term.clear()
term.setCursorPos(1,1)
print("Enter number next to the ")
print("file you wish to download:")
Z=io.read()
term.clear()
term.setCursorPos(1,1)
print("Enter name of the final file")
print("EX: Downloadedfile.txt")
Y=io.read()
file = io.open(Y, "w")
rednet.send(FTPSERVER,"download")
rednet.send(FTPSERVER,Z)
msg="none"
while msg~="StopDownloadNow" do
id, msg=rednet.receive(5)
if msg~="StopDownloadNow" then
print("Data stream: ",msg)
file:write(msg)
file:write("n")
end
end
file:close()
print("Download Completed...")
sleep(4)
os.reboot()
end
--[[functions in menu]]--
FTPSERVER=0
Files={}
while FTPSERVER<=0 do
term.clear()
term.setCursorPos(1,1)
print("Enter ID of ftp server")
FTPSERVER=io.read()
if tonumber(FTPSERVER)~=nil then
FTPSERVER=math.ceil(tonumber(FTPSERVER))
else
FTPSERVER=0
end
end
rednet.open(settings[1])
rednet.send(FTPSERVER,"ftp")
X=10
while X>0 do
id, msg=rednet.receive(5)
X=X-1
if msg=="confirmed" and id==FTPSERVER then
X=0
print("Ftp server found...")
sleep(1)
rednet.send(FTPSERVER,"/ftp")
else
print("Ftp server not found")
print("Attempt to restart server?")
print("y/n?")
Z=io.read()
if Z=="y" then
rednet.send(FTPSERVER,"exit")
end
os.reboot()
end
while true do
msg="none"
Files={}
while msg~="true" and msg~="false" do
id, msg=rednet.receive(5)
if msg~="true" and msg~="false" then
table.insert(Files,msg)
end
end
Print=true
Side=1
Printlist=1
Sides=math.ceil(#Files/14)
while Print do
term.clear()
term.setCursorPos(1,1)
for i=Printlist,Printlist+14 do
if i<10 then
print(i," : ",Files[i])
else
print(i," : ",Files[i])
end
end
print(Side,"/",Sides)
if msg=="true" then
print("Folder found")
else
print("Folder not found")
end
Y=io.read()
if Y=="exit" then
Print=false
rednet.send(FTPSERVER,"exit")
os.reboot()
elseif Y=="up" then
Print=false
rednet.send(FTPSERVER,"up")
elseif Y=="download" then
Print=false
Download()
elseif Y=="goto" then
term.clear()
term.setCursorPos(1,1)
print("Goto what page?")
Y=io.read()
if tonumber(Y)~=nil and tonumber(Y)<=Sides then
Side=math.ceil(tonumber(Y))
Printlist=Side*14
end
else
Print=false
if tonumber(Y)~=nil then
Y=math.ceil(tonumber(Y))
Directory=Files[Y]
Directory="/"..Directory
rednet.send(FTPSERVER,Directory)
end
end
end
end
end
FTP server:
Dontshow="config"
if fs.isDir("/ftp")==false then
fs.makeDir("/ftp")
end
function Download(X)
print("Uploadning file:")
msg=tonumber(msg)
TempDir=""
for i=1,#Directories do
TempDir=TempDir..Directories[i]
end
TempDir=TempDir.."/"..Files[msg]
print("Sending: ",TempDir)
if fs.exists(TempDir)==true then
file = io.open(TempDir, "r")
while X~=nil do
X = file:read()
if X~=nil then
print("Send data:",X)
rednet.send(idX,X)
end
sleep(0.1)
end
rednet.send(idX,"StopDownloadNow")
end
end
rednet.open("top")
msg="none"
idX, msg=rednet.receive()
if msg=="exit" then
os.reboot()
end
if msg=="ftp" then
Directories={}
rednet.send(idX,"confirmed")
TempDir=""
while true do
print("Waiting for command...")
msg="nullstate"
id, msg=rednet.receive(300)
if msg=="nullstate" then
os.reboot()
end
if id==idX then
if msg=="up" then
table.remove(Directories,#Directories)
elseif msg=="exit" then
os.reboot()
elseif msg=="download" then
id, msg=rednet.receive(5)
Download(msg)
else
print("Searching for files in: ",msg)
search=false
TempDir=""
for i=1,#Directories do
TempDir=TempDir..Directories[i]
end
TempDir=TempDir..msg
if fs.isDir(TempDir)==true then
search=true
print("Directory found")
table.insert(Directories,msg)
end
end
TempDir=""
for i=1,#Directories do
TempDir=TempDir..Directories[i]
end
print("Final directory: ",TempDir)
Files=fs.list(TempDir)
print("Files found: ")
for i=1,#Files do
if Files[i]~=Dontshow then
print(Files[i])
rednet.send(idX,Files[i])
sleep(0.1)
end
end
sleep(0.5)
if search==true then
rednet.send(idX,"true")
else
rednet.send(idX,"false")
end
end
end
end