Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RedNet FTP Client
- protocol = "FTP"
- serverhostname = "FTPServer"
- programName = "File Transfer Client "
- versionNumber = "3.1"
- modemSide = ""
- function FindModem()
- modemFound = false
- side = ""
- local pList = peripheral.getNames()
- for i = 1, #pList do
- if peripheral.getType(pList[i]) == "modem" then
- modemFound = true
- side = pList[i]
- end
- end
- if modemFound then
- return side
- else
- return false
- end
- end
- function WriteFile(name,filecontents)
- file = fs.open(name,"w")
- file.write(filecontents)
- file.close()
- end
- function TransmitFile(id,filename)
- file = fs.open(filename,"r")
- rednet.send(id,file.readAll(),protocol)
- file.close()
- end
- -- START PROGRAM
- term.clear()
- term.setCursorPos(1,1)
- if FindModem() == false then
- print("Modem not Found")
- return
- else
- modemSide = FindModem()
- end
- -- Attempting to connect to server.
- rednet.open(modemSide)
- serverid = rednet.lookup(protocol,serverhostname)
- if serverid then
- print("Found Server.")
- else
- print("Failed to Find Server")
- return
- end
- while true do
- print("'new', 'update', 'list', 'send' or 'exit'")
- ans = read()
- if ans == "exit" then
- break
- end
- -- Getting New files
- if ans == "new" then
- while true do
- print("Enter the name of the file")
- progName = read()
- if fs.exists(progName) then
- print("File already exists on device.")
- return
- end
- rednet.send(serverid,progName,protocol)
- print("Request Sent. Awaiting Response.")
- id, msg = rednet.receive(protocol)
- if id == serverid then
- if msg == "true" then
- print("File found on server. Downloading.")
- -- Start file Download.
- id2, msg2 = rednet.receive(protocol)
- WriteFile(progName,msg2)
- break
- elseif msg == "false" then
- print("File not found.")
- else
- print("Unexpected Response from server")
- end
- else
- print("Unexpected Sender")
- end
- end
- end
- -- Updating Files
- if ans == "update" then
- term.clear()
- term.setCursorPos(1,1)
- Flist = fs.list("")
- for i=1, #Flist do
- if not fs.isDir(Flist[i]) then
- rednet.send(serverid,Flist[i],protocol)
- id, msg = rednet.receive(protocol)
- if id == serverid and msg == "true" then
- print("Updating "..Flist[i])
- id2, msg2 = rednet.receive(protocol)
- fs.delete(Flist[i])
- WriteFile(Flist[i],msg2)
- end
- end
- end
- print("All files Updated.")
- end
- -- Uploading Files to Server
- if ans == "send" then
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the name of the File you'd like to upload.")
- sProgName = read()
- if sProgName == "startup" or sProgName == "startup.lua" then
- print("Rename file before uploading.")
- return
- end
- if fs.exists(sProgName) then
- print("Enter Password: ")
- sPass = read()
- term.clear()
- term.setCursorPos(1,1)
- -- ANSWER 1
- rednet.send(serverid,"SEND",protocol)
- -- ANSWER 2
- rednet.send(serverid,sPass,protocol)
- -- ANSWER 3
- id, perm = rednet.receive(protocol)
- if perm == "GRANTED" then
- -- ANSWER 4
- rednet.send(serverid,sProgName,protocol)
- -- ANSWER 5
- TransmitFile(serverid,sProgName)
- elseif perm == "DENIED" then
- print("Wrong Password.")
- return
- else
- print("Unexpected Response.")
- return
- end
- print("Uploaded file: "..sProgName.." to the server.")
- else
- print("File does not exist.")
- end
- end
- -- Getting a list of all files
- if ans == "list" then
- rednet.send(serverid,"LIST",protocol)
- id, msg = rednet.receive(protocol)
- term.clear()
- term.setCursorPos(1,1)
- print("Files on Server: "..msg)
- end
- end
- rednet.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement