Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RedNet FTP Server
- protocol = "FTP"
- hostname = "FTPServer"
- uploadPass = ""
- dirName = "files/"
- programName = "File Transfer Server "
- versionNumber = "3.1"
- modemSide = ""
- -- Find Wireless Modem
- 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)
- if fs.exists(dirName..name) then
- fs.delete(dirName..name)
- end
- file = fs.open(dirName..name,"w")
- file.write(filecontents)
- file.close()
- end
- function TransmitFile(id,filename)
- file = fs.open(dirName..filename,"r")
- rednet.send(id,file.readAll(),protocol)
- file.close()
- end
- if FindModem() == false then
- print("Modem not Found")
- return
- else
- modemSide = FindModem()
- end
- if not fs.exists(dirName) then
- fs.makeDir(dirName)
- end
- rednet.open(modemSide)
- rednet.host(protocol,hostname)
- -- Wait for Request
- term.clear()
- term.setCursorPos(1,1)
- print(programName..versionNumber)
- while true do
- id, msg = rednet.receive(protocol)
- -- If Request for List
- if msg == "LIST" then
- Flist = fs.list(dirName)
- HRlist = "\n"
- for i=1, #Flist do
- if not fs.isDir(Flist[i]) then
- HRlist = HRlist..tostring(Flist[i]).."\n"
- end
- end
- print("LIST:"..id)
- rednet.send(id,HRlist,protocol)
- end
- -- If Request to upload file
- -- REQUEST 1
- if msg == "SEND" then
- -- REQUEST 2
- id2, cPass = rednet.receive(protocol)
- if not id2 == id then
- print("CONNECTION BREAK.")
- return
- end
- if cPass == uploadPass then
- -- REQUEST 3
- rednet.send(id,"GRANTED",protocol)
- -- REQUEST 4
- local id1, pName = rednet.receive(protocol)
- -- REQUEST 5
- local id2, pContents = rednet.receive(protocol)
- WriteFile(pName,pContents)
- print("UPLOAD:"..pName..":"..id1)
- else
- rednet.send(id,"DENIED",protocol)
- print("DENY:"..id)
- end
- end
- -- If Request for File
- if fs.exists(dirName..msg) then
- rednet.send(id,"true",protocol)
- TransmitFile(id,msg)
- print("SEND:"..msg..":"..tostring(id))
- else
- rednet.send(id,"false",protocol)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement