Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("api/devices")
- -- GLOBAL VARIABLES --
- local ver="1.1"
- local id=os.getComputerID()
- local wifi
- local dest
- local sfile
- local sender
- local dcat
- local dfile
- local dpath
- -- /GLOBAL VARIABLES --
- function Main()
- Clear()
- write("Initializing")
- textutils.slowWrite(".....\n\n")
- wifi=devices.find("modem")
- if wifi~="none" then
- print("Wireless device found!")
- write("Opening connection")
- textutils.slowWrite(".....\n\n")
- rednet.open(wifi)
- print("Done!")
- sleep(1)
- Menu()
- else
- print("Wireless device not found!\nConnect a device and try again!")
- return 0
- end
- end
- function Menu()
- Clear()
- print("Your ID: "..id.."\n")
- print("\t1. Send file")
- print("\t2. Receive file")
- print("\t0. Exit")
- local event,p1=os.pullEvent("key")
- if p1==2 then
- SendMenu()
- elseif p1==3 then
- DownMenu()
- elseif p1==11 then
- term.clear()
- term.setCursorPos(1,1)
- print("Thanks for using rFTP!")
- rednet.close(wifi)
- return 0
- else
- Menu()
- end
- end
- function SendMenu()
- Clear()
- print("Enter destination ID (not label):")
- dest=tonumber(read())
- print("File to send: ")
- sfile=read()
- write("\nChecking file")
- textutils.slowWrite(".....")
- if fs.exists(sfile)==true then
- write(" File OK!\n")
- else
- print("\nFile not found! Any key to continue...")
- os.pullEvent("key")
- Menu()
- end
- print("Estabilishing connection")
- rednet.send(dest,"@rftpstart")
- rednet.send(dest,sfile)
- print("Waiting for device #"..dest.." to respond...\n[Press SPACE to cancel]")
- Waiting()
- end
- function Waiting()
- while true do
- local event,p1,p2=os.pullEvent()
- if (event=="rednet_message") and (p2=="@rftpagree") then
- print("File accepted by #"..dest.."\n")
- sleep(1)
- Transfer()
- break
- elseif (event=="rednet_message") and (p2=="@rftpdeny") then
- print("#"..dest.." denied connection! Any key to continue...")
- os.pullEvent("key")
- Menu()
- break
- elseif (event=="key") and (p1==57) then
- Menu()
- break
- end
- end
- end
- function Transfer()
- local plik=fs.open(sfile,"r")
- local tresc=plik.readAll()
- rednet.send(dest,tresc)
- sleep(0.3)
- rednet.send(dest,"@rftpend")
- plik.close()
- print("Transfer complete! Any key to continue...")
- os.pullEvent("key")
- Menu()
- end
- function DownMenu()
- Clear()
- print("Your ID: "..id.."\n")
- print("Waiting for an oncoming file...\n[Press SPACE to cancel]")
- local event,p1,p2
- repeat
- event,p1,p2=os.pullEvent()
- until ((event=="rednet_message") and (p2=="@rftpstart")) or ((event=="key") and (p1==57))
- if (event=="rednet_message") and (p2=="@rftpstart") then
- sender=p1
- event,p1,p2=os.pullEvent("rednet_message")
- print("Device "..p1.." want to send you a file: "..p2..". Accept? [y/N]")
- local event,pp1=os.pullEvent("key")
- if (pp1==20) or (pp1==21) then
- Download()
- else
- rednet.send(p1,"@rftpdeny")
- Menu()
- end
- elseif (event=="key") and (p1==57) then
- Menu()
- end
- end
- function Download()
- Clear()
- print("Select the folder: [default: dl]")
- dcat=read()
- if dcat=="" then
- dcat="dl"
- end
- fs.makeDir(dcat)
- print("Select filename:")
- dfile=read()
- dpath=fs.combine(dcat,dfile)
- if fs.exists(dpath)==false then
- SaveFile()
- else
- print("File already exists! Try again? [y/N]")
- local event,p1=os.pullEvent("key")
- if (p1==20) or (p1==21) then
- Download()
- else
- rednet.send(p1,"@rftpdeny")
- Menu()
- end
- end
- end
- function SaveFile()
- local plik=fs.open(dpath,"w")
- rednet.send(p1,"@rftpagree")
- local event,p1,p2
- repeat
- event,p1,p2=os.pullEvent("rednet_message")
- until p1==sender
- if p2~="@rftpend" then
- plik.write(p2)
- end
- os.startTimer(10)
- while true do
- local event,p1,p2=os.pullEvent()
- if event=="rednet_message" and p1==sender and p2=="@rftpend" then
- write("\nTransfer completed! ")
- break
- elseif event=="timer" then
- write("\nTimed out! Check the file. ")
- break
- end
- end
- plik.close()
- print("Any key to continue...")
- os.pullEvent("key")
- Menu()
- end
- function Clear()
- term.clear()
- term.setCursorPos(1,1)
- print("rFTP v"..ver.."\nby Jahmaican\n~~~~~~~~~~~~\n")
- end
- Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement